×
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

Journal for exporting to part file

Journal for exporting to part file

Journal for exporting to part file

(OP)
Looking to make an addition to a journal. Needing a portion of the journal to export all objects on a specific layer to a part file. This will be imported later, but I already have the code to do that. The number of objects is varied (and can be zero at times). Any help or link to the right code would be awesome. Thanks!

RE: Journal for exporting to part file

(OP)
cowski,

The code is helping a lot. I'm doing some editing to it and came across a snag. The original files come from a certain folder number, labeled as xxxx-xxxx. I want these new part files to be saved in their respective folders as "xxxx-xxxx new part". How do I need to adjust or add to this line so that I don't have to manually move them from 'temp' (or any other location)?:

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

RE: Journal for exporting to part file

(OP)
I'm currently using NX 8

RE: Journal for exporting to part file

This article shows how to write some information about an NX file to a text file. While the focus of the article is dealing with text files, the example code shows how to get the full path of the current work part, the file name without the ".prt" extension, the parent folder, etc. By using these commands (and others like them), you can specify whatever path and file name you want for your new file.

www.nxjournaling.com

RE: Journal for exporting to part file

(OP)
Cowski,

It's been a while since I've posted, but the code that I have now is working great! Only thing that would make it better is if there was a way to have the journal automatically select all of the bodies on a certain layer (there will be no bodies sometimes) instead of having to select the bodies. Any advise?

CODE -->

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

Module NXJournal
Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()
Dim currentPath as string
Dim currentFile as string
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display

Sub Main()
	
		currentPath = GetFilePath()
		currentFile = GetFileName()
		
        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(currentPath & "\" & currentFile & " crowns-overlay", bodyTags.Count, 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


Function GetFileName()
	Dim strPath as String
	Dim strPart as String
	Dim pos as Integer
	
	'get the full file path
	strPath = displayPart.fullpath
	'get the part file name
	pos = InStrRev(strPath, "\")
	strPart = Mid(strPath, pos + 1)
	
	strPath = Left(strPath, pos)
	'strip off the ".prt" extension
	strPart = Left(strPart, Len(strPart) - 4)
	
	GetFileName = strPart
End Function
'***********************************************************************

Function GetFilePath()
	Dim strPath as String
	Dim strPart as String
	Dim pos as Integer
	Dim pos2 as Integer
	
	'get the full file path
	strPath = displayPart.fullpath
	'get the part file name
	pos = InStrRev(strPath, "\")
	strPart = Mid(strPath, pos + 1)
	
	strPath = Left(strPath, pos - 1)
	'strip off the ".prt" extension
	strPart = Left(strPart, Len(strPart) - 4)

	'pos2 = InStrRev(strPath, "\")
	'strPath = Left(strPath, pos2)

	
	GetFilePath = strPath
End Function

End Module 

RE: Journal for exporting to part file

(OP)
So I believe that the journal is recognizing that it needs to select all bodies on the specified layer, but the "exporting with options" line is no longer working. My guess is that now, instead of referring to a list of "tags", it is referring to a list of "bodies". How does the line need to be adjusted? (the error that shows up is "Line 40: Value of type '1-dimensional arry of NXOpen.Body' cannot be converted to '1-dimensional array of NXOpen.Tag' because 'NXOpen.Body is not derived from 'NXOpen.Tag'.")

CODE -->

Option Strict Off
Imports System 'x
Imports System.Collections.Generic 'x
Imports NXOpen 'x
Imports NXOpen.UF 'x 
Imports NXOpen.UI 'x

Module NXJournal
Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()
Dim currentPath as string
Dim currentFile as string
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display

Sub Main()
	
		currentPath = GetFilePath()
		currentFile = GetFileName()
		
        Dim abody() As TaggedObject
		Dim overlayExport As Integer = 190
		Dim bodiesOnExportLayer As New List (Of Body)
		
        For Each temp As Body In workPart.Bodies
			If temp.IsSolidBody Then
				If temp.Layer = overlayExport Then
						bodiesOnExportLayer.Add(temp)
				End If
			End If
		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(currentPath & "\" & currentFile & " crowns-overlay", bodiesOnExportLayer.Count, bodiesOnExportLayer.ToArray, options) 

    End Sub

    Function SelectBodies(ByVal prompt As String, ByRef selObj() As Body) 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


Function GetFileName()
	Dim strPath as String
	Dim strPart as String
	Dim pos as Integer
	
	'get the full file path
	strPath = displayPart.fullpath
	'get the part file name
	pos = InStrRev(strPath, "\")
	strPart = Mid(strPath, pos + 1)
	
	strPath = Left(strPath, pos)
	'strip off the ".prt" extension
	strPart = Left(strPart, Len(strPart) - 4)
	
	GetFileName = strPart
End Function
'***********************************************************************

Function GetFilePath()
	Dim strPath as String
	Dim strPart as String
	Dim pos as Integer
	Dim pos2 as Integer
	
	'get the full file path
	strPath = displayPart.fullpath
	'get the part file name
	pos = InStrRev(strPath, "\")
	strPart = Mid(strPath, pos + 1)
	
	strPath = Left(strPath, pos - 1)
	'strip off the ".prt" extension
	strPart = Left(strPart, Len(strPart) - 4)

	'pos2 = InStrRev(strPath, "\")
	'strPath = Left(strPath, pos2)

	
	GetFilePath = strPath
End Function

End Module 

RE: Journal for exporting to part file

If you won't be needing the references to the body objects for other reasons, you could modify the list to hold the body tags instead of the body objects. Later in the code, when you use the .ToArray method, it will result in the needed array of tags instead of bodies.

CODE

Dim bodiesOnExportLayer As New List (Of Tag)

For Each temp As Body In workPart.Bodies
    If temp.IsSolidBody Then
        If temp.Layer = overlayExport Then
            bodiesOnExportLayer.Add(temp.Tag)
        End If
    End If
Next 

www.nxjournaling.com

RE: Journal for exporting to part file

(OP)
GREAT! Thanks a lot cowski. This is exactly what I was looking for.

RE: Journal for exporting to part file

(OP)
Hey Cowski,

Will a similar journal be needed if I wanted to export a part to a .stl file?

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