<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Create Layouts and Viewoirts in External drawings in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5716630#M39380</link>
    <description>&lt;P&gt;Is it possible to create layouts and viewports in an external drawing?&amp;nbsp; All the examples I've found in the documentation and on line assume using the current drawing.&amp;nbsp; The code below compiles and runs, creates the layouts, but when the created drawing is opened, the viewports are not there.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;BR /&gt;Paul&lt;/P&gt;</description>
    <pubDate>Mon, 13 Jul 2015 13:52:03 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2015-07-13T13:52:03Z</dc:date>
    <item>
      <title>Create Layouts and Viewoirts in External drawings</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5716630#M39380</link>
      <description>&lt;P&gt;Is it possible to create layouts and viewports in an external drawing?&amp;nbsp; All the examples I've found in the documentation and on line assume using the current drawing.&amp;nbsp; The code below compiles and runs, creates the layouts, but when the created drawing is opened, the viewports are not there.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;BR /&gt;Paul&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2015 13:52:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5716630#M39380</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-07-13T13:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create Layouts and Viewoirts in External drawings</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5718242#M39381</link>
      <description>&lt;P&gt;Short answer is yes it is possible. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just glanced at your code in passing and the only comment that I have is that I obtain paper space differently then what you did. &amp;nbsp;I use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;                Dim blockTable = CType(transaction.GetObject(database.BlockTableId, OpenMode.ForWrite), BlockTable)
                Dim paperSpace = CType(transaction.GetObject(blockTable(BlockTableRecord.PaperSpace), OpenMode.ForWrite), BlockTableRecord)&lt;/PRE&gt;
&lt;P&gt;Not sure if that is what is wrong or not without going more in depth in analyzing your code.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2015 12:42:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5718242#M39381</guid>
      <dc:creator>Keith.Brown</dc:creator>
      <dc:date>2015-07-14T12:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create Layouts and Viewoirts in External drawings</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5725478#M39382</link>
      <description>&lt;P&gt;I beleive the problem is that Layout needs to be activated first. See:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://adndevblog.typepad.com/autocad/2012/08/cloned-viewport-exists-but-is-invisible-in-a-newly-created-layout.html" target="_blank"&gt;http://adndevblog.typepad.com/autocad/2012/08/cloned-viewport-exists-but-is-invisible-in-a-newly-created-layout.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;So the real question is:&amp;nbsp; How do I make the newly created Layout active in an external database?&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;Paul&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jul 2015 19:58:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5725478#M39382</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-07-17T19:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create Layouts and Viewoirts in External drawings</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5725558#M39383</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try to make the side database the working one and then use the LayoutManager (don't forget to reset the working database to the active document one).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;HostApplicationServices.WorkingDatabase = sideDatabase;
