Skip to content

Instantly share code, notes, and snippets.

@jay3sh
Created November 9, 2011 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jay3sh/1351019 to your computer and use it in GitHub Desktop.
Save jay3sh/1351019 to your computer and use it in GitHub Desktop.
Build cube in PythonOCC (from vertex-face data)
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()
@selvakarna
Copy link

Can you share installation steps for Linux Machine above Packages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment