def from_line_linear(cls, linear_mass_kg_per_m: float, linear_entity: TopoDS_Shape, cad_unit: str, name: str = ""): r"""Construct a Weight from linear mass [kg / m] and length. Parameters ---------- linear_mass_kg_per_m : Linear mass [kg/m] linear_entity : An OCC linear shape cad_unit : the 1D unit in which the linear_entity Shape is defined. name : name given to the mass """ linear_types = GlobalProperties.linear_types if topo_lut[linear_entity.ShapeType()] not in linear_types: msg = "topo_lut[linear_entity.ShapeType()] should be a linear type" logger.error(msg) raise AssertionError(msg) mass_kg = linear_mass_kg_per_m * convert( GlobalProperties(linear_entity).length, from_unit=cad_unit, to_unit="m") return Mass.from_line_fixed(mass_kg, linear_entity, cad_unit, name)
def from_volume_fixed(cls, mass_kg: float, volume: TopoDS_Shape, cad_unit: str, name: str = ""): r""" Construct a Mass with its CG at the CG of the volume. Parameters ---------- mass_kg : Total mass [kg] volume : An OCC volumic shape cad_unit : the 1D unit in which the volume Shape is defined. name : name given to the mass """ assert isinstance(mass_kg, (float, int)) assert (mass_kg >= 0.) assert topo_lut[volume.ShapeType()] in GlobalProperties.volumic_types obj = cls() obj._mass_kg = mass_kg position_gp_pnt = GlobalProperties(volume).centre position_m = position2positionm( Position(position_gp_pnt.X(), position_gp_pnt.Y(), position_gp_pnt.Z(), unit=cad_unit)) obj._cg_m = position_m obj._name = name return obj
def get_type_as_string(topods_shape: TopoDS_Shape) -> str: """just get the type string, remove TopAbs_ and lowercas all ending letters""" types = { TopAbs_VERTEX: "Vertex", TopAbs_COMPSOLID: "CompSolid", TopAbs_FACE: "Face", TopAbs_WIRE: "Wire", TopAbs_EDGE: "Edge", TopAbs_COMPOUND: "Compound", TopAbs_COMPSOLID: "CompSolid", TopAbs_SOLID: "Solid", } return types[topods_shape.ShapeType()]
def from_line_fixed(cls, mass_kg: float, linear_entity: TopoDS_Shape, cad_unit: str, name: str = ""): r"""Construct a Mass with its CG at the CG of the linear entity. Parameters ---------- mass_kg : Total mass [kg] linear_entity : An OCC linear shape cad_unit : the 1D unit in which the linear_entity Shape is defined. name : name given to the mass """ if not isinstance(mass_kg, (float, int)): msg = "mass_kg should be a float or an int" logger.error(msg) raise ValueError(msg) if mass_kg < 0.: msg = "mass_kg should be positive or zero" logger.error(msg) raise ValueError(msg) if topo_lut[linear_entity.ShapeType( )] not in GlobalProperties.linear_types: msg = "linear_entity.ShapeType() should be a linear type" logger.error(msg) raise ValueError(msg) obj = cls() obj._mass_kg = mass_kg position_gp_pnt = GlobalProperties(linear_entity).centre position_m = position2positionm( Position(position_gp_pnt.X(), position_gp_pnt.Y(), position_gp_pnt.Z(), unit=cad_unit)) obj._cg_m = position_m obj._name = name return obj
def dump_topology_to_string(shape: TopoDS_Shape, level: Optional[int] = 0, buffer: Optional[str] = "") -> None: """ Return the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) print(".." * level + f"<Vertex {hash(shape)}: {pnt.X()} {pnt.Y()} {pnt.Z()}>\n") else: print(".." * level, end="") print(shape) it = TopoDS_Iterator(shape) while it.More() and level < 5: # LEVEL MAX shp = it.Value() it.Next() dump_topology_to_string(shp, level + 1, buffer)
def from_volume_volumic(cls, volumic_mass_kg_per_m3: float, volume: TopoDS_Shape, cad_unit: str, name: str): r"""Construct a Mass from volumic mass [kg/m3] and volume. The CG is at the CG of the volume. Parameters ---------- volumic_mass_kg_per_m3 : Volumic mass [kg/m3] volume : An OCC volumic shape cad_unit : the 1D unit in which the volume Shape is defined. name : name given to the mass """ if topo_lut[volume.ShapeType()] not in GlobalProperties.volumic_types: msg = "volume.ShapeType() should be of volumic type" logger.error(msg) raise ValueError(msg) mass_kg = volumic_mass_kg_per_m3 * convert( GlobalProperties(volume).volume, from_unit=f"{cad_unit}3", to_unit="m3") return Mass.from_volume_fixed(mass_kg, volume, cad_unit, name)
def from_surface_surfacic(cls, surfacic_mass_kg_per_m2: float, surface: TopoDS_Shape, cad_unit: str, name: str = ""): r"""Construct a Mass from surfacic mass [kg / m2] and surface. The CG is at the CG of the surface. Parameters ---------- surfacic_mass_kg_per_m2 : Surfacic mass [kg/m2] surface : An OCC surfacic shape cad_unit : the 1D unit in which the surface Shape is defined. name : name given to the mass """ assert topo_lut[surface.ShapeType()] in GlobalProperties.surfacic_types mass_kg = surfacic_mass_kg_per_m2 * convert( GlobalProperties(surface).area, from_unit=f"{cad_unit}2", to_unit="m2") return Mass.from_surface_fixed(mass_kg, surface, cad_unit, name)
def is_compsolid(topods_shape: TopoDS_Shape) -> bool: if not hasattr(topods_shape, "ShapeType"): return False return topods_shape.ShapeType() == TopAbs_COMPSOLID
def is_wire(topods_shape: TopoDS_Shape) -> bool: if not hasattr(topods_shape, "ShapeType"): return False return topods_shape.ShapeType() == TopAbs_WIRE
def is_vertex(topods_shape: TopoDS_Shape) -> bool: if not hasattr(topods_shape, "ShapeType"): return False return topods_shape.ShapeType() == TopAbs_VERTEX
def write_shape(self, l_shells, filename, tol): """ Method to recreate a TopoDS_Shape associated to a geometric shape after the modification of points of each Face. It returns a TopoDS_Shape (Shape). :param l_shells: the list of shells after initial parsing :param filename: the output filename :param tol: tolerance on the surface creation after modification :return: None """ self.outfile = filename # global compound containing multiple shells global_compound_builder = BRep_Builder() global_comp = TopoDS_Compound() global_compound_builder.MakeCompound(global_comp) if self.check_topo == 0: # cycle on shells (multiple objects) shape_shells_explorer = TopExp_Explorer( self.shape.Oriented(TopAbs_FORWARD), TopAbs_SHELL) ishell = 0 while shape_shells_explorer.More(): per_shell = topods_Shell(shape_shells_explorer.Current()) # a local compound containing a shell compound_builder = BRep_Builder() comp = TopoDS_Compound() compound_builder.MakeCompound(comp) # cycle on faces faces_explorer = TopExp_Explorer( per_shell.Oriented(TopAbs_FORWARD), TopAbs_FACE) iface = 0 while faces_explorer.More(): topoface = topods.Face(faces_explorer.Current()) newface = self.write_face(l_shells[ishell][iface][0], l_shells[ishell][iface][1], topoface, tol) # add face to compound compound_builder.Add(comp, newface) iface += 1 faces_explorer.Next() new_shell = self.combine_faces(comp, 0.01) itype = TopoDS_Shape.ShapeType(new_shell) # add the new shell to the global compound global_compound_builder.Add(global_comp, new_shell) # TODO #print("Shell {0} of type {1} Processed ".format(ishell, itype)) #print "==============================================" ishell += 1 shape_shells_explorer.Next() else: # cycle on faces # a local compound containing a shell compound_builder = BRep_Builder() comp = TopoDS_Compound() compound_builder.MakeCompound(comp) # cycle on faces faces_explorer = TopExp_Explorer( self.shape.Oriented(TopAbs_FORWARD), TopAbs_FACE) iface = 0 while faces_explorer.More(): topoface = topods.Face(faces_explorer.Current()) newface = self.write_face(l_shells[0][iface][0], l_shells[0][iface][1], topoface, tol) # add face to compound compound_builder.Add(comp, newface) iface += 1 faces_explorer.Next() new_shell = self.combine_faces(comp, 0.01) itype = TopoDS_Shape.ShapeType(new_shell) # add the new shell to the global compound global_compound_builder.Add(global_comp, new_shell) # TODO print to logging # print("Shell {0} of type {1} Processed ".format(0, itype)) # print "==============================================" self.write_shape_to_file(global_comp, self.outfile)