February 15, 2009

RhinoScript | Print Layout pages to Pdf files with Bullzip Pdf printer

지난 주말 도면 작업을 할 일이 있어 Layout 기능을 활용해 쉽게 작업할 수 있었다. 그런데, 여러장의 Layout을 사용하다 보니, 일일히 프린트 하는 것이 문제가 될줄이야...
Rhino의 Print 커맨드가 여러장을 선택적으로 프린트 하는 기능이 없어, 여러가지 방법을 생각하다가 Pdf printer를 이용하는 방법을 시도해보았다.

Bullzip Pdf printer는 문서출력의 결과를 Pdf파일로 저장해주는 가상 프린터이다.여러종류의 pdf프린터가 있지만, Bullzip은 무료로 쓸수 있으면서도 많은 기능을 제공한다. COM/ActiveX interface도 제공하기에 스크립트를 이용한 자동화를 가능 하다.
http://www.bullzip.com/products/pdf/info.php

아래 스크립트를 이용하기 위해서는 Bullzip printer가 설치되어 있어야 한다. 그리고 Page Layout이 있어야 하며 모든 Layout을 pdf파일로 저장한다. 하나의 파일로 저장할지, 각각의 파일로 저장할지 선택할 수 있다. 물론 각 Layout의 프린터를 Bullzip pdf printer로 설정되어 있어야 한다. 프린트를 할때 뿐만 아니라 pdf파일을 생성하기 위한 도구로도 사용이 가능해보인다.


Option Explicit
'2009.2.15
'Script by Sanghoon Yoon @ +plastic
'http://www.byRhino3d.com | http://www.rhinos.co.kr | http://sac3.blogspot.com
'sac3yoon@gmail.com
'This script let Rhino to print all layout pages as pdf files through Bullzip pdf printer.
'Download Bullzip pdf printer @ http://www.bullzip.com/products/pdf/info.php

PrintLayoutAsPDF
Sub PrintLayoutAsPDF
Dim strSS
strSS = Rhino.GetString ("File option" , "As_one_Pdf", Array ("As_one_Pdf", "As_each_Pdf") )
If IsNull(strSS) Then Exit Sub
Select Case strSS
Case "As_one_Pdf"
SaveAsOnePdf
Case "As_each_Pdf"
SaveAsEachPdf
End Select
End Sub

Sub SaveAsEachPdf()
Dim strFile,strFolder, arrViews,fileName, strM
strM=""
strFolder = Rhino.BrowseForFolder
arrViews = Rhino.ViewNames (True ,1)
If IsNull(arrViews) Then Rhino.Print "No layout pages!" : Exit Sub
For Each fileName In arrViews
Rhino.CurrentView fileName
strFile=strFolder&fileName&".pdf"
BatchPrint strFile,strM
Rhino.Print strFile&" was created."
Next
End Sub

Sub SaveAsOnePdf()
Dim strFile,strFolder, arrViews,fileName
strFile= Rhino.SaveFileName ("Save as pdf" ,"pdf files (*.pdf)|*.pdf||" , "" , ,"pdf")
arrViews = Rhino.ViewNames (True ,1)
If IsNull(arrViews) Then Rhino.Print "No layout pages!" : Exit Sub
For Each fileName In arrViews
Rhino.CurrentView fileName
BatchPrint strFile,strFile
Next
Rhino.Print strFile&" was created."
End Sub

Function BatchPrint(strFile,strM)
Const PDF_PRINTERNAME = "Bullzip PDF Printer"
Const PRINTER_PROGID = "Bullzip.PDFPrinterSettings"
Dim prtidx, obj
'Configure the PDF print job
Set obj = CreateObject(PRINTER_PROGID)
obj.SetValue "mergefile",strM
obj.SetValue "mergeposition","bottom"
obj.SetValue "Output", strFile
obj.SetValue "ConfirmOverwrite", "no"
obj.SetValue "ShowSaveAS", "never"
obj.SetValue "ShowSettings", "never"
obj.SetValue "ShowPDF", "no"
obj.SetValue "RememberLastFileName", "no"
obj.SetValue "RememberLastFolderName", "no"
obj.WriteSettings True

Rhino.Command "-print go ",False

'Wait for runonce settings file to disappear
Dim runonce, fso
runonce = obj.GetSettingsFileName(True)
Set fso = CreateObject("Scripting.FileSystemObject")
While fso.FileExists(runonce)=True
Sleep 100
Wend
End Function

No comments: