Avoiding Print Pop-up Window

Hello there!

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)

Moved to Scripting category

see if a simple save command helps:

rs.Command("_Save", False)

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?

Just at the very end, before loading a new file. I had almost the same issue and solved it this way.

edit: maybe I misunderstood, can you explain what you are doing in detail? When is the “save” prompt popping up?

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.

1 Like

This is why you get a prompt to save, I assume it has nothing to do with the File3dm class

Are you baking geometry, ar altering your 3dm file in any other way?

I am going to try it right now!

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.

Can you post a screenshot of the prompt at the end of your script?

Here it is:

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)
1 Like

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.

Yes I had a DIN A4 restriction which i removed, I tested a gh file to make sure it is working:

SavePageViews.gh (6.1 KB)

1 Like

Amazing!Thank you so much for your help!:slight_smile: