Correct syntax to find an element in a .afm?

Does any one know what is the correct syntax to find an element (i.e. check the user specified element exist) in a .afm?

Thanks

Regards

JXB

Does the following journal get what you need?

Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()

Dim lw As ListingWindow = theSession.ListingWindow

lw.Open()

'***************************************
'the element number we are interested in
Dim elem As Integer = 1492
'***************************************

Dim theAssyFemPrt As CAE.AssyFemPart
Try
theAssyFemPrt = CType(theSession.Parts.BaseWork, CAE.AssyFemPart)
Catch ex As Exception
'not AssyFem part
lw.WriteLine(ex.Message)
Return
End Try

Dim theFeElement As CAE.FEElement
Try
lw.WriteLine("theAssyFemPrt contains " & theAssyFemPrt.BaseFEModel.FeelementLabelMap.NumElements.ToString & " elements")
lw.WriteLine("")
theFeElement = theAssyFemPrt.BaseFEModel.FeelementLabelMap.GetElement(1492)
lw.WriteLine("Label: " & theFeElement.Label)
lw.WriteLine("Number of corner nodes: " & theFeElement.NumberOfCornerNodes.ToString)
lw.WriteLine("Order: " & theFeElement.Order.ToString)
lw.WriteLine("Shape: " & theFeElement.Shape.ToString)

Catch ex As NXException
lw.WriteLine("GetElement exception: " & ex.Message)
End Try

lw.Close()

End Sub

End Module

