def compute(self): aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # 1D an1DHypothesis = StdMeshers_Arithmetic1D( 0, 0, aMeshGen) #discretization of the wire # The smallest and longer distance between edges is computed from the _precision value min_dist = self.get_precision() / 3. max_dist = self.get_precision() * 3. an1DHypothesis.SetLength( min_dist, False) #the smallest distance between 2 points an1DHypothesis.SetLength(max_dist, True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dAlgo = StdMeshers_MEFISTO_2D(2, 0, aMeshGen) aMesh.ShapeToMesh(self._shape) #Assign hyptothesis to mesh aMesh.AddHypothesis(self._shape, 0) aMesh.AddHypothesis(self._shape, 1) aMesh.AddHypothesis(self._shape, 2) #Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) self._mesh = aMesh self._mesh_data_source = aMesh.GetMeshDS() #print self._mesh_data_source self.build_lists() return True
def smesh_MEFISTO2D(event=None): # Create the Mesh aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # 1D an1DHypothesis = StdMeshers_Arithmetic1D( 0, 0, aMeshGen) # discretization of the wire an1DHypothesis.SetLength(0.1, False) # the smallest distance between 2 points an1DHypothesis.SetLength(0.5, True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_TrianglePreference( 2, 0, aMeshGen) # define the boundary a2dAlgo = StdMeshers_MEFISTO_2D(3, 0, aMeshGen) # alculate mesh aMesh.ShapeToMesh(aShape) # Assign hyptothesis to mesh aMesh.AddHypothesis(aShape, 0) aMesh.AddHypothesis(aShape, 1) aMesh.AddHypothesis(aShape, 2) aMesh.AddHypothesis(aShape, 3) # Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) # Display the data display_mesh(aMesh)
def makeSMESH(shape): """ Make mesh using SMESH Most of this was taken from core_mesh_surfacic.py in pythonocc-demos """ aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # 1D an1DHypothesis = StdMeshers_MaxLength(0, 0, aMeshGen) an1DHypothesis.SetLength(10) an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_TrianglePreference( 2, 0, aMeshGen) # define the boundary a2dAlgo = StdMeshers_MEFISTO_2D(3, 0, aMeshGen) # alculate mesh aMesh.ShapeToMesh(diffuser.Shape()) # Assign hyptothesis to mesh aMesh.AddHypothesis(diffuser.Shape(), 0) aMesh.AddHypothesis(diffuser.Shape(), 1) aMesh.AddHypothesis(diffuser.Shape(), 2) aMesh.AddHypothesis(diffuser.Shape(), 3) # Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) return aMesh
def get_fast_stl_mesh_from_path(_path): """ open/parse STL file and get the resulting TopoDS_Shape instance """ gen = SMESH_Gen() mesh = gen.CreateMesh(0, True) # .STLToMesh does a poor job in catching errors, so let's check the path... if not os.path.isfile(_path): raise ValueError("{0} is not a valid path".format(_path)) mesh.STLToMesh(_path) # test for a minimal number of mesh vertices if mesh.NbNodes() < 1: raise ValueError("stl file {0} contains no geometry".format(_path)) aDS = SMESH_MeshVSLink(mesh) aMeshVS = MeshVS_Mesh(True) DMF = 2 # to wrap; 1 is wireframe MeshVS_BP_Mesh = 5 # To wrap! aPrsBuilder = MeshVS_MeshPrsBuilder(aMeshVS.GetHandle(), DMF, aDS.GetHandle()) #,0,MeshVS_BP_Mesh) aMeshVS.SetDataSource(aDS.GetHandle()) aMeshVS.AddBuilder(aPrsBuilder.GetHandle(), False) MeshVS_DMF_HilightPrs = int(0x0400) MeshVS_DMF_Wireframe = int(0x0001) MeshVS_DMF_Shading = int(0x0002) aMeshVS.SetDisplayMode(MeshVS_DMF_Shading) # 1 -> wireframe, 2 -> shaded # aMeshVS.SetDisplayMode(MeshVS_DMF_Wireframe) # 1 -> wireframe, 2 -> shaded # aMeshVS.SetColor(Quantity_NOC_AZURE) #Create the graphic window and display the mesh mesh_to_display = aMeshVS.GetHandle() # display the mesh edges in black mesh_drawer = aMeshVS.GetDrawer().GetObject() # Edges in black # mesh_drawer.SetColor(3,Quantity_Color(Quantity_NOC_BLACK))# 3 means Edge color # Markers in green # mesh_drawer.SetColor(13,Quantity_Color(Quantity_NOC_GREEN)) mesh_drawer.SetMaterial(MeshVS_DA_FrontMaterial, Graphic3d_MaterialAspect(Graphic3d_NOM_SATIN)) mesh_drawer.SetBoolean(MeshVS_DA_DisplayNodes, False) mesh_drawer.SetBoolean(MeshVS_DA_ShowEdges, False) mesh_drawer.SetBoolean(MeshVS_DA_SmoothShading, True) mesh_drawer.SetBoolean(MeshVS_DA_Reflection, True) # mesh_drawer.SetColor( MeshVS_DA_InteriorColor, Quantity_Color(Quantity_NOC_AZURE)) # mesh_drawer.SetBoolean(MeshVS_DA_ColorReflection, True) msh = mesh_to_display.GetObject() return msh
def test_mesh_sphere_quadrangle(self): aShape = BRepPrimAPI_MakeSphere(10.).Shape() # Create the Mesh aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # 1D an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen)#discretization of the wire an1DHypothesis.SetLength(0.1, False) #the smallest distance between 2 points an1DHypothesis.SetLength(0.5, True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_TrianglePreference(2, 0, aMeshGen) #define the boundary a2dAlgo = StdMeshers_Quadrangle_2D(3, 0, aMeshGen) # the 2D mesh #Calculate mesh aMesh.ShapeToMesh(aShape) #Assign hyptothesis to mesh aMesh.AddHypothesis(aShape, 0) aMesh.AddHypothesis(aShape, 1) aMesh.AddHypothesis(aShape, 2) aMesh.AddHypothesis(aShape, 3) #Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh())
def testSTLVectorDouble(self): ''' Checks the IntVector and DoubleVector classes that are used in the StdMeshers module ''' # The IntVector must be initialized from a list/tuple of floats/integers. Integers will # be converted to floats d_v = DoubleVector([0.1,0.2,0.6,0.7]) self.assertEqual(d_v[0],0.1) self.assertEqual(d_v[1],0.2) self.assertEqual(d_v[2],0.6) self.assertEqual(d_v[3],0.7) # If at least one item of the list is not an float or an integer, raise an exception self.assertRaises(TypeError,DoubleVector,[1.0,2.0,3.0,"string"]) # Test one method of StdMeshers that takes/returns such a parameter type from OCC.SMESH import SMESH_Gen number_of_segments = StdMeshers_NumberOfSegments(1,10,SMESH_Gen()) number_of_segments.SetTableFunction(d_v) self.assertEquals(number_of_segments.GetTableFunction(),(0.1,0.2,0.6,0.7))
MeshVS_DMF_NodalColorDataPrs) from OCC.SMDSAbs import SMDSAbs_Face from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() # First create a 'complex' shape (actually a boolean op between a box and a cylinder) print('Creating geometry ...', end='') box = BRepPrimAPI_MakeBox(200, 30, 30).Shape() sphere = BRepPrimAPI_MakeSphere(gp_Pnt(150, 20, 20), 80).Shape() aShape = BRepAlgoAPI_Cut(box, sphere).Shape() print('Done.') # Create the Mesh print('Creating mesh ...', end='') aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) print('Done.') print('Adding hypothesis and algorithms ...', end='') # 1D an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen) #discretization of the wire an1DHypothesis.SetLength(5., False) #the smallest distance between 2 points an1DHypothesis.SetLength(10., True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_QuadranglePreference(2, 0, aMeshGen) #define the boundary a2dAlgo = StdMeshers_Quadrangle_2D(3, 0, aMeshGen) # the 2D mesh
##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.SMESH import SMESH_Gen from OCC.StdMeshers import (StdMeshers_Arithmetic1D, StdMeshers_Regular_1D, StdMeshers_TrianglePreference, StdMeshers_MEFISTO_2D, StdMeshers_QuadranglePreference, StdMeshers_Quadrangle_2D) #Create the shape to mesh aShape = BRepPrimAPI_MakeBox(10, 20, 40).Shape() aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) def ComputeMesh(MEFISTO2=False): an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen) #print dir(an1DHypothesis) #print an1DHypothesis.SaveTo() an1DHypothesis.SetLength(1., False) an1DHypothesis.SetLength(2., True) an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) if MEFISTO2: #2D a2dHypothseis = StdMeshers_TrianglePreference(
def generate_stl(self, min_length=None, max_length=None, outfile_stl=None): """ Generate and export the .STL surface mesh for the blade as a whole, including the upper face, lower face and tip. The method utilizes modules from OCC SMESH which is standalone mesh framework based on SALOME mesher project. Please refer to https://github.com/tpaviot and http://docs.salome-platform.org/7/gui/SMESH/index.html for further details. This method requires PythonOCC and SMESH to be installed. :param double min_length: smallest distance between two nodes. Default value is None :param double max_length: largest distance between two nodes. Default value is None :param string outfile_stl: if string is passed then the method exports the generated 2D surface mesh into .stl file holding the name <outfile_stl>.stl. Default value is None We note that since the current implementation performs triangulation based on a topological compound that combines the blade 3 generated shapes without "fusion", it may happen that the generated triangulation of the upper and lower blade faces do not share the same exact nodes on the joint edge/wire resulting from the faces intersection. The current implementation can be enough for visualization purpose. However if the generated mesh is intended for computational analysis then a manual mesh healing is recommended by the user (e.g. see "Repair > Sewing" in SALOME GUI) for a proper mesh closure. """ from OCC.SMESH import SMESH_Gen from OCC.StdMeshers import (StdMeshers_Arithmetic1D, StdMeshers_TrianglePreference, StdMeshers_Regular_1D, StdMeshers_MEFISTO_2D) from OCC.BRep import BRep_Builder from OCC.TopoDS import TopoDS_Shape, TopoDS_Compound if min_length <= 0 or max_length <= 0: raise ValueError('min_length and max_length must be positive.') if min_length >= max_length: raise ValueError('min_length can not be greater than max_length') # First we check that blade shapes are generated, otherwise we generate # them. After that we combine the generated_upper_face, # generated_lower_face, and generated_tip into a topological compound # that we use to compute the surface mesh if (self.generated_upper_face is None) or not isinstance( self.generated_upper_face, TopoDS_Shape): # Upper face is generated with a maximal U degree = 1 self._generate_upper_face(maxDeg=1) if (self.generated_lower_face is None) or not isinstance( self.generated_lower_face, TopoDS_Shape): # Upper face is generated with a maximal U degree = 1 self._generate_lower_face(maxDeg=1) if (self.generated_tip is None) or not isinstance( self.generated_tip, TopoDS_Shape): # Upper face is generated with a maximal U degree = 1 self._generate_tip(maxDeg=1) # Now we regroup all the shapes into a TopoDS_Compound aCompound = TopoDS_Compound() aBuilder = BRep_Builder() aBuilder.MakeCompound(aCompound) # Add shapes aBuilder.Add(aCompound, self.generated_upper_face) aBuilder.Add(aCompound, self.generated_lower_face) aBuilder.Add(aCompound, self.generated_tip) # In the following we build the surface mesh according to the given # hypotheses aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # Adding 1D hypothesis and algorithms # Wire discretization. Nodes are distributed based on Arithmetic1D # hypothesis which allows to split edges into segments with a length # that changes in arithmetic progression (Lk = Lk-1 + d) beginning # from a given min length and up to a given max length. More about # 1D hypotheses can be viewed through: # http://docs.salome-platform.org/7/gui/SMESH/a1d_meshing_hypo_page.html an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen) # Smallest distance between 2 points an1DHypothesis.SetLength(min_length, False) # Longest distance between 2 points an1DHypothesis.SetLength(max_length, True) # Regular Interpolation an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # Adding 2D hypothesis and algorithms # 2D surface mesh -- Triangulations a2dHypothseis = StdMeshers_TrianglePreference(2, 0, aMeshGen) a2dAlgo = StdMeshers_MEFISTO_2D(3, 0, aMeshGen) #Calculate mesh for the topological compound containing the 3 shapes aMesh.ShapeToMesh(aCompound) #Assign hyptothesis to mesh aMesh.AddHypothesis(aCompound, 0) aMesh.AddHypothesis(aCompound, 1) aMesh.AddHypothesis(aCompound, 2) aMesh.AddHypothesis(aCompound, 3) if outfile_stl is not None: if not isinstance(outfile_stl, str): raise ValueError('outfile_stl must be a valid string.') #Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh()) # Export STL aMesh.ExportSTL(outfile_stl + '.stl', False)