Skip to content

Instantly share code, notes, and snippets.

@jay3sh
Created December 8, 2011 07:21
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/1446374 to your computer and use it in GitHub Desktop.
Save jay3sh/1446374 to your computer and use it in GitHub Desktop.
Create Shape from raw face/vertex information
import sys
from OCC.gp import gp_Pnt
from OCC.BRep import BRep_Builder
from OCC.BRepBuilderAPI import BRepBuilderAPI_Sewing
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakePolygon
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeFace
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeVertex
from OCC.TopoDS import TopoDS_Compound
from OCC import StlAPI
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.BRepAlgoAPI import BRepAlgoAPI_Fuse
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.6],[0.2,-0.2,0.6],[0.2,0.2,0.6],[-0.2,0.2,0.6]],
"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():
builder = BRep_Builder()
sewtool = BRepBuilderAPI_Sewing()
aComp = TopoDS_Compound()
builder.MakeCompound(aComp)
for face in mesh['faces']:
face.reverse()
corners = []
for corner in [ mesh['vertices'][i] for i in face ]:
p = gp_Pnt()
p.SetCoord(corner[0], corner[1], corner[2])
corners.append(p)
aktWire = BRepBuilderAPI_MakePolygon(
corners[0], corners[1], corners[2], corners[3], True)
if aktWire.IsDone():
aktFace = BRepBuilderAPI_MakeFace(aktWire.Wire())
if aktFace.IsDone():
builder.Add(aComp, aktFace.Shape())
else:
print 'Face is null'
else:
print 'Wire is null'
sewtool.Load(aComp)
sewtool.Perform()
aShape = sewtool.SewedShape()
print 'shape',aShape.IsNull()
aShape = aComp
'''
#
# UNCOMMENT THIS TO DO BOOLEAN FUSE WITH OTHER BOX
#
box = BRepPrimAPI_MakeBox(0.4,0.4,0.4).Shape()
union = BRepAlgoAPI_Fuse(aShape, box).Shape()
aShape = union
'''
stl_writer = StlAPI.StlAPI_Writer()
stl_writer.SetASCIIMode(True)
stl_writer.SetDeflection(0.01)
stl_writer.Write(aShape, 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