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 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()))