Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Export multiple sheet bodies from feature group, in batch, to .stl

Status
Not open for further replies.

miggyneer

Aerospace
May 11, 2023
4
Hello,

I am trying to export multiple numbered (I renamed them from 1-40 in the feature tree) sheet bodies (they are all in their own feature group) to .stl files. Ideally, keeping an organized naming structure consisting of 2 parameters: [Part Name]_[Number 1 to 40]

I have recorded a journal to try and understand how this is done, but I can't seem to figure out how to do this in a more elegant way. I am a novice when it comes to coding in general; Any help would be appreciated!

Code:
' NX 1973
' Journal created by u8206368 on Wed May 17 10:25:59 2023 Eastern Summer Time
'
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String) 

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work

Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
'   Menu: File->Export->STL...
' ----------------------------------------------
Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim sTLCreator1 As NXOpen.STLCreator = Nothing
sTLCreator1 = theSession.DexManager.CreateStlCreator()

sTLCreator1.AutoNormalGen = True

sTLCreator1.ChordalTol = 0.0030000000000000001

sTLCreator1.AdjacencyTol = 0.0030000000000000001

theSession.SetUndoMarkName(markId1, "STL Export Dialog")

Dim body1 As NXOpen.Body = CType(workPart.Bodies.FindObject("SPLIT BODY(5)1"), NXOpen.Body)

Dim added1 As Boolean = Nothing
added1 = sTLCreator1.ExportSelectionBlock.Add(body1)

Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "STL Export")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As NXOpen.Session.UndoMarkId = Nothing
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "STL Export")

sTLCreator1.OutputFile = "C:\Users\u8206368\Desktop\GOMScans\FixedVanesSTLs\SplitGOMScanSections\S8_RotatedBlade_1.stl"

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = sTLCreator1.Commit()

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "STL Export")

sTLCreator1.Destroy()

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module
 
Replies continue below

Recommended for you

That thread is closed. It is also for combining bodies into one .stl, I need to export split sheet bodies from a .prt file with an .stl assembly imported, and then separate them and save them as an individual file which is numbered 1-40 (I can do this manually, but would be nice).

This code works for solid bodies, but I need it to work with sheet bodies.

Can someone here help with that?

Here is the code:

Code:
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
 
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF
 
