Interacting with the Current Model in Altair Inspire: A Simple Guide

Karthi Kandasamy
Karthi Kandasamy
Altair Employee
edited September 20 in Altair HyperWorks

How to  interact with current model 

Below are the code examples that explains how we can interact with existing model.

SketchPart : Sketch Part hold only point, Curve and area feature in case of closed sketches. 

from hwx import inspire #get the current model model = inspire.getModel() # or you can use inspire.getActiveModel() #Model may consist of sketches, Parts, Named objects such as Force, Fastener, Motor. #Sketches and parts are defined by features  #How to retrieve features from sketches and parts  listofsketchpart =model.getChildren('SketchPart') # returns list of sketches present in the model.  #we can also filter the sketch part by its name listofsketchpart = model.getChildren(type ='SketchPart',name='Sketch 1')  # To get a the the object with  type SketchPart and name Sketch 1 sketchpart = model.getChild(type ='SketchPart',name='Sketch 1')  # How to retrieve the points, line, curve and area of the sketchpart  areaFeature = sketchpart.getFeatures(type='FeatureArea')  # finding the largest area Feature Planar from the sketch part.  largestareafeature = max(sketchpart.getFeatures(type = 'FeaturePlanar'), key=lambda f: f.area, default=None)  # finding the smallest area feature from the sketch part.  smallestareafeature = min(sketchpart.getFeatures(type = 'FeaturePlanar'), key=lambda f: f.area, default=None)  # finding the largest area feature from the sketch part.  largestareafeature = max(sketchpart.getFeatures(type = 'FeatureArea'), key=lambda f: f.area, default=None)  # finding the smallest area feature from the sketch part.  smallestareafeature = min(sketchpart.getFeatures(type = 'FeatureArea'), key=lambda f: f.area, default=None)   # finding the longest curve feature from the sketch part.  largestCurvefeature = max(sketchpart.getFeatures(type = 'FeatureCurve'), key=lambda f: f.length, default=None)  # finding the shortest curve feature from the sketch part.  smallestCurvefeature = min(sketchpart.getFeatures(type = 'FeatureCurve'), key=lambda f: f.length, default=None)  # FeatureCurve is the parent  of all the curve types FeatureLinear ,FeatureCircular ,FeatureCurveOther. # finding the longest linear curve feature from the sketch part.  largestlinearfeature = max(sketchpart.getFeatures(type = 'FeatureLinear'), key=lambda f: f.length, default=None)  # finding the shortest linear curve feature from the sketch part.  smallestlinearfeature = min(sketchpart.getFeatures(type = 'FeatureLinear'), key=lambda f: f.length, default=None)   

Part: In the case of 3D sketches, Part holds the vertices, edges, area, and volume features.

# How to get part from current model.   from hwx import inspire model = inspire.getModel() # retrieve  list of parts from the model  listofparts = model.getChildren(type='Part') # retrieve  Part object with the name Part 1 from the model  part1 = model.getChild(type='Part', name = 'Part 1')  #Similar to sketchpart we can get the feature from the part object. # finding the largest area Feature Areafrom the sketch part.  largestareafeature = max(part1.getFeatures(type = 'FeatureArea'), key=lambda f: f.area, default=None)  # finding the smallest area feature from the sketch part.  smallestareafeature = min(part1.getFeatures(type = 'FeatureArea'), key=lambda f: f.area, default=None) # FeatureArea is the parent  of all the area types FeaturePlanar,FeatureCylindrical,FeatureSpherical,FeatureConical,FeatureAreaOther  # finding the largest area Feature Planar from the sketch part.  largestareafeature = max(sketchpart.getFeatures(type = 'FeaturePlanar'), key=lambda f: f.area, default=None)  # finding the smallest area feature from the sketch part.  smallestareafeature = min(sketchpart.getFeatures(type = 'FeaturePlanar'), key=lambda f: f.area, default=None)  # finding the largest Cylindrical feature from the sketch part.  largestareafeature = max(sketchpart.getFeatures(type = 'FeatureCylindrical'), key=lambda f: f.radius, default=None)  # finding the smallest area feature from the sketch part.  smallestareafeature = min(sketchpart.getFeatures(type = 'FeatureCylindrical'), key=lambda f: f.radius, default=None)  # finding the largest Spherical feature from the sketch part.  largestareafeature = max(sketchpart.getFeatures(type = 'FeatureSpherical'), key=lambda f: f.radius, default=None)  # finding the smallest area feature from the sketch part.  smallestareafeature = min(sketchpart.getFeatures(type = 'FeatureSpherical'), key=lambda f: f.radius, default=None)

How to get a existing Force object and modify its magnitude 

from hwx import inspire model = inspire.getModel() #retrive list of forces in the model listofForces= model.getChildren(type='Force')  #get the force object named 'Force 1'  force1= model.getChild(type='Force',name ='Force 1') print(f"Current magnitude of {force1.name} is {force1.magnitude}")  #changing the force magnitude to 10 N force1.magnitude = 10 print(f"Current magnitude of {force1.name} is {force1.magnitude}")   #similarly direction of the force also can changed print(f"Current direction of {force1.name} is {force1.direction}")  #changing the force direction to to X direction force1.direction= [1,0,0] print(f"Current direction of {force1.name} is {force1.direction }")   

How to create and update the  model variables  

# How to update the model variables from hwx import inspire model = inspire.getModel()  # Add new variable model.variables.add("width", type="length", expression=1) # Alternatively model.variables["fov"] = "angle", 45  # Update variable model.variables.update("width", expression=2) # Alternatively model.variables["fov"] = "angle", 90

How to assign a Material to a part. 

from hwx import inspire  model = inspire.getModel()  # get default material defaultMaterial = inspire.Material.getDefault() print("Default material is: ", defaultMaterial.name) #get the dictionaries of existing materials name : material object pair  allMaterials =inspire.Material.getMaterials()  How to check a material available in the material dictionaries  print('Steel (C45E)'in inspire.Material.getMaterials().keys()) #how to assign a material to a part  part1 = model.getChild(type ='Part', name='Part 1') if 'Steel (C45E)'in inspire.Material.getMaterials().keys():   part1.material ='Steel (C45E)'   print(f" Material {part1.material.name} is assigned to {part1.name}") else:   print("'Steel (C45E)' is not available from the material dictionaries)   

How to assign thickness to a sheet part

from hwx import inspire  model = inspire.getModel() # get all the sheet parts from the model  #dimensions=1 Point  #dimensions=2 sheet  #dimensions=3 solid  sheetparts= model.getChildren(type='Part', dimensions=2)   #iterate over all the sheet part and assign 3 mm  as sheet thickness   for sheetpart in sheetparts:   #Applying 3 mm  as sheet thickness     sheetpart.sheetThickness = '3 mm'