示例#1
0
    def centre_mass(self, shape):
        from OCC.Core.GProp import GProp_GProps
        from OCC.Core.BRepGProp import brepgprop_VolumeProperties, brepgprop_SurfaceProperties, \
            brepgprop_LinearProperties

        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
示例#2
0
def ask_face_centroid(face):
    """
    """
    massProps = GProp_GProps()
    brepgprop.SurfaceProperties(face, massProps)
    gPt = massProps.CentreOfMass()
    return gPt.Coord()
    def test_inherit_topods_shape(self):
        at = self.assertTrue
        af = self.assertFalse

        class InheritEdge(TopoDS_Edge):
            def __init__(self, edge):
                # following constructor creates an empy TopoDS_Edge
                super(InheritEdge, self).__init__()
                # we need to copy the base shape using the following three
                # lines
                at(self.IsNull())
                self.TShape(edge.TShape())
                self.Location(edge.Location())
                self.Orientation(edge.Orientation())
                af(self.IsNull())
                # then it becomes possible to extend the base class

        # create a line, compute its length
        base_edge = BRepBuilderAPI_MakeEdge(gp_Pnt(100., 0., 0.),
                                            gp_Pnt(150., 0., 0.)).Edge()
        inherited_edge = InheritEdge(base_edge)
        g1 = GProp_GProps()
        brepgprop_LinearProperties(inherited_edge, g1)
        length = g1.Mass()
        self.assertEqual(length, 50.)
示例#4
0
def extrusion_to_solid(extrusion, name):
    from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing, BRepBuilderAPI_MakeSolid
    from OCC.Core.TopoDS import topods
    from OCC.Core.BRepGProp import brepgprop_VolumeProperties
    from OCC.Core.GProp import GProp_GProps
    from OCCUtils.Topology import Topo
    d = extrusion_to_ruled_surfaces(extrusion, cap=True)
    sew = BRepBuilderAPI_Sewing()

    make_solid = BRepBuilderAPI_MakeSolid()
    faces = {}
    for k in d.keys():
        faces[name + '_' + k] = d[k]
        sew.Add(d[k].Shape())
    sew.Perform()
    shell = sew.SewedShape()
    make_solid.Add(topods.Shell(shell))
    gp = GProp_GProps()

    #make_solid.Add(shell)
    solid = make_solid.Solid()
    brepgprop_VolumeProperties(shell, gp)
    print gp.Mass()

    return Part(name, faces, solid)
示例#5
0
 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))
示例#6
0
    def inter_objects(self):
        from OCC.Core.GProp import GProp_GProps
        from OCC.Core.BRepGProp import brepgprop_VolumeProperties, brepgprop_SurfaceProperties, \
            brepgprop_LinearProperties
        from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Fuse, BRepAlgoAPI_Common, BRepAlgoAPI_Section, BRepAlgoAPI_Cut

        # print('Start_inter_analyse')
        inter_mass = 0
        props = GProp_GProps()
        # print(self.names_models)
        names = []
        for name in self.modules:
            names.append(name)

        if len(names) > 1:
            for i in range(len(names) - 1):
                for j in range(i + 1, len(names)):
                    # print(self.names_models[i], self.names_models[j])
                    if self.names_models[i] == self.names_models[j]: continue
                    body_inter = BRepAlgoAPI_Section(
                        self.modules[self.names_models[i]],
                        self.modules[self.names_models[j]]).Shape()
                    brepgprop_LinearProperties(body_inter, props)
                    mass = props.Mass()
                    # print(mass)
                    if mass > 0:
                        inter_mass += mass

        # self.start_display()
        return inter_mass
示例#7
0
    def inter_objects(self):

        # print('Start_inter_analyse')
        self.inter_mass = 0
        props = GProp_GProps()
        # print(self.names_models)

        for i in range(len(self.names_models) - 1):
            for j in range(i + 1, len(self.names_models)):
                # self.display.DisplayShape(self.modules[self.names_models[i]], color='RED', transparency=0.9)
                # self.display.DisplayShape(self.modules[self.names_models[j]], color='RED', transparency=0.9)
                print(self.names_models[i], self.names_models[j])
                if self.names_models[i] == self.names_models[j]: continue
                body_inter = BRepAlgoAPI_Section(
                    self.modules[self.names_models[i]],
                    self.modules[self.names_models[j]]).Shape()
                # self.display.DisplayShape(body_inter, color='WHITE')
                brepgprop_LinearProperties(body_inter, props)
                mass = props.Mass()
                print(mass)
                if mass > 0:
                    self.inter_mass += mass
                    self.valume_inter_obj[self.names_models[i]][
                        self.names_models[j]] = mass

        #self.start_display()
        return self.inter_mass
示例#8
0
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)
示例#9
0
 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()
示例#10
0
 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()
示例#11
0
 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()
示例#12
0
    def __init__(self, touch=False, file=False):
        dispocc.__init__(self, touch=touch)
        self.prop = GProp_GProps()
        self.base = make_box(100, 100, 100)
        self.base_vol = self.cal_vol(self.base)

        self.splitter = BOPAlgo_Splitter()
        self.splitter.AddArgument(self.base)
        print(self.cal_vol(self.base))
