Skip to content

Commit ab1d24a

Browse files
authored
Maint : Refactoring (#216)
1 parent e184654 commit ab1d24a

11 files changed

+208
-312
lines changed

examples/basic/bolt_pretension.py

Lines changed: 61 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ def display_image(image_name):
4646
# Configure graphics for image export
4747
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4848

49-
ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)
50-
ExtAPI.Graphics.Camera.SetFit()
49+
Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)
50+
Graphics.Camera.SetFit()
5151
image_export_format = GraphicsImageExportFormat.PNG
5252
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
5353
settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution
5454
settings_720p.Background = GraphicsBackgroundType.White
5555
settings_720p.Width = 1280
5656
settings_720p.Height = 720
5757
settings_720p.CurrentGraphicsDisplay = False
58-
ExtAPI.Graphics.Camera.Rotate(180, CameraAxisType.ScreenY)
58+
Graphics.Camera.Rotate(180, CameraAxisType.ScreenY)
5959

6060
# %%
6161
# Download and import geometry
@@ -86,6 +86,7 @@ def display_image(image_name):
8686

8787
app.plot()
8888

89+
8990
# %%
9091
# Download and import material
9192
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -101,7 +102,7 @@ def display_image(image_name):
101102
# %%
102103
# Import materials
103104

104-
MAT = ExtAPI.DataModel.Project.Model.Materials
105+
MAT = Model.Materials
105106
MAT.Import(mat_Copper_file_path)
106107
MAT.Import(mat_Steel_file_path)
107108

@@ -127,68 +128,48 @@ def display_image(image_name):
127128
# %%
128129
# Store all main tree nodes as variables
129130

130-
MODEL = ExtAPI.DataModel.Project.Model
131-
GEOM = ExtAPI.DataModel.Project.Model.Geometry
132-
CONN_GRP = ExtAPI.DataModel.Project.Model.Connections
133-
CS_GRP = ExtAPI.DataModel.Project.Model.CoordinateSystems
134-
MSH = ExtAPI.DataModel.Project.Model.Mesh
135-
NS_GRP = ExtAPI.DataModel.Project.Model.NamedSelections
131+
MODEL = Model
132+
GEOM = Model.Geometry
133+
CONN_GRP = Model.Connections
134+
CS_GRP = Model.CoordinateSystems
135+
MSH = Model.Mesh
136+
NS_GRP = Model.NamedSelections
136137

137138
# %%
138139
# Store named selections
139140

