<?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 Re: How to Duplicate a Layout in a Database in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549507#M5548</link>
    <description>&lt;P&gt;This worked perfectly. Thank you for your solution. Here is the revised version:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;        private void CopyRebuildSheet()
        {
            var originalDB = HostApplicationServices.WorkingDatabase;
            Debug.Print("CopyRebuildSheet");
            string newLayoutName = "";
            foreach (DataGridViewRow Row in PlanSheetsAvailableTable.Rows)
            {
                DB_Sheet Sheet;
                Sheets.TryGetValue(Row.Cells[SheetNumColumnIndex].Value.ToString(), out Sheet);
                string FilePath = $"{Sheet.fullPath}\\{Sheet.fileName}";

                
                using(var db = OpenDrawingFile(FilePath))
                {
                    HostApplicationServices.WorkingDatabase = db;

                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        DBDictionary layoutDic = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) as DBDictionary;
                        foreach(DBDictionaryEntry entry in layoutDic)
                        {
                            ObjectId layoutId = entry.Value;
                            Layout layout = tr.GetObject(layoutId, OpenMode.ForRead) as Layout;
                            if (layout.LayoutName == Row.Cells[SheetNumColumnIndex].Value.ToString())
                            {
                                LayoutManager layoutManager = LayoutManager.Current;
                                newLayoutName =$"{layout.LayoutName} ({Row.Cells[RSNumColumnIndex].Value})";
                                layoutManager.CloneLayout(layout.LayoutName, newLayoutName, layoutDic.Count);

                                if (RSSheetsToBeImportedIntoSheetSetManager.ContainsKey(Sheet.fileName))
                                {
                                    RSSheetsToBeImportedIntoSheetSetManager[Sheet.fileName] += $"\n{newLayoutName}";
                                }
                                else
                                {
                                    RSSheetsToBeImportedIntoSheetSetManager.Add(Sheet.fileName, newLayoutName);
                                }
                                tr.Commit();
                                db.SaveAs(FilePath, DwgVersion.Current);

                                break;
                            }
                        }
                        HostApplicationServices.WorkingDatabase = originalDB;
                    }
                }

                EnableTheTitleBlock(newLayoutName);
            }
        }&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 08 Feb 2024 20:57:51 GMT</pubDate>
    <dc:creator>Poncho_Slim</dc:creator>
    <dc:date>2024-02-08T20:57:51Z</dc:date>
    <item>
      <title>How to Duplicate a Layout in a Database</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549262#M5546</link>
      <description>&lt;P&gt;I have attempted to Duplicate a Layout in a Database. I am unable to use a LayoutManager Object since I am trying to make changes to Documents (.dwg files) in the background to multiplle drawings in one sweep. The drawing has a layout with some viewports in it. A hard duplicate that LayoutManager.CopyLayout() worked great, but I realized that I need to be able to do this for drawings that are open in the background via accessing its Database, and since I dont have the drawing open, I wont be able to use LayoutManager.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the current code I had:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;private void CopyRebuildSheet()
        {
            Debug.Print("CopyRebuildSheet");
            string newLayoutName = "";
            foreach (DataGridViewRow Row in PlanSheetsAvailableTable.Rows)
            {
                DB_Sheet Sheet;
                Sheets.TryGetValue(Row.Cells[SheetNumColumnIndex].Value.ToString(), out Sheet);
                string FilePath = $"{Sheet.fullPath}\\{Sheet.fileName}";

                Database db = OpenDrawingFile(FilePath);
                using(Transaction tr = db.TransactionManager.StartTransaction())
                {
                    DBDictionary layoutDic = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) as DBDictionary;
                    foreach(DBDictionaryEntry entry in layoutDic)
                    {
                        ObjectId layoutId = entry.Value;
                        Layout layout = tr.GetObject(layoutId, OpenMode.ForRead) as Layout;
                        if (layout.LayoutName == Row.Cells[SheetNumColumnIndex].Value.ToString())
                        {
                            // Here is an ERROR
                            LayoutManager layoutManager = ;
                            
                            //ObjectId newLayoutId = layoutManager.CreateLayout($"{layout.LayoutName} ({Row.Cells[RSNumColumnIndex].Value.ToString()})");
                            newLayoutName =$"{layout.LayoutName} ({Row.Cells[RSNumColumnIndex].Value})";
                            //Layout newLayout = tr.GetObject(newLayoutId, OpenMode.ForWrite) as Layout;
                            layoutManager.CloneLayout(layout.LayoutName, newLayoutName, layoutDic.Count);

                            if (RSSheetsToBeImportedIntoSheetSetManager.ContainsKey(Sheet.fileName))
                            {
                                RSSheetsToBeImportedIntoSheetSetManager[Sheet.fileName] += $"\n{newLayoutName}";
                            }
                            else
                            {
                                RSSheetsToBeImportedIntoSheetSetManager.Add(Sheet.fileName, newLayoutName);
                            }

                            tr.Commit();
                            break;
                        }
                    }
                }

                EnableTheTitleBlock(newLayoutName);
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2024 18:54:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549262#M5546</guid>
      <dc:creator>Poncho_Slim</dc:creator>
      <dc:date>2024-02-08T18:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to Duplicate a Layout in a Database</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549347#M5547</link>
      <description>&lt;P&gt;You need to set the side database to HostApplicationService.WorkingDatabase in order to use LayoutManager against this database. Also, you need to dispose the side database when you are done with it. Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;// save original working database&lt;/P&gt;
&lt;P&gt;var originalDb=HostApplicationServices.WorkingDatabase;&lt;/P&gt;
&lt;P&gt;foreach (.....)&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp; using (var db=OpenDrawingFile(...))&lt;/P&gt;
&lt;P&gt;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;HostApplicationServices.WorkingDatabase=db;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;'' identify layout to copy&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;'' using LayerManager to copy a layout here&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;'db.SaveAs(...);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;// restore original working database&lt;/P&gt;
&lt;P&gt;HostApplicationServices.WorkingDatabase = originalDb;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2024 19:41:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549347#M5547</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2024-02-08T19:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to Duplicate a Layout in a Database</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549507#M5548</link>
      <description>&lt;P&gt;This worked perfectly. Thank you for your solution. Here is the revised version:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;        private void CopyRebuildSheet()
        {
            var originalDB = HostApplicationServices.WorkingDatabase;
            Debug.Print("CopyRebuildSheet");
            string newLayoutName = "";
            foreach (DataGridViewRow Row in PlanSheetsAvailableTable.Rows)
            {
                DB_Sheet Sheet;
                Sheets.TryGetValue(Row.Cells[SheetNumColumnIndex].Value.ToString(), out Sheet);
                string FilePath = $"{Sheet.fullPath}\\{Sheet.fileName}";

                
                using(var db = OpenDrawingFile(FilePath))
                {
                    HostApplicationServices.WorkingDatabase = db;

                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        DBDictionary layoutDic = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) as DBDictionary;
                        foreach(DBDictionaryEntry entry in layoutDic)
                        {
                            ObjectId layoutId = entry.Value;
                            Layout layout = tr.GetObject(layoutId, OpenMode.ForRead) as Layout;
                            if (layout.LayoutName == Row.Cells[SheetNumColumnIndex].Value.ToString())
                            {
                                LayoutManager layoutManager = LayoutManager.Current;
                                newLayoutName =$"{layout.LayoutName} ({Row.Cells[RSNumColumnIndex].Value})";
                                layoutManager.CloneLayout(layout.LayoutName, newLayoutName, layoutDic.Count);

                                if (RSSheetsToBeImportedIntoSheetSetManager.ContainsKey(Sheet.fileName))
                                {
                                    RSSheetsToBeImportedIntoSheetSetManager[Sheet.fileName] += $"\n{newLayoutName}";
                                }
                                else
                                {
                                    RSSheetsToBeImportedIntoSheetSetManager.Add(Sheet.fileName, newLayoutName);
                                }
                                tr.Commit();
                                db.SaveAs(FilePath, DwgVersion.Current);

                                break;
                            }
                        }
                        HostApplicationServices.WorkingDatabase = originalDB;
                    }
                }

                EnableTheTitleBlock(newLayoutName);
            }
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 08 Feb 2024 20:57:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549507#M5548</guid>
      <dc:creator>Poncho_Slim</dc:creator>
      <dc:date>2024-02-08T20:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to Duplicate a Layout in a Database</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549596#M5549</link>
      <description>&lt;P&gt;I think it would be slightly better, or more logically, to place&lt;/P&gt;
&lt;PRE class="lia-code-sample line-numbers language-general" tabindex="0"&gt;&lt;CODE&gt;HostApplicationServices.WorkingDatabase = originalDB;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;outside the "foreach..." loop: you only need to restore it back after all the files are processed. It would be even better to do it like this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var originalDb=HostApplicationServices.WorkingDatabase;&lt;/P&gt;
&lt;P&gt;try&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; foreach (....)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.....&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;finally&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; // this restoration is guaranteed, even exception is raised in the processing loop&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; HostApplicationServices.WorkingDatabase = origialDb;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2024 21:50:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-duplicate-a-layout-in-a-database/m-p/12549596#M5549</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2024-02-08T21:50:15Z</dc:date>
    </item>
  </channel>
</rss>

