Hello, I have made some progress for a WIP alternative for a batch raster pdf maker…
First you need to get PdfSharp via nuget cli. Download to a folder and run
nuget.exe Install-Package PdfSharp
make sure all files are unblocked in their properties tab.
Then we need to add some modules as well as the usual suspects…
from System import Environment as env
import clr
clr.AddReferenceToFileAndPath(r"c:\directory where you installed\lib\net20\PdfSharp.dll")
import PdfSharp as ipdf
I have modified the code to use pdfsharp…
def createSinglePDFRaster(view,wrkdir):
# scale sizing based on scripts by Kelvin Chang & Lando Schumpich have added
# layout sizes to accomodate pdfsharp
if sc.doc.ActiveDoc.PageUnitSystem == sc.doc.PageUnitSystem.Millimeters:
factor = 0.0393700787401575
vWidth = ipdf.Drawing.XUnit.FromMillimeter(view.PageWidth)
vHeight = ipdf.Drawing.XUnit.FromMillimeter(view.PageHeight)
else:
factor = 1.0
vWidth = ipdf.Drawing.XUnit.FromInch(view.PageWidth)
vHeight = ipdf.Drawing.XUnit.FromInch(view.PageHeight)
dpi = 300
# forces view to redraw, this doesn't appear to work so far...
sc.doc.Views.ActiveView = view
while view.Redraw() == False:
Rhino.RhinoApp.Wait()
# capture the layout image and send it to a memory stream, save as pdfsharp ximage
size = System.Drawing.Size(view.PageWidth * factor * dpi, view.PageHeight * factor * dpi)
settings = Rhino.Display.ViewCaptureSettings(view, size, dpi)
bitmap = Rhino.Display.ViewCapture.CaptureToBitmap(settings)
stream = System.IO.MemoryStream()
bitmap.Save(stream,System.Drawing.Imaging.ImageFormat.Png)
img = ipdf.Drawing.XBitmapImage.FromStream(stream)
# create pdf document at a given location, add some pdf properties
filename = os.path.join(wrkdir,view.PageName+'.pdf')
pdf = ipdf.Pdf.PdfDocument()
pdf.Info.Title = view.PageName
pdf.Info.Author = env.UserName
# add a single page to same dims as layout
layout = pdf.AddPage()
layout.Width = vWidth
layout.Height = vHeight
page = pdf.Pages[0]
# create the background image and draw, save pdf
gfx = ipdf.Drawing.XGraphics.FromPdfPage(page, ipdf.Drawing.XPageDirection.Downwards)
gfx.DrawImage(ipdf.Drawing.XImage.FromStream(stream),0,0,page.Width,page.Height)
pdf.Save(filename)
# pdf directory
pdfDirOut = r"c:\your\working\directory"
# create a list of layouts
viewList = [v for v in sc.doc.Views if v.GetType() == Rhino.Display.RhinoPageView]
# UI Progress bar
uiStr = '{} pdfs are being generated... '.format(len(viewList))
Rhino.UI.StatusBar.ShowProgressMeter(0,len(viewList),uiStr,True,True)
progressBar = Rhino.UI.StatusBar.UpdateProgressMeter
# run for loop script
for i,view in enumerate(viewList):
progressBar(i+1,True)
createSinglePDFRaster(view,pdfDirOut)
Rhino.UI.StatusBar.HideProgressMeter()
The script pdf’s all the views correctly, only problem is I can’t seem to get the layout to redraw, if its active its ok, if not then it seems to revert to wireframe.
Anyone able to help with redraw events?