rotating Assy 3 model to a desired angle and back to 0deg (solved)

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by realthunder »

Just for reference, see my reply regarding the angle lock implementation here.
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
User avatar
bambuko
Veteran
Posts: 2185
Joined: Thu Oct 24, 2019 12:53 pm
Location: UK, England, North Devon

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by bambuko »

edwilliams16 wrote: Fri May 21, 2021 6:14 pm ...I would attempt to identity the driving parameters for the various constraints...
Didn't get on very well with this one :oops:

Have decided to go back to the idea that worked so well for me previously (using sketcher solver to drive the geometry),
but this time wrap it in Assembly 3.

Managed to get confused (thinking that I needed to use Draft workbench) following this:
https://github.com/realthunder/FreeCAD_ ... ton-Sketch

but this got clarified here (I do not need Draft! can use normal sketches):
https://github.com/realthunder/FreeCAD_ ... -846475842

so... this is my plan now - experiment with skeleton sketches for the parts where assembly solver gets stuck.

In the meantime one more question to you, please:
How do I create the macro to animate assembly, driven by setting angle value for element offset?

It's easy now to rotate to any desired angle and back by just changing the value of element offset,
but haven't got the clue how to convert it to continous motion for animation?
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by edwilliams16 »

Hover over the offset part to find its ElementLink00x internal name and change it in the macro. If the rotaion direction is wrong, negate the input angles.

Code: Select all

from math import radians
import time
#animate from ang_b to ang_e insteps of ang_s
ang_b = 0
ang_e  = 270
ang_s = 1
ang = ang_b
while ang < ang_e:
	App.ActiveDocument.getObject("ElementLink003").Offset.Rotation.Angle = radians(ang)
	Gui.runCommand('asm3CmdQuickSolve',0)
	FreeCADGui.updateGui()
	time.sleep(0.001)  #increase to slow animation, or remove
	#print(f'Angle = {ang}.')
	ang += ang_s

App.ActiveDocument.getObject("ElementLink003").Offset.Rotation.Angle = radians(ang_e)
#print(f'Angle = {ang}.')
Gui.runCommand('asm3CmdQuickSolve',0)
FreeCADGui.updateGui()
is the macro. Enclosed is my test assembly.
Using the sketch solver sounds interesting. If you have several sketches in different planes, I expect you'll need to make sure they can be solved sequentially. A coupled solve isn't going to work.
Attachments
axle_indicator2.zip
(31.42 KiB) Downloaded 39 times
User avatar
bambuko
Veteran
Posts: 2185
Joined: Thu Oct 24, 2019 12:53 pm
Location: UK, England, North Devon

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by bambuko »

