Named selection : node ids, position and node connectivity with DPF
Using DPF in Mechanical how can I get:
the ids and position of the nodes of a named selection
know to which elements this node is attached to (node connectivity)
?
Best Answer
-
First, let's use DPF to get the ids of the nodes in the named selection. The NS is a face NS in Mechanical, called "my_NS*".* When written to the .rst file the named selection names are converted to capital letters so we need to search for the NS called "MY_NS":
# Import DPF import mech_dpf import Ans.DataProcessing as dpf mech_dpf.setExtAPI(ExtAPI) #Get the data source (i.e. result file) analysis = ExtAPI.DataModel.Project.Model.Analyses[0] dataSource = dpf.DataSources(analysis.ResultFileName) #Create Named Selection Operator ns_op = dpf.operators.scoping.on_named_selection() ns_op.inputs.data_sources.Connect(dataSource) ns_op.inputs.requested_location.Connect('Nodal') #Name should be in all caps ns_op.inputs.named_selection_name.Connect('MY_NS') # Get node numbers for nodes in named selection mesh_data = ns_op.outputs.mesh_scoping.GetData() mesh_data.Ids # node Ids
Then, let's grab the complete mesh through:
# Get complete mesh model=dpf.Model(dataSource) mesh=model.Mesh
Now we can use the info on the mesh and on the node Ids to get information on the nodes of this named selection:
# Get information for first node in named selection node1=mesh.NodeById(mesh_data.Ids[0]) # Get node position print(node1.X,node1.Y,node1.Z)
Finally, for the node connectivity, we can use the NodalConnectivityPropertyField property of the mesh. One thing that is important to notice is that the connectivity will return the index of the element in the element list, which is not equal to the element Id. Once the index is known, the element Id can be obtained from the element list:
# Get elements attached to node connectivities=mesh.NodalConnectivityPropertyField elem_conn = connectivities.GetEntityDataById(node1.Id) elem_id = mesh.Elements[elem_conn[0]].Id print(elem_id)
6
Answers
-
Hello,
What is corresponding to "ns_op.inputs.requested_location.Connect('Nodal')" ?
I mean, what have I to write inside Connect(...) ?
If I want all the elements ids, it deosn't work when i write ns_op.inputs.requested_location.Connect('Element') or ns_op.inputs.requested_location.Connect('Elemental').
Thanks,
Adrien
0 -
This is also a helpful function to convert any selection type in Mechanical to Node Ids (does not need dpf). You can then use this line to make a dpf mesh scoping:
dpf.MeshScopingFactory.NodalScoping(NodeIds) def ConvertToNodeIds(SelectionData, SelectionType=None): """ Convert a given Selection Info, (or list of ent/id and Selection Type) to list of Node Ids Args: SelectionData SelectionType (Ansys.ACT.Interfaces.Common.SelectionTypeEnum) if SelectionType is used, then SelInfo arg is a list of Ids Returns: List of node ids """ SelInfo=CreateSelInfo(SelectionData, SelectionType) Mesh = ExtAPI.DataModel.MeshDataByName("Global") SelTypeStr = str(SelInfo.SelectionType) if SelTypeStr == "GeometryEntities": Ids = set() for Id in SelInfo.Ids: MeshRegion = Mesh.MeshRegionById(Id) Ids.update(MeshRegion.NodeIds) Ids=list(Ids) elif SelTypeStr == "MeshNodes": Ids= list(SelInfo.Ids) elif SelTypeStr == "MeshElements": Ids= list(ExtAPI.DataModel.MeshDataByName("Global").NodeIdsFromElementIds(SelInfo.Ids)) elif SelTypeStr == "MeshElementFaces": Ids = set() for id,index in zip(SelInfo.Ids, SelInfo.ElementFaceIndices): el_face = Ansys.ACT.Common.Mesh.ElementFaceWrapper(Mesh, Mesh.ElementById(id), index) Ids.update(el_face.NodeIds) Ids= list(Ids) return Ids
1