Thanks NXJournaling for the supplied code. It looks like what I am coding is not far off (though I am mixing up my AFEM with my FEM!). I think the key difference is that I am attempting the "element check" from .sim file. The code I started with work OK and the one provided does indeed works in the .afm (the exception thrown if run n a .sim file is : Unable to cast object of type 'NXOpen.CAE.SimPart' to type 'NXOpen.CAE.AssyFemPart'.
The reason I am doing this is that the user will run the macro from the .sim file as the aim of the macro is to extract specific results for a given element. Hence the check "element exists" (in the .afm or .fem attached to the .sim) before getting the results.

Thanks
Regards

This is my first foray into writing journal code for assembly FEM files, so I'm not sure this is the proper way to do it, but I can get it to work if the assembly FEM is the display part. The code below finds the assembly FEM part in the SIM part, makes it the display part, reports the desired element, and finally switches the display part back to the SIM part.

Option Strict Off
Imports System
Imports NXOpen

Module Module2

Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

lw.Open()

'***************************************
'the element number we are interested in
Dim elem As Integer = 1492
'***************************************

Dim theSimPart As CAE.SimPart
Try
theSimPart = theSession.Parts.BaseWork
lw.WriteLine("theSimPart: " & theSimPart.Leaf)
lw.WriteLine(theSimPart.GetType.ToString)
lw.WriteLine("")
Catch ex As Exception
lw.WriteLine("simPart exception: " & ex.Message)
Return
End Try

Dim theAssyFemPrt As CAE.AssyFemPart
Try
theAssyFemPrt = theSimPart.FemPart
lw.WriteLine("theAssyFemPrt: " & theAssyFemPrt.Leaf)
lw.WriteLine(theAssyFemPrt.GetType.ToString)
lw.WriteLine("")
Catch ex As Exception
'not AssyFem part
lw.WriteLine("AssyFem part exception: " & ex.Message)
Return
End Try

'make the assembly FEM part the display part
Dim partLoadStatus1 As PartLoadStatus
Dim status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(theAssyFemPrt, False, False, partLoadStatus1)

Dim theFeElement As CAE.FEElement
Try
lw.WriteLine("theAssyFemPrt contains " & theAssyFemPrt.BaseFEModel.FeelementLabelMap.NumElements.ToString & " elements")
lw.WriteLine("")
theFeElement = theAssyFemPrt.BaseFEModel.FeelementLabelMap.GetElement(1492)
lw.WriteLine("Label: " & theFeElement.Label)
lw.WriteLine("Number of corner nodes: " & theFeElement.NumberOfCornerNodes.ToString)
lw.WriteLine("Order: " & theFeElement.Order.ToString)
lw.WriteLine("Shape: " & theFeElement.Shape.ToString)

Catch ex As NXException
lw.WriteLine("GetElement exception: " & ex.Message)
End Try

'switch back to the SIM part
Dim partLoadStatus2 As PartLoadStatus
Dim status2 As PartCollection.SdpsStatus
status2 = theSession.Parts.SetDisplay(theSimPart, False, False, partLoadStatus2)

lw.Close()

End Sub

End Module

Thanks a lot for that. Didn't think of switching back and forth between .sim & .afm. Preliminary testing is looking (very good). I have just done a test to deal with a .fem if an .afm is not attached to the .sim. Seems to work! Will aim at integrating the appropriate line of codes in the main program.

Thanks

Regards

JXB
'---------------------
Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()

Dim lw As ListingWindow = theSession.ListingWindow

Dim theFeElement As CAE.FEElement
Dim bItsAFEMpart As Boolean = False

lw.Open()

'***************************************
'the element number we are interested in
Dim lngElemID as Long = 4
'***************************************

Dim theSimPart As CAE.SimPart
Try
theSimPart = theSession.Parts.BaseWork
lw.WriteLine("theSimPart: " & theSimPart.Leaf)
lw.WriteLine(" sim part Type: " & theSimPart.GetType.ToString)
lw.WriteLine("")
Catch ex As Exception
lw.WriteLine("simPart exception: " & ex.Message)
Return
End Try

Dim theAssyFemPrt As CAE.AssyFemPart
Try
theAssyFemPrt = theSimPart.FemPart
lw.WriteLine("theAssyFemPrt: " & theAssyFemPrt.Leaf)
lw.WriteLine(" afem part Type: " &theAssyFemPrt.GetType.ToString)
lw.WriteLine("")
bItsAFEMpart = True
Catch ex As Exception
'not AssyFem part
lw.WriteLine("AssyFem part exception: " & ex.Message)
'Return
End Try

Dim theFEMPart As CAE.FemPart
Try
theFEMPart = theSimPart.FemPart
lw.WriteLine("theFemPrt: " & theFEMPart.Leaf)
lw.WriteLine(" fem part Type: " &theFEMPart.GetType.ToString)
lw.WriteLine("")
Catch ex As Exception
'not AssyFem part
lw.WriteLine("FEM part exception: " & ex.Message)
'Return
End Try

If bItsAFEMpart = True Then
'make the assembly FEM part the display part
Dim partLoadStatus1 As PartLoadStatus
Dim status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(theAssyFemPrt, False, False, partLoadStatus1)

Try
lw.WriteLine("theAssyFemPrt contains " & theAssyFemPrt.BaseFEModel.FeelementLabelMap.NumElements.ToString & " elements")
lw.WriteLine("")
theFeElement = theAssyFemPrt.BaseFEModel.FeelementLabelMap.GetElement(lngElemID)

Catch ex As NXException
lw.WriteLine("GetElement exception: " & ex.Message)
End Try

'switch back to the SIM part
Dim partLoadStatus2 As PartLoadStatus
Dim status2 As PartCollection.SdpsStatus
status2 = theSession.Parts.SetDisplay(theSimPart, False, False, partLoadStatus2)

Else
'deal with fem
Try
lw.WriteLine("")
theFeElement = theFEMPart.BaseFEModel.FeelementLabelMap.GetElement(lngElemID)
Catch ex As NXException
lw.WriteLine("GetElement exception: " & ex.Message)
End Try

End if

If (theFeElement Is Nothing) Then
lw.WriteLine ("--Specified Element ID was not found - Line is skipped")
Else
lw.WriteLine ("--Specified Element ID was found")
End If

'lw.Close()

End Sub

End Module

Thanks
Regards

Looking at the code above I wonder if there is a "smarter" way of doing this. Is there a way of checking/getting what is "associated" with the sim part (a .afm or a .fem) then execute accordingly? something like that

Dim theSimPart As CAE.SimPart
Try
theSimPart = theSession.Parts.BaseWork
theFEMCAEPart = get associated fem part with sim part ' !!!!!!!!
If theFEMCAEPart.GetType is GetType(CAE.AssyFemPart) Then
'it's a .afm
theAssyFemPrt = theSimPart.FemPart
Else 'it's a .fem
theFEMPrt = theSimPart.FemPart
End if
Catch ex As Exception
lw.WriteLine("simPart exception: " & ex.Message)
Return
End Try

Thanks
Regards

Maybe something like that which seems to work

Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()

Dim lw As ListingWindow = theSession.ListingWindow

Dim theFEMCAEPart As CAE.CaePart
Dim theFeElement As CAE.FEElement
Dim bItsAFEMpart As Boolean = False

lw.Open()

'***************************************
'the element number we are interested in
Dim lngElemID as Long = 3
'***************************************

Dim theSimPart As CAE.SimPart
Dim theAssyFemPrt As CAE.AssyFemPart
Dim theFEMPart As CAE.FemPart

Try
theSimPart = theSession.Parts.BaseWork
lw.WriteLine("theSimPart: " & theSimPart.Leaf)
lw.WriteLine(" sim part Type: " & theSimPart.GetType.ToString)
lw.WriteLine("")
theFEMCAEPart = theSimPart.FemPart
If (theFEMCAEPart Is Nothing) Then Return

lw.WriteLine(" femCAEPart Full Path: " & theFEMCAEPart.FullPath())
lw.WriteLine(" femCAEPart Leaf Name: " & theFEMCAEPart.Leaf)
lw.WriteLine("")
If theFEMCAEPart.GetType is GetType(CAE.AssyFemPart)
lw.WriteLine("the associated fem is a AFEM")
theAssyFemPrt = theSimPart.FemPart
lw.WriteLine("theAssyFemPrt: " & theAssyFemPrt.Leaf)
lw.WriteLine(" afem part Type: " &theAssyFemPrt.GetType.ToString)
lw.WriteLine("")
bItsAFEMpart = True
Else ' Deal with a .fem
theFEMPart = theSimPart.FemPart
lw.WriteLine("theFemPrt: " & theFEMPart.Leaf)
lw.WriteLine(" fem part Type: " &theFEMPart.GetType.ToString)
lw.WriteLine("")
End if
Catch ex As Exception
lw.WriteLine("simPart exception: " & ex.Message)
Return
End Try

If bItsAFEMpart = True Then
'make the assembly FEM part the display part
Dim partLoadStatus1 As PartLoadStatus
Dim status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(theAssyFemPrt, False, False, partLoadStatus1)

Try
theFeElement = theAssyFemPrt.BaseFEModel.FeelementLabelMap.GetElement(lngElemID)
Catch ex As NXException
lw.WriteLine("GetElement exception: " & ex.Message)
End Try

'switch back to the SIM part
Dim partLoadStatus2 As PartLoadStatus
Dim status2 As PartCollection.SdpsStatus
status2 = theSession.Parts.SetDisplay(theSimPart, False, False, partLoadStatus2)

Else
'deal with fem
Try
theFeElement = theFEMPart.BaseFEModel.FeelementLabelMap.GetElement(lngElemID)
Catch ex As NXException
lw.WriteLine("GetElement exception: " & ex.Message)
End Try

End if

If (theFeElement Is Nothing) Then
lw.WriteLine ("--Specified Element ID was not found")
Else
lw.WriteLine ("--Specified Element ID was found")
End If

End Sub

End Module

Thanks
Regards

Hi all

The code provided in this tread works well. After copying the appropriate lines of code in my main programme it partially works. The "glitch" is on finding the element. The programme never does! After doing some testing I believe this might be due to the fact that the search of the element (in my real program) is done in a function which is called from the Main sub(). I have therefore tried to replicate the problem but to no avail. I always get "Element not found".
What works
1. Switching from .sim to .afm and back to the .sim
2. establish the element Map (I think!)
What does not work.
Finding the element

Below is the code trying to replicate the problem. I have tried declaring variables as 'Public' (didn't not work!) and I am currently passing the Element Map as a an argument to the function ()still does not work). Has anyone have any oidea on what could be the source of the problem?

Thanks in advance

Regards

JXB

Option Strict Off
Imports System
Imports NXOpen

Module Module1

Dim theSession As Session = Session.GetSession()
Public Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

Dim theSimPart As CAE.SimPart 'the .sim file
Dim theFEMCAEPart As CAE.CaePart 'the fem/afm associated with the .sim
Dim theFEMPart As CAE.FemPart 'the .fem associated with .sim
Dim theAssyFemPart As CAE.AssyFemPart 'the .afm associated with .sim
Dim elementMap As CAE.FEElementLabelMap

lw.Open()

Try
theSimPart = theSession.Parts.BaseWork
Dim workSimPart As CAE.SimPart = CType(theSession.Parts.BaseWork, CAE.SimPart)
lw.WriteLine("theSimPart: " & theSimPart.Leaf)
lw.WriteLine(" sim part Type: " & theSimPart.GetType.ToString)
lw.WriteLine("")
theFEMCAEPart = theSimPart.FemPart

lw.WriteLine(" femCAEPart Full Path: " & theFEMCAEPart.FullPath())
lw.WriteLine(" femCAEPart Leaf Name: " & theFEMCAEPart.Leaf)
lw.WriteLine("")

If theFEMCAEPart.GetType is GetType(CAE.AssyFemPart) Then
theAssyFemPart = theSimPart.FemPart

'make the .afm the display part
Dim partLoadStatus1 As PartLoadStatus
Dim status1 As PartCollection.SdpsStatus

status1 = theSession.Parts.SetDisplay(theAssyFemPart, False, False, partLoadStatus1)
elementMap=theAssyFemPart.BaseFEModel.FeelementLabelMap

'switch back to the SIM part
Dim partLoadStatus2 As PartLoadStatus
Dim status2 As PartCollection.SdpsStatus
status2 = theSession.Parts.SetDisplay(theSimPart, False, False, partLoadStatus2)

Else ' Deal with a .fem
theFEMPart = theSimPart.FemPart
elementMap=theFEMPart.BaseFEModel.FeelementLabelMap
End if
Catch ex As Exception
lw.WriteLine("simPart exception: " & ex.Message)
'will return: Object reference not set to an instance of an object.
'if no .fem/afm is loaded
Return
End Try

GetOnWithIt(elementMap)

End Sub

Sub GetOnWithIt(theInputElmMap As CAE.FEElementLabelMap)

'***************************************
'the element number we are interested in
Dim lngElemID as Long = 136 '504
'***************************************
Dim theElmMap As CAE.FEElementLabelMap
Dim theFeElement As CAE.FEElement

theElmMap = theInputElmMap
If (theElmMap Is Nothing) Then
lw.WriteLine ("Elm Map is nothing!")
End if

theFeElement = theElmMap.GetElement(lngElemID)
If (theFeElement Is Nothing) Then
lw.WriteLine ("--Specified Element ID was not found - Line is skipped")
Else
lw.WriteLine ("--Specified Element ID was found")
End If
End Sub
End Module

Thanks
Regards

Have you resolved any conflicts with node/element ID numbers in the assembly FEM? Are you searching for the element number as listed in the component FEM or the (possibly different) number as listed in the assembly FEM?

node/element ID conflict resolved. The element tested was created in the .afm (it's a connecting bar) and the test script works fine ( dated Fri, 01/23/2015 ) (the element is found). It must be something with the "element search" being in a different function. Have you managed to test the code provided (with the calling function)? Does it work for you?

Thanks

Regards

JXB

Thanks
Regards

I've not been able to test your code yet, but I'd suggest searching for the element while the FEM part is either the work and/or displayed part. I've had mixed results when trying to access properties or methods of a part that is neither the display nor the work part. Occasionally it works, but mostly the attempts error out or give odd results.

Call your "GetOnWithIt" subroutine after the FEM is made the display (or work) part but before you switch back to the SIM part.

will have a look at suggestion tomorrow. I had in mind establishing the element map once and use later as the main programme loops through a list of elements. the programme has to be in the sim part to work and switching back and forth would be to slow. I will however review as there might be another way.

Thanks
Regards