pittbuck
Computer
- May 1, 2017
- 31
Hey everyone.
SO here's the goal: Have NX open a directory so that I can click on a folder and allow any and all STP file to be brought in to the file that I am presently working on. I have a journal for importing in STP files based on the name of the file and I have a journal that will open the a directory that will allow me to bring in STL files into it's own new part file. Below are the two respective journals. How do I need to combine these to have the STP files that I want to come in to the current? (we will sometimes have cases that either have multiple STP files or STP files with different names than what the journal presently looks for)
Pull in STL files into their own window to review:
Sorry for the weird spacing here. Literally just did a COPY-PASTE and it looked like this. This is the IMPORT STP files. I'll go ahead and let you guys know that the necessary DIM stuff is in this journal, but WAAAYYY before this part.
SO here's the goal: Have NX open a directory so that I can click on a folder and allow any and all STP file to be brought in to the file that I am presently working on. I have a journal for importing in STP files based on the name of the file and I have a journal that will open the a directory that will allow me to bring in STL files into it's own new part file. Below are the two respective journals. How do I need to combine these to have the STP files that I want to come in to the current? (we will sometimes have cases that either have multiple STP files or STP files with different names than what the journal presently looks for)
Pull in STL files into their own window to review:
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 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 nTotalStlFiles As Integer = 0
Sub Main()
'I am getting much of the naming info, including variable names, from the following journal:
''Copy Crowns (Z) Folder to Dropbox'
'That journal doesn't really get used anymore because I've moved that task to the
'MS Access environment
'This stuff is just to get the name of the root folder of the .prt we are currently in.
Dim fileName As String = IO.Path.GetFileName(workPart.FullPath)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim parentFolder As String = IO.Path.GetDirectoryName(workPart.FullPath)
Dim root As String = IO.Path.GetPathRoot(workPart.FullPath)
Dim OrderFolderC as String
Dim Position1 as Integer
Dim OrderFolder as String
Dim Position2 as Integer
Dim OrderName as String
'Finds last "\" in the parent folder directory path
Position1 = InStrRev(parentfolder, "\")
'Makes variable "OrderFolderC" contain path to the left of "\" = Cad Folder Directory
OrderFolderC = Left(workPart.FullPath, Position1 - 1)
'Finds last "\" in the Cad folder directory path
Position2 = InStrRev(OrderFolderC, "\")
'Makes variable "OrderFolder" contain path to the left of "\" = Root Order Folder Directory
OrderFolder = Left(OrderFolderC, Position2 - 1)
OrderName = Left(fileNameNoExt, Len(fileNameNoExt) - 6)
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 = OrderFolder
' 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("")
'The following code to open up the 'NEW_PART_TEMPLATE(NX8-STP)' is from 'STP Export, Review, and Import.vb'
' ----------------------------------------------
' Menu: File->Open...
' ----------------------------------------------
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = theSession.Parts.OpenBaseDisplay("\\Cagenixserver\Cagenix Data\Production Support Files\WORKING PRT FILES for FRAME Design\NEW_PART_TEMPLATE(NX8-STP).prt", partLoadStatus1)
'basePart1 = theSession.Parts.OpenBaseDisplay("G:\WORKING PRT FILES for FRAME Design\NEW_PART_TEMPLATE(NX8).prt", partLoadStatus1)
workPart = theSession.Parts.Work
displayPart = theSession.Parts.Display
partLoadStatus1.Dispose()
' ----------------------------------------------
' Menu: File->Save As...
' ----------------------------------------------
Dim partSaveStatus2 As PartSaveStatus
partSaveStatus2 = workPart.SaveAs(OrderFolder & "\" & OrderName & "C\" & OrderName & " STL Review.prt")
partSaveStatus2.Dispose()
processParts(strOutputFolder, False)
'LW.WriteLine("")
'LW.WriteLine("Total Part Files Scanned: " & nTotalStlFiles)
'LW.WriteLine("Stop Time: " & CType(TimeOfDay(), String))
'This last part comes from a journal I recorded called 'After stl files imported - adjusting view'
' ----------------------------------------------
' Menu: Orient View->Bottom
' ----------------------------------------------
'Had to add 'NXOpen' before view to get this to work, got the idea from:
'[URL unfurl="true"]http://www.eng-tips.com/viewthread.cfm?qid=399552[/URL]
workPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Bottom, NXOpen.View.ScaleAdjustment.Fit)
' ----------------------------------------------
' Menu: Rendering Style->Shaded with Edges
' ----------------------------------------------
workPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.ShadedWithEdges
' ----------------------------------------------
' Menu: File->Save
' ----------------------------------------------
Dim partSaveStatus3 As PartSaveStatus
partSaveStatus3 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
partSaveStatus3.Dispose()
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)
Dim nStlFiles As Integer = 0
Dim part1 As Part
Dim files() As String
If includeSubDirs Then
files = Directory.GetFiles(directoryPath, "*.stl", SearchOption.AllDirectories)
Else
files = Directory.GetFiles(directoryPath, "*.stl", SearchOption.TopDirectoryOnly)
End If
For Each fileName As String In files
nStlFiles += 1
nTotalStlFiles += 1
'LW.WriteLine(" " & nStlFiles & " " & Path.GetFileName(fileName))
If (IsNothing(initialPart)) OrElse (initialPart.FullPath <> fileName) Then
Try
part1 = theSession.Parts.OpenDisplay(fileName, Nothing)
Catch ex As NXException
'LW.WriteLine("open error: " & ex.Message)
Catch ex2 As Exception
LW.WriteLine("exception: " & ex2.Message)
End Try
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
'LW.WriteLine(" this part contains a total of " & CountBodies() & " solid and sheet bodies")
' ----------------------------------------------
' Menu: File->Import->STL...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim sTLImportBuilder1 As Facet.STLImportBuilder
sTLImportBuilder1 = workPart.FacetedBodies.CreateSTLImportBuilder()
sTLImportBuilder1.File = fileName
sTLImportBuilder1.AngularTolerance = Facet.STLImportBuilder.AngularToleranceTypes.Fine
sTLImportBuilder1.STLFileUnits = Facet.STLImportBuilder.STLFileUnitsTypes.Millimeters
theSession.SetUndoMarkName(markId1, "STL Import Dialog")
sTLImportBuilder1.File = fileName
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "STL Import")
Dim nXObject1 As NXObject
nXObject1 = sTLImportBuilder1.Commit()
theSession.DeleteUndoMark(markId2, Nothing)
'theSession.SetUndoMarkName(markId1, "STL Import")
sTLImportBuilder1.Destroy()
'close file unless this file was initially open
'If (IsNothing(initialPart)) OrElse (initialPart.FullPath <> fileName) Then
' part1.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
' part1 = Nothing
' workPart = Nothing
' displayPart = Nothing
'End If
REM If Not IsNothing(initialPart) Then
REM Dim partLoadStatus1 As PartLoadStatus
REM Dim status1 As PartCollection.SdpsStatus
REM status1 = theSession.Parts.SetDisplay(initialPart, False, False, partLoadStatus1)
REM displayPart = theSession.Parts.Display
REM partLoadStatus1.Dispose()
REM theSession.Parts.SetWork(displayPart)
REM workPart = theSession.Parts.Work
REM End If
Next fileName
End Sub
'***************************************************************************
Function CountBodies() As Integer
Dim total As Integer
total = workPart.Bodies.ToArray.Length
Return total
End Function
'***********************************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
End Function
'***********************************************************************
'Below are common functions I've used in the past and copied from another journal
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
'***********************************************************************
Function GetFileName2()
Dim strPath2 as String
Dim strPart2 as String
Dim pos as Integer
'get the full file path
strPath2 = displayPart.fullpath
'get the part file name
pos = InStrRev(strPath2, "\")
strPart2 = Mid(strPath2, pos + 1)
strPath2 = Left(strPath2, pos)
'strip off the ".prt" extension
strPart2 = Left(strPart2, Len(strPart2) - 15)
GetFileName2 = strPart2
End Function
'***********************************************************************
End Module
Sorry for the weird spacing here. Literally just did a COPY-PASTE and it looked like this. This is the IMPORT STP files. I'll go ahead and let you guys know that the necessary DIM stuff is in this journal, but WAAAYYY before this part.
Code:
' ----------------------------------------------
' Menu: File->Import->STEP214...
' ----------------------------------------------
'*******************************************
'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()
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim step214Importer1 As Step214Importer
step214Importer1 = theSession.DexManager.CreateStep214Importer()
step214Importer1.SimplifyGeometry = True
step214Importer1.LayerDefault = 1
step214Importer1.InputFile = CurrentPath & "\" & CurrentFile & " DWUCB.stp:"
step214Importer1.SettingsFile = "C:\Program Files (x86)\Siemens\NX 8.0\step214ug\step214ug.def"
step214Importer1.ObjectTypes.Curves = True
step214Importer1.ObjectTypes.Surfaces = True
step214Importer1.ObjectTypes.Solids = True
step214Importer1.LayerDefault = 1
step214Importer1.InputFile = ""
theSession.SetUndoMarkName(markId1, "Import from STEP214 Options Dialog")
step214Importer1.SewSurfaces = True
step214Importer1.InputFile = CurrentPath & "\" & CurrentFile & " DWUCB.stp:"
step214Importer1.OutputFile = CurrentPath & "\" & CurrentFile & " DWUCB.stp:"
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Import from STEP214 Options")
step214Importer1.FileOpenFlag = False
Dim nXObject1 As NXObject
nXObject1 = step214Importer1.Commit()
theSession.DeleteUndoMark(markId2, Nothing)
theSession.SetUndoMarkName(markId1, "Import from STEP214 Options")
step214Importer1.Destroy()
'*************************************
'Following code makes STP Bodies on layer 1 medium purple and translucent
'Declaring variables for recoloring bodies on layer 1
Dim allObjects as NXObject()
Dim ColorObject as Body
allObjects = workPart.Layers.GetAllObjectsOnLayer(1)
for each someObject as NXObject in allObjects
if someObject.GetType.ToString = "NXOpen.Body" then
ColorObject = someObject
ColorObject.Color = 127
ColorObject.RedisplayObject
end if
next
'*************************************************************************
'Moving Objects from layer 1 to 100
'*************************************************************************
'Declaring variables for moving objects to 100
Dim MoveObjects as NXObject()
Dim myMoveBodies as DisplayableObject()
Dim i as integer = 0
'displayModification for Moving objects
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
MoveObjects = workPart.Layers.GetAllObjectsOnLayer(1)
for each someObject as NXObject in MoveObjects
if someObject.GetType.ToString = "NXOpen.Body" then
redim preserve myMoveBodies(i)
myMoveBodies(i) = someObject
i += 1
end if
next
With displayModification1
'move layers with a display modification
.NewLayer = 180
.Apply(myMoveBodies)
.Dispose
End With
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) - 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