[solved] how to get subselection in order without observer?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

[solved] how to get subselection in order without observer?

Post by dprojects »

Is it possible to get selection list but in the order it was selected ? I mean vertices, sub-objects across many objects not within single object... e.g.

Code: Select all

v1 = FreeCADGui.Selection.getSelectionEx()[0].SubObjects[0]
v2 = FreeCADGui.Selection.getSelectionEx()[1].SubObjects[0]
v3 = FreeCADGui.Selection.getSelectionEx()[0].SubObjects[1]
How to use function FreeCAD.Selection.getPickedList() ? this returns empty array :-(
Last edited by dprojects on Thu Sep 29, 2022 5:49 pm, edited 1 time in total.

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: how to get subselection in order without observer?

Post by dprojects »

My current goal is to deal with irregular shapes, I mean, non-rectangle piece of wood.

I think the best way would be create Sketch and Pad because Pads are well supported and easy to edit via Sketch. But there is "little problem" with Sketches generally. To do it I would have to attach the Sketch to the face because the face, may not be flat with axis. Also the Sketch is created at the wrong place, to be honest I have problem with this approach.

So, I guess maybe the better way will be "extrude". It is good for code but I don't like the idea because the final object is raw. There is no sizes, and can't be edited later? however, after some tests I see the object can be drilled, so this might be supported and edited, after several tools improvements. I think the problems with sizes can be fixed, so this might be the right choice...

However, for any of the approach above I need to know the correct order of vertices, the same order they have been selected by the user to create valid shape. Now I have hard-coded the order so it works. I will probably go into observer way, so I will leave this question open, maybe there is some possibility to get such ordered list ?

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: how to get subselection in order without observer?

Post by openBrain »

I made some searches and can't find a direct solution.
I explored 'getCompleteSelection()' and I find this function pretty useless as, in the case where you selected several items of the same object, it will just return several times the pointer to the object, but nothing that would allow to know the items that was selected. But maybe I missed something.

If I didn't, I think it could be good to upgrade 'getCompleteSelection()' so it does what you expect : return a list of completely described items (subobjects) in the order they were added to the selection.

@wmayer Did I miss something ? Does above modification seem like something feasible to you ?
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: how to get subselection in order without observer?

Post by Chris_G »

In Curves WB I use my own Selection Observer in order to keep Selection Order, and save Camera Orientation :
https://github.com/tomate44/CurvesWB/bl ... y#L84-L121
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: how to get subselection in order without observer?

Post by dprojects »

openBrain wrote: Wed Sep 28, 2022 2:01 pm 'getCompleteSelection()' so it does what you expect : return a list of completely described items (subobjects) in the order they were added to the selection.
No, it returns objects only, not sub-objects. I have selected 4 vertices at the top of 2 objects, and I have only list of objects, ordered yes, but no way to get vertices, sub-selected-objects:

Code: Select all

>>> FreeCADGui.Selection.getCompleteSelection()
[<Part::PartFeature>, <Part::PartFeature>, <Part::PartFeature>, <Part::PartFeature>]
>>> FreeCADGui.Selection.getCompleteSelection()[0]
<Part::PartFeature>
>>> FreeCADGui.Selection.getCompleteSelection()[0].Name
'panelZY001'
>>> FreeCADGui.Selection.getCompleteSelection()[1].Name
'panelZY'
>>> FreeCADGui.Selection.getCompleteSelection()[2].Name
'panelZY'
>>> FreeCADGui.Selection.getCompleteSelection()[3].Name
'panelZY001'
>>> 
It would be fine if I would be able to get from each object the sub-selected-object, but I can't ?
I want the same as you mentioned but in relation to sub-objects only, something like that:

Code: Select all

>>> FreeCADGui.Selection.getSelectionEx()[0].SubObjects[0]
<Vertex object at 0x55a6464e5540>
Last edited by dprojects on Wed Sep 28, 2022 2:21 pm, edited 1 time in total.

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: how to get subselection in order without observer?

Post by openBrain »

dprojects wrote: Wed Sep 28, 2022 2:19 pm
openBrain wrote: Wed Sep 28, 2022 2:01 pm 'getCompleteSelection()' so it does what you expect : return a list of completely described items (subobjects) in the order they were added to the selection.
No, it returns objects not sub-objects. Look I have selected 4 vertices at the top of 2 objects, at the top, and I have only objects list:
That's exactly what I told, but as you cut my sentence out of context ...
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: how to get subselection in order without observer?

Post by dprojects »

openBrain wrote: Wed Sep 28, 2022 2:20 pm
dprojects wrote: Wed Sep 28, 2022 2:19 pm
openBrain wrote: Wed Sep 28, 2022 2:01 pm 'getCompleteSelection()' so it does what you expect : return a list of completely described items (subobjects) in the order they were added to the selection.
No, it returns objects not sub-objects. Look I have selected 4 vertices at the top of 2 objects, at the top, and I have only objects list:
That's exactly what I told, but as you cut my sentence out of context ...
OK ;-) sorry if so, maybe this is possible without observer?

