Another approach to assembly solver (A2plus)

Discussion about the development of the Assembly workbench.
vocx
Posts: 1898
Joined: Thu Oct 18, 2018 9:18 pm

Re: Another approach to assembly solver (A2plus)

Postby vocx » Tue Nov 19, 2019 4:33 pm

Kunda1 wrote:
Tue Nov 19, 2019 12:52 pm
Reason: users aren't aware it's possible to animate assemblies. See https://forum.freecadweb.org/viewtopic. ... 11#p348308
dcapeletti wrote:
Mon May 20, 2019 11:37 pm
I made an engine and assembly with some parts and a script to run the animation. You can see the gif and the repository of the project.

https://gitlab.com/dcapeletti/ejercicio ... A2Plus.gif

https://gitlab.com/dcapeletti/ejercicio ... %20tiempos
The script is pretty simple

Code: Select all

from a2p_solversystem import SolverSystem
import time
import FreeCADGui

class Motor():
    def __init__(self):
        self.angulo = 0
        self.solver = SolverSystem()

    def iniciar_animacion(self):
        for val in range(0, 90):
            self.angulo = val*4
            FreeCAD.ActiveDocument.b_Ciguenal_001_.Placement = App.Placement(App.Vector(0,0,0),App.Rotation(App.Vector(0,0,1),self.angulo))
            self.solver.solveSystem(App.ActiveDocument)
            FreeCADGui.updateGui()
            time.sleep(0.01)

    def stop(self):
        self.angulo = 0
        FreeCAD.ActiveDocument.b_Ciguenal_001_.Placement = App.Placement(App.Vector(0,0,0),App.Rotation(App.Vector(0,0,1),0))
        self.solver.solveSystem(App.ActiveDocument)

motor = Motor()
motor.iniciar_animacion()
# It stops after a while or can be stopped with a method as well
motor.stop()
There isn't much "scripting" from A2plus going on to be honest. From A2plus it's just one command, the rest are generic FreeCAD commands.

These are the steps
  • It creates a class to encapsulate everything. In this class it creates an instance of A2plus's SolverSystem.
  • Then a method is defined to cycle a few times. In this cycle the placement of the body is changed; the change could be in the base placement, in the rotation, or both. In this example it's the angle of rotation the only thing that changes.
  • After the change in placement, it solves the system again with solver.solveSytem(App.ActiveDocument).
  • Then it updates the graphical interface with FreeCADGui.updateGui()
  • Finally, to finish the cycle it makes a tiny pause of 0.01 seconds. This can be changed to control the rate of update of the frames in the animation.
  • The animation will stop itself after the number of cycles is completed. However, a method is defined to stop the animation at any time, by resetting the angle of rotation, and solving the system again only once.
kbwbe
Posts: 881
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Another approach to assembly solver (A2plus)

Postby kbwbe » Tue Nov 19, 2019 11:00 pm

Kunda1 wrote:
Tue Nov 19, 2019 12:52 pm
Bump.
Reason: users aren't aware it's possible to animate assemblies. See https://forum.freecadweb.org/viewtopic. ... 11#p348308
Hi @Kunda1, sorry for letting you wait.

As @vocx told in post above, there is not much space for scripting activities within A2plus except animation.
For most other operations as inserting parts and defining constraints user interaction is assumed as necessary.

Doing animations, only one command related to A2plus is needed to be known (additional to general freecad scripting knowledge):

Code: Select all

FreeCADGui.runCommand('a2p_SolverCommand',0)   # this solves all constraints of the active document
FreeCADGui.updateGui() # make results visible at once, not only at the end of a script
The rest depends on the users strategy how to implement an animation.
Variants:
(1) user changes the placement of a "driving" object: (example)

Code: Select all

import FreeCAD as App
import FreeCADGui as Gui
import time

doc = App.activeDocument()
drivingObject = doc.getObjectsByLabel("crankshaft")[0] # for example a crankshaft of a motor, called by it's label

deltaAngle = 1.0
startAngle = 0.0

drivingObject.Placement.Rotation.Angle = startAngle

for i in range(0,360): 
	drivingObject.Placement.Rotation.Angle = startAngle + i*deltaAngle # change placement of "driving" object
	App.runCommand("a2p_SolverCommand",0) # solve the constraints, move piston e.g.
	Gui.updateGui() # show the result immediatly and do not wait for finishing the script
	time.sleep(0.01) # delay the frame rate to your requirements
	
drivingObject.Placement.Rotation.Angle = startAngle # wind back to start
App.runCommand("a2p_SolverCommand",0) # solve last time
Gui.updateGui()
.
(2) user modifies a constraint and changes for example the offset of it:

Code: Select all

import FreeCAD as App
import FreeCADGui as Gui
import time

doc = App.activeDocument()
drivingConstraint = doc.getObjectsByLabel("<labelOfConstraint>")[0]  # place the label of your constraint within " "

deltaOffset = 1.0
startOffset = 0.0

for i in range(0,100): 
	drivingConstraint.offset = startOffset +i*deltaOffset # change offset of "driving" constraint
	App.runCommand("a2p_SolverCommand",0) # solve the constraints, move depending objects
	Gui.updateGui() # show the result immediatly and do not wait for finishing the script
	time.sleep(0.01) # delay the frame rate to your requirements
	
