Transformation of a selected part

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Grumot
Posts: 13
Joined: Fri Mar 07, 2014 2:11 pm

Transformation of a selected part

Post by Grumot »

Hello!

I am wanting to rotate a part in a STEP assembly. I use

Code: Select all

myObj = Gui.Selection.getSelection()[0]
to retrieve a reference to the part that I want to rotate.
But then, I am not allowed to do:

Code: Select all

>>> myObj.Shape.rotate(...)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ReferenceError: This object is immutable, you can not set any attribute or call a non const method
What I finally use, is:

Code: Select all

myShape = myObj.Shape.copy()
myShape.rotate(...)
myObj.Shape = myShape
... but it doesn't sound very clean. Is this code OK? or did I miss the way to directly access the shape?

Thanks!
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Transformation of a selected part

Post by ickby »

Hello,

if you want to rotate the shape than your way is correct. However, you should not do that but rotate the whoole part by setting it's placement property. something like this (untested):

Code: Select all

myObj = Gui.Selection.getSelection()[0]
myObj.Placement = Base.Placement(...)
you can use the various Placement constructors to create rotations and translations and also multiply multiple placements to create successive rotations, like:

Code: Select all

myObj = Gui.Selection.getSelection()[0]
myObj.Placement = myObj.Placement*Base.Placement(...)
Grumot
Posts: 13
Joined: Fri Mar 07, 2014 2:11 pm

Re: Transformation of a selected part

Post by Grumot »

I was afraid to directly use the placement since the placement of this part is somehow already very complicated (probably due to the STEP generation from the orginal CAD software). But if we can combine the placements, it is a very good news.

That is exactly the answer I was looking for. Thank you, it seems way much better!
Grumot
Posts: 13
Joined: Fri Mar 07, 2014 2:11 pm

Re: Transformation of a selected part

Post by Grumot »

Hi!
ickby wrote: you can use the various Placement constructors to create rotations and translations and also multiply multiple placements to create successive rotations, like:

Code: Select all

myObj = Gui.Selection.getSelection()[0]
myObj.Placement = myObj.Placement*Base.Placement(...)
I ran some tests (yeah... late)
It seems that we should use

Code: Select all

myObj.Placement = Base.Placement(...).multiply(myObj.Placement)
That way does not seem to work when myObj.Placement is already complicated:

Code: Select all

myObj.Placement = myObj.Placement.multiply(Base.Placement(...))
some informations came from viewtopic.php?f=22&t=6228 too
wmayer
Founder
Posts: 20310
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Transformation of a selected part

Post by wmayer »

Yes, if you apply several placements P1, P2, P3, ..., Pn then the final placement is P = Pn * ... * P3 * P2 * P1.
Post Reply