def UpdateZone(self, Surfaces, display): print(self.Surfaces) sewing = BRepBuilderAPI_Sewing() for i in self.Surfaces: sewing.Add(Surfaces[i].GHShape) sewing.Perform() sewed_shape = sewing.SewedShape() tds = topods() # FixedShape = ShapeFix_Shell(tds.Shell(sewed_shape)) # FixedShape.Perform() solid = BRepBuilderAPI_MakeSolid(tds.Shell(sewed_shape)) # Test = BRepClass3d_SolidClassifier() # Test.Perform() display.DisplayShape(solid.Shape()) #print('done') Props = GProp_GProps() brepgprop.VolumeProperties(solid.Shape(), Props) self.Vol = Props.Mass() print(self.Vol) return display
def face_area(face): """Area of a face. Parameters ---------- face : TopoDS_Face The face for which the area is searched. Returns ------- float Area of the face. Notes ----- For references, see - https://dev.opencascade.org/doc/refman/html/class_b_rep_g_prop.html#abd91b892df8d0f6b8571deed5562ca1f - https://techoverflow.net/2019/06/13/how-to-compute-surface-area-of-topods_face-in-opencascade/ - https://github.com/tpaviot/pythonocc-demos/blob/master/examples/core_shape_properties.py#L48 """ properties = GProp_GProps() brepgprop_SurfaceProperties(face, properties) return properties.Mass()
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.)
def updateGraphicObjects(self, points, display): self.GHPolygon = [] for i in self.PolygonPoints: if i in range(len(points)): self.GHPolygon.Add(points[i].Point) else: print('polygon point %d does not exist' % i) self.GHPolygon.Close() self.GHFace = BRepBuilderAPI_MakeFace(self.GHPolygon.Wire()) self.GHShape = BRepBuilderAPI_MakeShape.Shape(self.GHFace) #display.DisplayShape(self.GHShape) if len(self.Opening) > 0: print('processing openings') for i in range(len(self.Opening)): openingpolygon = BRepBuilderAPI_MakePolygon() for j in self.Opening[i]: openingpolygon.Add(points[j].Point) openingpolygon.Close() openingface = BRepBuilderAPI_MakeFace(openingpolygon.Wire()) openingshape = BRepBuilderAPI_MakeShape.Shape(openingface) cut = BRepAlgoAPI_Cut(self.GHShape, openingshape) self.GHShape = cut.Shape() #display.DisplayShape(self.GHShape) # calculate Area Props = GProp_GProps() brepgprop.SurfaceProperties(self.GHShape, Props) self.Area = Props.Mass()
def centerOfMass(obj): """ Calculates the 'mass' of an object. """ Properties = GProp_GProps() calc_function = shape_properties_LUT[obj.wrapped.ShapeType()] if calc_function: calc_function(obj.wrapped, Properties) return Vector(Properties.CentreOfMass()) else: raise NotImplemented
def __init__(self, instance): from OCC.GProp import GProp_GProps from OCC.BRepGProp import BRepGProp self.instance = instance self.system = GProp_GProps() _topo_type = self.instance.type if _topo_type == 'face' or _topo_type == 'shell': BRepGProp().SurfaceProperties(self.instance.topo, self.system) elif _topo_type == 'edge': BRepGProp().LinearProperties(self.instance.topo, self.system) elif _topo_type == 'solid': BRepGProp().VolumeProperties(self.instance.topo, self.system)
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 major_faces(geom): """ returns the 2 largest by area faces """ topo = Topo(geom) face_arr = [] for face in topo.faces(): system = GProp_GProps() brepgprop.SurfaceProperties(face, system) area = system.Mass() face_arr.append((area, face)) face_arr.sort(reverse=True, key=lambda x: x[0]) return [x[1] for x in face_arr[:2]]
def system(self): r"""Initialise the GProp_GProps depending on the topological type Notes ----- geom_type could be abstracted with TopoDS... instead of using _topo_type Returns ------- OCC.GProp.GProp_GProps """ self._system = GProp_GProps() if self._topo_type in GlobalProperties.surfacic_types: brepgprop_SurfaceProperties(self.shape, self._system) elif self._topo_type in GlobalProperties.linear_types: brepgprop_LinearProperties(self.shape, self._system) elif self._topo_type in GlobalProperties.volumic_types: brepgprop_VolumeProperties(self.shape, self._system) else: msg = "ShapeType is not linear, surfacic or volumic" logger.error(msg) raise WrongTopologicalType(msg) return self._system
def CalculateSurfaceArea(shape): """Calculates the surface area of input shape Parameters ---------- shape : TopoDS_Shape Returns ------- Area : scalar Calculated surface area """ from OCC.BRepGProp import brepgprop_SurfaceProperties from OCC.GProp import GProp_GProps System = GProp_GProps() brepgprop_SurfaceProperties(shape, System) Area = System.Mass() return Area
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 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
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
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()))
class GlobalProperties(object): ''' global properties for all topologies ''' def __init__(self, instance): from OCC.GProp import GProp_GProps from OCC.BRepGProp import BRepGProp self.instance = instance self.system = GProp_GProps() _topo_type = self.instance.type if _topo_type == 'face' or _topo_type == 'shell': BRepGProp().SurfaceProperties(self.instance.topo, self.system) elif _topo_type == 'edge': BRepGProp().LinearProperties(self.instance.topo, self.system) elif _topo_type == 'solid': BRepGProp().VolumeProperties(self.instance.topo, self.system) def centre(self): """ :return: centre of the entity """ return self.system.CentreOfMass() def inertia(self): '''returns the inertia matrix''' return self.system.MatrixOfInertia(), self.system.MomentOfInertia() def area(self): '''returns the area of the surface''' return self.system.Mass() def bbox(self): ''' returns the bounding box of the face ''' return get_boundingbox(self.instance.topo)
from OCC.gp import gp_Pnt, gp_Ax1, gp_Dir from OCC.GProp import GProp_GProps from OCC.BRepGProp import brepgprop_VolumeProperties from math import pi # original implementation with occ backend import siconos.io.mechanics_io siconos.io.mechanics_io.set_implementation('original') siconos.io.mechanics_io.set_backend('occ') # ball shape ball = BRepPrimAPI_MakeSphere(.15).Shape() ball_props = GProp_GProps() brepgprop_VolumeProperties(ball, ball_props) ball_mass = ball_props.Mass() ball_com = ball_props.CentreOfMass() ball_inertia = ball_props.MatrixOfInertia() ball_I1 = ball_props.MomentOfInertia(gp_Ax1(ball_com, gp_Dir(1, 0, 0))) ball_I2 = ball_props.MomentOfInertia(gp_Ax1(ball_com, gp_Dir(0, 1, 0))) ball_I3 = ball_props.MomentOfInertia(gp_Ax1(ball_com, gp_Dir(0, 0, 1))) print 'ball mass:', ball_mass print 'ball center of mass:', (ball_com.Coord(1), ball_com.Coord(2), ball_com.Coord(3)) print 'ball moment of inertia:', (ball_I1, ball_I2, ball_I3)
def Center(self): Properties = GProp_GProps() brepgprop_SurfaceProperties(self.wrapped, Properties) return Vector(Properties.CentreOfMass())
def solid_volume(occ_solid): props = GProp_GProps() brepgprop_VolumeProperties(occ_solid, props) volume = props.Mass() return volume
def area(shape, tolerance=1e-5): prop = GProp_GProps() brepgprop_SurfaceProperties(shape, prop, tolerance) return prop.Mass()
def linear(self): '''returns the length of a wire or edge ''' prop = GProp_GProps() error = self.bgprop.LinearProperties(self.shape, prop ) return prop
def compute_inertia_and_center_of_mass(shapes, io=None): """ Compute inertia from a list of Shapes. Returns ------- mass center_of_mass inertia inertia_matrix """ from OCC.GProp import GProp_GProps from OCC.BRepGProp import brepgprop_VolumeProperties from OCC.gp import gp_Ax1, gp_Dir from siconos.mechanics import occ system = GProp_GProps() for shape in shapes: iprops = GProp_GProps() if shape.data is None: if io is not None: shape.data = io._shape.get(shape.shape_name, new_instance=True) else: warn('cannot get shape {0}'.format(shape.shape_name)) return None iishape = shape.data ishape = occ.OccContactShape(iishape).data() # the shape relative displacement occ.occ_move(ishape, list(shape.translation) + list(shape.orientation)) brepgprop_VolumeProperties(iishape, iprops) density = None if hasattr(shape, 'mass') and shape.mass is not None: density = shape.mass / iprops.Mass() elif shape.parameters is not None and hasattr(shape.parameters, 'density'): density = shape.parameters.density #print('shape.parameters.density:', shape.parameters.density) else: density = 1. assert density is not None # print("shape", shape.shape_name) # print('density:', density) # print('iprops.Mass():', iprops.Mass()) system.Add(iprops, density) mass = system.Mass() assert (system.Mass() > 0.) computed_com = system.CentreOfMass() gp_mat = system.MatrixOfInertia() inertia_matrix = np.zeros((3, 3)) for i in range(0, 3): for j in range(0, 3): inertia_matrix[i, j] = gp_mat.Value(i + 1, j + 1) I1 = system.MomentOfInertia(gp_Ax1(computed_com, gp_Dir(1, 0, 0))) I2 = system.MomentOfInertia(gp_Ax1(computed_com, gp_Dir(0, 1, 0))) I3 = system.MomentOfInertia(gp_Ax1(computed_com, gp_Dir(0, 0, 1))) inertia = [I1, I2, I3] center_of_mass = np.array( [computed_com.Coord(1), computed_com.Coord(2), computed_com.Coord(3)]) return mass, center_of_mass, inertia, inertia_matrix
def volume(self): '''returns the volume of a solid ''' prop = GProp_GProps() error = self.bgprop.VolumeProperties(self.shape, prop, self.tolerance) return prop
def surface(self): '''returns the area of a surface ''' prop = GProp_GProps() error = self.bgprop.SurfaceProperties(self.shape, prop, self.tolerance) return prop
def compute_inertia_and_center_of_mass(shapes, mass, io=None): """ Compute inertia from a list of Shapes. """ from OCC.GProp import GProp_GProps from OCC.BRepGProp import brepgprop_VolumeProperties from OCC.gp import gp_Ax1, gp_Dir from siconos.mechanics import occ props = GProp_GProps() for shape in shapes: iprops = GProp_GProps() if shape.data is None: if io is not None: shape.data = io._shape.get(shape.shape_name, new_instance=True) else: warn('cannot get shape {0}'.format(shape.shape_name)) return None iishape = shape.data ishape = occ.OccContactShape(iishape).data() # the shape relative displacement occ.occ_move(ishape, list(shape.translation) + list(shape.orientation)) brepgprop_VolumeProperties(iishape, iprops) density = None if hasattr(shape, 'mass') and shape.mass is not None: density = shape.mass / iprops.Mass() elif shape.parameters is not None and \ hasattr(shape.parameters, 'density'): density = shape.parameters.density else: density = 1. assert density is not None props.Add(iprops, density) assert (props.Mass() > 0.) global_density = mass / props.Mass() computed_com = props.CentreOfMass() I1 = global_density * props.MomentOfInertia( gp_Ax1(computed_com, gp_Dir(1, 0, 0))) I2 = global_density * props.MomentOfInertia( gp_Ax1(computed_com, gp_Dir(0, 1, 0))) I3 = global_density * props.MomentOfInertia( gp_Ax1(computed_com, gp_Dir(0, 0, 1))) inertia = [I1, I2, I3] center_of_mass = np.array( [computed_com.Coord(1), computed_com.Coord(2), computed_com.Coord(3)]) return inertia, center_of_mass
def calculate_volume(shape): props = GProp_GProps() brepgprop_VolumeProperties(shape, props) return props.Mass()
def getFaceArea(face): """ Return the area of the face object given """ system = GProp_GProps() brepgprop_SurfaceProperties(face, system) return (system.Mass())
def centerOfMass(solid): prop = GProp_GProps() brepgprop_VolumeProperties(solid, prop) return prop.CentreOfMass()
def _center_of_mass(shape): Properties = GProp_GProps() brepgprop_VolumeProperties(shape, Properties) return Vector(Properties.CentreOfMass())
def Length(self): Properties = GProp_GProps() brepgprop_LinearProperties(self.wrapped, Properties) return Properties.Mass()
def linear(self): """returns the length of a wire or edge""" prop = GProp_GProps() brepgprop_LinearProperties(self.shape, prop) return prop