make faces from edges

Need help, or want to share a macro? Post here!
User avatar
bernd
Posts: 12392
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

make faces from edges

Post by bernd »

I have a bound of edges. I would like to get faces from them by python. but I do not know which edges gone make a face. I just have a bound of edges and for sure they build valid faces. Attached a sample edge file and a screen of the colored (the color is just for better showing) faces I would like to archive.
edges.fcstd
(18.3 KiB) Downloaded 38 times
edges.jpg
edges.jpg (13.99 KiB) Viewed 3305 times
faces.jpg
faces.jpg (18.23 KiB) Viewed 3305 times
User avatar
DeepSOIC
Posts: 7843
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: make faces from edges

Post by DeepSOIC »

User avatar
microelly2
Posts: 4691
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: make faces from edges

Post by microelly2 »

Some fun:
bp_019.png
bp_019.png (48.43 KiB) Viewed 3279 times
This is the image analysis of the edges.jpg

convert to gray -> invert -> threshold/morphing -> skeleton

faces are "not connected" components of the skeleton.
because the edges disconnect them.

Still not implemented (but a goal of the reconstruction wb): backward from skeleton to face: :geek:
bp_020.png
bp_020.png (35.59 KiB) Viewed 3279 times
User avatar
bernd
Posts: 12392
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: make faces from edges

Post by bernd »

I had to play a bit to find how it works in the gui ...

- Part --> Shape builder --> Make a big Face from all outer edges
- Part --> Fuse all edges to a wire
- Part --> Extrude the wire --> the result will be one non-manifold shell
- Part --> Cut The big outer Face and the non-manifold shell --> the result will be a compound of the faces

Perfect !

BTW:
My own idea was exactly the walking one with the lowest clock angle. But I thought I may ask before I start. Very good idea indeed.

@microelly2: nice one
User avatar
wandererfan
Posts: 4078
Joined: Tue Nov 06, 2012 5:42 pm

Re: make faces from edges

Post by wandererfan »

Does this method always work, or have I missed a step? Macro works great with one set of points, but not the second.
2DCutResult.fcstd
(9.25 KiB) Downloaded 46 times
2DCut.py.zip
Not really a zip file.
(2.48 KiB) Downloaded 58 times
OS: Ubuntu 12.04.5 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.7689 +86 (Git)
Build type: debug
Branch: FaceDetect
Hash: 21c8c5b2e68319a9758330b8611d1a7a18594440
Python version: 2.7.3
Qt version: 4.8.1
Coin version: 3.1.3
OCC version: 6.8.0.oce-0.17-dev
User avatar
DeepSOIC
Posts: 7843
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: make faces from edges

Post by DeepSOIC »

wandererfan wrote:Does this method always work, or have I missed a step? Macro works great with one set of points, but not the second.
I looked at the shapes in the project. The cutting shell in the case that fails is actually a compound of two shells. I disassembled it and fused it back together (Lattice Downgrade to faces, then Lattice Fuse Compound). Then the cut worked.

I'll take a look at your macro later...
User avatar
DeepSOIC
Posts: 7843
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: make faces from edges

Post by DeepSOIC »

Fixed script:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# tester for 2D cutting per DeepSOIC's method
import FreeCAD
import Part

org = FreeCAD.Vector(0,0,0)
zDir = FreeCAD.Vector(0,0,1)
xDir = FreeCAD.Vector(1,0,0)
xtr = FreeCAD.Vector(0,0,5)
xlate = FreeCAD.Vector(-100,-100,0)

