Valued Contributor
Posts: 95
Registered: ‎11-15-2011
Message 1 of 4 (262 Views)
Accepted Solution

create, manipulate and focus new vport in new ps layout

262 Views, 3 Replies
03-01-2012 07:35 PM

what i've managed to do is:


use the layoutmanager to create a new layout;

initialise the new layout;

create a vport from scratch;

get the new layout's blocktablerecord; and

append the new vport to the new layout's btr and add it to the transaction.

(focus is where i start the command, in ms, although i haven't safeguarded via commandflag)




although i can set the current view with the application's tilemode system variable, this will only ever focus on the existing ps layout, not the new one;

use of document.editor to switchtoms or -ps throws a runtime exception, and so does new vport.on;

use of the layoutmanager to set the current layout to the new layout after the vport is appended gets focus but also 'empties' the vport (showing nada);

use of the layoutmanager straight after the new layout is created gets new vport on new ps layout and focus, except that the vport's property values are all different from those that i assign e.g. width, height, scale, rotation etc, all different.


anybody get me out of this mess?


thanks, felix

Posts: 150
Registered: ‎07-20-2011
Message 2 of 4 (254 Views)

Re: create, manipulate and focus new vport in new ps layout

03-02-2012 12:04 AM in reply to: FFlix

This may help to get you to create a paperspace Viewport


using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace AcadViewport
    public class PVP

        [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
         EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")]
        extern static private int acedSetCurrentVPort(IntPtr AcDbVport);

        public static void CreateFloatingViewport()
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

                // Switch to the previous Paper space layout
                Application.SetSystemVariable("TILEMODE", 0);

                // Create a Viewport
                Viewport acVport = new Viewport();
                acVport.CenterPoint = new Point3d(3.25, 3, 0);
                acVport.Width = 6;
                acVport.Height = 5;
                acTrans.AddNewlyCreatedDBObject(acVport, true);

                acVport.ViewDirection = new Vector3d(1, 1, 1);
                acVport.On = true;





Posts: 150
Registered: ‎07-20-2011
Message 3 of 4 (251 Views)

Re: create, manipulate and focus new vport in new ps layout

03-02-2012 01:03 AM in reply to: SENL1362

This may help to understand paperspace viewports as well

create non rectangular viewport

freeze layer in viewport


        static public void CreateNonRectangularViewports()
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())

                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord ps = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);

                Polyline pl = new Polyline(4);
                pl.AddVertexAt(0, new Point2d(10, 10), 0, 0, 0);
                pl.AddVertexAt(1, new Point2d(200, 10), 0, 0, 0);
                pl.AddVertexAt(2, new Point2d(200, 287), 0, 0, 0);
                pl.AddVertexAt(3, new Point2d(10, 287), 0, 0, 0);
                pl.Closed = true;

                Entity ent = pl as Entity;
                Viewport vp = null;
                if (ent != null)
                    ObjectId plID = ps.AppendEntity(ent);
                    tr.AddNewlyCreatedDBObject(pl, true);

                    vp = new Viewport();
                    tr.AddNewlyCreatedDBObject(vp, true);

                    vp.NonRectClipEntityId = plID;
                    vp.NonRectClipOn = true;
                    vp.On = true;


                LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
                if (!lt.Has("myLayerName"))
                    LayerTableRecord ltr = new LayerTableRecord();
                    ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 30);
                    ltr.Name = "myLayerName";
                    ObjectId layerId = lt.Add(ltr);
                    tr.AddNewlyCreatedDBObject(ltr, true);
                    ObjectId[] layersToFreezeIDs = new ObjectId[1] { layerId };



            using (Transaction tr = db.TransactionManager.StartTransaction())

                ObjectId ldid = db.LayoutDictionaryId;
                DBDictionary ld = (DBDictionary)tr.GetObject(ldid, OpenMode.ForRead, false);
                LayerTable layers = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);

                foreach (DBDictionaryEntry de in ld)
                    Layout layout = (Layout)tr.GetObject(de.Value, OpenMode.ForRead, false);
                    if (layout.LayoutName.ToLower() != "model")
                        ObjectIdCollection vpIDs = layout.GetViewports();
                        for (int i = 1; i < vpIDs.Count; i++)
                            Viewport vp2 = (Viewport)tr.GetObject(vpIDs[i], OpenMode.ForRead, false);
                            if (vp2 != null)
                                foreach (ObjectId layerId in layers)
                                    LayerTableRecord layer = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead);
                                    if (vp2.IsLayerFrozenInViewport(layer.ObjectId))
                                        ed.WriteMessage("\n Frozen Layer: " + layer.Name);
                                    ObjectIdCollection frozenLayerIDs = vp2.GetFrozenLayers();
                                    ed.WriteMessage("\n Frozen Layers");
                                    foreach (ObjectId lyrId in frozenLayerIDs)
                                        LayerTableRecord lyrTblRec = (LayerTableRecord)tr.GetObject(lyrId, OpenMode.ForRead);
                                        ed.WriteMessage("\n   " + lyrTblRec.Name);




            db.TileMode = false;



Valued Contributor
Posts: 95
Registered: ‎11-15-2011
Message 4 of 4 (226 Views)

Re: create, manipulate and focus new vport in new ps layout

03-05-2012 07:22 PM in reply to: FFlix

thanks for your response, even if i ended up starting from scratch, which i had to.


the first flaw i came across was that i couldn't actually import the autocad.interop namespace, something which necessitates the project's property reference to the namesake .dll in the respective objectarx' inc- folder.


this then enables accessing the preferences, pref.display.layoutcreateviewport in particular which, set to true, enables the creation of a viewport automatically when creating a new ps layout. (i thought this might be robuster than creating a new viewport. and even if not used this way, it should help to manage the new layout by any means.)


using the layoutmanager to switch to the newly created ps layout then does not create any problems anymore, nor are there any runtime exceptions (yet), and i concluded with someone's advice on manipulating an existing viewport here: http://forums.autodesk.com/t5/NET/Setting-attributes-of-current-viewport/m-p/3359101#M27483.


good night and good luck

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community