Let's assume I have a vertex at (0,0,0). How could I create a named selection containing this vertex?
You can use the Worksheet scoping method to create a named selection with complex criteria using scripting. Here's an example selecting the vertex at (0,0,0) global coordinates:
# Generate a new Named Selection object in Mechanical myNS = Model.NamedSelections.AddNamedSelection() # Change the scoping method to "Worksheet" to add complex seleciton criteria myNS.ScopingMethod = GeometryDefineByType.Worksheet # Add Selection Criterion in Worksheet myNS.GenerationCriteria.Add(None) # Comment: Worksheet / Data View in Mechanical needs refresh to see command actions reflected # Set Entity type for first Selection criterion to "Vertex" myNS.GenerationCriteria[0].EntityType = SelectionType.GeoVertex # Set Criterion to "Location X" myNS.GenerationCriteria[0].Criterion = SelectionCriterionType.LocationX # Set operator to "Equals" myNS.GenerationCriteria[0].Operator = SelectionOperatorType.Equal # Set desired X location value myNS.GenerationCriteria[0].Value = Quantity('0 [mm]') ''' The above will select all vertices on x = 0. Hence, we need to filter this further by adding more criteria''' myNS.GenerationCriteria.Add(None) myNS.GenerationCriteria[1].Action = SelectionActionType.Filter myNS.GenerationCriteria[1].EntityType = SelectionType.GeoVertex myNS.GenerationCriteria[1].Criterion = SelectionCriterionType.LocationY myNS.GenerationCriteria[1].Operator = SelectionOperatorType.Equal myNS.GenerationCriteria[1].Value = Quantity('0 [mm]') myNS.GenerationCriteria.Add(None) myNS.GenerationCriteria[2].Action = SelectionActionType.Filter myNS.GenerationCriteria[2].EntityType = SelectionType.GeoVertex myNS.GenerationCriteria[2].Criterion = SelectionCriterionType.LocationZ myNS.GenerationCriteria[2].Operator = SelectionOperatorType.Equal myNS.GenerationCriteria[2].Value = Quantity('0 [mm]') # Execute the selection criteria and generate the selection: myNS.Generate()
The scripting process is largely the same, but an FYI you can also use the distance/closest criteria in the GenerationCriteria Criterion/Operator properties to find the closest vertex in case one doesn't exist at the exact location you want.
Here is perhaps a sample script to start with.
allBodies = DataModel.GetObjectsByType(DataModelObjectCategory.Body) allVertices = [] for body in allBodies: geoBody = body.GetGeoBody() allVertices.extend(geoBody.Vertices) def find_vertex(vertices, target_x, target_y, target_z, tolerance=1e-3): for vertex in vertices: if (abs(float(vertex.X) - target_x) < tolerance and abs(float(vertex.Y) - target_y) < tolerance and abs(float(vertex.Z) - target_z) < tolerance): return vertex return None #Coordinate input in Geometry unit (found in the properties of Geometry object in tree) target = find_vertex(allVertices, 31.96, 1.0968, 3.1091) if target: print("Found vertex") else: print("Vertex not found.") newNS = Model.AddNamedSelection() NewSelection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities) NewSelection.Ids = [target.Id] newNS.Location = NewSelection