There is also another small question ;-) how to get current cursor hover position at the object ? for example if I hover the edge close to the vertex I would like to be able to calculate the distance to the nearest vertex and decide if this should be selected. I mean that it is rather hard to select vertices, exactly punch the point ;-) so I was thinking about some helper in selection... if you know what I mean

EDIT: I can get hover cursor position at observer selection, but in preselection there is no such thing like "pos". So, if user only hover the edge close to the vertex I can't determine if this is close enough ? This is possible only if user select the edge close to the vertex ?

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: how to get subselection in order without observer?

Post by wmayer »

How to use function FreeCAD.Selection.getPickedList() ? this returns empty array
This is a very strange function and I don't get what it is supposed to do. When selecting an object then it indeed returns an empty list. However, when you call Gui.Selection.enablePickedList() beforehand you get a result with getPickedList() but its behaviour is weird:
  • when selecting sub-elements then not all of them are returned with getPickedList()
  • it doesn't work to support several independent objects
I explored 'getCompleteSelection()' and I find this function pretty useless as, in the case where you selected several items of the same object, it will just return several times the pointer to the object, but nothing that would allow to know the items that was selected. But maybe I missed something.
This actually would be the right function. However, it's only useful in C++ but pretty useless in Python. In C++ you get a list of "SelObj" that keeps all relevant information, like object name, sub-element name, (x,y,z) click point, ...

However, its Python wrapper only returns a list of document objects and ignores all the useful information. So, yes this wrapper must be fixed.
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: how to get subselection in order without observer?

Post by dprojects »

wmayer wrote: Wed Sep 28, 2022 5:29 pm call Gui.Selection.enablePickedList() beforehand you get a result with getPickedList() but its behaviour is weird
I saw there is something like enablePickedList() but I didn't know it should be called before selection ;-) so, it looks like this is some kind of observer embedded... there is also PickedPoints and this is almost what should be... but looks like the result refers only to first object? maybe there is some kind of switch to enablePickedList for all objects ?

Code: Select all

>>> FreeCADGui.Selection.enablePickedList()
>>> FreeCADGui.Selection.getPickedList()[0].PickedPoints
(Vector (18.0, 300.0, 618.0), Vector (18.0, 300.0, 618.0), Vector (18.0, 300.0, 618.0), Vector (18.0, 300.0, 617.7506103515625))

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: how to get subselection in order without observer?

Post by dprojects »

dprojects wrote: Wed Sep 28, 2022 2:25 pm for example if I hover the edge close to the vertex I would like to be able to calculate the distance to the nearest vertex and decide if this should be selected.
However, regarding this thing I made small progress, and if I select edge or face, the nearest vertex is selected. To do this I use observer and calculating distance. I have several problems with addSelection syntax ;-) but I guess finally get it to work and now the vertices selection will be pretty quick and easy, I hope so.

But also need to fix several other issues, with exact direction extrude and calculating size for new object ;-)

Image

Thanks
Darek
github.com/dprojects

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