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 for exporting to part file

Status
Not open for further replies.

pittbuck

Computer
May 1, 2017
31
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!
 
Replies continue below

Recommended for you

The following thread has some example code for exporting selected bodies to a new part file. It may serve as a starting point for what you are trying to do...
thread561-391289

www.nxjournaling.com
 
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)
 
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
 
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:
[s][/s]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
 
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
 
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
 
GREAT! Thanks a lot cowski. This is exactly what I was looking for.
 
Hey Cowski,

Will a similar journal be needed if I wanted to export a part to a .stl file?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor