Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Change color of object without crashing if no object selected 1

Status
Not open for further replies.

cpetersen

Structural
Nov 2, 2023
2
0
0
US
I have a journal created that works to change the color of a selected object. I want to use this code in another journal that does a number of other things also. The problem is that I want the user to be able to press escape to choose not to select an object without crashing the program. I get a Null tag not allowed error. How can I change the code so I avoid that error? Thanks in advance!!
Code:
Option Strict Off
 
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
 
Module NXJournal
Sub Main
 
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim ufs As UFSession = UFSession.GetUFSession()
 
Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")
 
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.ApplyToAllFaces = False
displayModification1.ApplyToOwningParts = False
displayModification1.NewColor = 193
 
Dim objects1(0) As DisplayableObject
Dim body1 As Body = SelectSolid()
objects1(0) = body1
displayModification1.Apply(objects1)
displayModification1.Dispose()
 
End Sub
 
Function SelectSolid() As Body
 
    Dim ui As UI = ui.GetUI
    Dim message As String = "Click on solid body."
    Dim title As String = "Selection"
 
    Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
    Dim keepHighlighted As Boolean = False
    Dim includeFeatures As Boolean = False
 
    Dim selectionAction As Selection.SelectionAction = _
       Selection.SelectionAction.ClearAndEnableSpecific
 
    Dim selectionMask_array(0) As Selection.MaskTriple
    With selectionMask_array(0)
       .Type = UFConstants.UF_solid_type
       .Subtype = 0
       .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
    End With
 
    Dim selectedObject As NXObject = Nothing
    Dim cursor As Point3d
 
    ui.SelectionManager.SelectObject(message, title, scope, _
                                    selectionAction, includeFeatures, _
                                    keepHighlighted, selectionMask_array, _
                                    selectedObject, cursor)
 
    Dim solid As Body = CType(selectedObject, Body)
 
    If solid Is Nothing Then
       Return Nothing
    End If
 
    Return solid
 
End Function
 
End Module
 
Replies continue below

Recommended for you

The easy way to do it in your current journal is to do the selection first and check if the return value is "nothing"; if so, exit the main sub (this will end the journal).

Code:
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Module Module213
    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display
        Dim ufs As UFSession = UFSession.GetUFSession()

        Dim objects1(0) As DisplayableObject
        Dim body1 As Body = SelectSolid()
        If IsNothing(body1) Then Return

        Dim markId3 As Session.UndoMarkId
        markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

        Dim displayModification1 As DisplayModification
        displayModification1 = theSession.DisplayManager.NewDisplayModification()
        displayModification1.ApplyToAllFaces = False
        displayModification1.ApplyToOwningParts = False
        displayModification1.NewColor = 193

        objects1(0) = body1
        displayModification1.Apply(objects1)
        displayModification1.Dispose()

    End Sub

    Function SelectSolid() As Body

        Dim ui As UI = UI.GetUI
        Dim message As String = "Click on solid body."
        Dim title As String = "Selection"

        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim keepHighlighted As Boolean = False
        Dim includeFeatures As Boolean = False

        Dim selectionAction As Selection.SelectionAction =
           Selection.SelectionAction.ClearAndEnableSpecific

        Dim selectionMask_array(0) As Selection.MaskTriple
        With selectionMask_array(0)
            .Type = UFConstants.UF_solid_type
            .Subtype = 0
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
        End With

        Dim selectedObject As NXObject = Nothing
        Dim cursor As Point3d

        ui.SelectionManager.SelectObject(message, title, scope,
                                        selectionAction, includeFeatures,
                                        keepHighlighted, selectionMask_array,
                                        selectedObject, cursor)

        Dim solid As Body = CType(selectedObject, Body)

        If solid Is Nothing Then
            Return Nothing
        End If

        Return solid

    End Function

End Module


www.nxjournaling.com
 
Status
Not open for further replies.
Back
Top