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

Need help, or want to share a macro? Post here!
User avatar
bambuko
Posts: 519
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)

Postby bambuko » Fri May 14, 2021 3:32 pm

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 1308 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 1308 times
Last edited by bambuko on Tue Jun 08, 2021 8:45 am, edited 3 times in total.
chrisb
Posts: 35233
Joined: Tue Mar 17, 2015 9:14 am

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

Postby chrisb » Fri May 14, 2021 5:17 pm

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
Posts: 519
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

Postby bambuko » Fri May 14, 2021 6:03 pm

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...
edwilliams16
Posts: 425
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

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

Postby edwilliams16 » Fri May 14, 2021 6:33 pm

Code: Select all

FreeCAD.ActiveDocument.getObject("Constraint016").Angle = 0
doesn't reset your model? What does it do?
User avatar
bambuko
Posts: 519
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

Postby bambuko » Fri May 14, 2021 7:06 pm

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).
mario52
Posts: 3519
Joined: Wed May 16, 2012 2:13 pm

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

Postby mario52 » Fri May 14, 2021 7:08 pm

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, Dialog creation. My macros on Gist.github.
User avatar
bambuko
Posts: 519
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

Postby bambuko » Fri May 14, 2021 7:33 pm

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 :!:
edwilliams16
Posts: 425
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

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

Postby edwilliams16 » Fri May 14, 2021 9:25 pm

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
Posts: 519
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

Postby bambuko » Sat May 15, 2021 9:45 am

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
edwilliams16
Posts: 425
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

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

Postby edwilliams16 » Sat May 15, 2021 1:59 pm

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.