140-
block3_block2_cont_NS = [
141-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "block3_block2_cont"
142-
][0]
143-
block3_block2_targ_NS = [
144-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "block3_block2_targ"
145-
][0]
146-
shank_block3_targ_NS = [
147-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank_block3_targ"
148-
][0]
149-
shank_block3_cont_NS = [
150-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank_block3_cont"
151-
][0]
152-
block1_washer_cont_NS = [
153-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "block1_washer_cont"
154-
][0]
155-
block1_washer_targ_NS = [
156-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "block1_washer_targ"
157-
][0]
158-
washer_bolt_cont_NS = [
159-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "washer_bolt_cont"
160-
][0]
161-
washer_bolt_targ_NS = [
162-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "washer_bolt_targ"
163-
][0]
164-
shank_bolt_targ_NS = [
165-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank_bolt_targ"
166-
][0]
167-
shank_bolt_cont_NS = [
168-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank_bolt_cont"
169-
][0]
170-
block2_block1_cont_NS = [
171-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "block2_block1_cont"
172-
][0]
173-
block2_block1_targ_NS = [
174-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "block2_block1_targ"
175-
][0]
176-
all_bodies = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "all_bodies"][0]
177-
bodies_5 = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "bodies_5"][0]
178-
shank = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank"][0]
179-
shank_face = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank_face"][0]
180-
shank_face2 = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank_face2"][
141+
block3_block2_cont_NS = [x for x in Tree.AllObjects if x.Name == "block3_block2_cont"][
142+
0
143+
]
144+
block3_block2_targ_NS = [x for x in Tree.AllObjects if x.Name == "block3_block2_targ"][
145+
0
146+
]
147+
shank_block3_targ_NS = [x for x in Tree.AllObjects if x.Name == "shank_block3_targ"][0]
148+
shank_block3_cont_NS = [x for x in Tree.AllObjects if x.Name == "shank_block3_cont"][0]
149+
block1_washer_cont_NS = [x for x in Tree.AllObjects if x.Name == "block1_washer_cont"][
150+
0
151+
]
152+
block1_washer_targ_NS = [x for x in Tree.AllObjects if x.Name == "block1_washer_targ"][
181153
0
182154
]
183-
bottom_surface = [
184-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "bottom_surface"
185-
][0]
186-
block2_surface = [
187-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "block2_surface"
188-
][0]
189-
shank_surface = [
190-
x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "shank_surface"
191-
][0]
155+
washer_bolt_cont_NS = [x for x in Tree.AllObjects if x.Name == "washer_bolt_cont"][0]
156+
washer_bolt_targ_NS = [x for x in Tree.AllObjects if x.Name == "washer_bolt_targ"][0]
157+
shank_bolt_targ_NS = [x for x in Tree.AllObjects if x.Name == "shank_bolt_targ"][0]
158+
shank_bolt_cont_NS = [x for x in Tree.AllObjects if x.Name == "shank_bolt_cont"][0]
159+
block2_block1_cont_NS = [x for x in Tree.AllObjects if x.Name == "block2_block1_cont"][
160+
0
161+
]
162+
block2_block1_targ_NS = [x for x in Tree.AllObjects if x.Name == "block2_block1_targ"][
163+
0
164+
]
165+
all_bodies = [x for x in Tree.AllObjects if x.Name == "all_bodies"][0]
166+
bodies_5 = [x for x in Tree.AllObjects if x.Name == "bodies_5"][0]
167+
shank = [x for x in Tree.AllObjects if x.Name == "shank"][0]
168+
shank_face = [x for x in Tree.AllObjects if x.Name == "shank_face"][0]
169+
shank_face2 = [x for x in Tree.AllObjects if x.Name == "shank_face2"][0]
170+
bottom_surface = [x for x in Tree.AllObjects if x.Name == "bottom_surface"][0]
171+
block2_surface = [x for x in Tree.AllObjects if x.Name == "block2_surface"][0]
172+
shank_surface = [x for x in Tree.AllObjects if x.Name == "shank_surface"][0]
192173

193174
# %%
194175
# Assign material to bodies
@@ -215,25 +196,22 @@ def display_image(image_name):
215196
# Define coordinate system
216197
# ~~~~~~~~~~~~~~~~~~~~~~~~~
217198

218-
coordinate_systems_17 = Model.CoordinateSystems
219-
coordinate_system_93 = coordinate_systems_17.AddCoordinateSystem()
220-
coordinate_system_93.OriginDefineBy = CoordinateSystemAlignmentType.Fixed
221-
coordinate_system_93.OriginX = Quantity(-195, "mm")
222-
coordinate_system_93.OriginY = Quantity(100, "mm")
223-
coordinate_system_93.OriginZ = Quantity(50, "mm")
224-
coordinate_system_93.PrimaryAxis = CoordinateSystemAxisType.PositiveZAxis
199+
coordinate_system = CS_GRP.AddCoordinateSystem()
200+
coordinate_system.OriginDefineBy = CoordinateSystemAlignmentType.Fixed
201+
coordinate_system.OriginX = Quantity(-195, "mm")
202+
coordinate_system.OriginY = Quantity(100, "mm")
203+
coordinate_system.OriginZ = Quantity(50, "mm")
204+
coordinate_system.PrimaryAxis = CoordinateSystemAxisType.PositiveZAxis
225205

226206
# %%
227207
# Define Contacts
228208
# ~~~~~~~~~~~~~~~
229209
# Change contact settings
230210

231-
connections = ExtAPI.DataModel.Project.Model.Connections
232-
233211
# %%
234212
# Delete existing contacts
235213

