Created
November 9, 2011 10:07
-
-
Save jay3sh/1351019 to your computer and use it in GitHub Desktop.
Build cube in PythonOCC (from vertex-face data)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from OCC.gp import gp_Pnt | |
from OCC.GC import GC_MakeSegment | |
from OCC.BRepBuilderAPI import \ | |
BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace, BRepBuilderAPI_MakeWire,\ | |
BRepBuilderAPI_MakeShell, BRepBuilderAPI_MakeSolid | |
from OCC.BRep import BRep_Builder | |
from OCC.TopoDS import TopoDS_Shell, TopoDS_Solid | |
from OCC import StlAPI | |
mesh = { | |
"vertices":[[-0.2,-0.2,0.2],[0.2,-0.2,0.2],[0.2,0.2,0.2],[-0.2,0.2,0.2],[-0.2,-0.2,0.6000000000000001],[0.2,-0.2,0.6000000000000001],[0.2,0.2,0.6000000000000001],[-0.2,0.2,0.6000000000000001]], | |
"faces":[[3,2,1,0],[4,5,6,7],[7,6,2,3],[5,4,0,1],[6,5,1,2],[4,7,3,0]] | |
} | |
def main(): | |
vertices = [ gp_Pnt(p[0],p[1],p[2]) for p in mesh['vertices'] ] | |
oFaces = [] | |
builder = BRep_Builder() | |
shell = TopoDS_Shell() | |
builder.MakeShell(shell) | |
for face in mesh['faces']: | |
edges = [] | |
face.reverse() | |
for i in range(len(face)): | |
cur = face[i] | |
nxt = face[(i+1)%len(face)] | |
segment = GC_MakeSegment(vertices[cur],vertices[nxt]) | |
edges.append(BRepBuilderAPI_MakeEdge(segment.Value())) | |
wire = BRepBuilderAPI_MakeWire() | |
for edge in edges: | |
wire.Add(edge.Edge()) | |
oFace = BRepBuilderAPI_MakeFace(wire.Wire()) | |
builder.Add(shell, oFace.Shape()) | |
stl_writer = StlAPI.StlAPI_Writer() | |
stl_writer.SetASCIIMode(True) | |
stl_writer.SetDeflection(0.01) | |
stl_writer.Write(shell, sys.argv[1]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you share installation steps for Linux Machine above Packages?