#v0 = FreeCAD.Vector(0.0,0.0,0.0)      #these points work
#v1 = FreeCAD.Vector(-10.0,-10.0,0.0)
#v2 = FreeCAD.Vector(-10.0,10.0,0.0)
#v3 = FreeCAD.Vector(10.0,10.0,0.0)
#v4 = FreeCAD.Vector(10.0,-10.0,0.0)
v0 = FreeCAD.Vector(-50.000,-10.000,0.000) #these points do not.  order?
v0a = FreeCAD.Vector(-30.00,-10.000,0.000)
v1 = FreeCAD.Vector(30.000,-10.000,0.000)
v1a = FreeCAD.Vector(50.00,-10.000,0.000)
v2 = FreeCAD.Vector(-50.000,10.000,0.000)
v2a = FreeCAD.Vector(-50.00,-10.000,0.000)
v3 = FreeCAD.Vector(50.000,10.000,0.000)
v3a = FreeCAD.Vector(50.00,-10.000,0.000)
v4 = FreeCAD.Vector(-50.000,30.000,0.000)
v4a = FreeCAD.Vector(50.00,30.000,0.000)
v5 = FreeCAD.Vector(-50.000,30.000,0.000)
v5a = FreeCAD.Vector(-50.00,10.000,0.000)
v6 = FreeCAD.Vector(50.000,30.000,0.000)
v6a = FreeCAD.Vector(50.00,10.000,0.000)
v7 = FreeCAD.Vector(-10.000,-30.000,0.000)
v7a = FreeCAD.Vector(10.00,-30.000,0.000)
v8 = FreeCAD.Vector(-50.000,10.000,0.000)
v8a = FreeCAD.Vector(-30.00,-10.000,0.000)
v9 = FreeCAD.Vector(-30.000,-10.000,0.000)
v9a = FreeCAD.Vector(-10.00,-30.000,0.000)
v10 = FreeCAD.Vector(50.000,10.000,0.000)
v10a = FreeCAD.Vector(30.00,-10.000,0.000)
v11 = FreeCAD.Vector(30.000,-10.000,0.000)
v11a = FreeCAD.Vector(10.00,-30.000,0.000)

#e = []
#e.append(Part.makeLine(v0,v1))
#e.append(Part.makeLine(v1,v2))
#e.append(Part.makeLine(v2,v0))
#e.append(Part.makeLine(v1,v3))
#e.append(Part.makeLine(v3,v4))
#e.append(Part.makeLine(v4,v1))
e.append(Part.makeLine(v0,v0a))
e.append(Part.makeLine(v1,v1a))
e.append(Part.makeLine(v2,v2a))
e.append(Part.makeLine(v3,v3a))
e.append(Part.makeLine(v4,v4a))
e.append(Part.makeLine(v5,v5a))
e.append(Part.makeLine(v6,v6a))
e.append(Part.makeLine(v7,v7a))
e.append(Part.makeLine(v8,v8a))
e.append(Part.makeLine(v9,v9a))
e.append(Part.makeLine(v10,v10a))
e.append(Part.makeLine(v11,v11a))

wire = e[0].multiFuse(e[1:])

shell = wire.extrude(xtr)    #extrude -> makePrism -> BRepPrimAPI_MakePrism

Part.show(shell)

p = Part.makePlane(200,200)
p.translate(xlate)
Part.show(p)

result = p.cut(shell)
Part.show(result)
The problem was in attempting to make a non-manifold shell with Part.makeShell. It is known to not work (I think it was discovered by @bernd in his attempts to do FEM on compsolids).

So:
first, fuse edges into a wire (I think using Part.makeWire will not work too, but I didn't test)
second, extrude the wire (yields a shell, or compound of shells)
third, cut the face with shell. Done.
User avatar
bernd
Posts: 12392
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: make faces from edges

Post by bernd »

DeepSOIC wrote:The problem was in attempting to make a non-manifold shell with Part.makeShell. It is known to not work (I think it was discovered by @bernd in his attempts to do FEM on compsolids).
So:
first, fuse edges into a wire (I think using Part.makeWire will not work too, but I didn't test)
second, extrude the wire (yields a shell, or compound of shells)
third, cut the face with shell. Done.
Exactly:
Neiter Part.makeWire with edges, nor Part.makeShell with face or Part.makeCompSolid with solids make valid non-manifold solids. For edges and faces the Part.fuse could be used (wink to DeepSOIC :D ) instead.
User avatar
bernd
Posts: 12392
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: make faces from edges

Post by bernd »

attached my code and file ...

Code: Select all

edges = []
for o in App.ActiveDocument.Objects:
    if o.Shape.ShapeType == 'Edge':
        edges.append(o.Shape)

fusion_wire = edges[0]
for no, e in enumerate(edges):
    no += 1
    if no > 1:
        fusion_wire = fusion_wire.fuse(e)

ex = fusion_wire.extrude(FreeCAD.Vector(0,0,5))
FreeCAD.ActiveDocument.recompute()
slab = App.ActiveDocument.Face.Shape.cut(ex)

for f in slab.Faces:
    Part.show(f)

Attachments
edges-and-face.fcstd
(19.71 KiB) Downloaded 41 times
User avatar
bernd
Posts: 12392
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: make faces from edges

Post by bernd »

Next problem to solve ...

I have a vertex and would like to know on which face the vertex is. The vertex is inside of one of the faces and on the plane of the faces.
Post Reply