236-
for connection in connections.Children:
214+
for connection in CONN_GRP.Children:
237215
if connection.DataModelObjectCategory == DataModelObjectCategory.ConnectionGroup:
238216
connection.Delete()
239217

@@ -333,10 +311,10 @@ def display_image(image_name):
333311
Sweep_Method.TargetLocation = shank_face2
334312

335313
MSH.Activate()
336-
ExtAPI.Graphics.Camera.SetFit()
337-
ExtAPI.Graphics.ExportImage(
338-
os.path.join(cwd, "mesh.png"), image_export_format, settings_720p
339-
)
314+
MSH.GenerateMesh()
315+
316+
Graphics.Camera.SetFit()
317+
Graphics.ExportImage(os.path.join(cwd, "mesh.png"), image_export_format, settings_720p)
340318
display_image("mesh.png")
341319

342320
# %%
@@ -407,7 +385,7 @@ def display_image(image_name):
407385
Bolt_Pretension.SetDefineBy(4, BoltLoadDefineBy.Lock)
408386

409387
Tree.Activate([Bolt_Pretension])
410-
ExtAPI.Graphics.ExportImage(
388+
Graphics.ExportImage(
411389
os.path.join(cwd, "loads_and_boundaryconditions.png"),
412390
image_export_format,
413391
settings_720p,
@@ -458,8 +436,8 @@ def display_image(image_name):
458436
# Total deformation
459437

460438
Tree.Activate([Total_Deformation])
461-
ExtAPI.Graphics.Camera.SetFit()
462-
ExtAPI.Graphics.ExportImage(
439+
Graphics.Camera.SetFit()
440+
Graphics.ExportImage(
463441
os.path.join(cwd, "total_deformation.png"), image_export_format, settings_720p
464442
)
465443
display_image("total_deformation.png")
@@ -468,8 +446,8 @@ def display_image(image_name):
468446
# Equivalent stress on all bodies
469447

470448
Tree.Activate([Equivalent_stress_1])
471-
ExtAPI.Graphics.Camera.SetFit()
472-
ExtAPI.Graphics.ExportImage(
449+
Graphics.Camera.SetFit()
450+
Graphics.ExportImage(
473451
os.path.join(cwd, "equivalent_stress_total.png"), image_export_format, settings_720p
474452
)
475453
display_image("equivalent_stress_total.png")
@@ -478,8 +456,8 @@ def display_image(image_name):
478456
# Equivalent stress on shank
479457

480458
Tree.Activate([Equivalent_stress_2])
481-
ExtAPI.Graphics.Camera.SetFit()
482-
ExtAPI.Graphics.ExportImage(
459+
Graphics.Camera.SetFit()
460+
Graphics.ExportImage(
483461
os.path.join(cwd, "equivalent_stress_shank.png"), image_export_format, settings_720p
484462
)
485463
display_image("equivalent_stress_shank.png")
@@ -489,7 +467,7 @@ def display_image(image_name):
489467

490468
Post_Contact_Tool_status = Post_Contact_Tool.Children[0]
491469
Tree.Activate([Post_Contact_Tool_status])
492-
ExtAPI.Graphics.Camera.SetFit()
470+
Graphics.Camera.SetFit()
493471
animation_export_format = (
494472
Ansys.Mechanical.DataModel.Enums.GraphicsAnimationExportFormat.GIF
495473
)
@@ -521,7 +499,7 @@ def update(frame):
521499
# Project tree
522500
# ~~~~~~~~~~~~
523501

524-
app.print_tree(DataModel.Project)
502+
app.print_tree()
525503

526504
# %%
527505
# Cleanup

examples/basic/fracture_analysis_contact_debonding.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def display_image(image_name):
4545
# Configure graphics for image export
4646
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4747

48-
ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Front)
48+
Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Front)
4949
image_export_format = GraphicsImageExportFormat.PNG
5050
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
5151
settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution
@@ -99,7 +99,7 @@ def display_image(image_name):
9999
MAT_GRP.Import(mat1_path)
100100
MAT_GRP.Import(mat2_path)
101101

102-
PART = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == "Part 2"][0]
102+
PART = [x for x in Tree.AllObjects if x.Name == "Part 2"][0]
103103
MAT_BODY = [
104104
i
105105
for i in MAT_GRP.GetChildren[Ansys.ACT.Automation.Mechanical.Material](True)
@@ -117,7 +117,7 @@ def display_image(image_name):
117117
connections = MODEL.AddConnections()
118118
CONNECTIONS_GRP = connections.AddConnectionGroup()
119119
MODEL.Connections.CreateAutomaticConnections()
120-
CONNECTIONS_GRP = ExtAPI.DataModel.Project.Model.Connections
120+
CONNECTIONS_GRP = Model.Connections
121121
CONTACTS = [
122122
i
123123
for i in CONNECTIONS_GRP.GetChildren[
@@ -136,7 +136,7 @@ def display_image(image_name):
136136
# %%
137137
# Named selections
138138

139-
NAMED_SELECTIONS = ExtAPI.DataModel.Project.Model.NamedSelections
139+
NAMED_SELECTIONS = Model.NamedSelections
140140
NS_EDGE_HIGH = [
141141
i
142142
for i in NAMED_SELECTIONS.GetChildren[
@@ -199,7 +199,7 @@ def display_image(image_name):
199199
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200200

201201
MODEL.AddStaticStructuralAnalysis()
202-
STATIC_STRUCTURAL = ExtAPI.DataModel.AnalysisByName("Static Structural")
202+
STATIC_STRUCTURAL = DataModel.AnalysisByName("Static Structural")
203203
ANALYSIS_SETTINGS = STATIC_STRUCTURAL.AnalysisSettings
204204
SOLUTION = STATIC_STRUCTURAL.Solution
205205
MESH = Model.Mesh
@@ -252,10 +252,8 @@ def display_image(image_name):
252252
MESH.Activate()
253253
MESH.GenerateMesh()
254254

255-
ExtAPI.Graphics.Camera.SetFit()
256-
ExtAPI.Graphics.ExportImage(
257-
os.path.join(cwd, "mesh.png"), image_export_format, settings_720p
258-
)
255+
Graphics.Camera.SetFit()
256+
Graphics.ExportImage(os.path.join(cwd, "mesh.png"), image_export_format, settings_720p)
259257
display_image("mesh.png")
260258

261259
# %%
@@ -307,8 +305,8 @@ def display_image(image_name):
307305

308306
STATIC_STRUCTURAL.Activate()
309307

310-
ExtAPI.Graphics.Camera.SetFit()
311-
ExtAPI.Graphics.ExportImage(
308+
Graphics.Camera.SetFit()
309+
Graphics.ExportImage(
312310
os.path.join(cwd, "boundary_conditions.png"), image_export_format, settings_720p
313311
)
314312
display_image("boundary_conditions.png")
@@ -353,7 +351,7 @@ def display_image(image_name):
353351

354352
DIRECTIONAL_DEFORMATION.Activate()
355353

356-
ExtAPI.Graphics.ExportImage(
354+
Graphics.ExportImage(
357355
os.path.join(cwd, "directional_deformation.png"), image_export_format, settings_720p
358356
)
359357
display_image("directional_deformation.png")
@@ -363,8 +361,8 @@ def display_image(image_name):
363361

364362
FORCE_REACTION.Activate()
365363

366-
ExtAPI.Graphics.Camera.SetFit()
367-
ExtAPI.Graphics.ExportImage(
364+
Graphics.Camera.SetFit()
365+
Graphics.ExportImage(
368366
os.path.join(cwd, "force_reaction.png"), image_export_format, settings_720p
369367
)
370368
display_image("force_reaction.png")
@@ -421,7 +419,7 @@ def write_file_contents_to_console(path):
421419
# Project tree
422420
# ~~~~~~~~~~~~
423421

424-
app.print_tree(DataModel.Project)
422+
app.print_tree()
425423

426424
# %%
427425
# Cleanup

0 commit comments

Comments
 (0)