Exporting Object to STL file; Journal
Exporting Object to STL file; Journal
(OP)
I am trying to write a journal that will export a single object from a specified layer to a .stl file. I've tried to "rewrite" the journal that is on the following website, but it doesn't seem to help. Maybe I am re-writing the code wrong (very likely).
http://www.nxjournaling.com/content/batch-export-p...
This file needs to go to a specified place and needs a specified name, but I'll worry about that later. If this file goes into the TEMP folder on my computer for now, that's fine. When export the object to an STL file, the following has to be in the "rapid prototyping" window:
.
Thanks in advance for any advice.
http://www.nxjournaling.com/content/batch-export-p...
This file needs to go to a specified place and needs a specified name, but I'll worry about that later. If this file goes into the TEMP folder on my computer for now, that's fine. When export the object to an STL file, the following has to be in the "rapid prototyping" window:
.Thanks in advance for any advice.





RE: Exporting Object to STL file; Journal
CODE
The code, as-is, creates the STL file in the same folder as the work part; but you can pass in the desired file path:
CODE
www.nxjournaling.com
RE: Exporting Object to STL file; Journal
Something still isn't working. It keeps giving me the "declaration expected" error in a variety of places. Here is the code currently.
CODE -->
Option Strict Off Imports System Imports System.IO Imports System.Collections.Generic Imports NXOpen Imports NXOpen.UI Imports NXOpen.UF Module NXJournal Dim theSession As Session = Session.GetSession() Dim workPart As Part = theSession.Parts.Work Dim displayPart As Part = theSession.Parts.Display Dim theUFSession As UFSession = UFSession.GetUFSession Const undoMarkName As String = "export solids to STL" Dim markId1 As Session.UndoMarkId markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, undoMarkName) Dim layerExport as NXObject() Dim myExportObjects as DisplayableObject() Dim l as Integer = 0 layerExport = workPart.Layers.GetAllObjectsOnLayer(195) for each someObject as NXObject in layerExport if someObject.GetType.ToString = "NXOpen.Body" then redim preserve myExportObjects(l) myExportObjects(l) = someObject l += 1 end if '******************************************* 'Following code is used to get the current file name and path for approval file Dim currentPath as string Dim currentFile as string 'currentFile = GetFilePath() & GetFileName() & ".prt" currentPath = GetFilePath() currentFile = GetFileName() Try ExportSTL(currentPath & "\" & currentFile & " Overlay", myExportObjects, 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() 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 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 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 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 ModuleRE: Exporting Object to STL file; Journal
www.nxjournaling.com
RE: Exporting Object to STL file; Journal
CODE -->
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 SubThere are a couple of small errors that come up, but I think I can figure those out. I'll post something here otherwise.
RE: Exporting Object to STL file; Journal
www.nxjournaling.com
RE: Exporting Object to STL file; Journal
First, I wanted to thank you for all the help you have given me. Things got a bit busy over here and I haven't been able to adjust the journal until recently. So because I had made so many adjustments to the code and wasn't getting anywhere, I started over. Now I'm on the right track again. With the code the way it is now, it takes all bodies that are in a particular part file and makes separate STL files for all of them. How do I get the journal select just the bodies on a particular layer?
CODE -->
Option Strict Off Imports System Imports System.Collections.Generic Imports NXOpen Imports NXOpen.UF Module Module2 Dim theSession As Session = Session.GetSession() 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 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) '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 ExportSTLFiles(workPart.FullPath, 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() 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 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(FileName, ".stl") Dim suffix As Integer = 1 For Each temp As Body In theObjects Dim FileHandle As IntPtr Dim fileNameWithSuffix As String = fileNameNoExt & "_" & suffix.ToString & ".stl" FileBaseName = IO.Path.GetFileName(FileName) Header = "Header: " & FileBaseName theUfSession.Std.OpenBinaryStlFile(IO.Path.Combine(parentFolder, fileNameWithSuffix), 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 ...") suffix += 1 Next End Sub '************************************************************* 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 ModuleThanks again!!
RE: Exporting Object to STL file; Journal
CODE
If temp.IsSolidBody andAlso temp.Layer = 1 Then theSolids.Add(temp) End IfChange the layer number as needed for your situation.
The above works well if you are interested in a single layer. If you need to get the solids from several layers, one strategy is to create a list(of integer) to keep track of the layers you want to process; then in the "If" block, check to see if the body's layer is contained in the list.
CODE
dim layersToProcess as new list(of integer) layersToProcess.Add(1) layersToProcess.Add(2) layersToProcess.Add(7) layersToProcess.Add(11) layersToProcess.Add(97) . . . If temp.IsSolidBody andAlso layersToProcess.Contains(temp.Layer) Then theSolids.Add(temp) End Ifwww.nxjournaling.com
RE: Exporting Object to STL file; Journal
The first one is exactly what I needed. Thanks! As I mentioned before, I am still new to Journaling and just didn't know how to do something that is clearly a simple change.
Now onto a question that may be a lot more complicated. How do I move the new STL file as follows:
Folder A
-->Folder B
----->Folder C
"new STL file"
The goal is to not only move new STL file outside of Folder C and Folder B to inside Folder A and create a new folder labeled as "Folder D". so it is more like this:
Folder A
-->Folder D
"new STL file"
Now, if creating a folder is something that isn't exactly something that can be created with a journal, that is ok. If I need to create folder D before running the journal, that is fine.
Hope all of this makes sense. Thanks in advance!
RE: Exporting Object to STL file; Journal
https://msdn.microsoft.com/en-us/library/54a0at6s(...
www.nxjournaling.com