Moving a rotated Part in Global XYZ

Need help, or want to share a macro? Post here!
freedman
Posts: 1978
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Moving a rotated Part in Global XYZ

Postby freedman » Mon May 03, 2021 3:51 pm

What I am calling Global is also the screen XYZ. When I create a Part the placement angle is set to zero so everything is fine but once Part rotation occurs then moving the Part in the global XYZ gets complicated and that's what I want to do. That's my first question: Is there a way to move a rotated Part in the Global XYZ without doing a bunch of calculations?

If I'm stuck with loosing easy editing of global XYZ placements on a rotated Part what do need to do?
Here is my second question: How to go about doing this, I hope someone has done this already :), here are a couple thoughts to move 1 mm.
1) Make a copy of Part (and hide it) and rotate it to zero, then move the 1 mm, then rotate back to the initial angle and copy placement to the original Part.
2) Do the trig. required to move the Part at angle 1 mm; This would be less accurate if moved a bunch.

The big picture if I see correctly: When Part is created it aligns to the global XYZ and can be moved easily (global XYZ) thru preferences but once Part is rotated the direct editing of the placement (global XYZ) is lost. It seems like the global XYZ data of a Part should be available but how to access it?????

Thanks
chrisb
Posts: 35280
Joined: Tue Mar 17, 2015 9:14 am

Re: Moving a rotated Part in Global XYZ

Postby chrisb » Mon May 03, 2021 5:48 pm

In the Placement dialog, check "Apply incremental changes".
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
freedman
Posts: 1978
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Moving a rotated Part in Global XYZ

Postby freedman » Mon May 03, 2021 7:02 pm

chrisb, while I'm not using the dialog I did read this:

Code: Select all

getGlobalPlacement()   # return the GlobalPlacement
I didn't know this existed. If it does what it says it is exactly what I need.

Thank you
User avatar
onekk
Posts: 958
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Moving a rotated Part in Global XYZ

Postby onekk » Tue May 04, 2021 10:54 am

Are you trying to rotate with python?

Placement is the orientation in the Global plane, there is no concept of local and global plane in Freecad.

You could do many things, the most easy to understand and quite good is using matrix (it have some problem with complex rotation and the usual gimbal lock problems) but if you do:

Code: Select all

m = obj.Placement.Matrix
m.rotX(90)
m.move(Vector(10,20,0))
m.rotZ(75)

obj.Placement.Matrix = m

As example to see what are the methods try to do:

Code: Select all

print(dir(FreeCAD.Placement.Matrix))
And you are presented with a list of methods and attribute of Matrix.

Now I'm on mobile so no FreeCAD available to check code so I'm using my Impaired memory, probably there are some errors in the methods name.

Hope it helps.

Regards

Carlo D.
Il mio Blog: https://onekk-maker.blogspot.com/

Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc
edwilliams16
Posts: 432
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Moving a rotated Part in Global XYZ

Postby edwilliams16 » Tue May 04, 2021 8:42 pm

If you just want to move (translate an object), just add the translation vector to its Placement.Base

Code: Select all

moveBy = App.Vector(10, 20, 30)
pl = obj.Placement
pl.Base = pl.Base + moveBy
moves the object by moveBy

If you want to move and rotate again, it gets more complicated.
openBrain
Posts: 5913
Joined: Fri Nov 09, 2018 5:38 pm

Re: Moving a rotated Part in Global XYZ

Postby openBrain » Tue May 04, 2021 8:50 pm

I dealt with rotations of objects nested in Part containers in Macro StraightenObject. You can have a look there.
edwilliams16
Posts: 432
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Moving a rotated Part in Global XYZ

Postby edwilliams16 » Tue May 04, 2021 9:01 pm

Looks like you can also use:

Code: Select all

obj.Placement.move(moveBy)
It does exactly the same as

Code: Select all

obj.Placement.Base = obj.Placement.Base + moveBy 
Take your pick!
freedman
Posts: 1978
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Moving a rotated Part in Global XYZ

Postby freedman » Wed May 05, 2021 7:04 am

Thanks all, I will digest the responses.
The goal of the macro is to select and drag/stack Part objects with a mouse and also rotate with a keyboard, it has options to drag different levels of Part, like Part in Part in Part. This all works until a parent Part is rotated.

The issue is this funtion,

Code: Select all

 self.listObjects = FreeCADGui.ActiveDocument.ActiveView.getObjectsInfo(v.getCursorPos(),0)      
I use this to get cursor position and reference it to the Part, once referenced I can directly move the mouse and move the Part or assembly of Parts. The problem is once any parent Part is rotated there is a new angled correlation between the mouse and angled placement as I move the mouse. The Part moves the correct amount just in the wrong angled direction.

It seems like a trig. fix might do it but I have been down this road a few times, I get the trig. working for X,Y and then Z comes into the picture. :?

I looked at using global placement but I still have the issue with the above function. I'm kind of stuck but I will look at what openBrain has done.
Thanks
User avatar
onekk
Posts: 958
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Moving a rotated Part in Global XYZ

Postby onekk » Wed May 05, 2021 3:52 pm

Take in account that each world has his rotation.

When you deal with mouse positions you are dealing with Coin3D in other word the visualization part of FreeCAD.

when you are using objects, they are stored and modeled by the "modeling engine" in other word OCCT.

So if say Coind3D use clockwise rotation convention and OCCT use counterclockwise (that is more common when dealing with mathematicha things, as it is the trigonometric convention), the angles you obtain from Coin3D are not "compatible" with those needed by OCCT.

For simple rotations, it is simple as invert them, for complex rotation, it is not so simple, but I think that in FreeCAD there is somewhere some helpers methods to do conversions between Coin3D and OCCT, to have as example the correct coordinates of a point, in the simpler user case you have to take in account the fact that zoom level might alter the correspondance by screen coordinates (that in good approximations are pixels) and "3d world" dimensions, so some math is involved, and is done somewhere in FreeCAD code.

To know where it is, or you try to inspect sources, or maybe someone might step in and gave some precise location, or methods to be used.

Hope it helps

Carlo D.
Il mio Blog: https://onekk-maker.blogspot.com/

Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc
freedman
Posts: 1978
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Moving a rotated Part in Global XYZ

Postby freedman » Wed May 05, 2021 7:08 pm

onekk wrote
to have as example the correct coordinates of a point, in the simpler user case you have to take in account the fact that zoom level might alter the correspondance by screen coordinates (that in good approximations are pixels) and "3d world" dimensions, so some math is involved, and is done somewhere in FreeCAD code.
I couldn't put that thought in words but Yes! that's what I was thinking. :)

I rotated a parent Part 180 and everything seems to move reversed so I think with XY swap and some trig., that might do the job, I'm not sure about what happens when I move in the Z direction.

I'm thinking about another approach but it has some issues also; What I want is to keep the Part (visually) under the cursor and ".getCursorPos()" does report what objects are under it. I might be able to track what ".getCursorPos()" is reporting and move the Part so it stays under the cursor. The only problem is when there is a large cursor movement.

Thanks