edwilliams16 wrote: Sun May 23, 2021 7:09 pmmbly.
Using the sketch solver sounds interesting...
Thank you for the macro!
I will play with it tomorrow (it's been long day and I crashed out tonight :mrgreen: )

Using skeleton sketches is a total 100% success.
Luckily for me they are all in one plane.
There will be five of them -two each (LH & RH) for main motion an two for valve gear, plus one for axle water pump.
They are very easy to use with Assembly 3.
I have so far done LH side motion and tested it.
The rest is just repetition ... :lol:
Thank you for all your support.

Will report back soon.
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by edwilliams16 »

Above only works for increasing angle. Here's a bug-fix:

Code: Select all

from math import radians
import time
#animate from ang_b to ang_e insteps of ang_s
ang_b = 0
ang_e  = 270
ang_s = 1


def float_range(first, last, step):
    x = first + 0.0
    while True:
        if step > 0 and x >= last: break
        elif step < 0 and x <= last: break
        yield x
        x += step

for ang in float_range(ang_b, ang_e, ang_s):
    App.ActiveDocument.getObject("ElementLink003").Offset.Rotation.Angle = radians(ang)
    Gui.runCommand('asm3CmdQuickSolve',0)
    FreeCADGui.updateGui()
    time.sleep(0.001)  #increase to slow animation
    #print(f'Angle = {ang}.')
Attachments
animateattach.FCMacro
(547 Bytes) Downloaded 44 times
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by edwilliams16 »

Couldn't figure out how to attach a video - I guess I have to host it somewhere.
Anyway, here's an example of an animated sketch, which could be used as a master for an animated model. I don't use any problematic angle constraint. I use the coordinates of a point on the circle.

Macro here:

Code: Select all

def float_range(first, last, step):
    while True:
        if step > 0 and first >= last:
            break
        elif step < 0 and first <= last:
            break
        yield(first + 0.0)
        first += step

ang_b = 40
ang_e = 400
ang_s = 2

for ang in float_range(ang_b, ang_e, ang_s):
    App.ActiveDocument.getObject('dd').ddTheta = ang%360
    App.ActiveDocument.recompute()
    FreeCADGui.updateGui()
Screen Shot 2021-05-23 at 12.12.54 PM.png
Screen Shot 2021-05-23 at 12.12.54 PM.png (13.33 KiB) Viewed 1902 times
Screen Shot 2021-05-23 at 12.14.24 PM.png
Screen Shot 2021-05-23 at 12.14.24 PM.png (13.87 KiB) Viewed 1902 times
Attachments
dynamicsketch.FCStd
(5.1 KiB) Downloaded 46 times
User avatar
bambuko
Veteran
Posts: 2185
Joined: Thu Oct 24, 2019 12:53 pm
Location: UK, England, North Devon

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by bambuko »

edwilliams16 wrote: Sun May 23, 2021 8:19 pm Above only works for increasing angle. Here's a bug-fix:

Code: Select all

from math import radians
import time
#animate from ang_b to ang_e insteps of ang_s
ang_b = 0
ang_e  = 270
ang_s = 1

def float_range(first, last, step):
    x = first + 0.0
    while True:
        if step > 0 and x >= last: break
        elif step < 0 and x <= last: break
        yield x
        x += step

for ang in float_range(ang_b, ang_e, ang_s):
    App.ActiveDocument.getObject("ElementLink003").Offset.Rotation.Angle = radians(ang)
    Gui.runCommand('asm3CmdQuickSolve',0)
    FreeCADGui.updateGui()
    time.sleep(0.001)  #increase to slow animation
    #print(f'Angle = {ang}.')
Just tested this one on my file, here are my comments:
- works fine on my file, but ...
- if you specify ang_e=270 it actually rotates to 269 ;)
- it can only deal with one element (as far as I understand it?)

Historically it was OK, because we tied front/driving_axle_sub-assembly with rear/coupled_axle_sub-assembly using assembly constraint via coupling_rod_sub-assembly and all other sub-assemblies were tied using assembly constraints to front/driving_axle_sub-assembly, so driving/rotating the assembly using one element or constraint was perfectly OK.

Now I have front/driving_axle_sub-assembly and rear/coupled_axle_sub-assembly not constrained to each other via assembly constraint, but both independently driven/rotated using the same angle of offset.
Plus all other sub-assemblies are assembly constrained to their respective skeleton sketches, using sketcher constrained angle of rotation.

Both element offsets and angle of rotation in skeleton sketches are tied together with one single value for angle using expressions and alias cell in the spreadsheet.

I see that you very cleverly came up with:
... I don't use any problematic angle constraint. I use the coordinates of a point on the circle...
I thought also, that we might have to go this way if angle constraints in the sketches cause problems, but...
it seems that sketcher solver is a different animal to assembly solver and is perfectly happy to use angles.
It hasn't failed yet in all my tests.
OK, you have to be careful how you constrain the sketch (some things don't work very well :cry: ),
but this is no big deal and I am comfortable with using sketches this way.

Interesting use of Dynamic Data - this is something new to me (haven't used if before).
I guess this is the way I would have to go if I wanted to control my sketches with macro rather than spreadsheet and alias cell?
Thank you for that example - I am learning all the time ;)
Will have to read up on that a bit more.
later edit:
installed, and playing with - what a brilliant tool.
Thank you for pointing me in this direction


as for:
edwilliams16 wrote: Sun May 23, 2021 10:16 pm Couldn't figure out how to attach a video - I guess I have to host it somewhere....
Yes, although embedding of youtube vids doesn't work very well on the forum at the moment.
If you want to attach something directly to the forum the best way is to create .gif file (providing it is within attachment limits of the forum).
I use https://www.screentogif.com/
very good and very easy to use - recommend it!

BTW, I am still perfectly happy to share with you my file (off the forum) if you are interested?
It would be a lot easier for you to see things for yourself rather than me trying to describe it in my posts on this thread ;)

