def build_plate(polygon, points): ''' build a surface from a constraining polygon(s) and point(s) @param polygon: list of polygons (TopoDS_Shape) @param points: list of points (gp_Pnt) ''' # plate surface bpSrf = GeomPlate_BuildPlateSurface(3, 15, 2) # add curve constraints for poly in polygon: for edg in WireExplorer(poly.Wire()).ordered_edges(): c = BRepAdaptor_HCurve() c.ChangeCurve().Initialize(edg) constraint = BRepFill_CurveConstraint(c.GetHandle(), 0) bpSrf.Add(constraint.GetHandle()) # add point constraint for pt in points: bpSrf.Add(GeomPlate_PointConstraint(pt, 0).GetHandle()) bpSrf.Perform() maxSeg, maxDeg, critOrder = 9, 8, 0 tol = 1e-4 dmax = max([tol, 10 * bpSrf.G0Error()]) srf = bpSrf.Surface() plate = GeomPlate_MakeApprox(srf, tol, maxSeg, maxDeg, dmax, critOrder) uMin, uMax, vMin, vMax = srf.GetObject().Bounds() face = BRepBuilderAPI_MakeFace(plate.Surface(), uMin, uMax, vMin, vMax) face.Build() return face
def from_wire(cls, topo: Union[Topo, TopoDS_Shape], plane: Optional[gp_Pln] = None) -> TopoDS_Face: """ create face from compound of edges """ # if topo.number_of_wires() == 0: from .Construct import assert_isdone wire_builder = BRepBuilderAPI_MakeWire() if isinstance(topo, TopoDS_Shape): top = Topo(topo) else: top = topo for edge in top.edges(): wire_builder.Add(edge) with assert_isdone(wire_builder, 'wire'): wire_builder.Build() w = wire_builder.Wire() if plane is not None: bface = BRepBuilderAPI_MakeFace(plane) else: # todo - this doesnt work ---- _face = TopoDS_Face() bface = BRepBuilderAPI_MakeFace(_face) bface.Add(w) with assert_isdone(bface, 'face'): bface.Build() face = bface.Shape() return face
def from_wires(cls, topo: List[TopoDS_Wire], plane: Optional[gp_Pln] = None) -> TopoDS_Face: """ create face from compound of edges """ # if topo.number_of_wires() == 0: wire = BRepBuilderAPI_MakeFace() for edge in topo: wire.Add(edge) wire.Build() w = wire.Face() return w
def heightmap_from_image(event=None): """ takes the heightmap from a jpeg file and apply a texture this example requires numpy/matplotlib """ print("opening image") heightmap = Image.open('images/mountain_heightmap.jpg') heightmap.show() width = heightmap.size[0] height = heightmap.size[1] # create the gp_Pnt array print("parse image and fill in point array") for i in range(1, width): for j in range(1, height): # all 3 RGB values are equal, just take the first one # vertex 1 height_value = heightmap.getpixel((i - 1, j - 1))[0] v1 = gp_Pnt(i, j, float(height_value) / 10) # vertex 2 height_value = heightmap.getpixel((i, j - 1))[0] v2 = gp_Pnt(i + 1, j, float(height_value) / 10) # vertex 3 height_value = heightmap.getpixel((i, j))[0] v3 = gp_Pnt(i + 1, j + 1, float(height_value) / 10) # vertex 4 height_value = heightmap.getpixel((i - 1, j))[0] v4 = gp_Pnt(i, j + 1, float(height_value) / 10) # boundaries b1 = boundary_curve_from_2_points(v1, v2) b2 = boundary_curve_from_2_points(v2, v3) b3 = boundary_curve_from_2_points(v3, v4) b4 = boundary_curve_from_2_points(v4, v1) # bConstrainedFilling = GeomFill_ConstrainedFilling(8, 2) bConstrainedFilling.Init(b1, b2, b3, b4, False) srf1 = bConstrainedFilling.Surface() # make a face from this srf patch = BRepBuilderAPI_MakeFace() bounds = True toldegen = 1e-6 patch.Init(srf1, bounds, toldegen) patch.Build() display.DisplayShape(patch.Face()) # then create faces print("%s%%" % int(float(i) / width * 100)) #display.process_events() display.FitAll() # finally display image heightmap.show()
def makeFromWires(cls, outerWire, innerWires=[]): ''' Makes a planar face from one or more wires ''' face_builder = BRepBuilderAPI_MakeFace(outerWire.wrapped, True) # True is for planar only for w in innerWires: face_builder.Add(w.wrapped) face_builder.Build() f = face_builder.Face() sf = ShapeFix_Face(f) # fix wire orientation sf.FixOrientation() return cls(sf.Face())
def constrained_filling(event=None): # left pts1 = point_list_to_TColgp_Array1OfPnt( (gp_Pnt(0, 0, 0.0), gp_Pnt(0, 1, 0.3), gp_Pnt(0, 2, -0.3), gp_Pnt(0, 3, 0.15), gp_Pnt(0, 4, 0))) # front pts2 = point_list_to_TColgp_Array1OfPnt( (gp_Pnt(0, 0, 0.0), gp_Pnt(1, 0, -0.3), gp_Pnt(2, 0, 0.15), gp_Pnt(3, 0, 0), gp_Pnt(4, 0, 0))) # back pts3 = point_list_to_TColgp_Array1OfPnt( (gp_Pnt(0, 4, 0), gp_Pnt(1, 4, 0.3), gp_Pnt(2, 4, -0.15), gp_Pnt(3, 4, 0), gp_Pnt(4, 4, 1))) # rechts pts4 = point_list_to_TColgp_Array1OfPnt( (gp_Pnt(4, 0, 0), gp_Pnt(4, 1, 0), gp_Pnt(4, 2, 2), gp_Pnt(4, 3, -0.15), gp_Pnt(4, 4, 1))) spl1, b1 = get_simple_bound(pts1) spl2, b2 = get_simple_bound(pts2) spl3, b3 = get_simple_bound(pts3) spl4, b4 = get_simple_bound(pts4) # build the constrained surface bConstrainedFilling = GeomFill_ConstrainedFilling(8, 2) bConstrainedFilling.Init(b1, b2, b3, b4, False) srf1 = bConstrainedFilling.Surface() display.EraseAll() for i in [spl1, spl2, spl3, spl4]: edg = BRepBuilderAPI_MakeEdge(i) edg.Build() _edg = edg.Shape() display.DisplayShape(_edg) f = BRepBuilderAPI_MakeFace(srf1, 1e-6) f.Build() shp = f.Shape() return shp
def from_topo_edges(cls, topo: Topo, plane: Optional[gp_Pln]=None) -> TopoDS_Face: """ create face from compound of edges """ # if topo.number_of_wires() == 0: wire = BRepBuilderAPI_MakeWire() for edge in topo.edges(): wire.Add(edge) wire.Build() w = wire.Wire() if plane is not None: bface = BRepBuilderAPI_MakeFace(plane) else: # todo - this doesnt work ---- _face = TopoDS_Face() bface = BRepBuilderAPI_MakeFace(_face) bface.Add(w) bface.Build() face = bface.Shape() return face
def brepfeat_prism(event=None): box = BRepPrimAPI_MakeBox(400, 250, 300).Shape() faces = Topo(box).faces() for i in range(3): face = next(faces) srf = BRep_Tool_Surface(face) c = gp_Circ2d(gp_Ax2d(gp_Pnt2d(200, 130), gp_Dir2d(1, 0)), 75) circle = Geom2d_Circle(c).GetHandle() wire = BRepBuilderAPI_MakeWire() wire.Add(BRepBuilderAPI_MakeEdge(circle, srf, 0., pi).Edge()) wire.Add(BRepBuilderAPI_MakeEdge(circle, srf, pi, 2. * pi).Edge()) wire.Build() display.DisplayShape(wire.Wire()) mkf = BRepBuilderAPI_MakeFace() mkf.Init(srf, False, 1e-6) mkf.Add(wire.Wire()) mkf.Build() new_face = mkf.Face() breplib_BuildCurves3d(new_face) display.DisplayColoredShape(box, 'GREEN') display.DisplayShape(new_face) """ prism = BRepFeat_MakeDPrism(box, mkf.Face(), face, 100, True, True) prism.Perform(400) assert prism.IsDone() display.EraseAll() display.DisplayShape(prism.Shape()) """ #display.DisplayColoredShape(wire.Wire(), 'RED') display.FitAll()
def DisplayShape(self, shapes, material=None, texture=None, color=None, transparency=None, update=False): ''' ''' # if a gp_Pnt is passed, first convert to vertex if issubclass(shapes.__class__, gp_Pnt): vertex = BRepBuilderAPI_MakeVertex(shapes) shapes = [vertex.Shape()] SOLO = True elif isinstance(shapes, gp_Pnt2d): vertex = BRepBuilderAPI_MakeVertex( gp_Pnt(shapes.X(), shapes.Y(), 0)) shapes = [vertex.Shape()] SOLO = True # if a Geom_Curve is passed elif callable(getattr(shapes, "GetHandle", None)): handle = shapes.GetHandle() if issubclass(handle.__class__, Handle_Geom_Curve): edge = BRepBuilderAPI_MakeEdge(handle) shapes = [edge.Shape()] SOLO = True elif issubclass(handle.__class__, Handle_Geom2d_Curve): edge2d = BRepBuilderAPI_MakeEdge2d(handle) shapes = [edge2d.Shape()] SOLO = True elif issubclass(handle.__class__, Handle_Geom_Surface): bounds = True toldegen = 1e-6 face = BRepBuilderAPI_MakeFace() face.Init(handle, bounds, toldegen) face.Build() shapes = [face.Shape()] SOLO = True elif isinstance(shapes, Handle_Geom_Surface): bounds = True toldegen = 1e-6 face = BRepBuilderAPI_MakeFace() face.Init(shapes, bounds, toldegen) face.Build() shapes = [face.Shape()] SOLO = True elif isinstance(shapes, Handle_Geom_Curve): edge = BRepBuilderAPI_MakeEdge(shapes) shapes = [edge.Shape()] SOLO = True elif isinstance(shapes, Handle_Geom2d_Curve): edge2d = BRepBuilderAPI_MakeEdge2d(shapes) shapes = [edge2d.Shape()] SOLO = True elif issubclass(shapes.__class__, TopoDS_Shape): shapes = [shapes] SOLO = True else: SOLO = False ais_shapes = [] for shape in shapes: if material or texture: if texture: self.View.SetSurfaceDetail(OCC.V3d.V3d_TEX_ALL) shape_to_display = OCC.AIS.AIS_TexturedShape(shape) filename, toScaleU, toScaleV, toRepeatU, toRepeatV, originU, originV = texture.GetProperties( ) shape_to_display.SetTextureFileName( TCollection_AsciiString(filename)) shape_to_display.SetTextureMapOn() shape_to_display.SetTextureScale(True, toScaleU, toScaleV) shape_to_display.SetTextureRepeat(True, toRepeatU, toRepeatV) shape_to_display.SetTextureOrigin(True, originU, originV) shape_to_display.SetDisplayMode(3) elif material: shape_to_display = AIS_Shape(shape) shape_to_display.SetMaterial(material) else: # TODO: can we use .Set to attach all TopoDS_Shapes # to this AIS_Shape instance? shape_to_display = AIS_Shape(shape) ais_shapes.append(shape_to_display.GetHandle()) if not SOLO: # computing graphic properties is expensive # if an iterable is found, so cluster all TopoDS_Shape under # an AIS_MultipleConnectedInteractive shape_to_display = AIS_MultipleConnectedInteractive() for i in ais_shapes: shape_to_display.Connect(i) # set the graphic properties if material is None: #The default material is too shiny to show the object #color well, so I set it to something less reflective shape_to_display.SetMaterial(Graphic3d_NOM_NEON_GNC) if color: if isinstance(color, str): color = get_color_from_name(color) for shp in ais_shapes: self.Context.SetColor(shp, color, False) if transparency: shape_to_display.SetTransparency(transparency) if update: # only update when explicitely told to do so self.Context.Display(shape_to_display.GetHandle(), False) # especially this call takes up a lot of time... self.FitAll() self.Repaint() else: self.Context.Display(shape_to_display.GetHandle(), False) if SOLO: return ais_shapes[0] else: return shape_to_display
def brep_feat_extrusion_protrusion(event=None): # Extrusion S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = Topo(S).faces() F = next(faces) surf1 = BRep_Tool_Surface(F) Pl1 = Handle_Geom_Plane_DownCast(surf1).GetObject() D1 = Pl1.Pln().Axis().Direction().Reversed() MW = BRepBuilderAPI_MakeWire() p1, p2 = gp_Pnt2d(200., -100.), gp_Pnt2d(100., -100.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(100., -100.), gp_Pnt2d(100., -200.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(100., -200.), gp_Pnt2d(200., -200.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(200., -200.), gp_Pnt2d(200., -100.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) MKF = BRepBuilderAPI_MakeFace() MKF.Init(surf1, False, 1e-6) MKF.Add(MW.Wire()) FP = MKF.Face() breplib_BuildCurves3d(FP) display.EraseAll() MKP = BRepFeat_MakePrism(S, FP, F, D1, 0, True) MKP.PerformThruAll() res1 = MKP.Shape() display.DisplayShape(res1) # Protrusion next(faces) F2 = next(faces) surf2 = BRep_Tool_Surface(F2) Pl2 = Handle_Geom_Plane_DownCast(surf2).GetObject() D2 = Pl2.Pln().Axis().Direction().Reversed() MW2 = BRepBuilderAPI_MakeWire() p1, p2 = gp_Pnt2d(100., 100.), gp_Pnt2d(200., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(200., 100.), gp_Pnt2d(150., 200.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(150., 200.), gp_Pnt2d(100., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) MKF2 = BRepBuilderAPI_MakeFace() MKF2.Init(surf2, False, 1e-6) MKF2.Add(MW2.Wire()) MKF2.Build() FP = MKF2.Face() breplib_BuildCurves3d(FP) MKP2 = BRepFeat_MakePrism(res1, FP, F2, D2, 0, True) MKP2.PerformThruAll() display.EraseAll() trf = gp_Trsf() trf.SetTranslation(gp_Vec(0, 0, 300)) gtrf = gp_GTrsf() gtrf.SetTrsf(trf) tr = BRepBuilderAPI_GTransform(MKP2.Shape(), gtrf, True) fused = BRepAlgoAPI_Fuse(tr.Shape(), MKP2.Shape()) fused.RefineEdges() fused.Build() print('Boolean operation error status:', fused.ErrorStatus()) display.DisplayShape(fused.Shape()) display.FitAll()