.NET

Reply
Member
horus20
Posts: 3
Registered: ‎04-29-2013
Message 1 of 8 (962 Views)
Accepted Solution

Add new layout this object

962 Views, 7 Replies
04-29-2013 02:24 AM

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.

 

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);
}
}

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 8 (938 Views)

Re: Add new layout this object

04-29-2013 12:04 PM in reply to: horus20
Try calling the Initialize() method of the Layout before you try to do anything with it.
Member
horus20
Posts: 3
Registered: ‎04-29-2013
Message 3 of 8 (929 Views)

Re: Add new layout this object

04-29-2013 05:20 PM in reply to: DiningPhilosopher
Layout lay = tr.GetObject(newLayoutId, OpenMode.ForRead) as Layout;
lay.Initialize();

return error

Error during command executing: 'Unsupported method!'.

Member
horus20
Posts: 3
Registered: ‎04-29-2013
Message 4 of 8 (916 Views)

Re: Add new layout this object

04-29-2013 08:22 PM in reply to: horus20

Layout is copy ok.

I have error on insert only ViewPort to new layout.

ADN Support Specialist
Balaji_Ram
Posts: 723
Registered: ‎03-21-2011
Message 5 of 8 (881 Views)

Re: Add new layout this object

05-03-2013 02:44 AM in reply to: horus20

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

Active Contributor
ditran
Posts: 27
Registered: ‎01-28-2013
Message 6 of 8 (858 Views)

Re: Add new layout this object

05-06-2013 10:47 PM in reply to: Balaji_Ram

Hi Balaji,

 

How to display an area in model at selected viewport?

 

Thank so much,

ADN Support Specialist
Balaji_Ram
Posts: 723
Registered: ‎03-21-2011
Message 7 of 8 (835 Views)

Re: Add new layout this object

05-09-2013 01:29 AM in reply to: ditran

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

Active Contributor
ditran
Posts: 27
Registered: ‎01-28-2013
Message 8 of 8 (809 Views)

Re: Add new layout this object

05-14-2013 03:23 AM in reply to: Balaji_Ram

Thanks so much

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.