class ShapeProps(object): """ Base class for shape properties. """ def __init__(self): self._props = GProp_GProps() @property def mass(self): """ :return: The mass of the shape. This corresponds to total length for linear properties, total area for surface properties, or total volume for volume properties. :rtype: float """ return self._props.Mass() @property def cg(self): """ :return: The center of gravity. :rtype: afem.geometry.entities.Point """ gp_pnt = self._props.CentreOfMass() return Point(gp_pnt.X(), gp_pnt.Y(), gp_pnt.Z()) @property def static_moments(self): """ :return: The static moments of inertia Ix, Iy, and Iz. :rtype: tuple(float) """ return self._props.StaticMoments(0., 0., 0.) @property def matrix_of_inertia(self): """ :return: The 3 x 3 matrix of inertia. :rtype: numpy.ndarray """ gp_mat = self._props.MatrixOfInertia() matrix = [] for j in range(1, 4): row = [] for i in range(1, 4): row.append(gp_mat.Value(i, j)) matrix.append(row) return array(matrix, dtype=float) def moment_of_inertia(self, axis): """ Compute the moment of inertia about the axis. :param afem.geometry.entities.Axis1 axis: The axis. :return: The moment of inertia. :rtype: float """ return self._props.MomentOfInertia(axis)
def volume(self): """ :return: The voume of all solids of the shape. :rtype: float """ props = GProp_GProps() BRepGProp.VolumeProperties_(self.object, props, True) return props.Mass()
def area(self): """ :return: The area of all faces of the shape. :rtype: float """ props = GProp_GProps() BRepGProp.SurfaceProperties_(self.object, props, True) return props.Mass()
def length(self): """ :return: The length of all edges of the shape. :rtype: float """ props = GProp_GProps() BRepGProp.LinearProperties_(self.object, props, True) return props.Mass()
def shape_faces_surface(): """ Compute the surface of each face of a shape """ # first create the shape the_shape = BRepPrimAPI_MakeBox(50., 30., 10.).Shape() # then loop over faces t = TopologyExplorer(the_shape) props = GProp_GProps() shp_idx = 1 for face in t.faces(): brepgprop_SurfaceProperties(face, props) face_surf = props.Mass() print("Surface for face nbr %i : %f" % (shp_idx, face_surf)) shp_idx += 1
def cube_inertia_properties(): """ Compute the inertia properties of a shape """ # Create and display cube print("Creating a cubic box shape (50*50*50)") cube_shape = BRepPrimAPI_MakeBox(50., 50., 50.).Shape() # Compute inertia properties props = GProp_GProps() brepgprop_VolumeProperties(cube_shape, props) # Get inertia properties mass = props.Mass() cog = props.CentreOfMass() matrix_of_inertia = props.MatrixOfInertia() # Display inertia properties print("Cube mass = %s" % mass) cog_x, cog_y, cog_z = cog.Coord() print("Center of mass: x = %f;y = %f;z = %f;" % (cog_x, cog_y, cog_z)) print("Matrix of inertia", matrix_of_inertia)
class PropsBase(object): """ Base class for shape properties. """ def __init__(self): self._props = GProp_GProps() @property def mass(self): """ :return: The mass of the shape. This corresponds to total length for linear properties, total area for surface properties, or total volume for volume properties. :rtype: float """ return self._props.Mass() @property def cg(self): """ :return: The center of gravity. :rtype: OCCT.gp.gp_Pnt """ return self._props.CentreOfMass() @property def static_moments(self): """ :return: The static moments of inertia Ix, Iy, and Iz. :rtype: tuple(float) """ return self._props.StaticMoments(0., 0., 0.) def moment_of_inertia(self, axis): """ Compute the moment of inertia about the axis. :param OCCT.gp.gp_Ax1 axis: The axis. :return: The moment of inertia. :rtype: float """ return self._props.MomentOfInertia(axis)
def on_select(shapes): """ Parameters ---------- shape : TopoDS_Shape """ g1 = GProp_GProps() for shape in shapes: brepgprop_LinearProperties(shape, g1) mass = g1.Mass() centre_of_mass = g1.CentreOfMass() com_x = centre_of_mass.X() com_y = centre_of_mass.Y() com_z = centre_of_mass.Z() static_moments = g1.StaticMoments() print("shape {shape}: \n mass: {mass}" "\n center of mass: {com_x}, {com_y}, {com_z}" "\n static moments: {static_moments}".format(**vars()))
def _build_solid(compound, divide_closed): """ Method to try and build a valid solid from an OpenVSP component. """ # Get all the faces in the compound. The surfaces must be split. Discard # any with zero area. top_exp = TopExp_Explorer(compound, TopAbs_FACE) faces = [] while top_exp.More(): shape = top_exp.Current() face = CheckShape.to_face(shape) fprop = GProp_GProps() BRepGProp.SurfaceProperties_(face, fprop, 1.0e-7) a = fprop.Mass() if a <= 1.0e-7: top_exp.Next() continue faces.append(face) top_exp.Next() # Replace any planar B-Spline surfaces with planes non_planar_faces = [] planar_faces = [] for f in faces: hsrf = BRep_Tool.Surface_(f) try: is_pln = GeomLib_IsPlanarSurface(hsrf, 1.0e-7) if is_pln.IsPlanar(): w = ShapeAnalysis.OuterWire_(f) # Fix the wire because they are usually degenerate edges in # the planar end caps. builder = BRepBuilderAPI_MakeWire() for e in ExploreShape.get_edges(w): if LinearProps(e).length > 1.0e-7: builder.Add(e) w = builder.Wire() fix = ShapeFix_Wire() fix.Load(w) geom_pln = Geom_Plane(is_pln.Plan()) fix.SetSurface(geom_pln) fix.FixReorder() fix.FixConnected() fix.FixEdgeCurves() fix.FixDegenerated() w = fix.WireAPIMake() # Build the planar face fnew = BRepBuilderAPI_MakeFace(w, True).Face() planar_faces.append(fnew) else: non_planar_faces.append(f) except RuntimeError: non_planar_faces.append(f) # Make a compound of the faces shape = CreateShape.compound(non_planar_faces + planar_faces) # Split closed faces if divide_closed: divide = ShapeUpgrade_ShapeDivideClosed(shape) divide.Perform() shape = divide.Result() # Sew shape sew = BRepBuilderAPI_Sewing(1.0e-7) sew.Load(shape) sew.Perform() sewn_shape = sew.SewedShape() if sewn_shape.ShapeType() == TopAbs_FACE: face = sewn_shape sewn_shape = TopoDS_Shell() builder = BRep_Builder() builder.MakeShell(sewn_shape) builder.Add(sewn_shape, face) # Attempt to unify planar domains unify_shp = ShapeUpgrade_UnifySameDomain(sewn_shape, False, True, False) unify_shp.Build() shape = unify_shp.Shape() # Make solid shell = ExploreShape.get_shells(shape)[0] solid = ShapeFix_Solid().SolidFromShell(shell) # Limit tolerance FixShape.limit_tolerance(solid) # Check shape validity check_shp = BRepCheck_Analyzer(solid, True) if check_shp.IsValid(): return solid, True, [] else: invalid_shapes = _topods_iterator_check(solid, check_shp) return solid, False, invalid_shapes
def length(self): props = GProp_GProps() BRepGProp.LinearProperties_(self.shape, props, True) return props.Mass() # Don't ask