Can't get geometry added to the right page/layout

I’m using a layout page to draw a schematic drawing of the model but I can’t get the geometry on the right layout. The geometry gets added to the first layout, although the ViewportId of the ObjectAttributes is set to the new page’s MainViewport ID. See code below.
Any suggestions?

I’m using a command using the following code:

    private static int s_pageNum = 0;

    private Result UpdatePageAddDelete(RhinoDoc doc)

        // get the existing pages
        var pageViews = doc.Views.GetPageViews();

        // RhinoPageView[] existingPages = pageViews.Where(page => page.PageName.Equals(EnglishName, StringComparison.OrdinalIgnoreCase)).ToArray();
        RhinoPageView[] existingPages = pageViews.Where(page => page.PageName.StartsWith(EnglishName, StringComparison.OrdinalIgnoreCase)).ToArray();

        // create the new page
        Double[] pageSize = new Double[] { 210, 297 };
        doc.PageUnitSystem = UnitSystem.Millimeters;
        var newPage = doc.Views.AddPageView(EnglishName+String.Format("{0}", s_pageNum), pageSize[0], pageSize[1]);
        if (newPage == null)
            RhinoApp.WriteLine("Failed to add new page");
            return Result.Failure;
        RhinoApp.WriteLine("Page added. Id: {0}, Active Viewport ID: {1}.", newPage.MainViewport.Id, newPage.ActiveViewportID);

        // add some geometry to the page space.
        DrawRectangle(newPage, pageSize, doc);
        // Note: geometry tends to be kept drawn on the first page, even if it's deleted.

        // delete the existing pages.
        // + foreach (var page in existingPages) page.Close(); // Close also disposes the page.
        // wrap up and return.
        RhinoApp.WriteLine("Done Update Page by Add and Delete.");
        return Result.Success;

    private void DrawRectangle(RhinoPageView pageView, Double[] size, RhinoDoc doc)
        ObjectAttributes atts = new ObjectAttributes()
                                        Name = EnglishName,
                                        Space = ActiveSpace.PageSpace,
                                        ViewportId = pageView.MainViewport.Id,
                                        ColorSource = ObjectColorSource.ColorFromObject,
                                        ObjectColor = System.Drawing.Color.Red,
        Line[] l = new Line[]
                           new Line(new Point3d(0,0,0), new Point3d(size[0], size[1], 0) ), 
                           new Line(new Point3d(0, size[1],0), new Point3d(size[0], 0, 0)),
        var added = l.Select(line => doc.Objects.Add(new LineCurve(line), atts));
        RhinoApp.WriteLine("Added lines to Page Id {0}. Line Ids: {1}", atts.ViewportId, String.Join(", ", added.Select(a => String.Format("{0}", a))));
        pageView.Redraw(); //+


I think you need to add a detail view. Here’s an example:

You typically shouldn’t need to set the viewport Id. Geometry is added to the “active” viewport, so make sure your new layout is active right before adding the geometry.

Hello Steve,

Thanks for the hint. I’ve got it working by using the following statement:

pageView.Document.Views.ActiveView = pageView;

I previously used pageView.SetPageAsActive(); but that kept the first layout active and caused the geometry to be added to the first layout I’ve added instead of the most recent layout.
The atts.Space and atts.ViewportId properties seems to be ignored. Are they obsolete?
I think there might be cases where it would be convenient to add geometry to pages in a background process, without that layout becoming active for a brief moment. Especially when multiple layouts have to be redrawn.