def write(self, mesh_points, filename, tolerance=None): """ Writes a output file, called filename, copying all the structures from self.filename but the coordinates. mesh_points is a matrix that contains the new coordinates to write in the output file. :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of the points of the mesh :param string filename: name of the output file. :param float tolerance: tolerance for the construction of the faces and wires in the write function. If not given it uses `self.tolerance`. """ self._check_filename_type(filename) self._check_extension(filename) self._check_infile_instantiation() self.outfile = filename if tolerance is not None: self.tolerance = tolerance # cycle on the faces to update the control points position # init some quantities faces_explorer = TopExp_Explorer(self.shape, TopAbs_FACE) n_faces = 0 control_point_position = self._control_point_position compound_builder = BRep_Builder() compound = OCC.TopoDS.TopoDS_Compound() compound_builder.MakeCompound(compound) while faces_explorer.More(): # similar to the parser method face = OCC.TopoDS.topods_Face(faces_explorer.Current()) nurbs_converter = BRepBuilderAPI_NurbsConvert(face) nurbs_converter.Perform(face) nurbs_face = nurbs_converter.Shape() face_aux = OCC.TopoDS.topods_Face(nurbs_face) brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) occ_face = bspline_face.GetObject() n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() i = 0 for pole_u_direction in range(n_poles_u): for pole_v_direction in range(n_poles_v): control_point_coordinates = mesh_points[ i + control_point_position[n_faces], :] point_xyz = gp_XYZ(*control_point_coordinates) gp_point = gp_Pnt(point_xyz) occ_face.SetPole(pole_u_direction + 1, pole_v_direction + 1, gp_point) i += 1 # construct the deformed wire for the trimmed surfaces wire_maker = BRepBuilderAPI_MakeWire() tol = ShapeFix_ShapeTolerance() brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), self.tolerance).Face() brep_face = BRep_Tool.Surface(brep) # cycle on the edges edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE) while edge_explorer.More(): edge = OCC.TopoDS.topods_Edge(edge_explorer.Current()) # edge in the (u,v) coordinates edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux) # evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(\ edge_uv_coordinates[0], brep_face) edge_phis_coordinates = edge_phis_coordinates_aux.Edge() tol.SetTolerance(edge_phis_coordinates, self.tolerance) wire_maker.Add(edge_phis_coordinates) edge_explorer.Next() # grouping the edges in a wire wire = wire_maker.Wire() # trimming the surfaces brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), wire).Shape() compound_builder.Add(compound, brep_surf) n_faces += 1 faces_explorer.Next() self.write_shape_to_file(compound, self.outfile)
def EdgeOnSurface(edge, section_plane, lim_coord1, lim_coord2, XYZ): section_face = BRepBuilderAPI_MakeFace(section_plane, lim_coord1[0], lim_coord1[1], lim_coord2[0], lim_coord2[1]).Face() curve_handle, first, last = BRep_Tool.CurveOnSurface(edge, section_face) plane = Geom_Plane(section_plane) edge_on_surface = BRepBuilderAPI_MakeEdge(curve_handle, plane.GetHandle(), first, last).Edge() curve_adaptor = BRepAdaptor_Curve(edge_on_surface) if curve_adaptor.GetType() == GeomAbs_Line: v = list(Topo(edge_on_surface).vertices()) v1 = BRep_Tool.Pnt(v[0]).X(), BRep_Tool.Pnt(v[0]).Y(), BRep_Tool.Pnt( v[0]).Z() v2 = BRep_Tool.Pnt(v[-1]).X(), BRep_Tool.Pnt(v[-1]).Y(), BRep_Tool.Pnt( v[-1]).Z() obj = Line3D(v1, v2) elif curve_adaptor.GetType() == GeomAbs_Circle: v = list(Topo(edge_on_surface).vertices()) v1 = BRep_Tool.Pnt(v[0]).X(), BRep_Tool.Pnt(v[0]).Y(), BRep_Tool.Pnt( v[0]).Z() v2 = BRep_Tool.Pnt(v[-1]).X(), BRep_Tool.Pnt(v[-1]).Y(), BRep_Tool.Pnt( v[-1]).Z() start = [v1[i] for i in range(len(XYZ)) if XYZ[i]] end = [v2[i] for i in range(len(XYZ)) if XYZ[i]] circle = curve_adaptor.Circle() center = [] for i in range(len(XYZ)): if XYZ[i] and i == 0: center.append(circle.Location().X()) elif XYZ[i] and i == 1: center.append(circle.Location().Y()) elif XYZ[i] and i == 2: center.append(circle.Location().Z()) else: center.append(0.5 * (v1[i] + v2[i])) radius = circle.Radius() vec_start = (start[0] - center[0], start[1] - center[1]) vec_end = (end[0] - center[0], end[1] - center[1]) t1 = angle360(vec_start) t2 = angle360(vec_end) if not XYZ[0]: axis = circle.Axis().Direction().X() elif not XYZ[1]: axis = circle.Axis().Direction().Y() elif not XYZ[2]: axis = circle.Axis().Direction().Z() if axis < 0: t1, t2 = t2, t1 obj = Arc3D(v1, v2, t1, t2, center, radius) elif curve_adaptor.GetType() == GeomAbs_BSplineCurve: bspline = curve_adaptor.BSpline().GetObject() degree = bspline.Degree() knots = [ bspline.Knot(index) for index in range(1, bspline.NbKnots() + 1) ] mults = [ bspline.Multiplicity(index) for index in range(1, bspline.NbKnots() + 1) ] poles = [(bspline.Pole(index).X(), bspline.Pole(index).Y(), bspline.Pole(index).Z()) for index in range(1, bspline.NbPoles() + 1)] periodic = bspline.IsPeriodic() obj = BSpline3D(poles, mults, knots, degree, periodic) else: print(curve_adaptor.GetType()) warnings.warn("Not recognized curve!") return obj
section_plane, zmin, zmax, ymin, ymax).Face() #ifcopenshell.geom.utils.display_shape(section_face) n_edges = 0 # Explore the faces of the shape (these are known to be named) exp = TopExp_Explorer(shape, TopAbs_FACE) while exp.More(): s = exp.Current() tp = Topo(s) for face in tp.faces(): ifcopenshell.geom.utils.display_shape(face) for edge in list(Topo(face).edges()): curve_handle, first, last = BRep_Tool.CurveOnSurface( edge, section_face) plane = Geom_Plane(section_plane) e = BRepBuilderAPI_MakeEdge(curve_handle, plane.GetHandle(), first, last).Edge() # handle_adaptor = Geom2dAdaptor_Curve(curve_handle) curve_adapt = BRepAdaptor_Curve(e) if curve_adapt.GetType() == GeomAbs_Line: v = list(Topo(e).vertices()) y1, z1 = BRep_Tool.Pnt(v[0]).Y(), BRep_Tool.Pnt(v[0]).Z() y2, z2 = BRep_Tool.Pnt(v[-1]).Y(), BRep_Tool.Pnt(v[-1]).Z() plt.plot([y1, y2], [z1, z2], color="red", alpha=0.2) ifcopenshell.geom.utils.display_shape(e, clr=RED)
def write(cls, filename, data, tolerance=1e-6): # cycle on the faces to update the control points position # init some quantities shape = data.shape control_point_position = data.control_point_position mesh_points = data.points faces_explorer = TopExp_Explorer(shape, TopAbs_FACE) n_faces = 0 compound_builder = BRep_Builder() compound = TopoDS_Compound() compound_builder.MakeCompound(compound) while faces_explorer.More(): # similar to the parser method face = topods_Face(faces_explorer.Current()) nurbs_converter = BRepBuilderAPI_NurbsConvert(face) nurbs_converter.Perform(face) nurbs_face = nurbs_converter.Shape() face_aux = topods_Face(nurbs_face) brep_face = BRep_Tool.Surface(topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) occ_face = bspline_face.GetObject() n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() i = 0 for pole_u_direction in range(n_poles_u): for pole_v_direction in range(n_poles_v): control_point_coordinates = mesh_points[ +control_point_position[n_faces], :] point_xyz = gp_XYZ(*control_point_coordinates) gp_point = gp_Pnt(point_xyz) occ_face.SetPole(pole_u_direction + 1, pole_v_direction + 1, gp_point) i += 1 # construct the deformed wire for the trimmed surfaces wire_maker = BRepBuilderAPI_MakeWire() tol = ShapeFix_ShapeTolerance() brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), tolerance).Face() brep_face = BRep_Tool.Surface(brep) # cycle on the edges edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE) while edge_explorer.More(): edge = topods_Edge(edge_explorer.Current()) # edge in the (u,v) coordinates edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux) # evaluating the new edge: same (u,v) coordinates, but # different (x,y,x) ones edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge( edge_uv_coordinates[0], brep_face) edge_phis_coordinates = edge_phis_coordinates_aux.Edge() tol.SetTolerance(edge_phis_coordinates, tolerance) wire_maker.Add(edge_phis_coordinates) edge_explorer.Next() # grouping the edges in a wire wire = wire_maker.Wire() # trimming the surfaces brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), wire).Shape() compound_builder.Add(compound, brep_surf) n_faces += 1 faces_explorer.Next() IGESControl_Controller_Init() writer = IGESControl_Writer() writer.AddShape(compound) writer.Write(filename)