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!
Post Reply
User avatar
bambuko
Veteran
Posts: 2165
Joined: Thu Oct 24, 2019 12:53 pm
Location: UK, England, North Devon

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

Post by bambuko »

I have an excellent macro (created by someone else - I am useless :oops: ) to animate Assy 3 model of steam loco.
It happily rotates full 360 deg, making a great animation:

Loco_Animation_4.gif
Loco_Animation_4.gif (740.57 KiB) Viewed 2468 times

but... animation, whilst nice (and I am sure impressive) doesn't do much for my design process.
I would like to be able to rotate it to a desired angle, do what I need to do at this angle, and re-set back to original.

This macro, kind of allows me to rotate to a specific desired angle (by editing macro values).
It doesn't do very well re-setting back to (what I consider) 0deg (or starting position).
Trouble is that if the rotation is less than 45deg resetting works fine, more than that and if I reset constraint angle to 0deg,
model springs back to the nearest (vertical or horizontal) axis.


This is, of course, no criticism of the current macro - it is really meant to simply animate the model,
whereas I want it to do something different...), hence the question:
Is there a better way of doing what I am trying to do, please?

macro, I am using at the moment:
macro.jpg
macro.jpg (213.19 KiB) Viewed 2468 times
Last edited by bambuko on Tue Jun 08, 2021 8:45 am, edited 3 times in total.
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
chrisb
Veteran
Posts: 53933
Joined: Tue Mar 17, 2015 9:14 am

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

Post by chrisb »

An image of the macro doesn't help as much as the macro itself.
You can create a new macro with lines 19, 20, 22 where you set the desired angle.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
bambuko
Veteran
Posts: 2165
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 »

chrisb wrote: Fri May 14, 2021 5:17 pm An image of the macro doesn't help as much as the macro itself...
sorry, having difficulty attaching anything right now, so hopefully this is OK?:

Code: Select all

import FreeCAD

import time

angle = 0
for i in range(361):
    FreeCAD.ActiveDocument.getObject("Constraint016").Angle = angle
    Gui.runCommand('asm3CmdQuickSolve',0)
    angle -= 1
    FreeCADGui.updateGui()
    time.sleep(0.001)

# Macro End:  +++++++++++++++++++++++++++++++++++++++++++++++++

chrisb wrote: Fri May 14, 2021 5:17 pmYou can create a new macro with lines 19, 20, 22 where you set the desired angle.
Setting desired angle is the least of my problems ;)
(although not exactly clear how you suggest to do it..)
I do it at the moment using line 18.

Defining what 0deg is and being able to re-set back to it after any change is the main challenge...
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
edwilliams16
Veteran
Posts: 3111
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 »

Code: Select all

FreeCAD.ActiveDocument.getObject("Constraint016").Angle = 0
doesn't reset your model? What does it do?
User avatar
bambuko
Veteran
Posts: 2165
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 14, 2021 6:33 pm ...doesn't reset your model? What does it do?
No, as I explained in my post, it snaps it to the nearest axis.
So, for example, if I move it to 120deg and want to re-set it to 0deg, it actually ends up at 90deg.

It works fine if the rotation is less than 45deg from 0deg(origin).
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

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

Post by mario52 »

hi bambuko

you have time the you have this beautiful macro Macro_Rotate_To_Point

principe : all manually rotation and translation step by step mm by mm degrees by degrees

the macro create one macro with all coordinate Placement and use this macro with different options

Q to Quit
D to Decrease speed
P to Pause/Continue or key RETURN or ESCAPE
S to Step by Step
R to Reverse
M for this message


example
the macro file created and the FCStd file associated Animation Pleuel-Stangen AWB

Image

other example

Image

enjoy with your beautiful tchouff tchouff

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
User avatar
bambuko
Veteran
Posts: 2165
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 »

mario52 wrote: Fri May 14, 2021 7:08 pm hi bambuko
enjoy with your beautiful tchouff tchouff
:mrgreen:

Thank you mario52
I will explore your macro and if it works with my Assembly 3 model - it is precisely what I need :!:
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
edwilliams16
Veteran
Posts: 3111
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 »

Without a file to test on, I'm working totally blind, but the following should get past what looks to be a "flipping root" problem, by rotating one degree at a time:

Code: Select all

angle = int(FreeCAD.ActiveDocument.getObject("Constraint016").Angle)
while angle != 0:
    angle = angle - sign(angle)
    FreeCAD.ActiveDocument.getObject("Constraint016").Angle = angle
    Gui.runCommand('asm3CmdQuickSolve',0)
FreeCADGui.updateGui()
If this works we can likely speed it up by rotating in bigger chunks.
User avatar
bambuko
Veteran
Posts: 2165
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 »

Thank you edwilliams16

edwilliams16 wrote: Fri May 14, 2021 9:25 pm Without a file to test on, I'm working totally blind..
If you are interested and fancy a challenge ;) I would be more than happy to put the file into some dropbox.


Used original macro to rotate things to a certain angle, and then tried your macro to rotate things back to 0deg.
(I have assumed that this was the intention? apologies if I got it wrong :oops: )

I am getting the following error message:
angle = angle - sign(angle)
<class 'NameError'>: name 'sign' is not defined
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
edwilliams16
Veteran
Posts: 3111
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 »

Oops, sign is a numpy function, not bare python

Replace that line of code with

Code: Select all

    angle = angle - cmp(angle, 0)
Yes this code is to zero the angle, starting anywhere. We can go the dropbox route if this doesn’t do it.
Post Reply