Bool op refactoring

This commit is contained in:
adam-urbanczyk
2020-04-05 22:38:24 +02:00
parent 8062b1023c
commit c6fc398ce6

View File

@ -75,7 +75,7 @@ from OCC.Core.TopoDS import (
topods_Solid,
)
from OCC.Core.TopoDS import TopoDS_Compound, TopoDS_Builder
from OCC.Core.TopoDS import TopoDS_Compound, TopoDS_Builder, TopoDS_Iterator
from OCC.Core.GC import GC_MakeArcOfCircle, GC_MakeArcOfEllipse # geometry construction
from OCC.Core.GCE2d import GCE2d_MakeSegment
@ -587,13 +587,37 @@ class Shape(object):
def __hash__(self):
return self.hashCode()
def cut(self, toCut):
def _bool_op(self, args, tools, op):
"""
Generic boolean operation
"""
arg = TopTools_ListOfShape()
for obj in args:
arg.Append(obj.wrapped)
tool = TopTools_ListOfShape()
for obj in tools:
tool.Append(obj.wrapped)
op.SetArguments(arg)
op.SetTools(tool)
op.SetRunParallel(True)
op.Build()
return Shape.cast(op.Shape())
def cut(self, *toCut):
"""
Remove a shape from another one
"""
return Shape.cast(BRepAlgoAPI_Cut(self.wrapped, toCut.wrapped).Shape())
def fuse(self, toFuse):
cut_op = BRepAlgoAPI_Cut()
return self._bool_op([self.wrapped,], toCut, cut_op)
def fuse(self, *toFuse):
"""
Fuse shapes together
"""
@ -602,15 +626,17 @@ class Shape(object):
fuse_op.RefineEdges()
fuse_op.FuseEdges()
# fuse_op.SetFuzzyValue(TOLERANCE)
fuse_op.Build()
return Shape.cast(fuse_op.Shape())
return self._bool_op([self.wrapped,], toFuse, fuse_op)
def intersect(self, toIntersect):
def intersect(self, *toIntersect):
"""
Construct shape intersection
"""
return Shape.cast(BRepAlgoAPI_Common(self.wrapped, toIntersect.wrapped).Shape())
intersect_op = BRepAlgoAPI_Common()
return self._bool_op([self.wrapped,], toIntersect, intersect_op)
def _repr_html_(self):
"""
@ -1939,6 +1965,48 @@ class Compound(Shape, Mixin3D):
return cls(text_3d.Shape()).transformShape(position.rG)
def __iter__(self):
"""
Iterate over subshapes.
"""
it = TopoDS_Iterator(self.wrapped)
while it.More():
yield Shape.cast(it.Value())
it.Next()
def cut(self, *toCut):
"""
Remove a shape from another one
"""
cut_op = BRepAlgoAPI_Cut()
return self._bool_op(self, toCut, cut_op)
def fuse(self, *toFuse):
"""
Fuse shapes together
"""
fuse_op = BRepAlgoAPI_Fuse(self.wrapped, toFuse.wrapped)
fuse_op.RefineEdges()
fuse_op.FuseEdges()
# fuse_op.SetFuzzyValue(TOLERANCE)
return self._bool_op(self, toFuse, fuse_op)
def intersect(self, *toIntersect):
"""
Construct shape intersection
"""
intersect_op = BRepAlgoAPI_Common()
return self._bool_op(self, toIntersect, intersect_op)
def sortWiresByBuildOrder(wireList, result={}):
"""Tries to determine how wires should be combined into faces.