There has been small change of plans and I will be away (travelling for a few days) so no access to FreeCAD (my old laptop is not up to it).
I will be able to use the forum but no chance to do any FreeCAD work until I am back :cry:

Thank you

Example below is real time :)
rotate(skeleton)2.gif
rotate(skeleton)2.gif (745.17 KiB) Viewed 1822 times
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by edwilliams16 »

Replace this function - dumb error on my part:

Code: Select all

def float_range(first, last, step):
    x = first + 0.0
    while True:
        if step > 0 and x > last+step/2: break
        elif step < 0 and x < last-step/2: break
        yield x
        x += step
I think one could use a spreadsheet in a macro OK. Dynamic data looks more straightforward.

If you have more parameters in the dd object to change (known functions of theta), they could be put in the loop.

Will PM re file.
User avatar
bambuko
Veteran
Posts: 2185
Joined: Thu Oct 24, 2019 12:53 pm
Location: UK, England, North Devon

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by bambuko »

edwilliams16 wrote: ...
I am making some progress.
Ended up using skeleton sketches, replaced angular sketch constraints with coordinates
(although angular constraints were not causing me problems) and used Dynamic Data for crank angle.
Quite happy with results:
Loco_animation2.gif
Loco_animation2.gif (942.98 KiB) Viewed 1386 times
but... animation (regardless of which macro I use) is still unreliable and sometime corrupts the assembly :oops:
however, rotating to any desired angle (manually modifying Dynamic Data for crank angle) seem to be working fine and without failures,
which is ironic, because I came here asking for help in creating a macro that would do it (thinking that macro was the only way to do it...)
and now I don't really need macro for that, whereas macro for animation is not working very well :roll:
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: rotating Assy 3 model to a desired angle and back to 0deg

Post by edwilliams16 »

Working with your skeleton version (with spreadsheet etc.) with Link branch, I do get motion using a macro driving the spreadsheet cell. However, I get redundant constraint errors in the report view. Is this fixed by your subsequent changes?

Code: Select all

Crank Angle=95.0
09:00:44  0.454658 <asm3.sys> system.py(213): auto relax (test_(skeleton)05#crosshead_body/crosshead_boss(LH)) test__skeleton_05#Constraint020 between (test_(skeleton)05#crosshead_body) test__skeleton_05#Body042 and (test_(skeleton)05#crosshead_boss) test__skeleton_05#Body043, 2
09:00:44  0.001813 <asm3.sys> system.py(213): auto relax (test_(skeleton)05#crosshead_body/crosshead_pin) test__skeleton_05#Constraint022 between (test_(skeleton)05#crosshead_body) test__skeleton_05#Body042 and (test_(skeleton)05#crosshead_pin) test__skeleton_05#Body046, 2
09:00:44  0.002086 <asm3.sys> sys_slvs.py(68): dof remaining: 0
09:00:44  0.067285 <asm3.sys> system.py(213): auto relax (test_(skeleton)05#coupl_rod/motion_sketch(coupl_crankpin)LH) test__skeleton_05#Constraint014 between (test_(skeleton)05#sub-As_motion_sketch_LH) test__skeleton_05#Assembly021 and (test_(skeleton)05#sub-As_coupl_rod_LH) test__skeleton_05#Assembly010, 2
09:00:44  0.001299 <asm3.sys> system.py(213): auto relax (test_(skeleton)05#conn_rod/motion_sketch(crosshead_pin)LH) test__skeleton_05#Constraint018 between (test_(skeleton)05#sub-As_motion_sketch_LH) test__skeleton_05#Assembly021 and (test_(skeleton)05#sub-Ass_conn_rod_LH) test__skeleton_05#Assembly019, 2
09:00:44  0.013855 <asm3.sys> sys_slvs.py(63): redundant constraints
09:00:44  0.000144 <asm3.sys> sys_slvs.py(68): dof remaining: 0
Post Reply