Add new layout this object

Add new layout this object

Anonymous
Not applicable
3,187 Views
7 Replies
Message 1 of 8

Add new layout this object

Anonymous
Not applicable

Hi, have a problem.

There is a function that creates a few new layouts.

On each page i want to add viewport, but nothing action.

 

Insert Layouts

[CommandMethod("myAddLayout")]
        public void GenerateLayouts() 
        {
            int layoutCount = this.length / this.layoutLength;
            using (Transaction tr = this.acCurDb.TransactionManager.StartTransaction())
            {
                LayoutManager acLayoutMgr = LayoutManager.Current;
                for (int i = 0; i < layoutCount; i++)
                {
                    // Create the new layout with default settings
                    String layoutName = i * this.layoutLength + " - " + (i + 1) * this.layoutLength;
                    ObjectId newLayoutId = acLayoutMgr.CreateLayout(layoutName);
                    Layout lay = tr.GetObject(newLayoutId, OpenMode.ForRead) as Layout;

                    CreateViewPort(lay.ObjectId);
                }
                tr.Commit();
            }
        }

 Insert ViewPort

public void CreateViewPort(ObjectId layoutId)
        {
            using (Transaction tr = this.acCurDb.TransactionManager.StartTransaction())
            {
                Layout layout = tr.GetObject(layoutId, OpenMode.ForRead) as Layout;
                BlockTableRecord acBlkTblRec2 = tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord;
                
                // Create a Viewport
                Viewport acVport = new Viewport();
                acVport.SetDatabaseDefaults();

                // Add the new object to the block table record and the transaction
                acBlkTblRec2.AppendEntity(acVport);
                tr.AddNewlyCreatedDBObject(acVport, true);

                acVport.ViewDirection = new Vector3d(0, 0, 1);
                // Enable the viewport
                acVport.On = true;

                // Save the new objects to the database
                tr.Commit();
            }     
        }

 But if call function - CreateViewPort, ViewPort is create on first page.

 

0 Likes
Accepted solutions (1)
3,188 Views
7 Replies
Replies (7)
Message 2 of 8

DiningPhilosopher
Collaborator
Collaborator
Try calling the Initialize() method of the Layout before you try to do anything with it.
0 Likes
Message 3 of 8

Anonymous
Not applicable
Layout lay = tr.GetObject(newLayoutId, OpenMode.ForRead) as Layout;
lay.Initialize();

return error

Error during command executing: 'Unsupported method!'.

0 Likes
Message 4 of 8

Anonymous
Not applicable

Layout is copy ok.

I have error on insert only ViewPort to new layout.

0 Likes
Message 5 of 8

Balaji_Ram
Alumni
Alumni
Accepted solution

As DP had mentioned, layout is to be initialized and it can be done by switching to it by making it current.

Here is a code snippet that uses"CurrentLayout" before creating the paperspace viewport.

 

You can also refer to this document for a code sample to create paperspace viewport :

http://docs.autodesk.com/CIV3D/2012/ENU/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-343a.htm

 

 

[CommandMethod("AddLayout")]
public void AddLayoutMethod() 
{
    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    Database db = Application.DocumentManager.MdiActiveDocument.Database;

    int layoutCount = 3;
    int layoutLength = 3;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        LayoutManager acLayoutMgr = LayoutManager.Current;
        for (int i = 0; i < layoutCount; i++)
        {
            String layoutName = i * layoutLength + " - " + (i + 1) * layoutLength;

            ObjectId newLayoutId = acLayoutMgr.CreateLayout(layoutName);
            Layout lay = tr.GetObject(newLayoutId, OpenMode.ForRead) as Layout;

            LayoutManager.Current.CurrentLayout = layoutName;

            CreateViewport(lay.ObjectId);
        }
        tr.Commit();
    }
}

public void CreateViewport(ObjectId layoutId)
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    ObjectId oid;

    try
    {
        using (Transaction Tx = db.TransactionManager.StartTransaction())
        {
            Layout LayoutDest = (Layout)Tx.GetObject(layoutId, OpenMode.ForRead);
            BlockTableRecord btrDest = (BlockTableRecord)Tx.GetObject(LayoutDest.BlockTableRecordId, OpenMode.ForWrite);

            Autodesk.AutoCAD.DatabaseServices.Viewport vpNew = new Autodesk.AutoCAD.DatabaseServices.Viewport();
            vpNew.SetDatabaseDefaults();
            vpNew.Width = 6.0;
            vpNew.Height = 5.0;
            vpNew.CenterPoint = new Point3d(3.25, 3, 0);

            oid = btrDest.AppendEntity(vpNew);
            Tx.AddNewlyCreatedDBObject(vpNew, true);

            vpNew.ViewDirection = Vector3d.ZAxis;
            vpNew.On = true;
                    
            Tx.Commit();
        }
    }
    catch (System.Exception ex)
    {
        ed.WriteMessage(ex.Message);
    }
}

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 6 of 8

Anonymous
Not applicable

Hi Balaji,

 

How to display an area in model at selected viewport?

 

Thank so much,

0 Likes
Message 7 of 8

Balaji_Ram
Alumni
Alumni

Hi,

 

The Viewport parameters such as "ViewHeight", "ViewTarget", "ViewDirection", "TwistAngle" and "ViewCenter" govern the part of the model space that you see in the viewport. Setting appropriate values for it will change the area of the model space that you see in the viewport.

 

Here is a sample code to set the viewport parameters such that it shows what you see in the model space.

 

Document activeDoc = Application.DocumentManager.MdiActiveDocument;
Database db = activeDoc.Database;
Editor ed = activeDoc.Editor;

PromptEntityOptions peo = new PromptEntityOptions("Select a viewport : ");
peo.SetRejectMessage("Select a viewport.");
peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Viewport), true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
    ed.WriteMessage("Nothing selected. Please run the command again.");
    return;
}

ObjectId oid = per.ObjectId;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    ViewportTable vt = tr.GetObject(db.ViewportTableId, OpenMode.ForRead) as ViewportTable;
    ViewportTableRecord vtr = tr.GetObject(vt["*Active"], OpenMode.ForWrite) as ViewportTableRecord;
    Autodesk.AutoCAD.DatabaseServices.Viewport vp
        = tr.GetObject(oid, OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.Viewport;

    if (vtr != null && vp != null)
    {
        vp.ViewHeight = vtr.Height;
        vp.ViewTarget = vtr.Target;
        vp.ViewDirection = vtr.ViewDirection;
        vp.TwistAngle = vtr.ViewTwist;
        vp.ViewCenter = vtr.CenterPoint;
    }
    tr.Commit();
}

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 8 of 8

Anonymous
Not applicable

Thanks so much

0 Likes