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!!
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