I patched-up a small definition for printing a layout through python (I am fresh-new to coding in Python). Now, at the end, a pop-up window is popping up obliging for confirming by hitting the “save” button. This is quite an inconvenience since I am trying to keep everything automated. Does anyone have an idea how to deal with this?
Thank you in advance!
import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
x = fileName
y = fileFolder
z = toggle
if z:
filename = rs.SaveFileName(folder = y, filename= x, extension=".pdf")
pages = sc.doc.Views.GetPageViews()
pdf = Rhino.FileIO.FilePdf.Create()
dpi = 300
#just one printing:
capture = Rhino.Display.ViewCaptureSettings(pages[2], dpi)
pdf.AddPage(capture)
pdf.Write(filename)
Thank you for the suggestion! Unfortunately it does not seams to work, I always end up wit ha pop-up window. Probably I am missing something, at what point of the script would you put your line?
Nope it isn’t working. Sure! I am trying to export one single pdf from one single layout, but many times. Now for full disclosure I am working on a GHpython within Grasshopper (because I am trying to save multiple pdf of gh previews).
Now here’s the logic I wanted to put in place: openfile > produce geometry preview in grasshopper > (always in grasshopper) run the Ghpython component in question in order to export the layout as pdf > close 3dm file > open a new one > start over again …
So the problem with this is that each time that I try to export a layout pdf via python a window asking for “save” confirmation pops up by breaking the automated process.
PS: I thought at one point to simulate a hotkey ENTER but without results
Okay I have not used the FilePDF class, it might be that there is something you have to do to close/finish the file.
What I would try, is to simply script the print command and be done with it. Here is a small c# snippet that should do the job, I converted it to python but might not be 100% as I can not test it right now:
foreach (var v in views)
{
if (v.ActiveViewport.Name == "NAME")
doc.Views.ActiveView = v;
}
RhinoApp.RunScript("_-Print " + " _Enter " + filename + " _Enter", true);
python:
for v in pages:
if v.ActiveViewport.Name == "NAME":
sc.doc.Views.ActiveView = v
rs.Command(("_-Print " + " _Enter " + filename + " _Enter", True)
pages being a list of all views, filename being the name you want your pdf to be called. run the print settings once to set up how you want it to be, then run the script.
Hi! No I am not actually altering my .3dm file. For each file I have a basic 3d model that I am linking to grasshopper, and then everyhting is done in grasshopper. So, no I am pretty sure.
Actually, the very problem is that I would like to avoid the print settings window. This is the problematic point. In other words, I do not want to intervene manually by clicking “save”, because I would have to do it 100 times.
This is the problem. The rs.SaveFileName function will open a prompt to get your desired filename.
If you construct the filename string in memory you will not get a prompt.
Here is an old script of mine which saves without any prompts all pageviews present in the model, you should be able to adjust it to your needs:
import Rhino
import scriptcontext as sc
import System.Drawing
import rhinoscriptsyntax as rs
def createSinglePDF(view):
pdf = Rhino.FileIO.FilePdf.Create()
dpi = 300
size = System.Drawing.Size(8.5*dpi,11*dpi)
settings = Rhino.Display.ViewCaptureSettings(view, size, dpi)
pdf.AddPage(settings)
filename = 'D:/Studium/SS19/'+view.PageName+'.pdf' # <- since you know your location, construct your filename in memory
pdf.Write(filename)
for i in sc.doc.Views:
if type(i) is Rhino.Display.RhinoPageView:
createSinglePDF(i)
you have to do it once to set it up and then never again open it, even if you start a new rhino session. in fact, if you are happy with your settings, you dont need to do anything at all.
Hey thank Lando for your script, super nice! Unfortunatelly I am not able to maake it work, I mean there is no error as Output, but none of the pageviews are being printed. Do you have an idea why?
EDIT: sorry my bed, I have now constructed the file in memory as you said and it works perfectly! Thank you! I would have just another question: would you know how to match the same size of the layout of the workspace with the settings export in your script? Thank you again by the way.