示例#13
0
def Simulate_Path():
    suction_Dia=12
    cover_percentage=0.4
    step_over=suction_Dia*cover_percentage
    step_down=suction_Dia*0.5
    point_list=Generating_Path_function(step_over,step_down)
    display.EraseAll()
    stl_shp1 = read_stl_file('mymodel1.stl')
    props3 = GProp_GProps()
    brepgprop_VolumeProperties(stl_shp1, props3)
    model_mass = props3.Mass()
    
    
    view_box=BRepPrimAPI_MakeBox(gp_Pnt(-30, -30, 0),100, 100, 60)
    display.DisplayShape(view_box.Shape(),color = "BLACK",transparency = 0.999)
    display.DisplayShape(stl_shp1)
    #powder box=material
    material_box=BRepPrimAPI_MakeBox(gp_Pnt(-18, -18, 0),58, 58, 22)

    props2 = GProp_GProps()
    brepgprop_VolumeProperties(material_box.Shape(), props2)
    mass2 = props2.Mass()
    
    
    first_simulate= simulate(suction_Dia,point_list[0],90,material_box)
    
    second_simulate=simulate(suction_Dia,point_list[1],45,first_simulate)

    props = GProp_GProps()
    brepgprop_VolumeProperties(second_simulate.Shape(), props)
    mass = props.Mass()
    print("remained volume = %",(mass*100/(mass2-model_mass)))
    display.DisplayShape(second_simulate.Shape(),color = "BLACK",transparency = 0.3)
示例#14
0
 def one_obj_with_all(self, name):
     mass = 0
     props = GProp_GProps()
     for body2 in self.modules:
         if name == body2: continue
         body_inter = BRepAlgoAPI_Common(self.modules[body2],
                                         self.modules[name]).Shape()
         # self.display.DisplayShape(body_inter, color='WHITE')
         brepgprop_VolumeProperties(body_inter, props)
         mass += props.Mass()
     return mass
示例#15
0
    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
示例#16
0
def get_volume(stepfile):

    from OCC.Core.GProp import GProp_GProps
    from OCC.Core.STEPControl import STEPControl_Reader
    from OCC.Core.BRepGProp import brepgprop_VolumeProperties

    step_reader = STEPControl_Reader()
    step_reader.ReadFile(stepfile)
    step_reader.TransferRoot()
    shape = step_reader.Shape()
    prop = GProp_GProps()
    brepgprop_VolumeProperties(shape, prop, 1e-5)
    return prop.Mass()
示例#17
0
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
示例#18
0
    def __init__(self):
        plotocc.__init__(self)
        self.prop = GProp_GProps()
        self.base = make_box(100, 100, 100)
        self.base_vol = self.cal_vol(self.base)

        self.splitter = BOPAlgo_Splitter()
        self.splitter.AddArgument(self.base)
        print(self.cal_vol(self.base))

        from OCC.Display.qtDisplay import qtViewer3d
        self.app = self.get_app()
        self.wi = self.app.topLevelWidgets()[0]
        self.vi = self.wi.findChild(qtViewer3d, "qt_viewer_3d")
        self.on_select()
示例#19
0
    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
示例#20
0
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
示例#21
0
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))
示例#22
0
    def inter_with_frame(self):
        # print('Start_inter_analyse')
        all_result = 0
        props = GProp_GProps()
        # print(self.modules)
        for model in self.modules:
            # print('#')
            body_inter = BRepAlgoAPI_Common(self.frame,
                                            self.modules[model]).Shape()
            # self.display.DisplayShape(body_inter, color='YELLOW')
            brepgprop_VolumeProperties(body_inter, props)
            mass = props.Mass()
            if mass > 0:
                self.valume_inter[model] = props.Mass()
                all_result += mass

        return all_result
示例#23
0
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()
示例#24
0
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
示例#25
0
def solid_volume(occsolid):
    """
    This function calculates the volume of the OCCsolid.
 
    Parameters
    ----------
    occsolid : OCCsolid
        The OCCsolid to be analysed.
        
    Returns
    -------
    volume : float
        The volume of the solid.
    """
    props = GProp_GProps()
    brepgprop_VolumeProperties(occsolid, props)
    volume = props.Mass()
    return volume
示例#26
0
def get_mass(compound):
    if not (isinstance(compound, TopoDS_Compound)
            or isinstance(compound, TopoDS_Solid)):
        raise RuntimeError("bad mass instance type")
    compound_ = copy.deepcopy(compound)

    if isinstance(compound, TopoDS_Compound):
        solids = _get_list_from_compound(compound_, CompoundSequenceType.SOLID)
    else:
        solids = [compound_]

    total_mass = 0
    for solid in solids:
        props = GProp_GProps()
        BRepGProp.brepgprop_VolumeProperties(solid, props)
        mass = props.Mass()
        total_mass += mass
    return total_mass
示例#27
0
 def system(self):
     self._system = GProp_GProps()
     # todo, type should be abstracted with TopoDS...
     _topo_type = self.instance.topo_type
     if _topo_type == 'face' or _topo_type == 'shell':
         brepgprop_SurfaceProperties(self.instance, self._system)
     elif _topo_type == 'edge':
         brepgprop_LinearProperties(self.instance, self._system)
     elif _topo_type == 'solid':
         brepgprop_VolumeProperties(self.instance, self._system)
     return self._system
示例#28
0
def reconstruct_object(array):
    from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing, BRepBuilderAPI_MakeSolid
    from OCC.Core.BRepGProp import brepgprop_VolumeProperties
    from OCC.Core.GProp import GProp_GProps
    from OCC.Core.TopoDS import topods
    obj = BRepBuilderAPI_Sewing()
    for i, si in enumerate(array):
        obj.Add(si)
    obj.Perform()
    shell = obj.SewedShape()
    make_solid = BRepBuilderAPI_MakeSolid()
    make_solid.Add(topods.Shell(shell))
    gp = GProp_GProps()

    #make_solid.Add(shell)
    solid = make_solid.Solid()
    brepgprop_VolumeProperties(shell, gp)
    print 'RECONSTRUCTED SOLID VOLUME :'
    print gp.Mass()
    return solid
示例#29
0
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()
示例#30
0
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()))