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!

NX Save As Journal

Status
Not open for further replies.

cmick31

Mechanical
Jun 1, 2021
19
Hello all,

Looking for some guidance on my journal. The purpose of this journal is to take an assembly, and save as all components & assembly file to specific numbers in a new folder being pulled from an excel sheet. I have this working correctly, however instead of doing a save as to each of the components, it does multiple save as' to the assembly file itself and ignores the components to leave the same. Could anyone review this and let me know where I'm going wrong to get it to save through the components instead?

I'm quite novice with coding, so this journal may not be done in the best way or not be the cleanest.





Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.Features
Imports System.Windows.Forms
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Collections.Generic

Module Main
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow
Dim NXMessageBox As NXMessageBox = NXOpen.UI.GetUI().NXMessageBox
Dim Folder As String = Nothing
Dim workPart As Part = s.Parts.Work
Dim PartList As List(Of Part) = New List(Of Part)

Sub Main()

Dim basePart As BasePart = s.Parts.BaseWork

If basePart Is Nothing Then
NXMessageBox.Show("", NXMessageBox.DialogType.Error, "No Work Part")
Return
End If

Do_The_Job()


End Sub
Sub Do_The_Job()

Const excelFileName As String = "U:\refexcel.xlsx"



'create Excel object
Dim objExcel = CreateObject("Excel.Application")

Dim objWorkbook = objExcel.Workbooks.Open(excelFileName)

Dim dp As Part = s.Parts.Display

Dim c As ComponentAssembly = s.Parts.Display.ComponentAssembly
Scan(c.RootComponent, 0)

' Add the assembly head or the unit part in the list to be processed(version 1.1)
If Not PartList.Contains(dp) Then PartList.Add(dp)

ufs.Ui.SetStatus("Number of Shares Found= " & PartList.Count)

' ----------------------------------------------
'Save Path Request
' ----------------------------------------------
lw.Open()

Dim FolderBrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()

FolderBrowserDialog.SelectedPath = "C:"
FolderBrowserDialog.Description = "Select the Save As Folder"

If (FolderBrowserDialog.ShowDialog() <> DialogResult.OK) Then Return ' Exit if the user has not indicated anything
Folder = FolderBrowserDialog.SelectedPath

lw.WriteLine("Chosen Directory is :" & Folder)


' ----------------------------------------------
'Save Path Request
' ----------------------------------------------




If (FolderBrowserDialog.ShowDialog() <> DialogResult.OK) Then Return ' Exit if the user has not indicated anything
Folder = FolderBrowserDialog.SelectedPath

'lw.WriteLine("Chosen Directory is :" & Folder)
Dim loadStatus As PartLoadStatus = Nothing







For Each part As Part In PartList
'Echo(vbCrLf & "Treatment Of : " & part.FullPath)
'Echo("")
s.Parts.SetDisplay(part, False, True, loadStatus)
Dim numeroProjet As String
Dim X as Long = 1
Dim y as Long = 3

Do Until X>25

Dim myString as String = (objExcel.Cells(X,Y)).value.ToString()

X = X + 1


numeroProjet = myString

' ----------------------------------------------
' Save As...
' ----------------------------------------------
Dim partSaveStatus1 As PartSaveStatus


Try
partSaveStatus1 = s.Parts.Work.SaveAs(Folder+"\"+numeroProjet+".prt")
partSaveStatus1.Dispose()

Catch ex as Exception



End Try

Loop


Next

' Returns Main Assembly to Display Part
s.Parts.SetDisplay(dp, False, True, loadStatus)

End Sub

Public Sub Scan(ByVal component As Component, ByVal niveau As Integer)
' this subroutine recursively scans the assembly
' it does not put in the list, the deleted and unopened files
Try
Dim part As Part = CType(component.Prototype, Part)
Dim enfants As Component() = component.GetChildren()

If Not PartList.Contains(part) Then
PartList.Add(part)
For Each comp As Component In enfants
Scan(comp, niveau + 1)
Next
End If
Catch ex As Exception

End Try
End Sub

Public Sub Echo(ByVal output As String)
lw.Open()
lw.WriteLine(output)
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor