Sketch vertex, index renumbering after edit?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Sketch vertex, index renumbering after edit?

Post by freedman »

I would like to use (in my macro) a few stand alone sketch vertex for my project and we normally ID vertex by index number. The useage of index is fragile during deletions of any kind, all the indexes could change.

Has anyone figured out a way to keep track of a vertex after deletions, the only thing I can think of is to use constraints and store the constraint values in added properties to later ID the vertex?
Can we add attributes to a Vertex, is there some way to add a unique number or string?
Thanks
chrisb
Veteran
Posts: 54280
Joined: Tue Mar 17, 2015 9:14 am

Re: Sketch vertex, index renumbering after edit?

Post by chrisb »

freedman wrote: Wed Dec 15, 2021 7:02 pm Has anyone figured out a way to keep track of a vertex after deletions, the only thing I can think of is to use constraints and store the constraint values in added properties to later ID the vertex?
That's the topological naming problem, and I'm sure you have heard about it. Realthunder has improved it in his branch, and I'm sure you have heard about it too.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Sketch vertex, index renumbering after edit?

Post by freedman »

That's the topological naming problem, and I'm sure you have heard about it.
In this case that's not what I'm referring to, lets say I make 8 vertex in a row 1 thru 8, then delete vertex #4, if I then go and check the indexes they are 1 thru 7. The indexes for what was 5 thru 8 are now 4 thru 7. This means I have no way to ID vertexes after a deletion. Can you think of some way I can give each of them some kind of unique identifier?
Thanks
chrisb
Veteran
Posts: 54280
Joined: Tue Mar 17, 2015 9:14 am

Re: Sketch vertex, index renumbering after edit?

Post by chrisb »

Vertex#8 before deletion is not Vertex#7 after deletion. I don't know how to map "before" to "after", but still think it's a TNP issue.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Pauvres_honteux
Posts: 728
Joined: Sun Feb 16, 2014 12:05 am
Location: Far side of the moon

Re: Sketch vertex, index renumbering after edit?

Post by Pauvres_honteux »

Wonder if using realthunders build would work?
I'm thinking of the "publish outside of sketch"-thingie (can't remember what he called it) he made. Perhaps it can circumvent your issues?
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Sketch vertex, index renumbering after edit?

Post by freedman »

I get the feeling the index is just a counting of the vertex, just the offset in the array structure.
chrisb
Veteran
Posts: 54280
Joined: Tue Mar 17, 2015 9:14 am

Re: Sketch vertex, index renumbering after edit?

Post by chrisb »

Fo me this is rather theoretically, because I don't delete any vertices by number. I edit sketches which results in a geometry having more or less vertices, and I use the sketches (or primitives or booleans) and create solids and these again have more or less vertices.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Sketch vertex, index renumbering after edit?

Post by onekk »

As you are asking in a Scripting forum and no code is given, maybe some things are not clear.

Abstract problems are more difficult to point out than some real code or some image.

Key points I suppose are:
  1. What do you want to achieve?
  2. From what you are starting?



Some suppositions:

If you are dealing with an existing sketch, you could ever find a way to retrieve vertexes, and maybe constraints and operate on them recreating the sketch from start.

If you are coding a Macro that operate on a sketch, it is another "user case", so maybe a "redraw from start" could be not feasible.

When starting from scratch, in other word if you are coding using simply coordinates that are transformed in vertex and joined I see no reason why not restart from scratch, when i delete a vertex, and maybe it is not necessary to create even a sketch, you could analyze simply "coordinates" maybe a list of Vectors and then after having keep "correct building bricks" create the sketch from scratch.

If you want to make other things, you could simply code directly the Polygon using Part.makePolygon and the transform in a Face and extrude it obtaining a solid without passing for a Sketch, in this case, you vertexes and their placement are left to your computations and placements, according maybe to some formulas.


A simple example:

Code: Select all

# This method will calculate a polygon supplying simply the center, number of sides, diameter and an align flag that roatate by a specific amount the polygon to align it to one axis

def poly(center=Vector(0, 0, 0), sides=6, dia=6, align=True):

    ang_dist = math.pi / sides
    
    rad = dia * 0.5

    theta = 0.0 if align else ang_dist

    print("Theta: ",theta)

    vertex = []

    for n_s in range(0, sides+1):
        vpx = rad * math.cos((2 * ang_dist * n_s) + theta) + center[0]
        vpy = rad * math.sin((2 * ang_dist * n_s) + theta) + center[1]
        vertex.append(Vector(vpx, vpy, 0))

    return vertex

lati = 8
diam_ext = 100
diam_int = 50

# star "external points"
esterno = poly(Vector(0,0,0), lati, diam_ext, True)

# star "internal points"
interno = poly(Vector(0,0,0), lati, diam_int, False)

# alternate internal and external point to make the star
star_points = []
for p_idx, point in enumerate(esterno):
    star_points.append(point)
    star_points.append(interno[p_idx])

# due the way it is costructed last point has to be deleted
star_points.pop()
# and first point has to be added to "close" the polygon
star_points.append(esterno[0])

print(star_points)

# points are passed to Part.makePolygon
stella = Part.makePolygon(star_points)

# a face is created
face = Part.Face(stella)
All things are calculated no need to decimate points or assigning constraints, same could be done for more complex forms or wires.

This way you have simply a list of points and if you delete an element the order is preserved, no TNP as points (vertexes) are simply not even "named".

as TNP mean Topological Naming Problem you are safe from all, as there is no use of a "name" to retrieve things.

And if you want to name them simply make a list of list with each element having a "name" and a position, like.

Code: Select all

named_points = [
["point1", Vector(0, 0, 0)],
["point2", Vector(0, 10, 0)].
.....
["point123", Vector(10, 150, 0),
]
if you delete the nth element the name is retained so "point30" is always "point30" except if you delete it.

you could even pop() it and retain maybe in another list what you have deleted or keep the original list and create a different "decimated point list".

Sketch is usually a "manual task" that do some checks and apply constraints to make things parallel , perpendicular or join lines.

When you have the power of the scripting and maybe even use some numpy or other elaborated library like in FreeCAD, you are not forced to mimick the "manual way".

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
jbi
Posts: 118
Joined: Sun Apr 24, 2016 3:28 pm

Re: Sketch vertex, index renumbering after edit?

Post by jbi »

Instead of the index use vertex.hashCode() So maintain a parallel list of vertex.hashCode()
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Sketch vertex, index renumbering after edit?

Post by heron »

onekk wrote: Thu Dec 16, 2021 12:10 pm A simple example:
Hello, I was playing with your macro simply to practice PythonFC. I realized that in the "star_points" list the two last points are the same, therefore the line that adds it can be eliminated:

Code: Select all

# and first point has to be added to "close" the polygon
star_points.append(esterno[0])
On the other hand, to see the list of points more clearly I have also replaced the line:

Code: Select all

print(star_points)
for this other

Code: Select all

[print(i) for i in star_points]
Your macro is cool, many thanks!
Attachments
Captura1.PNG
Captura1.PNG (23.66 KiB) Viewed 1877 times
Post Reply