Create Mesh in command line

Need help, or want to share a macro? Post here!
totyped
Posts: 13
Joined: Mon Jan 20, 2020 2:00 pm

Create Mesh in command line

Postby totyped » Thu Jan 30, 2020 3:51 pm

I am currently trying to do a complete FEM analysis only from the command line. I am now struggling with mesh creation. I have used this solution: https://forum.freecadweb.org/viewtopic.php?t=29719,

Code: Select all

# mesh
# Source:  https://forum.freecadweb.org/viewtopic.php?t=29719
# make the femmeshgmsh object
ObjectsFem.makeMeshGmsh(FreeCAD.ActiveDocument, 'FEMMeshGmsh')
# femmeshgmsh is now the active object, set its Part property to the cube
FreeCAD.ActiveDocument.ActiveObject.Part = FreeCAD.ActiveDocument.Body
obj = FreeCAD.ActiveDocument.ActiveObject
# make the task dialog, but don't show it
from femguiobjects import _ViewProviderFemMeshGmsh
taskd = _ViewProviderFemMeshGmsh._TaskPanelFemMeshGmsh(obj)
# set the task dialog's object property to the femmeshgmsh object
taskd.obj = obj
# call the method that gets called when the user clicks Apply button
taskd.run_gmsh()

# Put mesh into analysis object
App.getDocument("Unnamed").getObject("Analysis").addObject(App.getDocument("Unnamed").getObject("FEMMeshGmsh"))
App.ActiveDocument.recompute()

which works very nicely in the GUI, but when I try it in the command line, I get
"Cannot load Gui module in console application."
This is because the _ViewProviderFemMeshGmsh class needs FemGui as can bee seen in the source code: https://github.com/FreeCAD/FreeCAD/blob ... eshGmsh.py

Do you see any workaround?
User avatar
bernd
Posts: 9841
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Create Mesh in command line

Postby bernd » Thu Jan 30, 2020 4:37 pm

find lots of exanples in this regard here https://github.com/FreeCAD/FreeCAD/tree ... emexamples but none for the mesh creation ...

will post one later on ...
User avatar
bernd
Posts: 9841
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Create Mesh in command line

Postby bernd » Thu Jan 30, 2020 4:39 pm

totyped
Posts: 13
Joined: Mon Jan 20, 2020 2:00 pm

Re: Create Mesh in command line

Postby totyped » Fri Jan 31, 2020 9:41 am

Thank you very much ! This helped me a lot ! :D
I ended up using the nice example at the bottom of https://github.com/FreeCAD/FreeCAD/blob ... shtools.py

Code: Select all

"""
# simple example how to use the class GmshTools
import Part, ObjectsFem
doc = App.ActiveDocument
box_obj = doc.addObject("Part::Box", "Box")
doc.recompute()
box_obj.ViewObject.Visibility = False
femmesh_obj = ObjectsFem.makeMeshGmsh(doc, box_obj.Name + "_Mesh")
femmesh_obj.Part = box_obj
doc.recompute()
from femmesh.gmshtools import GmshTools as gt
gmsh_mesh = gt(femmesh_obj)
error = gmsh_mesh.create_mesh()
print(error)
doc.recompute()
"""
User avatar
bernd
Posts: 9841
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Create Mesh in command line

Postby bernd » Fri Jan 31, 2020 4:09 pm

:shock: :o Damn ... I forgot about that one ...
User avatar
bernd
Posts: 9841
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Create Mesh in command line

Postby bernd » Fri Jan 31, 2020 4:10 pm

I should move it to some more obvisious place ...
User avatar
bernd
Posts: 9841
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Create Mesh in command line

Postby bernd » Sat Feb 01, 2020 5:43 am