From 868d407d706f50a9d64ee896175d9be1f95cc6b0 Mon Sep 17 00:00:00 2001 From: rs-bh-n Date: Tue, 27 Jun 2023 23:10:45 +0530 Subject: [PATCH 1/7] first draft - to run 3 --- examples/basic/example_09_tracemapping.py | 283 ++++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 examples/basic/example_09_tracemapping.py diff --git a/examples/basic/example_09_tracemapping.py b/examples/basic/example_09_tracemapping.py new file mode 100644 index 00000000..6ec22100 --- /dev/null +++ b/examples/basic/example_09_tracemapping.py @@ -0,0 +1,283 @@ +""".. _ref_example_06: + +Trace Mapping Example Demonstration +--------------------------------------------------------------------------------- + +Using supplied files, this example shows how to import tracemap data into a +static structural analysis of a new Mechanical session and execute a sequence of +Python scripting commands that mesh the model and export an image. + +""" + +############################################################################### +# Import necessary libraries +# ~~~~~~~~~~~~~~~~~~~~~~~~~~ +import os + +from ansys.mechanical.core import launch_mechanical +from ansys.mechanical.core.examples import download_file +from matplotlib import image as mpimg +from matplotlib import pyplot as plt + +############################################################################### +# Launch Mechanical +# ~~~~~~~~~~~~~~~~~ +# Launch a new Mechanical session in batch, setting ``cleanup_on_exit`` to +# ``False``. To close this Mechanical session when finished, this example +# must call the ``mechanical.exit()`` method. + +mechanical = launch_mechanical(batch=True, cleanup_on_exit=False) +print(mechanical) + +############################################################################### +# Initialize variable for workflow +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Set the ``part_file_path`` variable on the server for later use. +# Make this variable compatible for Windows, Linux, and Docker containers. + +project_directory = mechanical.project_directory +print(f"project directory = {project_directory}") + +############################################################################### +# Download four required files : +# geometry file , def file , copper alloy material file, fr4 material file +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Download the required file. Print the file path for the geometry file. + +all_input_files = { + "geometry file name": "example_09_pcb.agdb", + "def file": "example_09_edb.def", + "copper alloy material file": "example_09_mat_copper_alloy.xml", + "fr4 material file": "example_09_mat_fr4.xml", +} + +for file_type, file_name in all_input_files.items(): + geometry_path = download_file(file_name, "pymechanical", "00_basic") + print(f"Downloaded the {file_type} to: {geometry_path}") + # Upload the file to the project directory. + mechanical.upload( + file_name=geometry_path, file_location_destination=project_directory + ) + # Build the path relative to project directory. + base_name = os.path.basename(geometry_path) + combined_path = os.path.join(project_directory, base_name) + part_file_path = combined_path.replace("\\", "\\\\") + parameter = file_type.replace(" ", "_") + mechanical.run_python_script(f"{parameter}='{part_file_path}'") + # ----------------------- Verify the path------------------- + result = mechanical.run_python_script("part_file_path") + print(f"path of {file_type} on server: {result}") + +mechdat_final = "remote.mechdat" +mechanical.run_python_script(f"final_mechdat_filename='{mechdat_final}'") +png_image_name = "myplot.png" +mechanical.run_python_script(f"image_name='{png_image_name}'") + +output = mechanical.run_python_script( + """ +import os + + + +mechdat_file_path = os.path.join(os.getcwd(), final_mechdat_filename).replace("\\", "//") + +# Imports a geometry file into the active model. +geometry_import = Model.GeometryImportGroup.AddGeometryImport() +geometry_import_format = ( + Ansys.Mechanical.DataModel.Enums.GeometryImportPreference.Format.Automatic +) +geometry_import_preferences = Ansys.ACT.Mechanical.Utilities.GeometryImportPreferences() +geometry_import_preferences.ProcessNamedSelections = True +geometry_import_preferences.NamedSelectionKey = "NS" +geometry_import.Import( + geometry_file_path, geometry_import_format, geometry_import_preferences +) + + +print("geometry import : Done ") + +# Static Structural Analysis +analysis = Model.AddStaticStructuralAnalysis() +print(analysis) + +ExtAPI.DataModel.Project.UnitSystem = UserUnitSystemType.StandardNMM + + +materials = ExtAPI.DataModel.Project.Model.Materials +materials.Import(copper_alloy_material_file) +materials.Import(fr4_material_file) + +ExtAPI.DataModel.Project.Save(mechdat_file_path) + +board_bodyids = [] +component_bodyids = [] +geo = ExtAPI.DataModel.GeoData +mesh = ExtAPI.DataModel.Project.Model.Mesh +for asm in geo.Assemblies: + for part in asm.Parts: + for body in part.Bodies: + if body.Name[:9] != "Component": + board_bodyids.append(body.Id) + else: + component_bodyids.append(body.Id) + + +parts = ExtAPI.DataModel.Project.Model.Geometry.Children # list of parts +for part in parts: + for body in part.Children: + body.Material = "Copper Alloy" if body.Name[:9] == "Component" else "FR-4" + + +def create_named_selection_from_id_list(ns_name, list_of_body_ids): + selection_manager = ExtAPI.SelectionManager + selection = ExtAPI.SelectionManager.CreateSelectionInfo( + SelectionTypeEnum.GeometryEntities + ) + selection.Ids = list_of_body_ids + selection_manager.NewSelection(selection) + + model = ExtAPI.DataModel.Project.Model + named_sel = model.AddNamedSelection() + named_sel.Name = ns_name + named_sel.Location = selection + selection_manager.ClearSelection() + + +create_named_selection_from_id_list("board_layers", board_bodyids) +create_named_selection_from_id_list("components", component_bodyids) + +selection_manager = ExtAPI.SelectionManager +selection = ExtAPI.SelectionManager.CreateSelectionInfo( + SelectionTypeEnum.GeometryEntities +) +selection.Ids = board_bodyids +selection_manager.NewSelection(selection) + +mesh = ExtAPI.DataModel.Project.Model.Mesh + +mesh_method = mesh.AddAutomaticMethod() +mesh_method.Location = selection +mesh_method.Method = MethodType.Sweep +mesh_method.ElementOrder = ElementOrder.Linear +mesh_method.SweepNumberDivisions = 1 + +mesh_sizing = mesh.AddSizing() +mesh_sizing.ElementSize = Quantity("0.25 [mm]") +mesh.GenerateMesh() + + +external_data_files = Ansys.Mechanical.ExternalData.ExternalDataFileCollection() +external_data_files.SaveFilesWithProject = True +external_data_file = Ansys.Mechanical.ExternalData.ExternalDataFile() +external_data_files.Add(external_data_file) # Single File +external_data_file.Identifier = "edb" +external_data_file.Description = "" +external_data_file.IsMainFile = False +external_data_file.FilePath = def_fileName +external_data_file.ImportSettings = ( + Ansys.Mechanical.ExternalData.ImportSettingsFactory.GetSettingsForFormat( + Ansys.Mechanical.DataModel.MechanicalEnums.ExternalData.ImportFormat.ECAD + ) +) +import_settings = external_data_file.ImportSettings +import_settings.UseDummyNetData = False +imported_trace_group = Model.Materials.AddImportedTraceExternalData() +imported_trace_group.ImportExternalDataFiles(external_data_files) + +allImpTraces = ExtAPI.DataModel.GetObjectsByType( + Ansys.Mechanical.DataModel.Enums.DataModelObjectCategory.ImportedTrace +) + +imp_trace = [ + x for x in allImpTraces if x.Parent.ObjectId == imported_trace_group.ObjectId +][0] +imp_trace.Activate() +imp_trace.InternalObject.GeometryDefineBy = 1 + +NSall = ExtAPI.DataModel.Project.Model.NamedSelections.GetChildren[ + Ansys.ACT.Automation.Mechanical.NamedSelection +](True) +ns_object = [i for i in NSall if i.Name == "board_layers"][0] +imp_trace.Location = ns_object +imp_trace.PropertyByName("PROPID_ExternalData").InternalValue = 1 + + +layers = imp_trace.Layers +num_layers = layers.Count +for layer in layers: + layer["Trace Material"] = "Copper Alloy" +vias = imp_trace.Vias +num_vias = vias.Count +for via in vias: + via["Plating Material"] = "Copper Alloy" +imp_trace.Import() + + +# Image to file settings +set2d = Ansys.Mechanical.Graphics.GraphicsImageExportSettings() +set2d.CurrentGraphicsDisplay = False +mechdir = analysis.Children[0].SolverFilesDirectory +png_file_path = os.path.join(mechdir, image_name) +Graphics.ExportImage(png_file_path, GraphicsImageExportFormat.PNG, set2d) +ExtAPI.DataModel.Project.Save(mechdat_file_path) + +""" +) +print(output) + +################################################################################### +# Initialize the variable needed for the image directory +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Set the ``image_dir`` for later use. +# Make the variable compatible for Windows, Linux, and Docker containers. + +# image_directory_modified = project_directory.replace("\\", "\\\\") +mechanical.run_python_script(f"image_dir=ExtAPI.DataModel.AnalysisList[0].WorkingDir") + + +# Verify the path for image directory. +result_image_dir_server = mechanical.run_python_script(f"image_dir") +print(f"Images are stored on the server at: {result_image_dir_server}") + +############################################################################### +# Download the image and plot +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Download one image file from the server to the current working directory and plot +# using matplotlib. + + +def get_image_path(image_name): + return os.path.join(result_image_dir_server, image_name) + + +def display_image(path): + print(f"Printing {path} using matplotlib") + image1 = mpimg.imread(path) + plt.figure(figsize=(15, 15)) + plt.axis("off") + plt.imshow(image1) + plt.show() + + +image_name = "contact_status.png" +image_path_server = get_image_path(image_name) + +if image_path_server != "": + current_working_directory = os.getcwd() + + local_file_path_list = mechanical.download( + image_path_server, target_dir=current_working_directory + ) + image_local_path = local_file_path_list[0] + print(f"Local image path : {image_local_path}") + + display_image(image_local_path) + +############################################################################### + + +# Close Mechanical +# ~~~~~~~~~~~~~~~~ +# Close the Mechanical instance. + +mechanical.exit() From 9f230be9c10fd007bfc686d13dba9b96896d1256 Mon Sep 17 00:00:00 2001 From: rs-bh-n Date: Wed, 28 Jun 2023 13:35:36 +0530 Subject: [PATCH 2/7] tracemap example 9 added --- examples/basic/example_09_tracemapping.py | 35 +++++++++++++---------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/examples/basic/example_09_tracemapping.py b/examples/basic/example_09_tracemapping.py index 6ec22100..5044799f 100644 --- a/examples/basic/example_09_tracemapping.py +++ b/examples/basic/example_09_tracemapping.py @@ -51,25 +51,31 @@ "fr4 material file": "example_09_mat_fr4.xml", } +all_input_files = { + "geometry_file_name": "example_01_geometry.agdb", + "def_file": "example_04_bicycle_crank_231.mechpz", + "copper_alloy_material_file": "example_06_Mat_Copper.xml", + "fr4_material_file": "example_06_Mat_Steel.xml", +} + + for file_type, file_name in all_input_files.items(): - geometry_path = download_file(file_name, "pymechanical", "00_basic") - print(f"Downloaded the {file_type} to: {geometry_path}") + file_path = download_file(file_name, "pymechanical", "00_basic") + print(f"Downloaded the {file_type} to: {file_path}") # Upload the file to the project directory. - mechanical.upload( - file_name=geometry_path, file_location_destination=project_directory - ) + mechanical.upload(file_name=file_path, file_location_destination=project_directory) # Build the path relative to project directory. - base_name = os.path.basename(geometry_path) + base_name = os.path.basename(file_path) combined_path = os.path.join(project_directory, base_name) part_file_path = combined_path.replace("\\", "\\\\") - parameter = file_type.replace(" ", "_") - mechanical.run_python_script(f"{parameter}='{part_file_path}'") + mechanical.run_python_script(f"{file_type} = '{part_file_path}'") # ----------------------- Verify the path------------------- - result = mechanical.run_python_script("part_file_path") + result = mechanical.run_python_script("'{file_type}'") print(f"path of {file_type} on server: {result}") mechdat_final = "remote.mechdat" mechanical.run_python_script(f"final_mechdat_filename='{mechdat_final}'") + png_image_name = "myplot.png" mechanical.run_python_script(f"image_name='{png_image_name}'") @@ -77,9 +83,8 @@ """ import os - - -mechdat_file_path = os.path.join(os.getcwd(), final_mechdat_filename).replace("\\", "//") +# mechdat_file_path = os.path.join(os.getcwd(), final_mechdat_filename).replace("\\", "//") +mechdat_file_path = os.path.join(os.getcwd(), final_mechdat_filename) # Imports a geometry file into the active model. geometry_import = Model.GeometryImportGroup.AddGeometryImport() @@ -90,7 +95,7 @@ geometry_import_preferences.ProcessNamedSelections = True geometry_import_preferences.NamedSelectionKey = "NS" geometry_import.Import( - geometry_file_path, geometry_import_format, geometry_import_preferences + geometry_file_name, geometry_import_format, geometry_import_preferences ) @@ -107,7 +112,7 @@ materials.Import(copper_alloy_material_file) materials.Import(fr4_material_file) -ExtAPI.DataModel.Project.Save(mechdat_file_path) +# ExtAPI.DataModel.Project.Save(mechdat_file_path) board_bodyids = [] component_bodyids = [] @@ -219,7 +224,7 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): mechdir = analysis.Children[0].SolverFilesDirectory png_file_path = os.path.join(mechdir, image_name) Graphics.ExportImage(png_file_path, GraphicsImageExportFormat.PNG, set2d) -ExtAPI.DataModel.Project.Save(mechdat_file_path) +# ExtAPI.DataModel.Project.Save(mechdat_file_path) """ ) From a229cd4fadcd2df7d75030329056221de5894319 Mon Sep 17 00:00:00 2001 From: rs-bh-n Date: Thu, 29 Jun 2023 23:39:21 +0530 Subject: [PATCH 3/7] ex9 ready --- examples/basic/example_09_tracemapping.py | 65 ++++++++++------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/examples/basic/example_09_tracemapping.py b/examples/basic/example_09_tracemapping.py index 5044799f..7d25dc4d 100644 --- a/examples/basic/example_09_tracemapping.py +++ b/examples/basic/example_09_tracemapping.py @@ -1,4 +1,4 @@ -""".. _ref_example_06: +""".. _ref_example_09: Trace Mapping Example Demonstration --------------------------------------------------------------------------------- @@ -21,7 +21,7 @@ ############################################################################### # Launch Mechanical -# ~~~~~~~~~~~~~~~~~ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~ # Launch a new Mechanical session in batch, setting ``cleanup_on_exit`` to # ``False``. To close this Mechanical session when finished, this example # must call the ``mechanical.exit()`` method. @@ -29,52 +29,41 @@ mechanical = launch_mechanical(batch=True, cleanup_on_exit=False) print(mechanical) -############################################################################### -# Initialize variable for workflow -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# Set the ``part_file_path`` variable on the server for later use. -# Make this variable compatible for Windows, Linux, and Docker containers. - -project_directory = mechanical.project_directory -print(f"project directory = {project_directory}") ############################################################################### -# Download four required files : -# geometry file , def file , copper alloy material file, fr4 material file -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# Download the required file. Print the file path for the geometry file. +# Download the required files : # geometry file , def file , copper alloy +# material file, fr4 material file +# Print the file paths to verify. +# ~~~~~~~~~~~~~~~~~~~~~~~~~~ -all_input_files = { - "geometry file name": "example_09_pcb.agdb", - "def file": "example_09_edb.def", - "copper alloy material file": "example_09_mat_copper_alloy.xml", - "fr4 material file": "example_09_mat_fr4.xml", -} all_input_files = { - "geometry_file_name": "example_01_geometry.agdb", - "def_file": "example_04_bicycle_crank_231.mechpz", - "copper_alloy_material_file": "example_06_Mat_Copper.xml", - "fr4_material_file": "example_06_Mat_Steel.xml", + "geometry_file_name": "example_09_pcb.agdb", + "def_file": "example_09_edb.def", + "copper_alloy_material_file": "example_09_mat_copper_alloy.xml", + "fr4_material_file": "example_09_mat_fr4.xml", } +project_directory = mechanical.project_directory +print(f"project directory = {project_directory}") + for file_type, file_name in all_input_files.items(): file_path = download_file(file_name, "pymechanical", "00_basic") + print(f"Downloaded the {file_type} to: {file_path}") + # Upload the file to the project directory. mechanical.upload(file_name=file_path, file_location_destination=project_directory) + # Build the path relative to project directory. base_name = os.path.basename(file_path) combined_path = os.path.join(project_directory, base_name) part_file_path = combined_path.replace("\\", "\\\\") mechanical.run_python_script(f"{file_type} = '{part_file_path}'") - # ----------------------- Verify the path------------------- result = mechanical.run_python_script("'{file_type}'") print(f"path of {file_type} on server: {result}") -mechdat_final = "remote.mechdat" -mechanical.run_python_script(f"final_mechdat_filename='{mechdat_final}'") png_image_name = "myplot.png" mechanical.run_python_script(f"image_name='{png_image_name}'") @@ -83,8 +72,6 @@ """ import os -# mechdat_file_path = os.path.join(os.getcwd(), final_mechdat_filename).replace("\\", "//") -mechdat_file_path = os.path.join(os.getcwd(), final_mechdat_filename) # Imports a geometry file into the active model. geometry_import = Model.GeometryImportGroup.AddGeometryImport() @@ -101,19 +88,19 @@ print("geometry import : Done ") -# Static Structural Analysis +# Insert a Static Structural Analysis analysis = Model.AddStaticStructuralAnalysis() print(analysis) ExtAPI.DataModel.Project.UnitSystem = UserUnitSystemType.StandardNMM - +# Import Materials materials = ExtAPI.DataModel.Project.Model.Materials materials.Import(copper_alloy_material_file) materials.Import(fr4_material_file) -# ExtAPI.DataModel.Project.Save(mechdat_file_path) +# create lists of body ids to create named selections later board_bodyids = [] component_bodyids = [] geo = ExtAPI.DataModel.GeoData @@ -126,14 +113,16 @@ else: component_bodyids.append(body.Id) - +# Assign Materials based on Body Names parts = ExtAPI.DataModel.Project.Model.Geometry.Children # list of parts for part in parts: for body in part.Children: body.Material = "Copper Alloy" if body.Name[:9] == "Component" else "FR-4" +# Function to create named selection from list of body ids def create_named_selection_from_id_list(ns_name, list_of_body_ids): + selection_manager = ExtAPI.SelectionManager selection = ExtAPI.SelectionManager.CreateSelectionInfo( SelectionTypeEnum.GeometryEntities @@ -151,6 +140,7 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): create_named_selection_from_id_list("board_layers", board_bodyids) create_named_selection_from_id_list("components", component_bodyids) +#make a selection to be used with mesh methods selection_manager = ExtAPI.SelectionManager selection = ExtAPI.SelectionManager.CreateSelectionInfo( SelectionTypeEnum.GeometryEntities @@ -171,6 +161,7 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): mesh.GenerateMesh() +# Defining External Data Object for Importing Trace external_data_files = Ansys.Mechanical.ExternalData.ExternalDataFileCollection() external_data_files.SaveFilesWithProject = True external_data_file = Ansys.Mechanical.ExternalData.ExternalDataFile() @@ -178,7 +169,7 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): external_data_file.Identifier = "edb" external_data_file.Description = "" external_data_file.IsMainFile = False -external_data_file.FilePath = def_fileName +external_data_file.FilePath = def_file external_data_file.ImportSettings = ( Ansys.Mechanical.ExternalData.ImportSettingsFactory.GetSettingsForFormat( Ansys.Mechanical.DataModel.MechanicalEnums.ExternalData.ImportFormat.ECAD @@ -218,7 +209,7 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): imp_trace.Import() -# Image to file settings +# Exporting trace map snapshot to a png file set2d = Ansys.Mechanical.Graphics.GraphicsImageExportSettings() set2d.CurrentGraphicsDisplay = False mechdir = analysis.Children[0].SolverFilesDirectory @@ -236,7 +227,6 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): # Set the ``image_dir`` for later use. # Make the variable compatible for Windows, Linux, and Docker containers. -# image_directory_modified = project_directory.replace("\\", "\\\\") mechanical.run_python_script(f"image_dir=ExtAPI.DataModel.AnalysisList[0].WorkingDir") @@ -264,8 +254,7 @@ def display_image(path): plt.show() -image_name = "contact_status.png" -image_path_server = get_image_path(image_name) +image_path_server = get_image_path(png_image_name) if image_path_server != "": current_working_directory = os.getcwd() From 1a0c22e05263f343f1fd98a09abcbde3c4371694 Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Sat, 20 Jan 2024 02:06:30 -0600 Subject: [PATCH 4/7] remove internal object calling --- examples/basic/example_09_tracemapping.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/basic/example_09_tracemapping.py b/examples/basic/example_09_tracemapping.py index 7d25dc4d..814294e7 100644 --- a/examples/basic/example_09_tracemapping.py +++ b/examples/basic/example_09_tracemapping.py @@ -188,7 +188,7 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): x for x in allImpTraces if x.Parent.ObjectId == imported_trace_group.ObjectId ][0] imp_trace.Activate() -imp_trace.InternalObject.GeometryDefineBy = 1 +# imp_trace.InternalObject.GeometryDefineBy = 1 NSall = ExtAPI.DataModel.Project.Model.NamedSelections.GetChildren[ Ansys.ACT.Automation.Mechanical.NamedSelection @@ -210,9 +210,10 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): # Exporting trace map snapshot to a png file +Graphics.Camera.SetFit() set2d = Ansys.Mechanical.Graphics.GraphicsImageExportSettings() set2d.CurrentGraphicsDisplay = False -mechdir = analysis.Children[0].SolverFilesDirectory +mechdir = ExtAPI.DataModel.AnalysisList[0].WorkingDir png_file_path = os.path.join(mechdir, image_name) Graphics.ExportImage(png_file_path, GraphicsImageExportFormat.PNG, set2d) # ExtAPI.DataModel.Project.Save(mechdat_file_path) From ae9ea512c9e5501a29851fb8eb62116266ede029 Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Mon, 22 Jan 2024 11:40:17 -0600 Subject: [PATCH 5/7] doc cleanup --- examples/basic/example_09_tracemapping.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/basic/example_09_tracemapping.py b/examples/basic/example_09_tracemapping.py index 814294e7..fd6ab209 100644 --- a/examples/basic/example_09_tracemapping.py +++ b/examples/basic/example_09_tracemapping.py @@ -74,6 +74,7 @@ # Imports a geometry file into the active model. + geometry_import = Model.GeometryImportGroup.AddGeometryImport() geometry_import_format = ( Ansys.Mechanical.DataModel.Enums.GeometryImportPreference.Format.Automatic @@ -89,18 +90,21 @@ print("geometry import : Done ") # Insert a Static Structural Analysis + analysis = Model.AddStaticStructuralAnalysis() print(analysis) ExtAPI.DataModel.Project.UnitSystem = UserUnitSystemType.StandardNMM # Import Materials + materials = ExtAPI.DataModel.Project.Model.Materials materials.Import(copper_alloy_material_file) materials.Import(fr4_material_file) # create lists of body ids to create named selections later + board_bodyids = [] component_bodyids = [] geo = ExtAPI.DataModel.GeoData @@ -114,6 +118,7 @@ component_bodyids.append(body.Id) # Assign Materials based on Body Names + parts = ExtAPI.DataModel.Project.Model.Geometry.Children # list of parts for part in parts: for body in part.Children: @@ -121,6 +126,7 @@ # Function to create named selection from list of body ids + def create_named_selection_from_id_list(ns_name, list_of_body_ids): selection_manager = ExtAPI.SelectionManager @@ -162,6 +168,7 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): # Defining External Data Object for Importing Trace + external_data_files = Ansys.Mechanical.ExternalData.ExternalDataFileCollection() external_data_files.SaveFilesWithProject = True external_data_file = Ansys.Mechanical.ExternalData.ExternalDataFile() @@ -210,13 +217,13 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): # Exporting trace map snapshot to a png file + Graphics.Camera.SetFit() set2d = Ansys.Mechanical.Graphics.GraphicsImageExportSettings() set2d.CurrentGraphicsDisplay = False mechdir = ExtAPI.DataModel.AnalysisList[0].WorkingDir png_file_path = os.path.join(mechdir, image_name) Graphics.ExportImage(png_file_path, GraphicsImageExportFormat.PNG, set2d) -# ExtAPI.DataModel.Project.Save(mechdat_file_path) """ ) @@ -236,8 +243,8 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): print(f"Images are stored on the server at: {result_image_dir_server}") ############################################################################### -# Download the image and plot -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Download the image and animation and plot +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Download one image file from the server to the current working directory and plot # using matplotlib. @@ -269,8 +276,6 @@ def display_image(path): display_image(image_local_path) ############################################################################### - - # Close Mechanical # ~~~~~~~~~~~~~~~~ # Close the Mechanical instance. From 9ba53db7ed9609f7d77bff1c1601f08b022f6f4c Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Tue, 23 Jan 2024 21:49:51 -0600 Subject: [PATCH 6/7] update section title --- examples/basic/example_09_tracemapping.py | 46 +++++++++++++---------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/examples/basic/example_09_tracemapping.py b/examples/basic/example_09_tracemapping.py index fd6ab209..a5eee0c0 100644 --- a/examples/basic/example_09_tracemapping.py +++ b/examples/basic/example_09_tracemapping.py @@ -9,9 +9,10 @@ """ -############################################################################### +# %% # Import necessary libraries # ~~~~~~~~~~~~~~~~~~~~~~~~~~ + import os from ansys.mechanical.core import launch_mechanical @@ -19,9 +20,9 @@ from matplotlib import image as mpimg from matplotlib import pyplot as plt -############################################################################### +# %% # Launch Mechanical -# ~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ~~~~~~~~~~~~~~~~~ # Launch a new Mechanical session in batch, setting ``cleanup_on_exit`` to # ``False``. To close this Mechanical session when finished, this example # must call the ``mechanical.exit()`` method. @@ -30,12 +31,10 @@ print(mechanical) -############################################################################### -# Download the required files : # geometry file , def file , copper alloy -# material file, fr4 material file -# Print the file paths to verify. -# ~~~~~~~~~~~~~~~~~~~~~~~~~~ - +# %% +# Download the required files +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Download files and print path all_input_files = { "geometry_file_name": "example_09_pcb.agdb", @@ -44,7 +43,6 @@ "fr4_material_file": "example_09_mat_fr4.xml", } - project_directory = mechanical.project_directory print(f"project directory = {project_directory}") @@ -61,13 +59,19 @@ combined_path = os.path.join(project_directory, base_name) part_file_path = combined_path.replace("\\", "\\\\") mechanical.run_python_script(f"{file_type} = '{part_file_path}'") - result = mechanical.run_python_script("'{file_type}'") + result = mechanical.run_python_script(f"{file_type}") print(f"path of {file_type} on server: {result}") png_image_name = "myplot.png" mechanical.run_python_script(f"image_name='{png_image_name}'") +# %% +# Run the script +# ~~~~~~~~~~~~~~ +# Run the Mechanical script to attach the geometry and set up and solve the +# analysis. + output = mechanical.run_python_script( """ import os @@ -229,24 +233,25 @@ def create_named_selection_from_id_list(ns_name, list_of_body_ids): ) print(output) -################################################################################### +# %% # Initialize the variable needed for the image directory -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Set the ``image_dir`` for later use. # Make the variable compatible for Windows, Linux, and Docker containers. mechanical.run_python_script(f"image_dir=ExtAPI.DataModel.AnalysisList[0].WorkingDir") +# %% +# Verify the path for image directory -# Verify the path for image directory. result_image_dir_server = mechanical.run_python_script(f"image_dir") print(f"Images are stored on the server at: {result_image_dir_server}") -############################################################################### -# Download the image and animation and plot -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# %% +# Download the image and plot +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Download one image file from the server to the current working directory and plot -# using matplotlib. +# using matplotlib def get_image_path(image_name): @@ -275,9 +280,12 @@ def display_image(path): display_image(image_local_path) -############################################################################### + +# %% # Close Mechanical # ~~~~~~~~~~~~~~~~ # Close the Mechanical instance. +print("Closing mechanical...") mechanical.exit() +print("Mechanical closed!") From bb4dbb3f168ba0fe84f734979b8e701f3ebfa7ea Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Wed, 24 Jan 2024 18:26:58 -0600 Subject: [PATCH 7/7] review feedback from @sbradrick --- examples/basic/example_09_tracemapping.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/basic/example_09_tracemapping.py b/examples/basic/example_09_tracemapping.py index a5eee0c0..c61bb166 100644 --- a/examples/basic/example_09_tracemapping.py +++ b/examples/basic/example_09_tracemapping.py @@ -1,12 +1,12 @@ """.. _ref_example_09: -Trace Mapping Example Demonstration ---------------------------------------------------------------------------------- - -Using supplied files, this example shows how to import tracemap data into a -static structural analysis of a new Mechanical session and execute a sequence of -Python scripting commands that mesh the model and export an image. +Trace Mapping Example +--------------------- +Using the provided file, this example demonstrates how to +import trace map data into a static structural analysis of +a new Mechanical session and execute a sequence of +Python scripting commands to mesh the model and export an image. """ # %%