- 积分
- 10
- 注册时间
- 2007-10-5
- 仿真币
-
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2009-6-13 16:30:37
|
显示全部楼层
来自 山西太原
export pdf of each drawing sheet in each part in specified directory
Build this as an executable NXOpen .NET Console Application
' and run it like this:
'
' batch_export_pdf.exe D:\Full\Path\To\NXPartFiles
'
' Or, you can use run_journal.exe to run it like this:
'
' run_journal batch_export_pdf.vb -args D:\Your\Full\Path\To\NXPartFiles
'
' (Both cases above assume that you save this file as "batch_export_pdf.vb")
'
'
' Note that while it screens for files whose names end in '.prt', it does
' not check all possible upper- and lower- case combinations. Also,
' if the directory contains files that have a .prt extension, and that
' are NOT NX part files, an error will result.
'
' Note that the export options in this example program MAY NOT represent the
' options that you would have chosen. You may change this as required to use
' any valid options. See the NXOpen for .Net Reference Guide for details.
Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Drawings
Imports NXOpen.UF
Imports NXOpen.Utilities
Module export_pdf_of_each_drawing_sheet_in_each_part_in_specified_directory
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow
Sub Main(ByVal args As String())
'
'Uncomment the following line and use your actual load options filespec
'including the entire path and the filename.
'
'ufs.Assem.RestoreLoadOptions("D:\Path\To\Your\load_options.def")
Try
Dim theDirectory As String = args(0)
'
'NOTE: If you want to run this from Tools->Journal->Play, then
'comment the line above and specify the directory as shown
'on the following commented line:
'
'Dim theDirectory As String = "D:\Path\to\your\part\files"
lw.Open()
lw.WriteLine(" ")
lw.WriteLine("Directory to process: " & theDirectory)
lw.WriteLine(" ")
Dim partFlag As Integer = 0
Dim partCount As Integer = 0
For Each fileName As String In My.Computer.FileSystem.GetFiles( _
theDirectory)
Dim extension As String = Right$(fileName, 4)
If extension = ".prt" Or _
extension = ".PRT" Or _
extension = ".Prt" Then
partFlag = open_part_file(fileName)
partCount = ufs.Part.AskNumParts()
End If
If partFlag > 0 Then
' Some parts failed to open.
' Take appropriate action here
'-----------------------------
Else
If partCount > 0 Then
process_each_drawing(s.Parts.Display(), fileName)
End If
End If
' Close any open part files
If partCount > 0 Then
s.Parts.CloseAll(BasePart.CloseModified.CloseModified, _
Nothing)
lw.WriteLine(" ")
End If
ufs.Undo.DeleteAllMarks()
Next
Catch ex As Exception
Console.OpenStandardOutput()
Console.WriteLine(ex.ToString)
Console.WriteLine()
End Try
End Sub
Sub process_each_drawing(ByVal thePart As Part, ByVal fileName As String)
If thePart.Tag = Tag.Null Then
Return
End If
Dim drawingSheets As DrawingSheet() = thePart.DrawingSheets.ToArray
If drawingSheets.Length() > 0 Then
lw.WriteLine(" Sheets found: " & drawingSheets.Length().ToString())
End If
Dim mySheet As DrawingSheet = Nothing
Dim partName As String = thePart.Leaf.ToString()
For Each mySheet In drawingSheets
lw.WriteLine(" Processing Sheet: " & mySheet.Name())
mySheet.Open()
s.Parts.Work.DraftingViews.UpdateViews( _
DraftingViewCollection.ViewUpdateOption.All, mySheet)
Dim pdf_output_name As String = ""
pdf_output_name = fileName.ToLower()
pdf_output_name = pdf_output_name.Replace(".prt", "")
Dim sheetName As String = mySheet.Name.ToLower()
pdf_output_name = String.Concat(pdf_output_name, "_", sheetName, ".PDF")
lw.WriteLine("PDF: " & pdf_output_name)
Dim workPart As Part = s.Parts.Work()
Dim printPDFBuilder1 As PrintPDFBuilder
printPDFBuilder1 = workPart.PlotManager.CreatePrintPdfbuilder()
Dim dwgUnits As DrawingSheet.Unit = mySheet.Units
If dwgUnits = DrawingSheet.Unit.Inches Then
printPDFBuilder1.Units = PrintPDFBuilder.UnitsOption.English
End If
If dwgUnits = DrawingSheet.Unit.Millimeters Then
printPDFBuilder1.Units = PrintPDFBuilder.UnitsOption.Metric
End If
printPDFBuilder1.Scale = 1.0
printPDFBuilder1.Action = PrintPDFBuilder.ActionOption.New
printPDFBuilder1.Colors = PrintPDFBuilder.Color.AsDisplayed
printPDFBuilder1.Widths = PrintPDFBuilder.Width.StandardWidths
printPDFBuilder1.Size = PrintPDFBuilder.SizeOption.ScaleFactor
printPDFBuilder1.XDimension = mySheet.Length()
printPDFBuilder1.YDimension = mySheet.Height()
printPDFBuilder1.RasterImages = True
printPDFBuilder1.ImageResolution = PrintPDFBuilder.ImageResolutionOption.High
printPDFBuilder1.ShadedGeometry = True
Dim sheets1(0) As NXObject
sheets1(0) = mySheet
printPDFBuilder1.SourceBuilder.SetSheets(sheets1)
printPDFBuilder1.Filename = pdf_output_name
Dim nXObject1 As NXObject
nXObject1 = printPDFBuilder1.Commit()
printPDFBuilder1.Destroy()
Next
End Sub
Public Function open_part_file(ByVal fileName As String) As Integer
Dim loadStatus As NXOpen.PartLoadStatus = Nothing
Dim currentPart As Part = _
s.Parts.OpenBaseDisplay(fileName, loadStatus)
If loadStatus.NumberUnloadedParts < 1 Then
lw.WriteLine(" Processing Part File: " & fileName)
Else
For inx As Integer = 0 To loadStatus.NumberUnloadedParts - 1
lw.WriteLine(loadStatus.GetStatus(inx).ToString())
Dim errno As Integer = CInt(loadStatus.GetStatus(inx))
Dim errmsg As String = ""
ufs.UF.GetFailMessage(errno, errmsg)
lw.WriteLine("Error: " & errmsg & " in: " & fileName)
lw.WriteLine(" ")
Next
End If
Return loadStatus.NumberUnloadedParts()
End Function
End Module |
|