Dealing with .afm - How to?

To all

I am struggling a bit with understanding the logic (i.e. the order one should define things ) to deal with .afm (FEM assembly). In other words the 'Inheritance Hierarchy' (It's more a reflection of my limited vb knowledge).

I am in a .sim file. So that's my base part (!)
I tested it as follows: If TypeOf theBasePart Is CAE.SimPart Then theLw.WriteLine("it's a SIM part!")

I want to access the .afm "attached" to the .sim file how does one go about it?
I am trying to modify the lines of codes I have (to access the element map) for a straightforward .fem but not no avail

Dim theSimPart As NXOpen.CAE.SimPart = theSession.Parts.BaseWork
Dim lngElemID As Long =1234
Dim femPart As NXOpen.CAE.BaseFemPart = theSimPart.FemPart
Dim elementMap As NXOpen.CAE.FEElementLabelMap = femPart.BaseFEModel.FeelementLabelMap
Dim theFEElm As NXOpen.CAE.FEElement = elementMap.GetElement(lngElemID)
Dim destinationElements1(0) As CAE.FEElement
destinationElements1(0)=theFEElm

Any help would be appreciated.

Thanks

Regards

JXB

the following seems to work

'$-------------------------------------------------------------------------
Dim theBasePart As BasePart = theSession.Parts.BaseWork
Dim theAssyFEModel As CAE.AssyFEModel
Dim theFEModel As CAE.FEModel
Dim theAFEModel As CAE.AssyFEModel
Dim elementMap As CAE.FEElementLabelMap

If (theBasePart Is Nothing Or Not (TypeOf theBasePart Is CAE.BaseFemPart Or TypeOf theBasePart Is CAE.AssyFemPart)) Then
theLw.WriteLine("Not a FEM or AFEM")
Return
Else
If TypeOf theBasePart Is CAE.FemPart Then
thefemPart = CType(theBasePart, CAE.FemPart)
theFEModel = CType(thefemPart.BaseFEModel(), CAE.FEModel)
elementMap = theFEModel.FEElementLabelMap
End if
If TypeOf theBasePart Is CAE.AssyFemPart Then
workAssyFemPart = CType(theBasePart, CAE.AssyFemPart)
theAFEModel = CType(workAssyFemPart.BaseFEModel(), CAE.AssyFEModel)
elementMap = theAFEModel.FEElementLabelMap
End if
End If

Dim lngElemID as Long = 1201 '<---user input

Dim theFEElm As CAE.FEElement = elementMap.GetElement(lngElemID)
Dim destinationElements1(0) As CAE.FEElement
destinationElements1(0)=theFEElm

If (theFEElm Is Nothing) Then
theLW.WriteLine ("Specified Element ID was not found - Line is skipped")
Else
theLW.WriteLine ("Specified Element ID was found")
End If
'$-------------------------------------------------------------------------

However I cannot get the right syntax to get the fem (either a .fem or .afm) associated with the simulation (.sim)
I tried the following but line of codes to get the Fempart associated. Any hints/pointers on which keyword is wrongly used?

Thanks

Regards

JXB

'$----------------------------------------
If TypeOf theBasePart Is CAE.SimPart Then
theLw.WriteLine("it's a SIM part")
'Returns the fem part associated with the sim part
Dim femPart As CAE.CAEPart = theSimPart.FemPart
'tried this one too but does not work
'Dim femPart As CAE.BaseFemPart = theSimPart.FemPart
theLw.WriteLine("the part associated is " & femPart.Name)
Else
theLw.WriteLine("it's NOT a SIM part")
End If

Thanks
Regards

The code that you are currently using:

Dim femPart As CAE.CAEPart = theSimPart.FemPart

should work. I see that you are trying to report the .Name of the FEM part; for most NX objects, the name property is empty* - it is there so the user can give the object a custom name. Instead of the .Name property, try reporting the .Leaf or .FullPath properties to see what the result is.

* Expression objects are one notable exception; NX will assign a value to the .Name property for created expressions.

Thanks. It should yet it doesn't
If I try with a .fem associated with the .sim I get an error about 'Object reference not set to an instance of an object'
If I try with a .afm associated with the .sim using: Dim femPart As CAE.CAEPart = theSimPart.AssyFemPart, I get an error an about 'AssyFEMPart' is not a member of 'NXOpen.CAE.SimPart'
The 2nd one is clearly telling me that it's the wrong way of "checking" teh .afm assocaited with the .sim

Thanks
Regards

It seems that the code loop through the fe models in the .afm but for some reasons the element is not found (it does exist!). I have the following code. Any idea why?

Sub Main ()
Dim i,j,k as Integer

Dim theSession As Session = Session.GetSession()
Dim theUISession As UI = UI.GetUI
Dim theLW As ListingWindow = theSession.ListingWindow

theLW.Open()

Dim theBasePart As BasePart = theSession.Parts.BaseWork
Dim theSimPart As CAE.SimPart
Dim displaySimPart As CAE.SimPart
Dim theFEMCAEPart As CAE.CaePart
Dim theFEMPart As CAE.FemPart
Dim theAFemPart As CAE.AssyFemPart
Dim theFEModel As CAE.FEModel
Dim theAFEModel As CAE.AssyFEModel
Dim theFEElm As CAE.FEElement
Dim elementMap As CAE.FEElementLabelMap
Dim destinationElements1(0) As CAE.FEElement
Dim lngElemID as Long = 2 '1201 '501 1 '<---user input

If TypeOf theBasePart Is CAE.SimPart Then
theSimPart = CType(theBasePart, CAE.SimPart)
displaySimPart = CType(theSession.Parts.BaseDisplay, CAE.SimPart)

theFEMCAEPart = theSimPart.FemPart

If theFEMCAEPart.GetType is GetType(CAE.AssyFemPart)
theLW.WriteLine("the associated fem is a AFEM")
theAFemPart = CType(theFEMCAEPart, CAE.AssyFemPart)
theAFEModel = CType(theAFemPart.BaseFEModel(), CAE.AssyFEModel)

Dim thefemoccattr As CAE.FEModelOccAttribute
Dim theFEModelOccur As CAE.FEModelOccurrence

For each theFEModelOccur In theAFEModel.GetChildren()
theLW.WriteLine("")
theLW.WriteLine(" Component Name: " & theFEModelOccur.GetCOmponent().Name)
theLW.WriteLine(" Display Name: " & theFEModelOccur.GetCOmponent().DisplayName)
elementMap = theFEModelOccur.FEElementLabelMap
theFEElm = elementMap.GetElement(lngElemID)
destinationElements1(0)=theFEElm

If (theFEElm Is Nothing) Then
theLW.WriteLine ("Specified Element ID was not found")
Else
theLW.WriteLine ("Specified Element ID was found")
End If

Next

Else
theLW.WriteLine("the associated fem is a FEM")
theFemPart = CType(theFEMCAEPart, CAE.FemPart)
theFEModel = CType(theFemPart.BaseFEModel(), CAE.FEModel)
elementMap = theFEModel.FEElementLabelMap
End If

Else
theLw.WriteLine("Base part is NOT a SIM part")
End If

Thanks
Regards