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
[Solved] Retrieve 3 selected points snippet code
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[Solved] Retrieve 3 selected points snippet code
Last edited by johnwang on Sat Sep 18, 2021 2:03 am, edited 2 times in total.
Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points
Hi
you must select 3 points on 3 objects different
(1 point by one object)
mario
you must select 3 points on 3 objects different
(1 point by one object)
Code: Select all
sel = FreeCADGui.Selection.getSelection()
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.
My macros on Gist.github here complete macros Wiki and forum.
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points
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")
Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points
OK, I was picking from one BooleanFragments object.
Thank you very much.
Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points
Works perfectly.TheMarkster wrote: ↑Fri Sep 17, 2021 7:36 pm getSelectionEx() can be used if you want subobjects (faces, edges, vertices).
Thank you very much.
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: Code_snippets question: Retrieve_the_coordinates_of_3_selected_points
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:johnwang wrote: ↑Sat Sep 18, 2021 2:00 amWorks perfectly.TheMarkster wrote: ↑Fri Sep 17, 2021 7:36 pm getSelectionEx() can be used if you want subobjects (faces, edges, vertices).
Thank you very much.
[sel1.Object, sel2.Object, sel3.Object...]
Re: [Solved] Retrieve 3 selected points snippet code
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
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))