drivingConstraint.offset = startOffset # wind back to start
App.runCommand("a2p_SolverCommand",0) # solve last time
Gui.updateGui()
So the needed knowledge about scripting A2plus is calling the solver command. Everything else depends on general FreeCAD scripting experience.
uwestoehr wrote: ping
Please can you help with some little documentation in A2plus scripting section ? Thank you.
I can provide working examples.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.30, installable via FreeCAD's addon manager
Tutorials:
Paul Randall: https://youtu.be/mnkecA9S7kc
anisim (deutsch): https://www.youtube.com/watch?v=vDcaFq6IEJM
User avatar
uwestoehr
Posts: 794
Joined: Sun Jan 27, 2019 3:21 am

Re: Another approach to assembly solver (A2plus)

Postby uwestoehr » Sun Nov 24, 2019 12:16 am

kbwbe wrote:
Tue Nov 19, 2019 11:00 pm
Please can you help with some little documentation in A2plus scripting section ? Thank you.
I can provide working examples.
Hi Klaus. Yes, I will do so. The question is what scripting is useful to be described. I think a good example of an animation would be nice. This way the user see how scripting is done in genral. If you have such an example, please send me one by private mail that I can experiment with it.
User avatar
uwestoehr
Posts: 794
Joined: Sun Jan 27, 2019 3:21 am

Re: Another approach to assembly solver (A2plus)

Postby uwestoehr » Tue Dec 03, 2019 1:32 am

uwestoehr wrote:
Sun Nov 24, 2019 12:16 am
Yes, I will do so.
Here is a basic scripting description: https://www.freecadweb.org/wiki/A2plus_ ... #Animation

Can you please have a look at the questions I sent you by mail that I can remove the ''??'' in the Wiki page?
Attachments
A2p_animation-advanced-script.py
advanced animation
(6.35 KiB) Downloaded 14 times
A2p_animation-example-script.py
animation example script
(1.43 KiB) Downloaded 21 times
A2p_animated-example.FCStd
animation example assembly
(26.58 KiB) Downloaded 43 times
Last edited by uwestoehr on Mon Dec 09, 2019 1:32 am, edited 5 times in total.
catman
Posts: 90
Joined: Fri Jan 11, 2019 10:42 pm

Re: Another approach to assembly solver (A2plus)

Postby catman » Tue Dec 03, 2019 4:48 pm

When I have a file with 3 PartDesign bodies
  • partname
    • Body1, visible
      Body2, invisible
      Body3, visible
and import this file into a new empty A2+ Assembly file I observe that all bodies are visible. I would have expected I only would see Body1 and Body3 visible. I tried other combinations. Visibility works find on full assemblies, e.g. if each body would be a separate file.

Is that intended behaviour of A2+ or a technical limitation?

Tested in V0.19 17505(Git)


PS: my actual problem behind this is that I need to have a constraint to a face of Body3 which covered by the other bodies. Without being able to make some of them invisible, I can not select that face. Maybe there is another workaround for that...
kbwbe
Posts: 881
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Another approach to assembly solver (A2plus)

Postby kbwbe » Tue Dec 03, 2019 4:59 pm

catman wrote:
Tue Dec 03, 2019 4:48 pm
and import this file into a new empty A2+ Assembly file I observe that all bodies are visible. I would have expected I only would see Body1 and Body3 visible. I tried other combinations. Visibility works find on full assemblies, e.g. if each body would be a separate file.
Hi,
you have to activate the option "do not import invisible parts" in A2p preferences.
This should work.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.30, installable via FreeCAD's addon manager
Tutorials:
Paul Randall: https://youtu.be/mnkecA9S7kc
anisim (deutsch): https://www.youtube.com/watch?v=vDcaFq6IEJM
kbwbe
Posts: 881
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Another approach to assembly solver (A2plus)

Postby kbwbe » Tue Dec 03, 2019 6:39 pm

uwestoehr wrote:
Tue Dec 03, 2019 1:32 am
Can you please have a look at the questions I sent you by mail that I can remove the ''??'' in the Wiki page?
Hi Uwe,
thank you for your good documentation. I have send you an email.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.30, installable via FreeCAD's addon manager
Tutorials:
Paul Randall: https://youtu.be/mnkecA9S7kc
anisim (deutsch): https://www.youtube.com/watch?v=vDcaFq6IEJM
catman
Posts: 90
Joined: Fri Jan 11, 2019 10:42 pm

Re: Another approach to assembly solver (A2plus)

Postby catman » Tue Dec 03, 2019 8:02 pm

kbwbe wrote:
Tue Dec 03, 2019 4:59 pm
you have to activate the option "do not import invisible parts" in A2p preferences.
This should work.
Great, thanks it does.

Does the warning that links referring to invisible geometry might break imply that without that option checked this does not happen? I observed that visibility settings are passed from added parts (there is another option on by default) do break the links when parts are toggled to visible/invisible.
Is there technically a difference between those cases?
User avatar
uwestoehr
Posts: 794
Joined: Sun Jan 27, 2019 3:21 am

Re: Another approach to assembly solver (A2plus)

Postby uwestoehr » Tue Dec 03, 2019 11:24 pm

kbwbe wrote:
Tue Dec 03, 2019 6:39 pm
thank you for your good documentation.
I have now improved the documentation: https://www.freecadweb.org/wiki/A2plus_ ... #Animation
The animation has now a progress bar and a button to stop the animation.
User avatar
uwestoehr
Posts: 794
Joined: Sun Jan 27, 2019 3:21 am

Re: Another approach to assembly solver (A2plus)

Postby uwestoehr » Sun Dec 08, 2019 10:36 pm

uwestoehr wrote:
Tue Dec 03, 2019 11:24 pm
I have now improved the documentation: https://www.freecadweb.org/wiki/A2plus_ ... #Animation
The animation has now a progress bar and a button to stop the animation.
I provided now also an interactive example: https://www.freecadweb.org/wiki/A2plus_ ... ve_Example