[SOLVED]How to get the coordinates of a point at the Global Coordinate System ?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

[SOLVED]How to get the coordinates of a point at the Global Coordinate System ?

Post by manos »

Hello
The following code gives the coordinates of a selected point.

Code: Select all

o =FreeCADGui.Selection.getSelectionEx()[0].SubObjects[i]
	ap1=[o.X,o.Y,o.Z,1]

But unfortunately these are coordinates of Local Coor. System.
So the question of the day: How can I get the coordinates of a selected point at the Global Coordinate System ?
Attachments
Box1.FCStd
(7.7 KiB) Downloaded 26 times
Last edited by manos on Wed Sep 14, 2022 4:05 pm, edited 1 time in total.
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to get the coordinates of a point at the Global Coordinate System ?

Post by Chris_G »

Code: Select all

sel = FreeCADGui.Selection.getSelectionEx()[0]
obj = sel.Object
sub = sel.SubObjects[0]
if hasattr(obj, "getGlobalPlacement"):
    gpl = obj.getGlobalPlacement()
    sub.transformShape(gpl.Matrix)

manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: How to get the coordinates of a point at the Global Coordinate System ?

Post by manos »

Chris_G wrote: Wed Sep 14, 2022 10:21 am

Code: Select all

sel = FreeCADGui.Selection.getSelectionEx()[0]
obj = sel.Object
sub = sel.SubObjects[0]
if hasattr(obj, "getGlobalPlacement"):
    gpl = obj.getGlobalPlacement()
    sub.transformShape(gpl.Matrix)
    
print(' gpl=',gpl)

I got this answer: " gpl= Placement [Pos=(0,0,0), Yaw-Pitch-Roll=(0,0,0)]"

Thanks so much Chris_G but it is not the placement parameters I look for.
Ideally coordinates like those at preselection panel would be my best.
Any help ? Any merciful human being?
Last edited by manos on Wed Sep 14, 2022 12:12 pm, edited 1 time in total.
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to get the coordinates of a point at the Global Coordinate System ?

Post by Chris_G »

If I select a vertex of your box example, I get the global coordinates :

Code: Select all

sel = FreeCADGui.Selection.getSelectionEx()[0]
obj = sel.Object
sub = sel.SubObjects[0]

print(sub.Point)
# >>> Vector (10.0, 0.0, 10.0) = Local Coordinate System

if hasattr(obj, "getGlobalPlacement"):
    gpl = obj.getGlobalPlacement()
    sub.transformShape(gpl.Matrix)

print(sub.Point)
# >>> Vector (11.198563820557883, -2.9883623873011977, 17.03744871391589) = Global Coordinate System

openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to get the coordinates of a point at the Global Coordinate System ?

Post by openBrain »

The best I know that also support Links is :

Code: Select all

sel = Gui.Selection.getSelectionEx('',0)[0]
obj = sel.Object
subobj = sel.SubElementNames[0]
coord = obj.getSubObject(subobj).Point

App.Console.PrintWarning(f"Global coordinates of {subobj} : {coord}\n")
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: How to get the coordinates of a point at the Global Coordinate System ?

Post by manos »

Splendid @Chris_G.
Thanks again.
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: [SOLVED]How to get the coordinates of a point at the Global Coordinate System ?

Post by manos »

Thanks @openBrain.
I will try it.
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [SOLVED]How to get the coordinates of a point at the Global Coordinate System ?

Post by edwilliams16 »

Just to clarify. If you use @Chris_G 's code on a vertex of a linked object, you'll get the global coordinates of the original, not the link. With @openBrain 's code you'll get the global coordinates of the linked object's vertex that you selected. This is all moot if you don't use links. Try it on the enclosed file.

To get the global placement of a selected linked object, you can use

Code: Select all

for sel in Gui.Selection.getSelectionEx('',0):
  for path in sel.SubElementNames if sel.SubElementNames else ['']:
    placement = sel.Object.getSubObject(path, retType=3) # retType 3 returns the placement. Check the doc string for more info
    print('%s.%s -- %s' % (sel.Object.Name, path, placement))

Attachments
Box1Link.FCStd
(8.7 KiB) Downloaded 24 times
Post Reply