LayoutManager layoutMgr = LayoutManager.Current;
layoutMgr.CurrentLayout = "TheNewlyCreatedLayout"; &lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jul 2015 21:22:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5725558#M39383</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2015-07-17T21:22:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create Layouts and Viewoirts in External drawings</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5732430#M39384</link>
      <description>&lt;P&gt;Ok, that helped somewhat.&amp;nbsp; One of the viewports gets created (not both), but now AutoCAD crashes after it's all done.&lt;BR /&gt;&lt;BR /&gt;I had to separate the viewport creation into two transactions.&amp;nbsp; Inside one transaction, only the second viewport would be created; the first not at all.&amp;nbsp; With a separate transactions, the first viewport gets created, but nothing is visible inside it.&lt;BR /&gt;&lt;BR /&gt;Also, after the command is done, and control is passed back the user, a few seconds later AutoCAD crashes.&amp;nbsp; This does not happen if I remove the viewport creation code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;public static void CreateLayoutDrawing(string file1, string file2)
{
  Document doc = Application.DocumentManager.MdiActiveDocument;
  Editor ed = doc.Editor;
  //Database db = doc.Database;
  BlockTable bt;
  BlockTableRecord btr;
  BlockReference bref;
  Object obj;
  ObjectId loid;
  DBObject dbobj;
  Point3d pt;
  Transaction tr;
  Layout layout;
  Viewport vp;

  // A new database, drawing
  Database db = new Database(false, true);
      
  try
  {
    db.ReadDwgFile(file1, System.IO.FileShare.ReadWrite, true, "");
  }
  catch (System.Exception)
  {
    ed.WriteMessage("\nUnable to open file: " + file1);
    return;
  }

  HostApplicationServices.WorkingDatabase = db;
  LayoutManager lmgr = LayoutManager.Current;

  tr = db.TransactionManager.StartTransaction();
  using (tr)
  {

    bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

    btr = new BlockTableRecord();
    btr.Name = "*Paper_Space";
    layout = new Layout();
    layout.LayoutName = "Block 1";
    bt.UpgradeOpen();
    loid = bt.Add(btr);
    bt.DowngradeOpen();
    tr.AddNewlyCreatedDBObject(btr, true);
    // Add the layout to the layout dictionary associating the databse and block table record to the layout
    layout.AddToLayoutDictionary(db, loid);
    tr.AddNewlyCreatedDBObject(layout, true);

    lmgr.CurrentLayout = "Block 1";
    //db.TileMode = true;	// Makes no difference if true or false

    // Create and Setup the viewport
    vp = new Viewport();
    vp.SetDatabaseDefaults();
    vp.ViewCenter = new Point2d(10, 10);
    vp.ViewHeight = 15;
    btr.AppendEntity(vp);
    tr.AddNewlyCreatedDBObject(vp, true);
    vp.CenterPoint = new Point3d(3, 2, 0);
    vp.Height = 7;
    vp.Width = 12;
    vp.Visible = true;

    tr.Commit();
    // Almost works.  Viewport exists, but nothing is visible in it.
    // If I try to put both View ports in the same transaction, then "Block 1" does
    // not get created at all.
  }

  tr = db.TransactionManager.StartTransaction();
  using (tr)
  {
    bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

    btr = new BlockTableRecord();
    btr.Name = "*Paper_Space";
    layout = new Layout();
    layout.LayoutName = "Block 2";
    bt.UpgradeOpen();
    loid = bt.Add(btr);
    bt.DowngradeOpen();
    tr.AddNewlyCreatedDBObject(btr, true);
    // Add the layout to the layout dictionary associating the databse and block table record to the layout
    layout.AddToLayoutDictionary(db, loid);
    tr.AddNewlyCreatedDBObject(layout, true);

    lmgr.CurrentLayout = "Block 2";
    //db.TileMode = true;	// Makes no difference if true or false

    // Create and Setup the viewport
    vp = new Viewport();
    vp.SetDatabaseDefaults();
    vp.ViewCenter = new Point2d(10, 10);
    vp.ViewHeight = 15;
    btr.AppendEntity(vp);
    tr.AddNewlyCreatedDBObject(vp, true);
    vp.CenterPoint = new Point3d(3, 2, 0);
    vp.Height = 7;
    vp.Width = 12;
    vp.Visible = true;

    tr.Commit();
	// This one works. And it's exactly the same as the first, which doesn't
	// Why ?????
  }

  HostApplicationServices.WorkingDatabase = doc.Database;

  // Save the new drawing
  db.SaveAs(file2, true, DwgVersion.Current, db.SecurityParameters);
  db.Dispose();
}

// After this is all done, AutoCAD crashes. Not immediately, but after a few seconds.
// If I remove the viewport creation code, no crash.&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jul 2015 20:39:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5732430#M39384</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-07-21T20:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create Layouts and Viewoirts in External drawings</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5733351#M39385</link>
      <description>&lt;P&gt;I've had better results using the LayoutManager to create layouts, like so:&lt;/P&gt;&lt;PRE&gt;           LayoutManager layoutManager = LayoutManager.Current;
            layoutId = layoutManager.GetLayoutId(layoutName);
            if (layoutId.IsNull)
            {
               
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                layoutId = layoutManager.CreateLayout(layoutName);
               Layout layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
                layout.UpgradeOpen();
                layout.Initialize();
                tr.Commit();
             }
&lt;BR /&gt;...
               
           using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Layout layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead);
 
                Viewport vp = new Viewport();
                vp.SetDatabaseDefaults();
                btr.UpgradeOpen();
                btr.AppendEntity(vp);
                tr.AddNewlyCreatedDBObject(vp, true);

                vp.Visible = true;
                vp.On = true;
                vp.Locked = true;
                vp.UpdateDisplay();

                tr.Commit();
            }
