[Solved] Retrieve 3 selected points snippet code

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

[Solved] Retrieve 3 selected points snippet code

Post by johnwang »

Hi,

Tried this on FreeCAD_0.20.25541_Win-LPv12.5.4_vc17.x-x86-64:
https://wiki.freecadweb.org/Code_snippe ... or_objects

I selected 3 points, then run it as a Macro. But it tells me 'Select 3 points exactly'.

cheers,

John
Last edited by johnwang on Sat Sep 18, 2021 2:03 am, edited 2 times in total.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points

Post by mario52 »

Hi

you must select 3 points on 3 objects different

(1 point by one object)

Code: Select all

sel = FreeCADGui.Selection.getSelection()
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points

Post by TheMarkster »

getSelectionEx() can be used if you want subobjects (faces, edges, vertices). It has property called PickedPoints that will be the XYZ coordinates of the selected points, which can be vertices, points along edges, or some point on a face.

Code: Select all

selX = FreeCADGui.Selection.getSelectionEx()
FreeCAD.Console.PrintMessage("Objects selected:\n")
for s in selX:
    FreeCAD.Console.PrintMessage(s.Object.Name + "("+s.Object.Label+")\n")
    for ii in range(0,len(s.PickedPoints)):
        p = s.PickedPoints[ii]
        subName = s.SubElementNames[ii]
        FreeCAD.Console.PrintMessage("     "+subName+"---->(X,Y,Z) = ("+str(p.x)+","+str(p.y)+","+str(p.z)+")\n")
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points

Post by johnwang »

mario52 wrote: Fri Sep 17, 2021 2:23 pm you must select 3 points on 3 objects different
OK, I was picking from one BooleanFragments object.

Thank you very much.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points

Post by johnwang »

TheMarkster wrote: Fri Sep 17, 2021 7:36 pm getSelectionEx() can be used if you want subobjects (faces, edges, vertices).
Works perfectly.
Thank you very much.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points

Post by TheMarkster »

johnwang wrote: Sat Sep 18, 2021 2:00 am
TheMarkster wrote: Fri Sep 17, 2021 7:36 pm getSelectionEx() can be used if you want subobjects (faces, edges, vertices).
Works perfectly.
Thank you very much.
There is a difference, which can be confusing at first, between getSelection() and getSelectionEx(). The first one returns a list of the selected document objects while the second one returns a list of selection objects. For example, if you select a cube and a cylinder and run getSelection() the returned value is a list of objects [cube,cylinder]. But getSelectionEx() returns not a list of document objects, but rather a list of selection objects [sel1, sel2, sel3, sel4, etc.] To get at the selected objects:

[sel1.Object, sel2.Object, sel3.Object...]
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: [Solved] Retrieve 3 selected points snippet code

Post by heda »

this is just a copy of Markster's working code, utilizing some of the "batteries included" in python
just putting an alternative style of code out there, making use of pythons 3 versions of string formatting,
as well as the ability to loop over objects

Code: Select all

selX = FreeCADGui.Selection.getSelectionEx()
PrintMsg = FreeCAD.Console.PrintMessage
PrintMsg("Objects selected:\n")
for sel in selX:
    maxname = max((len(name) for name in sel.SubElementNames)) + 4
    msg = '{:>%d}  --->  (X,Y,Z) = ({:.3f}, {:.3f}, {:.3f})\n' % maxname
    PrintMsg(f'{sel.Object.Name} ({sel.Object.Label})\n')
    for subName, p in zip(sel.SubElementNames, sel.PickedPoints):
        PrintMsg(msg.format(subName, p.x, p.y, p.z))
Post Reply