Get Vertex Info of Selected Sketch Line

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
chrishuck
Posts: 4
Joined: Sun Jan 29, 2023 4:52 pm
Location: Akron, OH
Contact:

Get Vertex Info of Selected Sketch Line

Post by chrishuck »

I'm new to scripting in FreeCAD, but I've been able to research various methods for extracting info from selected objects. I've been successful in extracting some info, but the one thing I just cannot seem to figure out is how to get the vertex names of a selected sketch line.

The following code correctly tells me the name of a selected sketch line and that it has two vertex objects as SubShapes. However, I can't figure out how to get the names of those vertex objects.

Code: Select all

Sel = Gui.Selection.getSelectionEx()[0]

print("Sel SubElementNames:", Sel.SubElementNames[0])
print("Sel SubObjects:", Sel.SubObjects[0])
print("Sel SubShapes:", Sel.SubObjects[0].SubShapes)
The report view output is:

12:39:52 Sel SubElementNames: Edge3
12:39:52 Sel SubObjects: <Edge object at 0x55be6e4147a0>
12:39:52 Sel SubShapes: [<Vertex object at 0x55be7222d390>, <Vertex object at 0x55be72547810>]

I know from hovering over each vertex that their names are: vertex5 and vertex6

Ultimately, what I'd like to do is deselect the line and select the vertices instead.

Can anyone shed any light on how to get those names and create a selection with them?

Thanks in advance!

-Chris
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Get Vertex Info of Selected Sketch Line

Post by edwilliams16 »

@chrishuck

This works if you don't use links - that would require a little more spelunking on my part.

Code: Select all

#select an Edge
sel = Gui.Selection.getSelectionEx()[0]
edge = sel.SubObjects[0]
obj = sel.Object
vlist = []
for i, v in enumerate(obj.Shape.Vertexes):
    if v.isSame(edge.Vertexes[0]) or v.isSame(edge.Vertexes[1]):
        vlist.append(f'Vertex{i+1}')

Gui.Selection.clearSelection()
Gui.Selection.addSelection(obj, vlist, True)

#check
sel = Gui.Selection.getSelectionEx()[0]
print(f'{sel.ObjectName}  {sel.SubElementNames}')
chrishuck
Posts: 4
Joined: Sun Jan 29, 2023 4:52 pm
Location: Akron, OH
Contact:

Re: Get Vertex Info of Selected Sketch Line

Post by chrishuck »

@edwilliams16

Your solution worked perfectly! I wouldn't have thought to do it that way at all.

Correct me if I'm wrong, but this is my interpretation:

You're comparing the vertexes in the selection subObject against all of the vertexes in the Sketch object, which is ultimately the base of the selection. If they match, append the list, being careful to add 1 to the names because of the index base difference in the OCC kernel.

I'll be completely honest when I say that there's a lot about Python that I still need to learn, but I know enough from other languages that I can usually look at code and deduce what's going on.

Thank you again.

-Chris
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Get Vertex Info of Selected Sketch Line

Post by edwilliams16 »

chrishuck wrote: Sun Jan 29, 2023 9:14 pm
Correct me if I'm wrong, but this is my interpretation:

You're comparing the vertexes in the selection subObject against all of the vertexes in the Sketch object, which is ultimately the base of the selection. If they match, append the list, being careful to add 1 to the names because of the index base difference in the OCC kernel.

That is correct.

Here is a link-aware solution. This is more complicated because the same object can be linked into multiple locations. The documentation on this is sparse. I suspect there's a cleaner way, but I haven't found a better way than hacking the paths to the vertices.

Code: Select all

#link aware selection
sel = Gui.Selection.getSelectionEx('',0)[0]
path = sel.SubElementNames[0]
edge = sel.Object.getSubObject(path)
edgev1 = edge.Vertexes[0]
edgev2 = edge.Vertexes[1]
#print(f'{edgev1.Point} {edgev2.Point}')
i = 1
vlist = []
while True:
    splitonperiod = path.rsplit('.',1)
    if len(splitonperiod) == 2:
        vertexpath  = splitonperiod[0] + '.Vertex' + str(i)
    else:
        vertexpath  = 'Vertex' + str(i)

    vertex = sel.Object.getSubObject(vertexpath)   
    if not vertex or len(vlist) == 2:
        break
    
    #print(vertexpath)
    #print(f'{vertex.Point}')
    if vertex.isPartner(edgev1) or vertex.isPartner(edgev2):
        vlist.append(vertexpath)

    i += 1

Gui.Selection.clearSelection()
Gui.Selection.addSelection(sel.Object, vlist, True)

#check
sel = Gui.Selection.getSelectionEx('',0)[0]
print(f'{sel.ObjectName}  {sel.SubElementNames}')

danish
Posts: 1
Joined: Fri Jan 27, 2023 7:42 am
Contact:

Re: Get Vertex Info of Selected Sketch Line

Post by danish »

i have array of vertices like in next example and it creates strange face. all vertices follows one by another and edges directions everywhere are same (from first vertice to last). in both right coinciding edges face (circled yellow) looks fine, but left one cuts triangle (circled red).
chrishuck
Posts: 4
Joined: Sun Jan 29, 2023 4:52 pm
Location: Akron, OH
Contact:

Re: Get Vertex Info of Selected Sketch Line

Post by chrishuck »

@edwilliams16

Thank you for providing the additional solution! It does make me feel better to hear that the documentation is sparse, because I dug and dug a lot before resorting to posting on here. I probably should have done it sooner, but I wanted to try to figure it out by reading the documentation.

Cheers!

-Chris
Post Reply