...&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jul 2015 11:49:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/5733351#M39385</guid>
      <dc:creator>SENL1362</dc:creator>
      <dc:date>2015-07-22T11:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create Layouts and Viewoirts in External drawings</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/6749427#M39386</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know it's an old topic but I've been having the same problem while creating viewports on unnactivated layouts and I came up with a solution.&lt;/P&gt;&lt;P&gt;Its a tricky workaround and I'd like to share it &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First I create the layout and its blocktablerecord:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary layoutsEx = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
                BlockTable blkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

                using (BlockTableRecord blkBlkRec = new BlockTableRecord())
                {
                    int layoutCount = layoutsEx.Count - 1;

                    blkBlkRec.Name = "*Paper_Space" + layoutCount.ToString();
                    blkTbl.Add(blkBlkRec);
                    tr.AddNewlyCreatedDBObject(blkBlkRec, true);

                    DBDictionary layouts = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) as DBDictionary;

                    using (Layout lay = new Layout())
                    {
                        lay.LayoutName = LayoutName;
                        lay.AddToLayoutDictionary(db, blkBlkRec.ObjectId);
                        tr.AddNewlyCreatedDBObject(lay, true);
                    }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The thing is that AutoCad always recognize the first Viewport on a layout's Blocktablerecord as the layout's own ViewportTableRecord alike (because Layouts do no have ViewportTable like the Modelspace), so when you create and configure the first Viewport of a Layouts BlockTableRecord manually AutoCad makes a mess and assumes it to be Layout View.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a solution,&amp;nbsp;before committing the transaction I'd add a Viewport to the BlockTableRecord with the same custom properties that Autocad would use in this situation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;                    using (Autodesk.AutoCAD.DatabaseServices.Viewport VP = new Autodesk.AutoCAD.DatabaseServices.Viewport()
                        {
                            Color = Color.FromRgb(51, 51, 51),
                            BackClipDistance = 0,
                            BackClipOn = false,
                            Background = ObjectId.Null,
                            Brightness = 0,
                            CenterPoint = new Point3d(0, 0, 0),
                            CircleSides = 1000,
                            Contrast = 0,
                            CustomScale = 0.0143,
                            DefaultLightingOn = true,
                            DefaultLightingType = Autodesk.AutoCAD.DatabaseServices.DefaultLightingType.TwoDistantLights,
                            Elevation = 0,
                            FastZoomOn = true,
                            FrontClipAtEyeOn = true,
                            FrontClipDistance = 0,
                            FrontClipOn = false,
                            GridAdaptive = true,
                            GridBoundToLimits = false,
                            GridFollow = false,
                            GridIncrement = new Vector2d(10, 10),
                            GridMajor = 5,
                            GridSubdivisionRestricted = true,
                            Height = 9,
                            HiddenLinesRemoved = false,
                            LensLength = 50,
                            Locked = false,
                            NonRectClipEntityId = ObjectId.Null,
                            NonRectClipOn = false,
                            PerspectiveOn = false,
                            ShadePlot = ShadePlotType.AsDisplayed,
                            SnapAngle = 0,
                            SnapBasePoint = new Point2d(0, 0),
                            SnapIncrement = new Vector2d(10, 10),
                            SnapIsometric = false,
                            SnapIsoPair = 0,
                            SnapOn = false,
                            TwistAngle = 0,
                            UcsFollowModeOn = false,
                            UcsIconAtOrigin = false,
                            UcsIconVisible = false,
                            VisualStyleId = (db.VisualStyleDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary).GetAt("2dWireframe")
                            })
                    {
                        blkBlkRec.AppendEntity(VP);
                        tr.AddNewlyCreatedDBObject(VP, true);
&lt;BR /&gt;                        //These properties after adding to Database because they are Database Dependent:
                        VP.GridOn = false;
                        VP.UcsFollowModeOn = false;
                        VP.UcsIconAtOrigin = false;
                        VP.UcsIconVisible = true;
                        VP.UcsPerViewport = true;
                    }
                }

                tr.Commit();
            }&lt;/PRE&gt;&lt;P&gt;Now you can add to this layout BlockTableRecord as many viewports as you'd like and there should be no more errors. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 17:23:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-layouts-and-viewoirts-in-external-drawings/m-p/6749427#M39386</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-12-14T17:23:26Z</dc:date>
    </item>
  </channel>
</rss>

