AddPageView has no effect

Hi All,

I’m trying to export my drawings to PDF using Python. I’ve found the example below, but the AddPageView line return ‘None’. It’s a simple enough function I don’t know what to try… but I tried executing the code before and after saving the document - in case that made a difference.

def PDFExport(doc,FName:str):

    dpi = 300
    pdf_settings = Rhino.FileIO.FilePdf.Create()

    layout_name = "A4_Miller"
    page_height = 210
    page_width = 297
    
    # Create a new view for printing
    view = doc.Views.AddPageView(layout_name, page_width, page_height)

I’m using Python via VSCode btw.

Any Suggesions

Hi @Gerrard_Hickson, below works for me either using python 2 or 3 in the old and new editor:

import Rhino
import scriptcontext

def PDFExport():

    dpi = 300
    pdf_settings = Rhino.FileIO.FilePdf.Create()

    layout_name = "A4_Miller"
    page_height = 210
    page_width = 297
    
    # Create a new view for printing
    view = scriptcontext.doc.Views.AddPageView(layout_name, page_width, page_height)
    
PDFExport()

_
c.

I’m using VScode and I can’t get scriptcontext or rhinoscriptsyntax working… Any suggestions there?

Hi @Gerrard_Hickson, below works without using scriptcontext for me:

import Rhino

def PDFExport():

    dpi = 300
    pdf_settings = Rhino.FileIO.FilePdf.Create()

    layout_name = "A4_Miller"
    page_height = 210
    page_width = 297
    
    # Create a new view for printing
    doc = Rhino.RhinoDoc.ActiveDoc
    view = doc.Views.AddPageView(layout_name, page_width, page_height)
    
PDFExport()

Note, i have no VSCode to test with.

_
c.

1 Like

OK, so I’ve tried this a few different ways, and I’m still getting a problem.

In my case, the doc object is passed into the function. doc is created via the CreateHeadless method, but I’ve also tried using the Create method - neither work.
I also tried assigning the doc objct using Rhino.RhinoDoc.ActiveDoc as you have, but that doesn’t work either.

In all cases, doc returns as None.

For what it’s worth, I had problems creating with templates not creating layouts using the CreateHeadless method previously - see here: RhinoDoc.CreateHeadless doesn't create document layouts
I wonder if they’re related issues?

Hi @Gerrard_Hickson,

This seems to work in a Rhino-controlled document.

#! python 2
import Rhino
import scriptcontext

def AddLayout():
    scriptcontext.doc.PageUnitSystem = Rhino.UnitSystem.Millimeters
    page_views = scriptcontext.doc.Views.GetPageViews()
    page_number = 1
    if page_views: page_number = len(page_views) + 1
    pageview = scriptcontext.doc.Views.AddPageView("My Page {0}".format(page_number), 1189, 841)
    if pageview:
        top_left = Rhino.Geometry.Point2d(20,821)
        bottom_right = Rhino.Geometry.Point2d(1169, 20)
        detail = pageview.AddDetailView("ModelView", top_left, bottom_right, Rhino.Display.DefinedViewportProjection.Top)
        if detail:
            pageview.SetActiveDetail(detail.Id)
            detail.Viewport.ZoomExtents()
            detail.DetailGeometry.IsProjectionLocked = True
            detail.DetailGeometry.SetScale(1, scriptcontext.doc.ModelUnitSystem, 10, scriptcontext.doc.PageUnitSystem)
            # Commit changes tells the document to replace the document's detail object
            # with the modified one that we just adjusted
            detail.CommitChanges()
        pageview.SetPageAsActive()
        scriptcontext.doc.Views.ActiveView = pageview
        scriptcontext.doc.Views.Redraw()

if __name__=="__main__":
    AddLayout()

– Dale

Hi Dale, thanks for the reply.

In my case len(doc.Views.GetPageView()) returns 0

Also - I’m using VSCode, how do I access scriptcontext? I’ve managed to avoid it so far, but it feels more trouble to avoid than its worth.

Cheers

I can’t help with VSCode - sorry.

I’m just using Rhino’s built-in editors.

When Rhino runs your script, scriptcontext is available to it.

– Dale

In that case, do you have an example that doesn’t use scriptcontext or can you offer any reason why the examples above wouldn’t work on my system?

Hi @Gerrard_Hickson,

Like I said, I cannot help with VSCode.

What is your requirement to use it?

Thanks,

– Dale

Familiarity, convenience, so far, it’s done everything I need and I haven’t found a reason not to use it…

The end result of my project will be a standalone python script that carries out a number of functions - a significant part of which requires interactions with Rhino.

Are you suggesting that the problem is VSCode?

Hi @Gerrard_Hickson, i cannot say anything about CreateHeadless as i tested only with a regular rhino document. Does your script run from one of the build in python editors or using RunPythonScript ?

Maybe you should first try to solve the problem with scriptcontext ? Both script editors import module search paths before running scripts. It could be that this is the reason you’re not able to access the modules in VS which are imported using:

import scriptcontext

You might try this:

1.Open _EditPythonScript (the old IronPython script editor)
2.Go to Tools > Options > Files > Module Search Paths
3.Note the paths listed in the dialog

In my case, the scriptcontext module is located here:

C:\Users\Clement\AppData\Roaming\McNeel\Rhinoceros\8.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib

Can you try to append that path on top of your script using:

import sys
sys.path.append(the_path)

and access scriptcontext without getting an error ?

_
c.

LOL, no, I am just asking questions.

Can I assume, since you’re outside of Rhino, that you’re using Rhino.Inside Python? Or are you doing something else?

Thanks,

– Dale

I’ll respond to this in the other thread to keep this one on topic.