Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Exporting multiple bodies to a single STL file

Status
Not open for further replies.

pittbuck

Computer
May 1, 2017
31
Hey NX Fam!

I am trying to utilize this journal to grab all bodies that are on Layer 36 as a single STL file. The number of bodies will typically range from 2-8. When I run the journal below, it only grabs one of these bodies and it isn't totally consistent which one it is. Please send help!

Thanks in advance!!

Code:
' NX 8.0.0.25
' Journal created by zbuckler on Thu Aug 24 09:04:00 2017 Central Daylight Time
'
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports Microsoft.VisualBasic
Imports NXOpen
Imports NXOpen.UF
 
Module Module2
 
    Dim theSession As Session = Session.GetSession()
	Dim displayPart As Part = theSession.Parts.Display
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow
 
    Sub Main()
 
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If
 
        Dim workPart As Part = theSession.Parts.Work
        lw.Open()
		
		
		Const undoMarkName As String = "export solids to STL"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, undoMarkName)
 
        Dim theSolids As New List(Of Body)
		Dim currentFile as string
		currentFile = GetFileName()
		Dim currentPath2 as string = "\\Cagenixserver\Cagenix Data\Production" & "\" & currentFile & "\" & currentFile  & " APPROVAL"

	msgbox (currentFile)
	
		Try
			'Try to create the directory
			Dim di As DirectoryInfo = Directory.CreateDirectory(currentPath2)
			Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(currentPath2))
			
		Catch e As Exception
			Console.WriteLine("The process failed: {0}.", e.ToString())
		End Try
 
        'collect the solid bodies in the work part
        For Each temp As Body In workPart.Bodies
            If temp.IsSolidBody andAlso temp.Layer = 36 Then
                theSolids.Add(temp)
            End If
        Next
 
		Try
            ExportSTLFiles(currentPath2 & "\" & currentFile, theSolids, 0.01, 0.01)

        Catch ex As NXException
           lw.WriteLine("NX Error: " & ex.Message)
        Catch ex As Exception
           lw.WriteLine("Error: " & ex.Message)
        End Try
 
        lw.Close()
		
	' ----------------------------------------------
	'   Menu: File->Save
	' ----------------------------------------------
	Dim partSaveStatus5 As PartSaveStatus
	partSaveStatus5 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
	
	partSaveStatus5.Dispose()	
 
    End Sub
 
 '***********************************************************************

 
    Sub ExportSTLFiles(ByVal FileName As String, ByVal theObjects As List(Of Body), ByVal triangleTolerance As Double, ByVal adjacencyTolerance As Double)
 
        Dim NumErrors As Integer
        Dim InfoError() As UFStd.StlError
        Dim Header, FileBaseName As String
        Dim numNegated As Integer
        Dim Negated() As Tag
 
        Negated = Nothing
        InfoError = Nothing
		
		Dim parentFolder As String = IO.Path.GetDirectoryName(FileName)
		Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(FileName)
 
        FileName = IO.Path.ChangeExtension(GetFileName & " MINIMUM COPING SUBTRACTIONS", ".stl")
		
        For Each temp As Body In theObjects
		
 
           Dim FileHandle As IntPtr

 
           FileBaseName = IO.Path.GetFileName(FileName)
           Header = "Header: " & FileBaseName
 
           theUfSession.Std.OpenBinaryStlFile(IO.Path.Combine(parentFolder, FileName), False, Header, FileHandle)
		   
 
            theUfSession.Ui.SetPrompt("Creating file ... " & FileBaseName & " ...")
 
            theUfSession.Std.PutSolidInStlFile(FileHandle, Tag.Null, temp.Tag, 0.0, 0.0, triangleTolerance, NumErrors, InfoError)
 
            theUfSession.Std.CloseStlFile(FileHandle)
 
            theUfSession.Ui.SetStatus("File ... " & FileBaseName & " generated ...")
			
 
 
       Next
 

    End Sub
 
	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) - 13)
	
		GetFileName = strPart
	End Function
	
    
	Public Function GetUnloadOption(ByVal dummy As String) As Integer
 
        'Unloads the image immediately after execution within NX
       GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
	 
    End Function
 
End Module
 
Replies continue below

Recommended for you

If I recall correctly, at least in terms of what is exported from NX, an STL file can only include a single body. If you're attempting to stage multiple bodies so as to maximize the utilization of the build space of the 3D Printer, I thought that that sort of thing was done using the staging software provided by the 3D Printer vendor.

John R. Baker, P.E. (ret)
Irvine, CA
Siemens PLM:
UG/NX Museum:

The secret of life is not finding someone to live with
It's finding someone you can't live without
 
This code might help you. It will put all the solid bodies from the part into a single STL file. However, when you import the file, all the bodies will be treated as one. If you round-trip the file back to NX, the 'convergent' body option will only import a single body. The other options (NX or JT) will import all the bodies, but they will collectively be considered a single object.

www.nxjournaling.com
 
cowski,

Once again, you have directed me to the right code. I had some things "out of order" in my initial code, which is why only one body was being exported instead of all bodies. This helped a lot! Thank you!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor