×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Add body to reference set

Add body to reference set

Add body to reference set

(OP)
Hello every one,

I'm trying to write journal which selected body:
- change translucency to 60
- change line to dashed
- put it on layer 100
- and finally put it in reference set "false"

First 3 point work perfect but the last one not. Below is my code:

CODE

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports System.Collections
Imports System.Collections.Generic

Module Ref_False

    Dim theSession As Session = Session.GetSession()
    Dim sel As Selection = NXOpen.UI.GetUI.SelectionManager 
    Dim myRefSetNames As New List(Of String)
    Dim ref_temp as ReferenceSet

    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display

    Sub Main()


	Dim referenceSet1 As ReferenceSet
        Dim selectedObjectsArray() As NXObject

        Selectbody(selectedObjectsArray)

        Dim BodyArray(selectedObjectsArray.Length - 1) As DisplayableObject

        For i As Integer = 0 To selectedObjectsArray.Length - 1
            BodyArray(i) = CType(selectedObjectsArray(i), DisplayableObject)

        Next

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

	myRefSetNames.Add("FALSE")

        For Each tempRefSetName As String In myRefSetNames
            Dim refSetExists As Boolean = False

            For Each tempRefSet As ReferenceSet In workPart.GetAllReferenceSets

                If tempRefSet.Name.ToUpper = tempRefSetName.ToUpper Then
                    refSetExists = True
                End If

		If tempRefSet.Name.ToUpper() = "FALSE" Then
			ref_temp = tempRefSet
			ref_temp.SetAddComponentsAutomatically(true, true)
		end if 	

            Next

            If refSetExists Then

		referenceSet1.AddObjectsToReferenceSet(BodyArray)

            Else

                referenceSet1 = workPart.CreateReferenceSet()
                referenceSet1.SetName(tempRefSetName)
		referenceSet1.SetAddComponentsAutomatically(True, True)
		referenceSet1.AddObjectsToReferenceSet(BodyArray)
            End If

        Next

	myRefSetNames.clear()

        Dim changeBodytransluency As DisplayModification = theSession.DisplayManager.NewDisplayModification()

        With changeBodytransluency
            .ApplyToAllFaces = true
            .NewTranslucency = 60
	    .NewFont = NXOpen.DisplayableObject.ObjectFont.Dashed
	    .NewLayer = 100
            .Apply(bodyArray)
            .Dispose()
        End With

    End Sub

    Sub Selectbody(ByRef selectedObjects As NXObject())

        Dim ui As UI = NXOpen.UI.GetUI

        Dim message As String = "Select body"
        Dim title As String = "Selection"

        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim keepHighlighted As Boolean = False
        Dim includeFeatures As Boolean = False
        Dim response As Selection.Response

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

        Dim selectionMask_array(1) As Selection.MaskTriple
	With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY 
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY 
        End With

        With selectionMask_array(1)
            .Type = UFConstants.UF_solid_type
            .Subtype = 0
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY 
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY 
	End with

        response = ui.SelectionManager.SelectObjects(message, title, scope, _
                                         selectionAction, includeFeatures, _
                                     keepHighlighted, selectionMask_array, _
                                                      selectedObjects)

        If response = Selection.Response.Cancel Or response = Selection.Response.Back Then
            Return
        End If

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = NXOpen.UF.UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function
End Module 

I receive an error in line:

CODE

referenceSet1.AddObjectsToReferenceSet(BodyArray) 

I tried many different ways but no one has worked. Any one can help me to solve this problem?

With best regards
Michael

RE: Add body to reference set

CODE

If refSetExists Then
    referenceSet1.AddObjectsToReferenceSet(BodyArray)
Else 

At this point in your code, the referenceSet1 variable doesn't actually refer to any reference set; I'd guess that you are getting a "null reference" error. It looks like ref_temp does hold the desired reference set, so you could use that variable or update the referenceSet1 as needed.

www.nxjournaling.com

RE: Add body to reference set

(OP)
Yes, I'm receiving an error:

CODE

System.NullReferenceException: Object reference not set to an instance of an object 

Like You write, I didn't have reference to referenceset1, so that's why the code doesn't worked. When I set:

CODE

referenceSet1 = ref_temp 

The code now works correct. Thanks @cowski for Your support.
I tried many ways to solve this, but didn't think it's that simple. I tried figure out what selectedObjectsArray return:

