Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Journal to export selected (multiple) bodies to temp part, and remove parameters

Status
Not open for further replies.

NetDog

Marine/Ocean
Joined
Jun 7, 2013
Messages
10
Location
GB
How can I modify this journal, to select multipe bodies to export to a temp part, as opposed it just selecting one body to export?

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
Sub Main

    Dim theSession As Session = Session.GetSession()
    Dim theUFSession As UFSession = UFSession.GetUFSession()

    Dim abody As Body = SelectABody("Export")

    Dim objects() as Tag = { abody.Tag }

    Dim options as UFPart.ExportOptions
        With options
            .expression_mode = UFPart.ExportExpMode.CopyExpShallowly
            .new_part = True
            .params_mode = UFPart.ExportParamsMode.RemoveParams
        End With

    theUFSession.Part.ExportWithOptions("C:\temp\6118251.prt", 1, objects, options)

End Sub

    Function SelectABody(ByVal prompt As String) As Body

        Dim ui As UI = GetUI()
        Dim mask(0) As Selection.MaskTriple
            With mask(0)
                .Type = UFConstants.UF_solid_type
                .Subtype = UFConstants.UF_solid_body_subtype
                .SolidBodySubtype = 0
            End With
        Dim obj As NXObject = Nothing
        Dim cursor As Point3d = Nothing

        ui.SelectionManager.SelectObject(prompt, "Select a body", _
            Selection.SelectionScope.AnyInAssembly, _
            Selection.SelectionAction.ClearAndEnableSpecific, _
            False, False, mask, obj, cursor)

        Return obj

    End Function

End Module

Thanks in advance!
 
It's for NX 8.5 cowski.

Thanks for your help
 
The following function will return an array of body objects. It isn't a direct replacement for the function that you have. You will need to make a few other changes in your code:
[ul]
[li]create an array variable of type "body" to hold the selected body object[/li]
[li]pass this array into the selection function[/li]
[li]extract the tag from each body into an array for input to the .ExportWithOptions method[/li]
[/ul]



Code:
Function SelectBodies(ByVal prompt As String, ByRef selObj() as TaggedObject) As Selection.Response

	Dim theUI as UI = UI.GetUI
	Dim title As String = "Select one or more bodies"
	Dim includeFeatures As Boolean = False
	Dim keepHighlighted As Boolean = False
	Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
	Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
	Dim selectionMask_array(0) As Selection.MaskTriple

	With selectionMask_array(0)
		.Type = UFConstants.UF_solid_type
		.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
	End With

	Dim resp as Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
		title, scope, selAction, _
		includeFeatures, keepHighlighted, selectionMask_array, _
		selobj)
	If resp = Selection.Response.Ok Then
		Return Selection.Response.Ok
	Else
		Return Selection.Response.Cancel
	End If

End Function

www.nxjournaling.com
 
Thanks , but I am still struggling with the other changes that are required, and where about to put them. :)

 
Warning: untested code ahead...

Code:
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUFSession As UFSession = UFSession.GetUFSession()

        Dim abody() As Body
        If SelectBodies("select bodies", abody) = Selection.Response.Cancel Then
            Return
        End If

        Dim bodyTags As New List(Of Tag)

        For Each temp As Body In abody
            bodyTags.Add(temp.Tag)
        Next

        Dim options As UFPart.ExportOptions
        With options
            .expression_mode = UFPart.ExportExpMode.CopyExpShallowly
            .new_part = True
            .params_mode = UFPart.ExportParamsMode.RemoveParams
        End With

        theUFSession.Part.ExportWithOptions("C:\temp\6118251.prt", 1, bodyTags.ToArray, options)

    End Sub

    Function SelectBodies(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select one or more bodies"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_solid_type
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selobj)
        If resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function
End Module

www.nxjournaling.com
 
Again, thank you for your help. Im sure it is close to working now.
I currently get a "casting" runtime error:

Code:
Runtime error:
System.InvalidCastException: Unable to cast object of type 'NXOpen.TaggedObject[]' to type 'NXOpen.Body[]'.
   at NXJournal.Main() in C:\Users\tempuser\AppData\Local\Temp\NXJournals6524\journal.vb:line 15

I am now googling to try and find out how to resolve the error..
 
Try this:

Code:
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI

Module NXJournal
    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUFSession As UFSession = UFSession.GetUFSession()

        Dim abody() As TaggedObject
        If SelectBodies("select bodies", abody) = Selection.Response.Cancel Then
            Return
        End If

        Dim bodyTags As New List(Of Tag)

        For Each temp As TaggedObject In abody
            bodyTags.Add(temp.Tag)
        Next

        Dim options As UFPart.ExportOptions
        With options
            .expression_mode = UFPart.ExportExpMode.CopyExpShallowly
            .new_part = True
            .params_mode = UFPart.ExportParamsMode.RemoveParams
        End With

        theUFSession.Part.ExportWithOptions("C:\temp\6118251.prt", 1, bodyTags.ToArray, options)

    End Sub

    Function SelectBodies(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select one or more bodies"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_solid_type
            .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selobj)
        If resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function
End Module

www.nxjournaling.com
 
Awesome work cowski!

It no longer throws errors.
However, when multiple bodies are selected, it only exports the first body selected, and fails to export the other bodies that were selected.

:)
 
Apparently, you have to tell the function how many items are in the array.

Code:
theUFSession.Part.ExportWithOptions("C:\temp\6118251.prt", [highlight #FCE94F]bodyTags.Count[/highlight], bodyTags.ToArray, options)

www.nxjournaling.com
 
Understood, but is there any way around this cowski? Otherwise we would need to count the bodies that we want to export before running the journal?

 
I gave you the fix in the post above. Edit the listed line in the journal; the required change has been highlighted.

www.nxjournaling.com
 
Sorry cowski, what I was meaning, was that in order to run the journal I would need to count up all the bodies that I wish to export into the temp part before running the journal.
Then I would enter the number of bodies and run the journal (4 bodies for example below)

Code:
theUFSession.Part.ExportWithOptions("C:\temp\Exported.prt", 4, bodyTags.ToArray, options)

Is there a way to do this without haveing to enter the exact number of bodies in the code before the journal is run?
 
Yes, I gave you the solution above. Find the "ExportWithOptions" line in the journal code timestamped: 15 Jul 15 14:53 and replace it with the new line posted at timestamp: 15 Jul 15 15:15.

Run the newly edited journal and it should work for any number of objects chosen.

www.nxjournaling.com
 
Ahh, sorry for being a muppet! That works just great!
Thank you very much for your help and patience!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top