2

我在 FreeCAD 中创建了一个宏(python 脚本),它生成悬臂梁、创建分析、选择材料并将约束应用于主体。

现在如何扩展脚本,以便生成网格(使用 GMSH)并在同一个脚本中使用 Calculix 运行分析?

请看下面我的代码:

import FreeCAD
import PartDesign
import PartDesignGui
import Sketcher
import FemGui
import ObjectsFem
import Fem

# Open new Document
exec(open('/usr/share/freecad/Mod/Start/StartPage/LoadNew.py').read())
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")

#
# generate cantilever beam
#

# Start new part design
App.activeDocument().addObject('PartDesign::Body','Body')
App.activeDocument().Body.newObject('Sketcher::SketchObject','Sketch')
App.activeDocument().Sketch.Support = (App.activeDocument().XY_Plane, [''])
App.activeDocument().Sketch.MapMode = 'FlatFace'
App.ActiveDocument.recompute()

# create cross section sketch
geoList = []
geoList.append(Part.LineSegment(App.Vector(-10,  9,0),App.Vector( 10,  9,0)))
geoList.append(Part.LineSegment(App.Vector( 10,  9,0),App.Vector( 10,-10,0)))
geoList.append(Part.LineSegment(App.Vector( 10,-10,0),App.Vector(-10,-10,0)))
geoList.append(Part.LineSegment(App.Vector(-10,-10,0),App.Vector(-10,  9,0)))
App.ActiveDocument.Sketch.addGeometry(geoList,False)
conList = []
conList.append(Sketcher.Constraint('Coincident',0,2,1,1))
conList.append(Sketcher.Constraint('Coincident',1,2,2,1))
conList.append(Sketcher.Constraint('Coincident',2,2,3,1))
conList.append(Sketcher.Constraint('Coincident',3,2,0,1))
conList.append(Sketcher.Constraint('Horizontal',0))
conList.append(Sketcher.Constraint('Horizontal',2))
conList.append(Sketcher.Constraint('Vertical',1))
conList.append(Sketcher.Constraint('Vertical',3))
App.ActiveDocument.Sketch.addConstraint(conList)

# Pad the cross section to create beam
App.getDocument('Unnamed').recompute()
App.activeDocument().Body.newObject("PartDesign::Pad","Pad")
App.activeDocument().Pad.Profile = App.activeDocument().Sketch
App.activeDocument().Pad.Length = 10.0
App.ActiveDocument.recompute()
App.ActiveDocument.recompute()
App.ActiveDocument.Pad.Length = 200.000000
App.ActiveDocument.Pad.Length2 = 100.000000
App.ActiveDocument.Pad.Type = 0
App.ActiveDocument.Pad.UpToFace = None
App.ActiveDocument.Pad.Reversed = 0
App.ActiveDocument.Pad.Midplane = 0
App.ActiveDocument.Pad.Offset = 0.000000
App.ActiveDocument.recompute()

#
# Start FEM process
#

# make analysis
ObjectsFem.makeAnalysis(FreeCAD.ActiveDocument, 'Analysis')
FemGui.setActiveAnalysis(FreeCAD.ActiveDocument.ActiveObject)
ObjectsFem.makeSolverCalculixCcxTools(FreeCAD.ActiveDocument)
FemGui.getActiveAnalysis().addObject(FreeCAD.ActiveDocument.ActiveObject)
# Select Material
FemGui.getActiveAnalysis().addObject(ObjectsFem.makeMaterialSolid(FreeCAD.ActiveDocument, 'SolidMaterial'))
FreeCADGui.ActiveDocument.setEdit(FreeCAD.ActiveDocument.ActiveObject.Name)
# Add Fixed Constraint
App.activeDocument().addObject("Fem::ConstraintFixed","FemConstraintFixed")
App.activeDocument().FemConstraintFixed.Scale = 1
App.activeDocument().Analysis.addObject(App.activeDocument().FemConstraintFixed)
for amesh in App.activeDocument().Objects:
    if "FemConstraintFixed" == amesh.Name:
        amesh.ViewObject.Visibility = True
    elif "Mesh" in amesh.TypeId:
        aparttoshow = amesh.Name.replace("_Mesh","")
        for apart in App.activeDocument().Objects:
            if aparttoshow == apart.Name:
                apart.ViewObject.Visibility = True
        amesh.ViewObject.Visibility = False

App.ActiveDocument.recompute()
App.ActiveDocument.FemConstraintFixed.Scale = 1
App.ActiveDocument.FemConstraintFixed.References = [(App.ActiveDocument.Pad,"Face5")]
App.ActiveDocument.recompute()

# Add Force Constraint
App.activeDocument().addObject("Fem::ConstraintForce","FemConstraintForce")
App.activeDocument().FemConstraintForce.Force = 1.0
App.activeDocument().FemConstraintForce.Reversed = False
App.activeDocument().FemConstraintForce.Scale = 1
App.activeDocument().Analysis.addObject(App.activeDocument().FemConstraintForce)
for amesh in App.activeDocument().Objects:
    if "FemConstraintForce" == amesh.Name:
        amesh.ViewObject.Visibility = True
    elif "Mesh" in amesh.TypeId:
        aparttoshow = amesh.Name.replace("_Mesh","")
        for apart in App.activeDocument().Objects:
            if aparttoshow == apart.Name:
                apart.ViewObject.Visibility = True
        amesh.ViewObject.Visibility = False

App.ActiveDocument.recompute()
App.ActiveDocument.FemConstraintForce.Force = 1
App.ActiveDocument.FemConstraintForce.Direction = None
App.ActiveDocument.FemConstraintForce.Reversed = False
App.ActiveDocument.FemConstraintForce.Scale = 1
App.ActiveDocument.FemConstraintForce.References = [(App.ActiveDocument.Pad,"Face6")]
App.ActiveDocument.recompute()

# Generate Mesh
ObjectsFem.makeMeshGmsh(FreeCAD.ActiveDocument, 'FEMMeshGmsh')
FreeCAD.ActiveDocument.ActiveObject.Part = FreeCAD.ActiveDocument.Pad
FemGui.getActiveAnalysis().addObject(FreeCAD.ActiveDocument.ActiveObject)
FreeCADGui.ActiveDocument.setEdit(FreeCAD.ActiveDocument.ActiveObject.Name)

当我运行我的代码时,我得到了应用约束和材料选择的完整设置分析。FreeCAD 向我展示了“Gmsh 的 FEM 网格”视图,我可以在其中选择参数并按下按钮来生成网格文件。我想在代码中按下那个按钮。

同样,我可以双击我的 Calculix Solver 并获得“机械分析”视图,它允许我选择分析类型、生成 .inp 文件并运行分析。如何在代码中执行这些步骤。

我愿意从我的 python 文件中调用其他用其他语言编写的脚本文件。但我需要它是可自动化的,因为我可以在 for 循环中运行各种分析。

4

0 回答 0