Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Journal nullreferenceexception and I don't know what wrong

Status
Not open for further replies.

Ehaviv

Computer
Jul 2, 2003
1,012
Hi and thank you in advanced.

I get nullreferenceexception and I don't know what wrong ?

Code:
Imports System
Imports NXOpen
Imports NXOpen.UF

Module AskAllBodies
Sub Main (ByVal args() As String)

 Dim s As Session = Session.GetSession()
 Dim ufs As UFSession = UFSession.GetUFSession()
 Dim theUI As UI = UI.GetUI()

 Dim dp As Part = s.Parts.Display
 Dim allBodies() As Body

 Dim markId1 As Session.UndoMarkId
 markId1 = s.SetUndoMark(Session.MarkVisibility.Visible, "AskAllBodies")

 Dim theComponent As Assemblies.Component
 If SelectComponent("Select a component", theComponent) = Selection.Response.Cancel Then
   Exit Sub
 End If
 Dim a_part As Part = theComponent.Prototype

  'Dim part_occur As Tag ' Input Tag of part occurrence
  'Dim object_prototype As Tag ' Input Tag of object prototype
  Dim a_tag As Tag  ' Returns tag of object occurrence or NULL_TAG if object is not found
  Dim obj_occ As Body = Nothing

 allBodies = AskAllBodies(a_part)
 For Each aBody As Body In allBodies
   a_tag = ufs.Assem.FindOccurrence(theComponent.Tag, aBody.Tag)
   obj_occ = NXOpen.Utilities.NXObjectManager.Get(a_tag)
   obj_occ.Highlight
   'obj_occ.Color = 5
   'obj_occ.RedisplayObject()
 Next
 
 'msgbox("OK to UnHighlight")

 For Each aBody As Body In allBodies
   'aBody.UnHighlight
 Next 

End Sub

 Function AskAllBodies(ByVal thePart As Part) As Body()

   Dim s As Session = Session.GetSession()
   Dim ufs As UFSession = UFSession.GetUFSession()
   Dim theBodies As New System.Collections.ArrayList()
 
   Dim aBodyTag As Tag = Tag.Null
   Do
     ufs.Obj.CycleObjsInPart(thePart.Tag, UFConstants.UF_solid_type, aBodyTag)
     If aBodyTag = Tag.Null Then Exit Do 
     Dim theType As Integer, theSubtype As Integer
     ufs.Obj.AskTypeAndSubtype(aBodyTag, theType, theSubtype)
     If theSubtype = UFConstants.UF_solid_body_subtype Then
       theBodies.Add(s.GetObjectManager.GetTaggedObject(aBodyTag))
     End If
   Loop While True 
   Return DirectCast(theBodies.ToArray(GetType(Body)), Body())

 End Function

    Function SelectComponent(ByVal prompt As String, ByRef myComp As Assemblies.Component) As Selection.Response

        Dim selObj As TaggedObject
        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_all_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selobj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            myComp = selObj
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
    End Function

End Module
 
Replies continue below

Recommended for you

Are all the bodies in the part contained in the current reference set used in the assembly? If not, you might be getting the error because NX can't find an occurrence body to match up with the given prototype body. If the assembly contains promoted or deformed bodies, it might give a similar error.

www.nxjournaling.com
 
Hi Cowski and thank you.

Maybe I'm missing some basic of NX use

Please if you can give me an example of
'current reference set used in the assembly'

In the assembly I work on there are only these ref sets: Entire part and Empty.
 
Each component in the assembly will use a certain reference set in the assembly. In addition to the "empty" and "entire part" ref sets, there is (using the default NX options) usually a "model" ref set. Additionally, the user can create any number of ref sets to help manage the objects.

I have not done extensive testing of your code, but when I ran it on a small assembly that I have, it completed without error on a component with a single body but errored on a component where 5 bodies were found in the part but the model ref set only contains 2 bodies. In this case, I think that the null reference is due to the .FindOccurrence function returning a null tag.

Again, I've not done extensive testing so this may or may not be the root cause of the error message you see.

www.nxjournaling.com
 
I understand.

Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor