Position and orientation in the reference frame of a LCS

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Position and orientation in the reference frame of a LCS

Post by openBrain »

carlopav wrote: Sat Mar 05, 2022 2:04 pm Se more thoughts? To me this is quite a crucial topic and would be nice if we could have a consistent api exposed to the developers...
Actually no, not really enough time for this. But I still think there is something to be improved there. ;)
josegegas
Posts: 255
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: Position and orientation in the reference frame of a LCS

Post by josegegas »

Hello.

Just to clarify my question. Imagine a simple draft point:

point = Draft.makePoint(X, Y, Z)

then such a point will be at (X,Y,Z) relative to the global reference frame.

Now, imagine a local coordinate system that is at any position and orientation relative to the global coordinate system.

The question is, how can I get the position of the point, but as viewed from the local coordinate system?

I know I can do some simple geometry or a transformed coordinate system for this, but I though FreeCAD may already have this option...

The same applies for the position of the points that define a line.
User avatar
Roy_043
Veteran
Posts: 8550
Joined: Thu Dec 27, 2018 12:28 pm

Re: Position and orientation in the reference frame of a LCS

Post by Roy_043 »

Code: Select all

import FreeCAD as App
import Draft

doc = App.newDocument()

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1000, 500, 0)

line = Draft.make_line(p1, p2)
place = line.Placement
place.rotate(App.Vector(0, 0, 0), App.Vector(1, 1, 1), 35)

doc.recompute()

global_point = place.multVec(line.Points[1])
print(global_point)
local_point = place.inverse().multVec(global_point)
print(local_point)
ryryss
Posts: 11
Joined: Tue Sep 07, 2021 1:31 am

Re: Position and orientation in the reference frame of a LCS

Post by ryryss »

Roy_043 wrote: Sun Jul 17, 2022 2:00 pm

Code: Select all

import FreeCAD as App
import Draft

doc = App.newDocument()

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1000, 500, 0)

line = Draft.make_line(p1, p2)
place = line.Placement
place.rotate(App.Vector(0, 0, 0), App.Vector(1, 1, 1), 35)

doc.recompute()

global_point = place.multVec(line.Points[1])
print(global_point)
local_point = place.inverse().multVec(global_point)
print(local_point)
Hi, Roy
How to get the global location of an object in a link group? when the link group move, the placement information of objects in the group is not updated.
User avatar
Roy_043
Veteran
Posts: 8550
Joined: Thu Dec 27, 2018 12:28 pm

Re: Position and orientation in the reference frame of a LCS

Post by Roy_043 »

Your code should determine the cumulative effect of the placements (see Option 1). Part.getShape can also be used, but is less efficient if you do not need the shape it returns (see Option 2).

Code: Select all

import FreeCAD as App
import Part

doc = App.ActiveDocument
prt = doc.Part
grp = doc.LinkGroup
lnk = doc.Link
lin = doc.Line


# Option 1
point = lin.Points[1]
for obj in [lin, lnk, grp, prt]:
    point = obj.Placement.multVec(point)

print(point)


# Option 2
shape, matrix, obj = Part.getShape(prt, "LinkGroup.Link", needSubElement=True, retType=2)
place = App.Placement(matrix)

point = lin.Points[1]
point = place.multVec(point)

print(point)
Attachments
Placement_Test_LinkGroup.FCStd
(7.39 KiB) Downloaded 46 times
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Position and orientation in the reference frame of a LCS

Post by edwilliams16 »

Roy_043 wrote: Fri Jul 22, 2022 10:37 am
What is the meaning of this error your file generates when clicking on LinkGroup?

Code: Select all

Error adjustedGlobalPlacement
I get it with

Code: Select all

OS: macOS High Sierra (10.13)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: (HEAD detached at 0.20)
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.9.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: C/Default (C)
Installed mods: 
  * Curves 0.4.4
  * DynamicData 2.46.0
  * fasteners 0.3.44
  * fcgear 1.0.0
  * GDML 1.8.0
  * Help 1.0.2
  * lattice2 1.0.0
  * MeshRemodel 1.8919.0
  * toSketch
but not with

Code: Select all

OS: macOS High Sierra (10.13)
Word size of FreeCAD: 64-bit
Version: 0.21.29393 (Git)
Build type: Release
Branch: master
Hash: 6820e0a9ec85203a6f342ca72a2ff8fd417beaf1
Python 3.10.5, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: C/Default (C)
Installed mods: 
  * Curves 0.4.4
  * DynamicData 2.46.0
  * fasteners 0.3.44
  * fcgear 1.0.0
  * GDML 1.8.0
  * Help 1.0.2
  * lattice2 1.0.0
  * MeshRemodel 1.8919.0
  * toSketch
User avatar
Roy_043
Veteran
Posts: 8550
Joined: Thu Dec 27, 2018 12:28 pm

Re: Position and orientation in the reference frame of a LCS

Post by Roy_043 »

edwilliams16 wrote: Fri Jul 22, 2022 11:43 pm What is the meaning of this error your file generates when clicking on LinkGroup?

Code: Select all

Error adjustedGlobalPlacement
I am pretty clueless.

I don't get the error with V0.20 either BTW.

Code: Select all

OS: Windows 8.1 Version 6.3 (Build 9600)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: English/United States (en_US) [ OS: Dutch/Netherlands (nl_NL) ]
josegegas
Posts: 255
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: Position and orientation in the reference frame of a LCS

Post by josegegas »

I am doing what I think may be the most efficient way to solve this:

Code: Select all

#Create a placement:
p1 = App.Placement() 
#Move to (0,0,1). The "point" is now (0,0,1).
p1.Base = (0,0,1) 
#Create a local coordinate system (initially orthonormal to the global coordinate system):
lcs = App.activeDocument().addObject( 'PartDesign::CoordinateSystem', 'LCS' )
#Get the placement of p1 as seen from LCS (same as global placement of p1):
lcs.Placement.inverse().multiply(p1)
#Move and rotate the LCS:
lcs.Placement=App.Placement(App.Vector(0,0,-1), App.Rotation(0,0,90), App.Vector(0,0,0))
#Get the new placement of p1 as seen from the translated and rotated LCS:
lcs.Placement.inverse().multiply(p1)
Is this correct?
User avatar
dprojects
Posts: 722
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: Position and orientation in the reference frame of a LCS

Post by dprojects »

realthunder wrote: Mon Feb 07, 2022 1:31 am

Code: Select all

Gui.Selection.getSelectionEx('',0):
Is it documented somewhere? is there any API for that? or this is hidden knowledge borrowed from ancient aliens and posted at forum only for chosen? LOL

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
Post Reply