Updating parts in A2plus macro

Need help, or want to share a macro? Post here!
abghrnjd
Posts: 21
Joined: Tue Dec 22, 2020 12:02 pm

Updating parts in A2plus macro

Postby abghrnjd » Tue Feb 23, 2021 7:08 am

I have a macro which opens an A2plus assembly but every time it runs my part dimensions are changed. So I have to update parts every time. But when I click on the update icon, no command is shown in the console and I can't put it in my macro. what is the command (method) to update parts in a2plus?

I see these codes sometimes in the console:

Code: Select all

App.setActiveDocument("Link_0")
App.ActiveDocument=App.getDocument("Link_0")
Gui.ActiveDocument=Gui.getDocument("Link_0")
App.setActiveDocument("")
App.ActiveDocument=None
Gui.ActiveDocument=None
App.setActiveDocument("test")
App.ActiveDocument=App.getDocument("test")
Gui.ActiveDocument=Gui.getDocument("test")
App.setActiveDocument("Link_1")
App.ActiveDocument=App.getDocument("Link_1")
Gui.ActiveDocument=Gui.getDocument("Link_1")
App.setActiveDocument("")
App.ActiveDocument=None
Gui.ActiveDocument=None
App.setActiveDocument("test")
App.ActiveDocument=App.getDocument("test")
Gui.ActiveDocument=Gui.getDocument("test")
App.setActiveDocument("Link_2")
App.ActiveDocument=App.getDocument("Link_2")
Gui.ActiveDocument=Gui.getDocument("Link_2")
App.setActiveDocument("")
App.ActiveDocument=None
Gui.ActiveDocument=None
App.setActiveDocument("test")
App.ActiveDocument=App.getDocument("test")
Gui.ActiveDocument=Gui.getDocument("test")
App.setActiveDocument("Link_3")
App.ActiveDocument=App.getDocument("Link_3")
Gui.ActiveDocument=Gui.getDocument("Link_3")
App.setActiveDocument("")
App.ActiveDocument=None
Gui.ActiveDocument=None
App.setActiveDocument("test")
App.ActiveDocument=App.getDocument("test")
Gui.ActiveDocument=Gui.getDocument("test")
I've put it in my macro but it does nothing!
onekk
Posts: 517
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Updating parts in A2plus macro

Postby onekk » Tue Feb 23, 2021 7:42 am

Generally usng:

Code: Select all

doc.recompute()
with doc = FreeCAD.activeDocument() will do the job, but if the problem is to force the macro or the WorkBench to recompute all "his" generated geometries, I don't know.

Maybe inspecting the sources will do some hints.

Console Output is generallly "repeating" the commands, but sadly not all the commands isssued, plus some are "masked" by the Gui part, so if you type commands shown in "green" in the console they will be executed, but it is not guaranteed that in a Macro or script they will have same effects.

Plus some commands, operate on "Selection" so if you don't have a correct "Selection" they will do nothing, some care have to be taken on "replicating" commands shown if you don't know from where they are generated, maybe they are the "result" of some action done by the macro, and the macro prior of issuing a command, do some other actions, like selecting the "correct" objects, these actions maybe are not shown in "green" as they are not issuing any "Gui command" but are directly manipulate internal variables and this "action" is not shown in the Console.

Sadly to be not more helpful.

Regards

Carlo D.
abghrnjd
Posts: 21
Joined: Tue Dec 22, 2020 12:02 pm

Re: Updating parts in A2plus macro

Postby abghrnjd » Tue Feb 23, 2021 10:32 am

onekk wrote:
Tue Feb 23, 2021 7:42 am
Generally usng:

Code: Select all

doc.recompute()
with doc = FreeCAD.activeDocument() will do the job, but if the problem is to force the macro or the WorkBench to recompute all "his" generated geometries, I don't know.

Maybe inspecting the sources will do some hints.

Console Output is generallly "repeating" the commands, but sadly not all the commands isssued, plus some are "masked" by the Gui part, so if you type commands shown in "green" in the console they will be executed, but it is not guaranteed that in a Macro or script they will have same effects.

Plus some commands, operate on "Selection" so if you don't have a correct "Selection" they will do nothing, some care have to be taken on "replicating" commands shown if you don't know from where they are generated, maybe they are the "result" of some action done by the macro, and the macro prior of issuing a command, do some other actions, like selecting the "correct" objects, these actions maybe are not shown in "green" as they are not issuing any "Gui command" but are directly manipulate internal variables and this "action" is not shown in the Console.

Sadly to be not more helpful.
Regards
Carlo D.
Thank You.
recompute didn't help. :(
i found

Code: Select all

 def updateImportedParts(doc, partial=False) 
in a2p_importpart.py in a2plus mod folder
How can I use it in my macro?
onekk
Posts: 517
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Updating parts in A2plus macro

Postby onekk » Tue Feb 23, 2021 3:06 pm

Code: Select all

 def updateImportedParts(doc, partial=False) 
It is not very obscure, from what I could see from this code:

doc, is the reference of your document so:

Code: Select all

doc = FreeCAD.activeDocument()

updateImportedParts(doc)

will suffice, but I don't know what will happens i you set partial to True

eventually doc could be:

Code: Select all

doc_name = "doc name"
doc = FreeCAD.getDocument(doc_name)
but take in account that some characters are "transformed" in _ (underscore) i.e the code above "your name" will be

Code: Select all

doc_name = "your name"
doc = FreeCAD.getDocument(doc_name)
print(doc.Name)
will result in "your_name", smae for "your-name" will be "your_name" take care on how it is "transformed", if you retain the doc_name as the "document name" and want to utilize it to retrieve the document "instance".

Hope it helps

Carlo D.
kbwbe
Posts: 1011
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Updating parts in A2plus macro

Postby kbwbe » Tue Feb 23, 2021 8:38 pm

abghrnjd wrote:
Tue Feb 23, 2021 10:32 am
i found

Code: Select all

 def updateImportedParts(doc, partial=False) 
in a2p_importpart.py in a2plus mod folder
How can I use it in my macro?
You can use it in macro's...

Code: Select all

from FreeCAD.A2plus.a2p_importpart import updateImportedParts
doc = FreeCAD.activeDocument()
updateImportedParts(doc) 
if you use partial=True...

Code: Select all

from FreeCAD.A2plus.a2p_importpart import updateImportedParts
doc = FreeCAD.activeDocument()
updateImportedParts(doc, partial=True) 
only selected imported parts are updated.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.50, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
abghrnjd
Posts: 21
Joined: Tue Dec 22, 2020 12:02 pm

Re: Updating parts in A2plus macro

Postby abghrnjd » Wed Feb 24, 2021 5:19 am

kbwbe wrote:
Tue Feb 23, 2021 8:38 pm
abghrnjd wrote:
Tue Feb 23, 2021 10:32 am
i found

Code: Select all

 def updateImportedParts(doc, partial=False) 
in a2p_importpart.py in a2plus mod folder
How can I use it in my macro?
You can use it in macro's...

Code: Select all

from FreeCAD.A2plus.a2p_importpart import updateImportedParts
doc = FreeCAD.activeDocument()
updateImportedParts(doc) 
if you use partial=True...

Code: Select all

from FreeCAD.A2plus.a2p_importpart import updateImportedParts
doc = FreeCAD.activeDocument()
updateImportedParts(doc, partial=True) 
only selected imported parts are updated.
Hi,
Thank You. It works. :)
but I had to write:

Code: Select all

from a2p_importpart import updateImportedParts