Module Cycle_Files_and_Folders_b
    Dim theSession As Session = Session.GetSession
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim LW As ListingWindow = theSession.ListingWindow
 
    Dim workPart As Part = theSession.Parts.Work
    Dim displayPart As Part = theSession.Parts.Display
    Dim initialPart As Part = theSession.Parts.Display
 
    Dim nTotalPartFiles As Integer = 0
 
    Sub Main()
 
        Dim strOutputFolder As String
        LW.Open()
        Try
 
            Dim FolderBrowserDialog1 As New FolderBrowserDialog
            ' Change the .SelectedPath property to the default location
            With FolderBrowserDialog1
                ' Desktop is the root folder in the dialog.
                .RootFolder = Environment.SpecialFolder.Desktop
                ' Change the following line to default to a given path
                .SelectedPath = "C:\"
                ' Prompt the user with a custom message.
                .Description = "Select the directory to scan"
                If .ShowDialog = DialogResult.OK Then
                    ' Display the selected folder if the user clicked on the OK button.
                    'msgbox(.SelectedPath)
                    strOutputFolder = .SelectedPath
                Else
                    'user pressed "cancel", exit the journal
                    Exit Sub
                End If
            End With
 
            LW.WriteLine("Cycle All Parts in a Folder Tree")
            LW.WriteLine("Start Time: " & CType(TimeOfDay(), String))
            LW.WriteLine("")
 
            processParts(strOutputFolder, False)
 
            LW.WriteLine("")
            LW.WriteLine("Total Part Files Scanned: " & nTotalPartFiles)
            LW.WriteLine("Stop Time: " & CType(TimeOfDay(), String))
 
        Catch ex As NXException
            LW.WriteLine("Cycle Files and Folders Error: " & ex.Message)
            Exit Sub
        End Try
    End Sub
 
    '***************************************************************************
    'Process All Parts in a Directory
 
    Sub processParts(ByVal directoryPath As String, ByVal includeSubDirs As Boolean)
 
        Try
            Dim nPartFiles As Integer = 0
            Dim part1 As Part
            Dim files() As String
 
            If includeSubDirs Then
                files = Directory.GetFiles(directoryPath, "*.prt", SearchOption.AllDirectories)
            Else
                files = Directory.GetFiles(directoryPath, "*.prt", SearchOption.TopDirectoryOnly)
            End If
            For Each fileName As String In files
                nPartFiles += 1
                nTotalPartFiles += 1
                LW.WriteLine("   " & nPartFiles & " " & Path.GetFileName(fileName))
 
                If (IsNothing(initialPart)) OrElse (initialPart.FullPath <> fileName) Then
                    part1 = theSession.Parts.OpenDisplay(fileName, Nothing)
                Else
                    'LW.WriteLine("initial part equals display part: " & initialPart.Equals(displayPart).ToString)
                    part1 = displayPart
                End If
 
                displayPart = theSession.Parts.Display
                workPart = theSession.Parts.Display
 
                'do something
                'write your own subroutines and/or functions to process the part and call them from here
 
		    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)
 
	          'collect the solid bodies in the work part
	          For Each temp As Body In workPart.Bodies
                If temp.IsSolidBody Then
    	            theSolids.Add(temp)
                End If
        	    Next
 
	          Try
        	    	ExportSTL(workPart.FullPath, theSolids, 0.003, 0.003)
        	    Catch ex As NXException
            	LW.WriteLine("NX Error: " & ex.Message)
        	    Catch ex As Exception
            	LW.WriteLine("Error: " & ex.Message)
        	    End Try                
 
                'close file unless this file was initially open
                If (IsNothing(initialPart)) OrElse (initialPart.FullPath <> fileName) Then
                    'part1.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.True)
                    part1.Close(BasePart.CloseWholeTree.False, BasePart.CloseModified.UseResponses, Nothing)
                    part1 = Nothing
                    workPart = Nothing
                    displayPart = Nothing
                End If
 
                If Not IsNothing(initialPart) Then
                    Dim partLoadStatus1 As PartLoadStatus
                    Dim status1 As PartCollection.SdpsStatus
                    status1 = theSession.Parts.SetDisplay(initialPart, False, False, partLoadStatus1)
 
                    displayPart = theSession.Parts.Display
                    partLoadStatus1.Dispose()
                    theSession.Parts.SetWork(displayPart)
                    workPart = theSession.Parts.Work
                End If
 
            Next fileName
        Catch ex As Exception
            LW.WriteLine("Part Scan Error: " & ex.Message)
        End Try
 
    End Sub
 
    '***************************************************************************
    Sub ExportSTL(ByVal FileName As String, ByVal theObjects As List(Of Body), ByVal triangleTolerance As Double, ByVal adjacencyTolerance As Double)
 
        Dim NumErrors As Integer
        Dim FileHandle As IntPtr
        Dim InfoError() As UFStd.StlError
        Dim Header, FileBaseName As String
        'Dim numNegated As Integer
        'Dim Negated() As Tag
 
        'Negated = Nothing
        InfoError = Nothing
 
        FileName = IO.Path.ChangeExtension(FileName, ".stl")
 
        FileBaseName = IO.Path.GetFileName(FileName)
        Header = "Header: " & FileBaseName
 
        theUfSession.Std.OpenBinaryStlFile(FileName, False, Header, FileHandle)
 
        theUfSession.Ui.SetPrompt("Creating file ... " & FileBaseName & " ...")
 
        For Each temp As Body In theObjects
            If temp.IsSolidBody Then
                theUfSession.Std.PutSolidInStlFile(FileHandle, Tag.Null, temp.Tag, 0.0, 0.0, triangleTolerance, NumErrors, InfoError)
            End If
        Next
 
        theUfSession.Std.CloseStlFile(FileHandle)
 
        theUfSession.Ui.SetStatus("File ... " & FileBaseName & " generated ...")
 
    End Sub
 
    '***********************************************************************
 
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
 
        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
 
    End Function
 
End Module
 
Hello,

this week I haven't access to NX and I can't check it, but I suggest change:
1. line 108: IsSolidBody to IsSheetBody
2. line 171: like above
3. line 172 PutSolidInStlFile to PutSheetsInStlFile with the right parameters.

Best regards

MANok
NX2027
TC14
 
miggyneer said:
I am trying to export multiple numbered (I renamed them from 1-40 in the feature tree) sheet bodies (they are all in their own feature group) to .stl files.

Are these unparameterized bodies (they show up as "Body (n)" in the feature tree)? Have you renamed the feature or the sheet body object itself?

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor