随笔

##创建Geometry组件,并将文件导入到组件中。
def CreateGeometry(step):
global geoSystem
template1 = GetTemplate(TemplateName="Geometry")
geoSystem = template1.CreateSystem()
geometry1 = geoSystem.GetContainer(ComponentName="Geometry")
geometry1.SetFile(FilePath=step.Properties["definition/filename"].Value)
###向导配置文件中的字段
<propertygroup display="caption" name="definition" caption="Basic properties" >
<property name="filename" caption="Geometry file name" control="fileopen" />
</propertygroup>

###清空模型
def DeleteGeometry(step):
global geoSystem
geoSystem.Delete()

###创建Mechanical组件,求解器为ANSYS,并与Geometry组件相连
def CreateMechanical(step):
global dsSystem, geoSystem
template2 = GetTemplate(
TemplateName="Static Structural",
Solver="ANSYS")
geometryComponent1 = geoSystem.GetComponent(Name="Geometry")
dsSystem = template2.CreateSystem(
ComponentsToShare=[geometryComponent1],
Position="Right",
RelativeTo=geoSystem)
if step.Properties["name"].Value=="error":
raise UserErrorMessageException("Invalid system name. Please try again.")
dsSystem.DisplayText = step.Properties["name"].Value

 

model = ExtAPI.DataModel.Project.Model
msh = model.Mesh
mws = msh.Worksheet
nsels = model.NamedSelections
ns1 = nsels.Children[0]
mws.AddRow()
mws.SetNamedSelection(0,ns1)
mws.SetActiveState(0,True)
mws.GenerateMesh()


#网格划分方法
mesh = ExtAPI.DataModel.Project.Model.Mesh
mesh_method = mesh.AddAutomaticMethod()
my_selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.SurfaceSpecific)
my_selection.Ids = [2]
mesh_method.Location = my_selection
mesh_method.Method = MethodType.AllTriAllTet
mesh_method.Algorithm = MeshMethodAlgorithm.PatchIndependent
mesh_method.MaximumElementSize = Quantity("0.05 [m]")
mesh_method.FeatureAngle = Quantity("12.000000000000002 [degree]")
mesh_method.MeshBasedDefeaturing = True
mesh_method.DefeaturingTolerance = Quantity("0.0001 [m]")
mesh_method.MinimumSizeLimit = Quantity("0.001 [m]")
mesh_method.NumberOfCellsAcrossGap = 1
mesh_method.CurvatureNormalAngle = Quantity("36 [degree]")
mesh_method.SmoothTransition = True
mesh_method.TetraGrowthRate = 1

#创建无摩擦接触链接
connection = ExtAPI.DataModel.Project.Model.Connections
contact_region = connection.Children[0].Children[0]
contact_region.ContactType = ContactType.Frictionless
beam = connection.AddBeam()
beam.Radius = Quantity("0.005 [m]")
reference_scoping = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
reference_scoping.Ids = [110]
beam.ReferenceLocation = reference_scoping
beam.ReferenceBehavior = LoadBehavior.Deformable
beam.ReferencePinballRegion = Quantity("0.001 [m]")
mobile_scoping = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
mobile_scoping.Ids = [38]
beam.MobileLocation = mobile_scoping
beam.MobileZCoordinate = Quantity("6.5E-03 [m]")
beam.MobilePinballRegion = Quantity("0.001 [m]")

 


#结果显示
solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution
total_deformation = solution.AddTotalDeformation()
analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
analysis.Solve(True)
minimum_deformation = total_deformation.Minimum
maximum_deformation = total_deformation.Maximum


####获取选择集合Ids
def elementcounter(analysis):
geometry = analysis.GeoData
mesh = analysis.MeshData
selectedids = ExtAPI.SelectionMgr.CurrentSelection.Ids
if selectedids.Count == 0:
MessageBox.Show("Nothing Selected!")
else:
for selectedid in selectedids:
entity = geometry.GeoEntityById(selectedid)
meshregion = mesh.MeshRegionById(selectedid)
try:
numelem = meshregion.ElementCount
MessageBox.Show("Entity of type: "+entity.Type.ToString()+
" contains "+numelem.ToString()+
" elements.")
except:
MessageBox.Show("The mesh is empty!")
return
return

####
#####施加压强
load = ExtAPI.DataModel.Project.Model.Analyses[0]
load1 = load.AddPressure()
load1.Magnitude.Output.Formula = '10*time'
load1.Magnitude.Output.DiscreteValues = [Quantity('6 [Pa]')]

####施加力
load = ExtAPI.DataModel.Project.Model.Analyses[0]
load1 = load.AddForce()
force_scoping = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
force_scoping.Ids = [219]
force.Location = force_scoping
force.Magnitude.Output.DiscreteValues=[Quantity('11.3 [N]'), Quantity('12.85 [N]')]

#施加螺栓预紧力
load = ExtAPI.DataModel.Project.Model.Analyses[0]
bolt = analysis1.AddBoltPretension()
bolt.SetDefineBy(1, BoltLoadDefineBy.Load) # Change definition for step #1.
bolt.Preload.Output.SetDiscreteValue(0, Quantity("15 [N]")) # Change preload value for step #1.

##施加固支
load = ExtAPI.DataModel.Project.Model.Analyses[0]
support = load.AddFixedSupport()
support.Location = sel

##特征选择
sel = step.Properties["FixedSupport/Location"].Value
support.Location = sel

##获取任务参数及赋值
task = ExtAPI.DataModel.Tasks[0]
myParameters = task.Parameters
myParam = myParameters[0]
myParamValue = myParam.Value

###获取图像
model = ExtAPI.DataModel.Project.Model
view_manager = ExtAPI.Graphics.ModelViewManager
views_count = view_manager.NumberOfViews
view_manager.CaptureModelView("Right View", "JPG", "D:\My_Projects\Views")
#view_manager.CaptureObjectImage(Ansys.ACT.Automation.Mechanical.DataModelObject "Plate", "BMP")

###创建几何组件
template1 = GetTemplate(TemplateName="Geometry")
system1 = template1.CreateSystem()

####创建静强度分析组件
template1 = GetTemplate(TemplateName="Static Structural",Solver="ANSYS")
system1 = GetSystem(Name="Geom")
system2 = template1.CreateSystem(Position="Right",RelativeTo=system1)

####关联几何组件及静强度组件几何数据流
system1 = GetSystem(Name="SYS")
geometryComponent1 = system1.GetComponent(Name="Geometry")
system2 = GetSystem(Name="Geom")
geometryComponent2 = system2.GetComponent(Name="Geometry")
geometryComponent1.ReplaceWithShare(TargetSystem=system1,ComponentToShare=geometryComponent2,SourceSystem=system2)

###打开几何组件几何模块DM
system1 = GetSystem(Name="Geom")
geometry1 = system1.GetContainer(ComponentName="Geometry")
geometry1.SetFile(FilePath="D:/test1.stp")
geometry1.Edit()

####打开静强度分析组件model模块
system2 = GetSystem(Name="SYS")
modelComponent1 = system2.GetComponent(Name="Model")
model1 = system2.GetContainer(ComponentName="Model")
model1.Edit()

posted on 2018-12-20 13:43  wj5891  阅读(307)  评论(0编辑  收藏  举报

导航