Forcing viewport redraw and pdf export in a for loop

I try to export a large set of pdf’s, all based on a single layout template.

In grasshopper I have a large number of geometries.
My script currently works for 1 geometry, but not for multiple.

Based on 1 geometry I create a detail on the layout and print it as a pdf.
This works beautifully.

My issue when I loop through the geometries is that the geometry is not redrawn. Only the first item is properly drawn and all pdf’s exported show the same (first) geometry

The code looks as follows (credit goes to Dharman.Gersch for initial script):

Input L = layout name
Input P = path
Input N = filename (LIST)
input G: Geometry (LIST)
scale = scale of drawing
B = boolean (run or don’t run)

        
ghenv.Component.Name = "AUR_Layout.Print"
ghenv.Component.NickName = 'Print'
ghenv.Component.Message = ''#'VER ALPHA 0.0.1\n 2016_04_10'
ghenv.Component.Category = "layout"
ghenv.Component.SubCategory = "1"
        
        
import Rhino
import scriptcontext as sc
import System.Drawing
import rhinoscriptsyntax as rs
import os
        
def get_layouts():
    layouts = []
    for view in Rhino.RhinoDoc.ActiveDoc.Views:
        if isinstance(view, Rhino.Display.RhinoPageView):
            layouts.append(view)
    return layouts
        
def layouts_get_by_names(layout_names):
    layouts = []
    for layout_name in layout_names:
        view = Rhino.RhinoDoc.ActiveDoc.Views.Find(layout_name, True)
        if isinstance(view, Rhino.Display.RhinoPageView):
            layouts.append(view)
    return layouts
        
def mmToInch(num):
    #surely pages are always in mm?
    return num/25.4
            
        
        
        
if B:
    activeDoc = Rhino.RhinoDoc.ActiveDoc
    dpi = 300
    #loop through all layouts and print to single page pdf
    for i, view in enumerate(layouts_get_by_names(L)):
                
        top_left = Rhino.Geometry.Point2d(20.793756, 408.78)
        bottom_right = Rhino.Geometry.Point2d(288.588, 60.43832)
        detail = view.AddDetailView("ModelView", top_left, bottom_right, Rhino.Display.DefinedViewportProjection.Top)
        for name, geo in zip(N,G):
            pdf = Rhino.FileIO.FilePdf.Create()
            view.SetActiveDetail(detail.Id)
            detail.Viewport.ZoomBoundingBox(Rhino.Geometry.GeometryBase.GetBoundingBox(geo,False))
            detail.CommitViewportChanges();
               
            detail.DetailGeometry.IsProjectionLocked = True
            detail.DetailGeometry.SetScale(1, activeDoc.ModelUnitSystem, scale, activeDoc.PageUnitSystem)
            # Commit changes tells the document to replace the document's detail object
            # with the modified one that we just adjusted
            detail.CommitChanges()
            view.SetPageAsActive()
            activeDoc.Views.ActiveView = view
            activeDoc.Views.Redraw()
                    
            #Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.PageHeight
            #Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.PageWidth
            w = mmToInch(view.PageHeight)
            h = mmToInch(view.PageWidth)
            size = System.Drawing.Size(h*dpi, w*dpi)
            #size = System.Drawing.Size(8.5*dpi,11*dpi)
            
            settings = Rhino.Display.ViewCaptureSettings(view, size, dpi)
            pdf.AddPage(settings)
            filepath = os.path.join(P, name+".pdf")
            print filepath
            pdf.Write(filepath)
        

After pdf.Write(filepath) I have tried to place an ghenv.Component.ExpireSolution(False) without much luck.

How can I force a redraw in each loop?

So I think I have solved it:

       
        
ghenv.Component.Name = "AUR_Layout.Print"
ghenv.Component.NickName = 'Print'
ghenv.Component.Message = ''#'VER ALPHA 0.0.1\n 2016_04_10'
ghenv.Component.Category = "layout"
ghenv.Component.SubCategory = "1"

import time
        
        
import Rhino
import scriptcontext as sc
import System.Drawing
import rhinoscriptsyntax as rs
import os

        
def get_layouts():
    layouts = []
    for view in Rhino.RhinoDoc.ActiveDoc.Views:
        if isinstance(view, Rhino.Display.RhinoPageView):
            layouts.append(view)
    return layouts
        
def layouts_get_by_names(layout_names):
    layouts = []
    for layout_name in layout_names:
        view = Rhino.RhinoDoc.ActiveDoc.Views.Find(layout_name, True)
        if isinstance(view, Rhino.Display.RhinoPageView):
            layouts.append(view)
    return layouts
        
def mmToInch(num):
    #surely pages are always in mm?
    return num/25.4
            
        
        
        
if B:
    activeDoc = Rhino.RhinoDoc.ActiveDoc
    dpi = 300
    #loop through all layouts and print to single page pdf
    for i, view in enumerate(layouts_get_by_names(L)):
        top_left = Rhino.Geometry.Point2d(20.793756, 408.78)
        bottom_right = Rhino.Geometry.Point2d(288.588, 60.43832)
        for name, geo in zip(N,G):
            detail = view.AddDetailView("ModelView", top_left, bottom_right, Rhino.Display.DefinedViewportProjection.Top)
            pdf = Rhino.FileIO.FilePdf.Create()
            view.SetActiveDetail(detail.Id)
            detail.Viewport.ZoomBoundingBox(Rhino.Geometry.GeometryBase.GetBoundingBox(geo,False))
            detail.CommitViewportChanges();
               
            detail.DetailGeometry.IsProjectionLocked = True
            detail.DetailGeometry.SetScale(1, activeDoc.ModelUnitSystem, scale, activeDoc.PageUnitSystem)
            # Commit changes tells the document to replace the document's detail object
            # with the modified one that we just adjusted
            detail.CommitChanges()
            view.SetPageAsActive()
            activeDoc.Views.ActiveView = view
            activeDoc.Views.Redraw()
            sc.doc.Views.Redraw()
                    
            #Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.PageHeight
            #Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.PageWidth
            w = mmToInch(view.PageHeight)
            h = mmToInch(view.PageWidth)
            size = System.Drawing.Size(h*dpi, w*dpi)
            #size = System.Drawing.Size(8.5*dpi,11*dpi)
            
            settings = Rhino.Display.ViewCaptureSettings(view, size, dpi)
            pdf.AddPage(settings)
            filepath = os.path.join(P, name+".pdf")
            print filepath
            pdf.Write(filepath)
            activeDoc.Objects.Delete(detail)
        

The difference being that I use detail.CommitViewportChanges();
And that I delete the detail again after printing

1 Like