.NET

Reply
Valued Contributor
spanqy
Posts: 90
Registered: ‎11-13-2006
Message 1 of 6 (399 Views)
Accepted Solution

Adding a View (viewTableRecord) to Paper Space Layout

399 Views, 5 Replies
01-16-2013 01:13 PM

I have used the following code in a small app to create named views: LINK_TO_Autodesk_Help_Topic

HOWEVER...A user of mine tried running the command while in a paper space layout.  It appears the view still gets created but only within Model Space. 

 

The help DOES SAY that "a default model space view is created", but how would I specify that I want the view to be created within the "current" layout, either Model or Paper space?

 

I tried setting the .OwnerID property of the ViewTableRecord to "doc.CurrentSpaceID" but that did not work - no error either.

This is the same in my code:

pViewTR.IsPaperspaceView = true;

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 6 (370 Views)

Re: Adding a View (viewTableRecord) to Paper Space Layout

01-17-2013 12:19 AM in reply to: spanqy
Spoiler
 

See article here

http://adndevblog.typepad.com/autocad/2012/08/create-paper-space-viewports.html

Here is a code very quickly translated on C#:

        [CommandMethod("VW")]
        public void tryCreateViewPort()
        {
            // Only works in paperspace
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    ObjectId mCurViewportId = ed.ActiveViewportId; 

                    if (mCurViewportId == ObjectId.Null)
                    {
                        ed.WriteMessage("\nCommand only works in paperspace.");

                        return;

                    }

                    Viewport pCurViewport = tr.GetObject(mCurViewportId, OpenMode.ForRead) as Viewport;

                    if (pCurViewport == null)
                    {

                        ed.WriteMessage("\nCannot get active viewport.");

                        return;

                    }

                    if (pCurViewport.Number != 1)
                    {

                        ed.WriteMessage("\nCommand only works in paperspace.");

                        return;

                    }

                    // Ask for the position

                    Point3d pt1; Point3d pt2;

                    pt1 = ed.GetPoint("\nSelect first corner: ").Value;
             
                    pt2 = ed.GetCorner("\nSelect second corner: ", pt1).Value;
       
                    // Ask for the view to use

                    string mViewName;

                    mViewName = ed.GetString("\nEnter name of view to use: ").StringResult;
                
                    // Create new viewport

                    Viewport pViewport = new Viewport();

                    pViewport.Width = Math.Abs(pt2.X - pt1.X);

                    pViewport.Height = Math.Abs(pt2.Y - pt1.Y);

                    pViewport.CenterPoint = new Point3d(

                      pt1.X + (pt2.X - pt1.X) / 2,

                      pt1.Y + (pt2.Y - pt1.Y) / 2,

                      pt1.Z);

                    // Append new viewport to paper space

                    BlockTable pTable;

                    BlockTableRecord pPsBTR;

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

                    ObjectId mViewPortId = pPsBTR.AppendEntity(pViewport);

                    tr.AddNewlyCreatedDBObject(pViewport, true);
                    pViewport.On = true;
                    pViewport.FastZoomOn = true;

                    pViewport.SetUcs(pViewport.CenterPoint, Vector3d.XAxis, Vector3d.YAxis);
               

                    // Set the view

                    

                    ViewTable pViewTable = (ViewTable)tr.GetObject(db.ViewTableId, OpenMode.ForRead);
                  
                    // initialize the layout

                   // Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("TILEMODE", 0);
                  //  Layout layout = tr.GetObject(pPsBTR.LayoutId, OpenMode.ForWrite) as Layout;
                    //  LayoutManager.Current.CurrentLayout = "Layout2";
                    // layout.Initialize();

                    ViewTableRecord pViewTR;
                    if (pViewTable.Has(mViewName))
                    {
                        pViewTR = (ViewTableRecord)tr.GetObject(pViewTable[mViewName], OpenMode.ForWrite);
                    }
                    else
                    {
                        pViewTR = new ViewTableRecord();
                        pViewTR.Name = mViewName;
                        pViewTR.IsPaperspaceView = true;
                        pViewTable.UpgradeOpen();
                        ObjectId vtId = pViewTable.Add(pViewTR);

                        tr.AddNewlyCreatedDBObject(pViewTR, true);
                    }
                    

                    ed.SetCurrentView(pViewTR);

                    tr.Commit();

                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("TILEMODE", 0);
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
spanqy
Posts: 90
Registered: ‎11-13-2006
Message 3 of 6 (360 Views)

Re: Adding a View (viewTableRecord) to Paper Space Layout

01-17-2013 07:47 AM in reply to: Hallex

I really appreciate you taking the time to reply but...

 

Your post and the link refer to a paper space "Viewport" like you do manually with the "MVIEW" command. What I needed to do was create a "View" in paper space like you do manually with the "VIEW" command.

 

Regards

Moderator
Alexander.Rivilis
Posts: 1,412
Registered: ‎04-09-2008
Message 4 of 6 (357 Views)

Re: Adding a View (viewTableRecord) to Paper Space Layout

01-17-2013 09:08 AM in reply to: spanqy

ViewTableRecord.IsPaperspaceView = true can help you


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 6 (342 Views)

Re: Adding a View (viewTableRecord) to Paper Space Layout

01-17-2013 12:39 PM in reply to: Alexander.Rivilis

This is the same in my code:

pViewTR.IsPaperspaceView = true;

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
spanqy
Posts: 90
Registered: ‎11-13-2006
Message 6 of 6 (338 Views)

Re: Adding a View (viewTableRecord) to Paper Space Layout

01-17-2013 12:54 PM in reply to: Hallex

That was easy!

 

Thank you both Hallex and Alexander!

Announcements
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.