Mesh Boundary

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
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Mesh Boundary

Post by HakanSeven12 »

Hi I want to fint the boundary of a mesh. My code working partially. Can someone help me?

Code: Select all

import Part

obj = App.ActiveDocument.getObject("Surface")
bigbro = 4294967295

border=[]
for i in obj.Mesh.Facets:
    for index in range(0,3):
        if bigbro == i.NeighbourIndices[index]:
            prev = i.Points[index-1]
            next = i.Points[index-2]
            current = i.Points[index]
    
            if prev in border:
                idx = border.index(prev)
                border.insert(idx+1, current)
    
            if next in border:
                idx = border.index(next)
                border.insert(idx, current)
            else:
                border.append(current)

pl=Part.makePolygon(border)
Part.show(pl)
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Mesh Boundary

Post by heda »

Code: Select all

# free boundary of mesh
# handles multiple free boundaries
# by heda @ fc-forum 2021

import Part

import itertools as itools
from collections import Counter
from ast import literal_eval

obj, = Gui.Selection.getSelection()

_, facet_pidx = obj.Mesh.Topology

edges = itools.chain(*(itools.permutations(pidx, 2) for pidx in facet_pidx))
count = Counter((str(edge) for edge in edges))

double_boundary = list((literal_eval(k) for k, v in count.items() if v == 1))

boundary = double_boundary[:1]
for candidate in double_boundary[1:]:
    if candidate in boundary or candidate[::-1] in boundary:
        pass
    else:
        boundary.append(candidate)

def mkEdge(p1, p2):
    return Part.makeLine((p1.x, p1.y, p1.z), (p2.x, p2.y, p2.z))

points = obj.Mesh.Points
edges = list()
for p1, p2 in boundary:
    edges.append(mkEdge(points[p1], points[p2]))

for opening in Part.sortEdges(edges):
    Part.show(Part.Wire(opening))
User avatar
Chris_G
Veteran
Posts: 2596
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Mesh Boundary

Post by Chris_G »

Wow, nice !
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: Mesh Boundary

Post by HakanSeven12 »

Yes it works. Thank you so much ;)
schöne
Posts: 12
Joined: Fri Nov 25, 2022 6:42 pm

Re: Mesh Boundary

Post by schöne »

I run the code from heda but the result was like that
empty list
does anyone know why it return empty list ?
Last edited by schöne on Sun Nov 27, 2022 4:26 pm, edited 1 time in total.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Mesh Boundary

Post by heda »

"does anyone know why it return empty list"

nope, and no one will ever know unless you make the mesh you tried it on available

fwiw, here's a guess though.
you do not have any free edges in the mesh, i.e. there is no boundary to be found.
the algo does free edges, not crest edges.

dunno if anyone has made crest edge detection for fc, don't think so but could be wrong, in any case not overly hard to make a naive crest edge algo, so if that is what you are after - feel free to roll your own, or use meshlab, blender, or similar, which has some form of crest detect already
schöne
Posts: 12
Joined: Fri Nov 25, 2022 6:42 pm

Re: Mesh Boundary

Post by schöne »

ok thank you so much
Last edited by schöne on Sun Nov 27, 2022 4:25 pm, edited 1 time in total.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Mesh Boundary

Post by heda »

if you do not want to bother with preparing an understandable question.
just use https://github.com/mwganson/MeshRemodel to make a wireframe,
break it apart and delete what you do not want from the complete wireframe.
you can box-select for deletion, so there is no reason why it should not be quick enough to get it done.
if you mesh is large, one can always split it into chunks first (also with fc).

or, you really have to attach a mesh and an image of that mesh with what you want done (for example handmarked edges).
preferably for something simple so the workflow is in focus, not the model size or something else.

why should you go through the trouble of making an example of your question...
simple, it saves everyone time, including your own...
a question that cannot be understood, well, it tends to be unanswered...

if you go ahead an prepare a proper question,
do it in the other thread you made, since have a feeling your question has little to do with the original content of this thread.

@moderators, as far as i am concerned the recent posts in this thread could be moved to the other thread the poster created https://forum.freecadweb.org/viewtopic.php?f=22&t=73910
Post Reply