Exporting PDFs of each part in an ASM?
Exporting PDFs of each part in an ASM?
(OP)
Hi everybody,
Can someone tell me how I can export automatically all drawings from each part
in ASM and Sub ASMs into defined folder out of TC?
Thank you in forward for the help!
br
BB
Can someone tell me how I can export automatically all drawings from each part
in ASM and Sub ASMs into defined folder out of TC?
Thank you in forward for the help!
br
BB





RE: Exporting PDFs of each part in an ASM?
Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module NXJournal
Public theSession As Session = Session.GetSession()
Public ufs As UFSession = UFSession.GetUFSession()
Public lw As ListingWindow = theSession.ListingWindow
Sub Main()
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
theSession.Preferences.ScreenVisualization.TriadVisibility = 0
'*******************************************************
' TO get the folder name for jpgs
'******************************************************
Dim folderName As String = "C:"
Dim folderBrowserDialog1 As New FolderBrowserDialog
With folderBrowserDialog1
.Description = "Specify folder for screenshot output"
.ShowNewFolderButton = False
.RootFolder = Environment.SpecialFolder.Desktop
'use folderName as default directory
.SelectedPath = folderName
End With
Dim result As DialogResult = folderBrowserDialog1.ShowDialog()
If (result = DialogResult.OK) Then
folderName = folderBrowserDialog1.SelectedPath
Else
'user pressed cancel, exit the journal
Exit Sub
End If
'**********************************************
'**********************************************
'lw.Open
Try
Dim c As ComponentAssembly = dispPart.ComponentAssembly
'to process the work part rather than the display part,
' comment the previous line and uncomment the following line
'Dim c As ComponentAssembly = workPart.ComponentAssembly
if not IsNothing(c.RootComponent) then
'*** insert code to process 'root component' (assembly file)
'lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
'lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name)
'lw.WriteLine(folderName & "\" & c.RootComponent.DisplayName)
'*** end of code to process root component
Dim nullAssemblies_Component As Assemblies.Component = Nothing
Dim partLoadStatus3 As PartLoadStatus
theSession.Parts.SetWorkComponent(nullAssemblies_Component, partLoadStatus3)
workPart = theSession.Parts.Work
partLoadStatus3.Dispose()
dispPart.ModelingViews.WorkView.Fit()
dispPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Isometric, NXOpen.View.ScaleAdjustment.Fit)
dispPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.ShadedWithEdges
dispPart.Preferences.NamesBorderVisualization.ShowModelViewNames = False
theSession.Preferences.ScreenVisualization.TriadVisibility = 0
For i As Integer = 2 To 256
Dim stateArray1(0) As Layer.StateInfo
stateArray1(0) = New Layer.StateInfo(i, Layer.State.Hidden)
dispPart.Layers.ChangeStates(stateArray1, False)
Next
Dim strPartJpg As String = ""
strPartJpg = Path.GetFileNameWithoutExtension(dispPart.FullPath) & ".jpg"
strPartJpg = Path.Combine(folderName, strPartJpg)
ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.White)
ReportComponentChildren(c.RootComponent, 0, folderName, theSession)
else
'*** insert code to process piece part
'lw.WriteLine("Part has no components")
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
'lw.Close
End Sub
'**********************************************************
Sub reportComponentChildren( ByVal comp As Component, _
ByVal indent As Integer, ByVal fName As String, ByVal theSes As Session)
For Each child As Component In comp.GetChildren()
'*** insert code to process component or subassembly
'lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
'lw.WriteLine(New String(" ", indent * 2) & fName & "\" & child.DisplayName())
'****************************************************
Dim Part1 As Part = CType(theSes.Parts.FindObject(child.DisplayName()), Part)
Dim partLoadStatus1 As PartLoadStatus
Dim status1 As PartCollection.SdpsStatus
status1 = theSes.Parts.SetDisplay(Part1, True, True, partLoadStatus1)
Dim workPart As Part = theSes.Parts.Work
Dim displayPart As Part = theSes.Parts.Display
workPart = theSes.Parts.Work
displayPart = theSes.Parts.Display
partLoadStatus1.Dispose()
displayPart.ModelingViews.WorkView.Fit()
displayPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Isometric, NXOpen.View.ScaleAdjustment.Fit)
displayPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.ShadedWithEdges
displayPart.Preferences.NamesBorderVisualization.ShowModelViewNames = False
theSes.Preferences.ScreenVisualization.TriadVisibility = 0
For i As Integer = 2 To 256
Dim stateArray1(0) As Layer.StateInfo
stateArray1(0) = New Layer.StateInfo(i, Layer.State.Hidden)
displayPart.Layers.ChangeStates(stateArray1, False)
Next
Dim strPartJpg As String = ""
strPartJpg = Path.GetFileNameWithoutExtension(displayPart.FullPath) & ".jpg"
strPartJpg = Path.Combine(fName, strPartJpg)
ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.White)
'Dim markId As Session.UndoMarkId
'markId = theSes.SetUndoMark(Session.MarkVisibility.Visible, "Display Parent")
'************************************
'*** end of code to process component or subassembly
if child.GetChildren.Length <> 0 then
'*** this is a subassembly, add code specific to subassemblies
'lw.WriteLine(New String(" ", indent * 2) & _
'"* subassembly with " & _
' child.GetChildren.Length & " components")
'lw.WriteLine(New String(" ", indent * 2) & _
'" + Active Arrangement: " & _
'child.OwningPart.ComponentAssembly.ActiveArrangement.Name)
'*** end of code to process subassembly
'lw.WriteLine(New String(" ", indent * 2) & fName & "\" & child.DisplayName())
else
'this component has no children (it is a leaf node)
'add any code specific to bottom level components
'lw.WriteLine(New String(" ", indent * 2) & fName & "\" & child.DisplayName())
end if
reportComponentChildren(child, indent + 1, fName, theSes)
Next
End Sub
'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************
End Module
Thank you...
Using NX 8 and TC9.1
RE: Exporting PDFs of each part in an ASM?
Try to create a merge of this two journal.
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Windows.Forms.MessageBox
Imports NXOpen
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
'**********************************************************
Sub Main
Dim dwgs As Drawings.DrawingSheetCollection
dwgs = workPart.DrawingSheets
Dim sheet As Drawings.DrawingSheet
Dim i As Integer
Dim pdfFile As String
Dim currentPath As String
Dim currentFile As String
Dim exportFile As String
Dim partUnits As Integer
Dim strOutputFolder As String
Dim strRevision As String
Dim rspFileExists
Dim rspAdvancePrint
'determine if we are running under TC or native
Dim IsTcEng As Boolean = False
Dim UFSes As UFSession = UFSession.GetUFSession()
UFSes.UF.IsUgmanagerActive(IsTcEng)
partUnits = displayPart.PartUnits
'0 = inch
'1 = metric
If IsTcEng Then
currentFile = workPart.GetStringAttribute("DB_PART_NO")
strRevision = workPart.GetStringAttribute("DB_PART_REV")
Else 'running in native mode
'currentFile = GetFilePath() & GetFileName() & ".prt"
currentPath = GetFilePath()
currentFile = GetFileName()
Try
strRevision = workPart.GetStringAttribute("REVISION")
strRevision = Trim(strRevision)
Catch ex As Exception
strRevision = ""
End Try
End If
exportFile = currentFile
strOutputFolder = OutputPath()
'if we don't have a valid directory (ie the user pressed 'cancel') exit the journal
If Not Directory.Exists(strOutputFolder) Then
Exit Sub
End If
strOutputFolder = strOutputFolder & "\"
rspAdvancePrint = MessageBox.Show("Add advance print watermark?", "Add Watermark?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Dim shts As New ArrayList()
For Each sheet in dwgs
shts.Add(sheet.Name)
Next
shts.Sort()
i = 0
Dim sht As String
For Each sht in shts
For Each sheet in dwgs
If sheet.name = sht Then
i = i + 1
If rspAdvancePrint = vbyes Then
pdfFile = strOutputFolder & exportFile & "_advance" & ".pdf"
Else
If strRevision <> "" Then
pdfFile = strOutputFolder & exportFile & "_" & strRevision & ".pdf"
Else
pdfFile = strOutputFolder & exportFile & ".pdf"
End If
End If
'the pdf export uses 'append file', if we are on sheet 1 make sure the user wants to overwrite
'if the drawing is multisheet, don't ask on subsequent sheets
If i = 1 Then
If File.Exists(pdfFile) Then
rspFileExists = msgbox("The file: '" & pdfFile & "' already exists; overwrite?", vbyesno + vbquestion)
If rspFileExists = vbYes Then
Try
File.Delete(pdfFile)
Catch ex As Exception
msgbox(ex.message & vbcrlf & "Journal exiting", vbcritical + vbokonly, "Error")
Exit Sub
End Try
Else
'msgbox("journal exiting", vbokonly)
Exit Sub
End If
End If
End If
'update any views that are out of date
theSession.Parts.Work.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, sheet)
Try
ExportPDF(sheet, pdfFile, partUnits, rspAdvancePrint)
Catch ex As exception
msgbox("Error occurred in PDF export" & vbcrlf & ex.message & vbcrlf & "journal exiting", vbcritical + vbokonly, "Error")
Exit Sub
End Try
Exit For
End If
Next
Next
If i = 0 Then
MessageBox.Show("This part has no drawing sheets to export", "PDF export failure", MessageBoxButtons.ok, MessageBoxIcon.Warning)
Else
MessageBox.Show("Exported: " & i & " sheet(s) to pdf file" & vbcrlf & pdfFile, "PDF export success", MessageBoxButtons.ok, MessageBoxIcon.Information)
End If
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
'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)
GetFilePath = strPath
End Function
'**********************************************************
Function OutputPath()
'Requires:
' Imports System.IO
' Imports System.Windows.Forms
'if the user presses OK on the dialog box, the chosen path is returned
'if the user presses cancel on the dialog box, 0 is returned
Dim strLastPath As String
Dim strOutputPath As String
'Key will show up in HKEY_CURRENT_USER\Software\VB and VBA Program Settings
Try
'Get the last path used from the registry
strLastPath = GetSetting("NX journal", "Export pdf", "ExportPath")
'msgbox("Last Path: " & strLastPath)
Catch e As ArgumentException
Catch e As Exception
msgbox (e.GetType.ToString)
Finally
End Try
Dim FolderBrowserDialog1 As New FolderBrowserDialog
' Then use the following code to create the Dialog window
' Change the .SelectedPath property to the default location
With FolderBrowserDialog1
' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop
' Select the D:\home directory on entry.
If Directory.Exists(strLastPath) Then
.SelectedPath = strLastPath
Else
.SelectedPath = "D:\home"
End If
' Prompt the user with a custom message.
.Description = "Select the directory to export .pdf file"
If .ShowDialog = DialogResult.OK Then
' Display the selected folder if the user clicked on the OK button.
OutputPath = .SelectedPath
' save the output folder path in the registry for use on next run
SaveSetting("NX journal", "Export pdf", "ExportPath", .SelectedPath)
Else
'user pressed 'cancel', exit the subroutine
OutputPath = 0
'exit sub
End If
End With
End Function
'**********************************************************
Sub ExportPDF(dwg As Drawings.DrawingSheet, outputFile As String, units As Integer, advancePrint As Integer)
Dim printPDFBuilder1 As PrintPDFBuilder
printPDFBuilder1 = workPart.PlotManager.CreatePrintPdfbuilder()
printPDFBuilder1.Scale = 1.0
printPDFBuilder1.Action = PrintPDFBuilder.ActionOption.Native
printPDFBuilder1.Colors = PrintPDFBuilder.Color.BlackOnWhite
printPDFBuilder1.Size = PrintPDFBuilder.SizeOption.ScaleFactor
If units = 0 Then
printPDFBuilder1.Units = PrintPDFBuilder.UnitsOption.English
Else
printPDFBuilder1.Units = PrintPDFBuilder.UnitsOption.Metric
End If
printPDFBuilder1.XDimension = dwg.height
printPDFBuilder1.YDimension = dwg.length
printPDFBuilder1.OutputText = PrintPDFBuilder.OutputTextOption.Polylines
printPDFBuilder1.RasterImages = True
printPDFBuilder1.ImageResolution = PrintPDFBuilder.ImageResolutionOption.Medium
printPDFBuilder1.Append = True
If advancePrint = vbyes Then
printPDFBuilder1.AddWatermark = True
printPDFBuilder1.Watermark = "ADVANCE PRINT NOT TO BE USED FOR PRODUCTION " & Today
Else
printPDFBuilder1.AddWatermark = False
printPDFBuilder1.Watermark = ""
End If
Dim sheets1(0) As NXObject
Dim drawingSheet1 As Drawings.DrawingSheet = CType(dwg, Drawings.DrawingSheet)
sheets1(0) = drawingSheet1
printPDFBuilder1.SourceBuilder.SetSheets(sheets1)
printPDFBuilder1.Filename = outputFile
Dim nXObject1 As NXObject
nXObject1 = printPDFBuilder1.Commit()
printPDFBuilder1.Destroy()
End Sub
'**********************************************************
End Module
Thank you...
Using NX 8 and TC9.1
RE: Exporting PDFs of each part in an ASM?
Thank you for the fast replay
What I want is, to print the drawings from every part in the ASM! I supposed that I must jump from part to part and export the drawings as PDF!?
Your Script just export the pdf from the Workpart!
br
BB
RE: Exporting PDFs of each part in an ASM?
If you are using the master model approach (which is recommended), this is more difficult as you will have to find and open the drawing file corresponding to the components in your assembly...
Also, native NX or Teamcenter?
www.nxjournaling.com
RE: Exporting PDFs of each part in an ASM?
Are all the drawing within the same file, but on differnt sheets?
RE: Exporting PDFs of each part in an ASM?
Just noticed that last bit; ignore my last question!
www.nxjournaling.com
RE: Exporting PDFs of each part in an ASM?
I use the master model approach... is this within reach???
I could keep all the "dwg" files in a folder separate from the "part" file... if that would make it easier to achieve.
TIA
Dave
RE: Exporting PDFs of each part in an ASM?
sorry for the late replay!
you are right, the Model contains the drawing i don't use Mastermodel!
If you say "not so difficult" can give me some push code where i can try my luck
best regards and big thanks to you!
Boro
RE: Exporting PDFs of each part in an ASM?
Since I am not using TC and I do use the master model method, I'm not able to do proper testing. Please post back with any problems you encounter.
www.nxjournaling.com
RE: Exporting PDFs of each part in an ASM?
thank you for your time you spend!
I have test now your journal but I get this error:
Line 28: 'tempPart' is not declared. It may be inaccessible due to its protection level.
what should I change?
br
Boro
RE: Exporting PDFs of each part in an ASM?
CODE
For Each tempPart As Part In myAsmInfo.AllUniquePartsAlter the line as above. Also, the code at the given link above has been updated.
www.nxjournaling.com
RE: Exporting PDFs of each part in an ASM?
I try it again and receive following errors:
First at Line 528:
I suppose this was a typing mistake at the counter Variable so I change it by my self
From sheetCount -+=1 To sheetCount += 1
After that I got following error:
Runtime error:
System.NullReferenceException: Object reference not set to an instance of an object. at NXJ_Assembly_info.GetAllInfo() in C:\Users\boro\AppData\Local\Temp\NXJournals6788\journal.vb:Line 1012.
bei NXJ_Assembly_info..ctor() in C:\Users\boro\AppData\Local\Temp\NXJournals6788\journal.vb:Line 998.
bei Module1.Main() in C:\Users\boro\AppData\Local\Temp\NXJournals6788\journal.vb:Line 24.
br
Boro
RE: Exporting PDFs of each part in an ASM?
After line 24, add the highlighted line (or re-download from the link above).
CODE
Dim myAsmInfo As New NXJ_Assembly_info myAsmInfo.Part = workPartwww.nxjournaling.com
RE: Exporting PDFs of each part in an ASM?
RE: Exporting PDFs of each part in an ASM?
www.nxjournaling.com