Distinguished Contributor spu
Distinguished Contributor
Posts: 141
Registered: ‎02-21-2007
Message 1 of 3 (1,136 Views)
Accepted Solution

create paper space viewport and show some model portion in view

1136 Views, 2 Replies
11-11-2012 12:52 AM



We like to allow user to select a rectagular area in modal scpace. Then the code creates a paperscace viewport in a spcified layout. The viewport will show the selected area. Please find the old VBA code that achived this and the .net code I am trying with. .Net code always gives some exception.


VBA Code


 'Lets find the center of the viewport in paperspace.

    Dim center(0 To 2) As Double
    center(0) = newWidth / 2
    center(1) = newLength / 2
    center(2) = 0

    Dim lay As AcadLayout

    For Each ent In ThisDrawing.layouts
        If ent.Name = "Betsy" Then
            Set lay = ent
        End If

    ThisDrawing.ActiveLayout = lay
    ThisDrawing.ActiveSpace = acPaperSpace

    'Lets create the actual viewport

    Set newVport = ThisDrawing.PaperSpace.AddPViewport(center, newWidth, newLength)
    newVport.TrueColor = myclr
    ' Enable the viewport
    newVport.Display True
    ' Turn on model space editing
    ThisDrawing.MSpace = True    'Here pls check

    ' Set newVport current
    ' (not always necessary but a good idea)
    ThisDrawing.ActivePViewport = newVport

    Dim ModalLeftLower(0 To 2) As Double
    Dim ModalRightUpper(0 To 2) As Double

    ModalLeftLower(0) = ModalLeftUpper(0)
    ModalLeftLower(1) = ModalRightBottom(1)
    ModalLeftLower(2) = 0

    ModalRightUpper(0) = ModalRightBottom(0)
    ModalRightUpper(1) = ModalLeftUpper(1)
    ModalRightUpper(2) = 0

    ' Zoom window in model space
    ZoomWindow ModalLeftLower, ModalRightUpper

    ' Turn model space editing off
    ThisDrawing.MSpace = False


More code


.Net Code


 Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

      Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database;

      Autodesk.AutoCAD.EditorInput.Editor ed = doc.Editor;


      // We're accessing drawing objects, so we need a transaction



      using (Autodesk.AutoCAD.DatabaseServices.Transaction tr = db.TransactionManager.StartTransaction())


        // Get the primary paperspace from the block table

        Autodesk.AutoCAD.DatabaseServices.BlockTable bt =  (Autodesk.AutoCAD.DatabaseServices.BlockTable)tr.GetObject(db.BlockTableId,Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);

        Autodesk.AutoCAD.DatabaseServices.BlockTableRecord ps =(Autodesk.AutoCAD.DatabaseServices.BlockTableRecord)tr.GetObject(bt[Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.PaperSpace], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);

         // Create a variety of objects for our clip boundaries


           //Autodesk.AutoCAD.DatabaseServices.DBObjectCollection objs = new Autodesk.AutoCAD.DatabaseServices.DBObjectCollection();

           // Create our viewport, adding that also

           Autodesk.AutoCAD.DatabaseServices.Viewport vp = new Autodesk.AutoCAD.DatabaseServices.Viewport();
           vp.CenterPoint = new Autodesk.AutoCAD.Geometry.Point3d(newWidth/2, newLength/2, 0);
           vp.Width = newWidth;
           vp.Height = newLength;


            tr.AddNewlyCreatedDBObject(vp, true);
           //vp.ViewDirection = new Autodesk.AutoCAD.Geometry.Vector3d(1, 1, 1);
          //  vp.On = true;
            // Set the new viewport current via an imported ObjectARX function
          //  acedSetCurrentVPort(vp.UnmanagedObject);
          //  tr.Commit();
           Autodesk.AutoCAD.Geometry.Point3d ptll = new Autodesk.AutoCAD.Geometry.Point3d(ModalLeftUpper.X, ModalRightBottom.Y, 0);
              Autodesk.AutoCAD.Geometry.Point3d ptur = new Autodesk.AutoCAD.Geometry.Point3d(ModalRightBottom.X, ModalLeftUpper.Y, 0);
             Zoom.ZoomWindow(ptll, ptur);
            //Autodesk.AutoCAD.Geometry.Point3d ptll = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);
            //Autodesk.AutoCAD.Geometry.Point3d ptur = new Autodesk.AutoCAD.Geometry.Point3d(100, 100, 0);

            //Autodesk.AutoCAD.Interop.AcadApplication pApp;
            //pApp = (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

            //pApp.ZoomWindow(ptll.ToArray(), ptur.ToArray());










ADN Support Specialist
Posts: 693
Registered: ‎03-21-2011
Message 2 of 3 (1,074 Views)

Re: create paper space viewport and show some model portion in view

11-20-2012 03:51 AM in reply to: spu

Hi Shijith,


I dont have a ready code to do exactly the same steps.


But here is a Devblog post that can give you a start.



It sets the paperspace viewport parameters based on the database extents. You can modify it to set the viewport parameters based on any other rectangle corner points. In the sample code from the blog post, the "x_Min" and "x_Max" are the database extent points but in your case it will represent the extents of the selected rectangle in model space.


This will allow you to set the paperspace viewport to show the model within the selected rectangle.


Hope this helps.


Developer Technical Services
Autodesk Developer Network

Distinguished Contributor spu
Distinguished Contributor
Posts: 141
Registered: ‎02-21-2007
Message 3 of 3 (1,016 Views)

Re: create paper space viewport and show some model portion in view

12-04-2012 04:54 AM in reply to: Balaji_Ram



Thanks Balaji. It worked like a charm....





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 or visit the Installation and Licensing Forum to get help installing your software.