def ask_face_centroid(face): """ """ massProps = GProp_GProps() brepgprop.SurfaceProperties(face, massProps) gPt = massProps.CentreOfMass() return gPt.Coord()
def SetCenter(self, theShape): props = GProp_GProps() brepgprop_SurfaceProperties(theShape, props) cog = props.CentreOfMass() cog_x, cog_y, cog_z = cog.Coord() self.myCenter = gp_Pnt(cog_x, cog_y, cog_z) print("Center of mass: x = %f;y = %f;z = %f;" % (cog_x, cog_y, cog_z))
def centre_mass2(self): from OCC.Core.GProp import GProp_GProps from OCC.Core.BRepGProp import brepgprop_VolumeProperties, brepgprop_SurfaceProperties, \ brepgprop_LinearProperties props = GProp_GProps() brepgprop_VolumeProperties(self.m_w_frame, 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)) mat = props.MatrixOfInertia() ####################################################################################### variation_inertial = abs(mat.Value(2, 3)) + abs(mat.Value(1, 2)) + abs( mat.Value(1, 3)) + abs(mat.Value(2, 1)) + abs(mat.Value( 3, 1)) + abs(mat.Value(3, 2)) var1, var2 = (cog_x**2 + cog_y**2 + cog_z**2)**0.5, variation_inertial if var1 < 0.0001 or var2 < 0.0001: var1, var2 = 10**10, 10**10 return var1, var2
def centre_mass_vizuall(self): flag = 0 shape = 0 for name in self.modules: if flag == 0: cp = BRepBuilderAPI_Copy(self.modules[name]) cp.Perform(self.modules[name]) shape = cp.Shape() flag = 1 shape = BRepAlgoAPI_Fuse(shape, self.modules[name]).Shape() props = GProp_GProps() brepgprop_VolumeProperties(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)) mat = props.MatrixOfInertia() ####################################################################################### list_1 = [mat.Value(1, 1), mat.Value(1, 2), mat.Value(1, 3)] list_2 = [mat.Value(2, 1), mat.Value(2, 2), mat.Value(2, 3)] list_3 = [mat.Value(3, 1), mat.Value(3, 2), mat.Value(3, 3)] print('\t'.join(str(i) for i in list_1)) print('\t'.join(str(i) for i in list_2)) print('\t'.join(str(i) for i in list_3)) var1 = (cog_x**2 + cog_y**2 + cog_z**2)**0.5 print(var1)
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 __init__(self, size, face=None, faceU=None, ax3=None): # gp_Ax3 of XYZ coord system origin = gp_Pnt(0, 0, 0) wDir = gp_Dir(0, 0, 1) uDir = gp_Dir(1, 0, 0) vDir = gp_Dir(0, 1, 0) xyzAx3 = gp_Ax3(origin, wDir, uDir) if (not face and not ax3): # create default wp (in XY plane at 0,0,0) axis3 = xyzAx3 gpPlane = gp_Pln(xyzAx3) self.gpPlane = gpPlane # type: gp_Pln self.plane = Geom_Plane(gpPlane) # type: Geom_Plane elif face: # create workplane on face, uDir defined by faceU wDir = face_normal(face) # from OCCUtils.Construct module props = GProp_GProps() brepgprop_SurfaceProperties(face, props) origin = props.CentreOfMass() ''' surface = BRep_Tool_Surface(face) # type: Handle_Geom_Surface plane = Handle_Geom_Plane.DownCast(surface).GetObject() # type: Geom_Plane gpPlane = plane.Pln() # type: gp_Pln origin = gpPlane.Location() # type: gp_Pnt ''' uDir = face_normal(faceU) # from OCCUtils.Construct module axis3 = gp_Ax3(origin, wDir, uDir) vDir = axis3.YDirection() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane elif ax3: axis3 = ax3 uDir = axis3.XDirection() vDir = axis3.YDirection() wDir = axis3.Axis().Direction() origin = axis3.Location() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane self.Trsf = gp_Trsf() self.Trsf.SetTransformation(axis3) self.Trsf.Invert() self.origin = origin self.uDir = uDir self.vDir = vDir self.wDir = wDir self.face = face self.size = size self.border = self.makeWpBorder(self.size) self.clList = [] # List of 'native' construction lines self.clineList = [] # List of pyOCC construction lines self.ccircList = [] # List of pyOCC construction circles self.wireList = [] # List of pyOCC wires self.wire = None self.hvcl((0, 0)) # Make H-V clines through origin self.accuracy = 0.001 # min distance between two points
def __init__(self, size, face=None, faceU=None, ax3=None): # gp_Ax3 of XYZ coord system origin = gp_Pnt(0, 0, 0) wDir = gp_Dir(0, 0, 1) uDir = gp_Dir(1, 0, 0) vDir = gp_Dir(0, 1, 0) xyzAx3 = gp_Ax3(origin, wDir, uDir) if (not face and not ax3): # create default wp (in XY plane at 0,0,0) axis3 = xyzAx3 gpPlane = gp_Pln(xyzAx3) self.gpPlane = gpPlane # type: gp_Pln self.plane = Geom_Plane(gpPlane) # type: Geom_Plane elif face: # create workplane on face, uDir defined by faceU wDir = face_normal(face) # from OCCUtils.Construct module props = GProp_GProps() brepgprop_SurfaceProperties(face, props) origin = props.CentreOfMass() uDir = face_normal(faceU) # from OCCUtils.Construct module axis3 = gp_Ax3(origin, wDir, uDir) vDir = axis3.YDirection() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane elif ax3: axis3 = ax3 uDir = axis3.XDirection() vDir = axis3.YDirection() wDir = axis3.Axis().Direction() origin = axis3.Location() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane self.Trsf = gp_Trsf() self.Trsf.SetTransformation(axis3) self.Trsf.Invert() self.origin = origin self.uDir = uDir self.vDir = vDir self.wDir = wDir self.wVec = gp_Vec(wDir) self.face = face self.size = size self.border = self.makeWpBorder(self.size) self.clines = set() # set of c-lines with (a, b, c) coefficients self.ccircs = set() # set of c-circs with (pc, r) coefficients self.edgeList = [] # List of profile lines type: <TopoDS_Edge> self.wire = None self.accuracy = 1e-6 # min distance between two points self.hvcl((0, 0)) # Make H-V clines through origin
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))
def measure_shape_mass_center_of_gravity(shape): """Returns the shape center of gravity Returns a gp_Pnt if requested (set as_Pnt to True) or a list of 3 coordinates, by default.""" inertia_props = GProp_GProps() if is_edge(shape): brepgprop_LinearProperties(shape, inertia_props) mass_property = "Length" elif is_face(shape): brepgprop_SurfaceProperties(shape, inertia_props) mass_property = "Area" else: brepgprop_VolumeProperties(shape, inertia_props) mass_property = "Volume" cog = inertia_props.CentreOfMass() mass = inertia_props.Mass() return cog, mass, mass_property
def _centre_of_mass(shape): r""" Parameters ---------- shape : OCC Shape Returns ------- gp_Pnt """ from OCC.Core.BRepGProp import brepgprop_VolumeProperties from OCC.Core.GProp import GProp_GProps g = GProp_GProps() brepgprop_VolumeProperties(shape, g) return g.CentreOfMass()
def line_clicked(shp, *kwargs): """ This function is called whenever a line is selected """ try: pass for shape in shp: # this should be a TopoDS_Edge print("Edge selected: ", shape) e = topods_Edge(shape) props = GProp_GProps() brepgprop_LinearProperties(e, props) length = props.Mass() print("此边的长度为: %f" % length) centerMass = props.CentreOfMass() print("此边的中心点为", centerMass.X(), centerMass.Y(), centerMass.Z()) except: pass
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 line_clicked(shp, *kwargs): """ This function is called whenever a line is selected """ for shape in shp: # this should be a TopoDS_Edge print("Edge selected: ", shape) e = topods_Edge(shape) props = GProp_GProps() brepgprop_LinearProperties(e, props) length = props.Mass() print("此边的长度为: %f" % length) centerMass = props.CentreOfMass() print("此边的中心点为", centerMass.X(), centerMass.Y(), centerMass.Z()) list_edge.append(e) if len(list_edge) == 2: pass am = AIS_AngleDimension(list_edge[0], list_edge[1]) print(123) display.Context.Display(am, True) list_edge.clear()
def glue_solids(event=None): display.EraseAll() display.Context.RemoveAll(True) # Without common edges S1 = read_step_file(os.path.join('part_of_sattelate', 'pribore', 'DAV_WS16.STEP')) display.DisplayShape(S1, color='BLUE', transparency=0.9) measure(S1) # the face to glue S2 = read_step_file(os.path.join('part_of_sattelate', 'pribore', 'Camara_WS16.STEP')) trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(750, 0, 0)) S2.Move(TopLoc_Location(trsf)) fuse_shp = BRepAlgoAPI_Fuse(S1, S2).Shape() props = GProp_GProps() brepgprop_VolumeProperties(fuse_shp, 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)) display.DisplayShape(fuse_shp) #pstring = 'x: % \n y: % \n z: %' % (cog_x, cog_y, cog_z) pnt = gp_Pnt(cog_x, cog_y, cog_z) # display points display.DisplayShape(pnt, update=True) pnt = gp_Pnt(0, 0, 0) # display points display.DisplayShape(pnt, update=True) #display.DisplayMessage(pnt, pstring) display.FitAll()
def centre_mass(self, shape): props = GProp_GProps() brepgprop_VolumeProperties(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)) mat = props.MatrixOfInertia() ####################################################################################### variation_inertial = abs(mat.Value(2, 3)) + abs(mat.Value(1, 2)) + abs( mat.Value(1, 3)) + abs(mat.Value(2, 1)) + abs(mat.Value( 3, 1)) + abs(mat.Value(3, 2)) var1, var2 = (cog_x**2 + cog_y**2 + cog_z**2)**0.5, variation_inertial if var1 < 0.0001 or var2 < 0.0001: var1, var2 = 10**10, 10**10 return var1, var2