def import_gmsh_mesh(filename, analysis=None): '''insert a FreeCAD FEM Mesh object in the ActiveDocument ''' assert filename[-4:] == u".geo" mesh_filename = filename[:-4] + u".unv" # temp file name, assuming input filename is unicode cmdlist = [u'gmsh', u"-format", u"unv", filename, u"-o", mesh_filename, u"-"] # gmsh can also convert from msh to other format in GUI error = _run_command(cmdlist) if not error: if analysis: # `and analysis.isDerivedFrom("Fem::FemAnalysisObject")` docName = analysis.Document.Name else: docName = None import Fem Fem.insert(mesh_filename, docName)
def gmshmesh(compound, NG2D, name=""): ''' export, mesh compound at gmsh, reimport as .unv (3d to be implemented) ''' if name =="": name="m_"+compound.Label # ImportGui.export([FreeCAD.activeDocument().getObject(compound.Name)], "/tmp/tmpNO2c.step") # command="/usr/bin/gmsh /tmp/tmpNO2c.step -2 -format unv -o /tmp/"+name+".unv -algo "+str(NG2D.gmshalgoname)+" -clmax " +str(NG2D.NGParamSetMaxSize)+" -clmin "+str(NG2D.NGParamSetMinSize)+" -string Geometry.OCCSewFaces=1;" #command="/usr/bin/gmsh /tmp/tmpNO2c.step -2 -format unv -o /tmp/Compound019_Mesh.unv -algo auto -string Geometry.OCCSewFaces=1;" #command="g" # #print command output = subprocess.check_output([command, '-1'], shell=True, stderr=subprocess.STDOUT,) FreeCAD.Console.PrintMessage(output) # Fem.insert("/tmp/"+name+".unv", FreeCAD.ActiveDocument.Name) #Fem.insert("/tmp/Compound019_Mesh.unv", FreeCAD.ActiveDocument.Name) femmesh=FreeCAD.ActiveDocument.Objects[len(FreeCAD.ActiveDocument.Objects)-1] FreeCADGui.ActiveDocument.getObject(femmesh.Name).DisplayMode = "Wireframe" return femmesh
def proceed(self): temp_file = tempfile.mkstemp(suffix='.step')[1] selection = FreeCADGui.Selection.getSelection() if not selection: QtGui.QMessageBox.critical( None, "GMSHMesh macro", "An object has to be selected to run gmsh!") return # Export a part in step format ImportGui.export(selection, temp_file) selection_name = selection[0].Name # Mesh temporaly file file_format = self.cmb_format.currentText() temp_mesh_file = os.path.join(tempfile.tempdir, selection_name + '_Mesh.' + file_format) # OPTIONS GMSH: clmax = self.sb_max_element_size.text() clmin = self.sb_min_element_size.text() cmd_line_opt = self.le_cmd_line_opt.text() algo = self.cmb_algorithm.currentText() mesh_order = self.sb_mesh_order.text() if self.cb_optimized.isChecked(): cmd_optimize = ' -optimize' else: cmd_optimize = '' if self.rb_3D.isChecked(): dim = ' -3 ' if self.rb_2D.isChecked(): dim = ' -2 ' if self.rb_1D.isChecked(): dim = ' -1 ' if self.cb_max_elme_size.isChecked(): max_size = ' -clmax ' + clmax else: max_size = '' if self.cb_min_elme_size.isChecked(): min_size = ' -clmin ' + clmin else: min_size = '' if self.cb_mesh_order.isChecked(): order = ' -order ' + mesh_order else: order = '' options = ' -algo ' + algo + max_size + min_size + cmd_optimize + order + cmd_line_opt # RUN GMSH command = gmsh_bin + ' ' + temp_file + dim + '-format ' + file_format + ' -o ' + temp_mesh_file + '' + options FreeCAD.Console.PrintMessage("Running: \"{}\"\n".format(command)) try: output = subprocess.check_output( [command, '-1'], shell=True, stderr=subprocess.STDOUT, ) for line in output.split('\n'): if "Error" in line: FreeCAD.Console.PrintError("{}\n".format(line)) elif "Warning" in line: FreeCAD.Console.PrintWarning("{}\n".format(line)) #FreeCAD.Console.PrintMessage("Output: \"{}\"\n".format(output)) if file_format in ('unv', 'med'): Fem.insert(temp_mesh_file, FreeCAD.ActiveDocument.Name) if file_format == 'stl': Mesh.insert(temp_mesh_file, FreeCAD.ActiveDocument.Name) if file_format == 'msh': out_mesh_file = os.path.join( os.path.dirname(os.path.abspath(__file__)), "geometry.msh") shutil.move(temp_mesh_file, out_mesh_file) FreeCAD.Console.PrintMessage( "Output file written to: {}\n".format(out_mesh_file)) if self.cb_mec_anal.isChecked(): FMesh = App.activeDocument().ActiveObject MechanicalAnalysis.makeMechanicalAnalysis('MechanicalAnalysis') FemGui.setActiveAnalysis(App.activeDocument().ActiveObject) App.activeDocument().ActiveObject.Member = App.activeDocument( ).ActiveObject.Member + [FMesh] if self.rb_1D.isChecked(): FMeshG = Gui.ActiveDocument.ActiveObject FMeshG.DisplayMode = "Elements & Nodes" except: FreeCAD.Console.PrintError( "Unexpected error in GMSHMesh macro: {} {}\n".format( sys.exc_info()[0], sys.exc_info()[1])) finally: try: del temp_file except: pass try: del temp_mesh_file except: pass
def proceed(self): temp_file = tempfile.mkstemp(suffix='.step')[1] selection = FreeCADGui.Selection.getSelection() if not selection: QtGui.QMessageBox.critical(None, "GMSHMesh macro", "An object has to be selected to run gmsh!") return # Export a part in step format ImportGui.export(selection, temp_file) selection_name = selection[0].Name # Mesh temporaly file file_format = self.cmb_format.currentText() temp_mesh_file = os.path.join(tempfile.tempdir, selection_name + '_Mesh.' + file_format) # OPTIONS GMSH: clmax = self.sb_max_element_size.text() clmin = self.sb_min_element_size.text() cmd_line_opt = self.le_cmd_line_opt.text() algo = self.cmb_algorithm.currentText() mesh_order = self.sb_mesh_order.text() if self.cb_optimized.isChecked(): cmd_optimize = ' -optimize' else: cmd_optimize = '' if self.rb_3D.isChecked(): dim = ' -3 ' if self.rb_2D.isChecked(): dim = ' -2 ' if self.rb_1D.isChecked(): dim = ' -1 ' if self.cb_max_elme_size.isChecked(): max_size = ' -clmax ' + clmax else: max_size = '' if self.cb_min_elme_size.isChecked(): min_size = ' -clmin ' + clmin else: min_size = '' if self.cb_mesh_order.isChecked(): order = ' -order ' + mesh_order else: order = '' options = ' -algo ' + algo + max_size + min_size + cmd_optimize + order + cmd_line_opt # RUN GMSH command = gmsh_bin + ' ' + temp_file + dim + '-format ' + file_format + ' -o ' + temp_mesh_file + '' + options FreeCAD.Console.PrintMessage("Running: \"{}\"\n".format(command)) try: output = subprocess.check_output([command, '-1'], shell=True, stderr=subprocess.STDOUT,) for line in output.split('\n'): if "Error" in line: FreeCAD.Console.PrintError("{}\n".format(line)) elif "Warning" in line: FreeCAD.Console.PrintWarning("{}\n".format(line)) #FreeCAD.Console.PrintMessage("Output: \"{}\"\n".format(output)) if file_format in ('unv', 'med'): Fem.insert(temp_mesh_file, FreeCAD.ActiveDocument.Name) if file_format == 'stl': Mesh.insert(temp_mesh_file, FreeCAD.ActiveDocument.Name) if file_format == 'msh': out_mesh_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "geometry.msh") shutil.move(temp_mesh_file, out_mesh_file) FreeCAD.Console.PrintMessage("Output file written to: {}\n".format(out_mesh_file)) if self.cb_mec_anal.isChecked(): FMesh = App.activeDocument().ActiveObject MechanicalAnalysis.makeMechanicalAnalysis('MechanicalAnalysis') FemGui.setActiveAnalysis(App.activeDocument().ActiveObject) App.activeDocument().ActiveObject.Member = App.activeDocument().ActiveObject.Member + [FMesh] if self.rb_1D.isChecked(): FMeshG = Gui.ActiveDocument.ActiveObject FMeshG.DisplayMode = "Elements & Nodes" except: FreeCAD.Console.PrintError("Unexpected error in GMSHMesh macro: {} {}\n".format(sys.exc_info()[0], sys.exc_info()[1])) finally: try: del temp_file except: pass try: del temp_mesh_file except: pass
def proceed(self): temp_file = tempfile.mkstemp(suffix='.step')[1] selection = FreeCADGui.Selection.getSelection() if not selection: QtGui.QMessageBox.critical( None, "SnappyHexMesh macro", "An object has to be selected to run snappy!") return # Export a part in step format ImportGui.export(selection, temp_file) selection_name = selection[0].Name # Mesh temporaly file file_format = self.cmb_format.currentText() temp_mesh_file = tempfile.tempdir + path_sep + selection_name + '_Mesh.' + file_format # OPTIONS snappy: clmax = self.sb_max_element_size.text() clmin = self.sb_min_element_size.text() cmd_line_opt = self.le_cmd_line_opt.text() algo = self.cmb_algorithm.currentText() mesh_order = self.sb_mesh_order.text() if self.cb_optimized.isChecked(): cmd_optimize = ' -optimize' else: cmd_optimize = '' if self.rb_3D.isChecked(): dim = ' -3 ' if self.rb_2D.isChecked(): dim = ' -2 ' if self.rb_1D.isChecked(): dim = ' -1 ' if self.cb_max_elme_size.isChecked(): max_size = ' -clmax ' + clmax else: max_size = '' if self.cb_min_elme_size.isChecked(): min_size = ' -clmin ' + clmin else: min_size = '' if self.cb_mesh_order.isChecked(): order = ' -order ' + mesh_order else: order = '' options = ' -algo ' + algo + max_size + min_size + cmd_optimize + order + cmd_line_opt # RUN Snappy command = snappy_bin + ' ' + temp_file + dim + '-format ' + file_format + ' -o ' + temp_mesh_file + '' + options FreeCAD.Console.PrintMessage("Running: {}".format(command)) try: if system() == "Linux": output = subprocess.check_output( [command, '-1'], shell=True, stderr=subprocess.STDOUT, ) elif system() == "Windows": output = subprocess.check_output( command, shell=True, stderr=subprocess.STDOUT, ) else: output = subprocess.check_output( [command, '-1'], shell=True, stderr=subprocess.STDOUT, ) FreeCAD.Console.PrintMessage(output) if file_format in ('unv', 'med'): Fem.insert(temp_mesh_file, FreeCAD.ActiveDocument.Name) if file_format == 'stl': Mesh.insert(temp_mesh_file, FreeCAD.ActiveDocument.Name) if self.cb_mec_anal.isChecked(): FMesh = App.activeDocument().ActiveObject FemAnalysis.makeFemAnalysis('MechanicalAnalysis') FemGui.setActiveAnalysis(App.activeDocument().ActiveObject) App.activeDocument().ActiveObject.Member = App.activeDocument( ).ActiveObject.Member + [FMesh] if self.rb_1D.isChecked(): FMeshG = Gui.ActiveDocument.ActiveObject FMeshG.DisplayMode = "Elements & Nodes" except: FreeCAD.Console.PrintError( "Unexpected error in snappyHexMesh macro: {}".format( sys.exc_info()[0])) finally: try: del temp_file except: pass try: del temp_mesh_file except: pass