CODE

selectedObjectsArray.tostring -> NXOpen.NXObject[]
selectedObjectsArray.length -> number of elements
bodyarray.length -> number of elements
bodyarray.tostring -> NXOpen.DisplayableObject[] 

In my next step I would try something with tag object, but now I don't need to. Thanks again for Your help.

With best regards
Michael

RE: Add body to reference set

Hi All,

Interesting tool, tagging along on it...
I made the same correction as Cowski mentioned, however the error remains for me.

CODE --> VB

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports System.Collections
Imports System.Collections.Generic

Module Ref_Ambient

    Dim theSession As Session = Session.GetSession()
    Dim sel As Selection = NXOpen.UI.GetUI.SelectionManager 
    Dim myRefSetNames As New List(Of String)
    Dim ref_temp as ReferenceSet

    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display

    Sub Main()


	Dim referenceSet1 As ReferenceSet
        Dim selectedObjectsArray() As NXObject

        Selectbody(selectedObjectsArray)

        Dim BodyArray(selectedObjectsArray.Length - 1) As DisplayableObject

        For i As Integer = 0 To selectedObjectsArray.Length - 1
            BodyArray(i) = CType(selectedObjectsArray(i), DisplayableObject)

        Next

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

	myRefSetNames.Add("AMBIENT")

        For Each tempRefSetName As String In myRefSetNames
            Dim refSetExists As Boolean = False

            For Each tempRefSet As ReferenceSet In workPart.GetAllReferenceSets

                If tempRefSet.Name.ToUpper = tempRefSetName.ToUpper Then
                    refSetExists = True
                End If

		If tempRefSet.Name.ToUpper() = "AMBIENT" Then
			ref_temp = tempRefSet
			ref_temp.SetAddComponentsAutomatically(true, true)
		end if 	

            Next

            If refSetExists Then

		referenceSet1.AddObjectsToReferenceSet(BodyArray)

            Else

                referenceSet1 = ref_temp
                referenceSet1.SetName(tempRefSetName)
		referenceSet1.SetAddComponentsAutomatically(True, True)
		referenceSet1.AddObjectsToReferenceSet(BodyArray)
            End If

        Next

	myRefSetNames.clear()

        Dim changeBodytransluency As DisplayModification = theSession.DisplayManager.NewDisplayModification()

        With changeBodytransluency
            .ApplyToAllFaces = true
            .NewTranslucency = 0
	    .NewFont = NXOpen.DisplayableObject.ObjectFont.Solid
		.NewWidth = DisplayableObject.ObjectWidth.Two
	    .NewLayer = 190
            .Apply(bodyArray)
            .Dispose()
        End With

    End Sub

    Sub Selectbody(ByRef selectedObjects As NXObject())

        Dim ui As UI = NXOpen.UI.GetUI

        Dim message As String = "Select body"
        Dim title As String = "Selection"

        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim keepHighlighted As Boolean = False
        Dim includeFeatures As Boolean = False
        Dim response As Selection.Response

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

        Dim selectionMask_array(1) As Selection.MaskTriple
	With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY 
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY 
        End With

        With selectionMask_array(1)
            .Type = UFConstants.UF_solid_type
            .Subtype = 0
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY 
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY 
	End with

        response = ui.SelectionManager.SelectObjects(message, title, scope, _
                                         selectionAction, includeFeatures, _
                                     keepHighlighted, selectionMask_array, _
                                                      selectedObjects)

        If response = Selection.Response.Cancel Or response = Selection.Response.Back Then
            Return
        End If

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = NXOpen.UF.UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function
End Module 

Error comlains on line 65 in code which is;

CODE --> VB

referenceSet1 = ref_temp
Line 65 --->               referenceSet1.SetName(tempRefSetName) 

Ronald van den Broek
Senior Application Engineer
Winterthur Gas & Diesel Ltd
NX9 / TC10.1.2

Building new PLM environment from Scratch using NX11 / TC11

RE: Add body to reference set

(OP)
Hello,

I compared Your code with mine, and You don't have line to create reference set, which should be:

CODE

referenceSet1 = workPart.CreateReferenceSet() 

That's why You receive an error. I also added this

CODE

referenceSet1 = ref_temp 

before

CODE

If refSetExists Then 

so I'don't have to place it twice in code - first if ref. exists and second if not.

With best regards
Michael

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources