Author Topic: Current layout renaming  (Read 2588 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Current layout renaming
« on: May 17, 2013, 04:25:57 AM »
Below are located two variants of the layouts  iteration, and renaming them. But both methods ain't rename the current layout. Why?

Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using cad = Autodesk.AutoCAD.ApplicationServices.Application;
  6. using App = Autodesk.AutoCAD.ApplicationServices;
  7. using Rtm = Autodesk.AutoCAD.Runtime;
  8. using Db = Autodesk.AutoCAD.DatabaseServices;
  9. using Ed = Autodesk.AutoCAD.EditorInput;
  10.  
  11. namespace Layouts {
  12.  
  13.     public class Class1 {
  14.  
  15.         [Rtm.CommandMethod("RenameAllLayouts1")]
  16.         public void testRenameAllLayouts1() {
  17.             App.Document doc = cad.DocumentManager.MdiActiveDocument;
  18.             Db.Database db = doc.Database;
  19.             Ed.Editor ed = doc.Editor;
  20.  
  21.             using (Db.Transaction tr = db.TransactionManager.StartTransaction()) {
  22.                 Db.BlockTable bt = tr.GetObject(db.BlockTableId, Db.OpenMode.ForRead)
  23.                     as Db.BlockTable;
  24.  
  25.                 foreach (Db.ObjectId id in bt) {
  26.                     Db.BlockTableRecord btr = tr.GetObject(id, Db.OpenMode.ForRead)
  27.                         as Db.BlockTableRecord;
  28.                     if (btr.IsLayout) {
  29.                         Db.Layout layout = tr.GetObject(btr.LayoutId, Db.OpenMode.ForWrite)
  30.                             as Db.Layout;
  31.                         if (!layout.LayoutName.Equals("Model"))
  32.                             layout.LayoutName += " (renamed)";
  33.                     }
  34.                 }
  35.                 tr.Commit();
  36.             }
  37.         }
  38.  
  39.         [Rtm.CommandMethod("RenameAllLayouts2")]
  40.         public void testRenameAllLayouts2() {
  41.             App.Document doc = cad.DocumentManager.MdiActiveDocument;
  42.             Db.Database db = doc.Database;
  43.             using (Db.Transaction tr = db.TransactionManager.StartTransaction()) {
  44.                 Db.DBDictionary ldict = (Db.DBDictionary) tr.GetObject(db.LayoutDictionaryId,
  45.                     Db.OpenMode.ForRead);
  46.                 Db.LayoutManager lm = Db.LayoutManager.Current;
  47.                 foreach (Db.DBDictionaryEntry dicent in ldict) {
  48.                     Db.Layout lay = (Db.Layout) dicent.Value.GetObject(Db.OpenMode.ForRead);
  49.                     if (!lay.LayoutName.Equals("Model")) {
  50.                         lay.UpgradeOpen();
  51.                         lm.RenameLayout(lay.LayoutName, lay.LayoutName + " (renamed2)");
  52.                     }
  53.                 }
  54.                 tr.Commit();
  55.             }
  56.         }
  57.     }
  58. }
  59.  
Best Regards.

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Current layout renaming
« Reply #1 on: May 17, 2013, 12:43:34 PM »
What version are you using? It works fine on 2014, however the changes weren't visual until doing a regen.  At first I thought this would be a simple fix by using the documents transaction manager instead of the database, but for some reason that still didn't work and was forced to add ed.Regen() after your transaction.

Hope that helps

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Current layout renaming
« Reply #2 on: May 17, 2013, 01:12:36 PM »
What version are you using? It works fine on 2014, however the changes weren't visual until doing a regen.  At first I thought this would be a simple fix by using the documents transaction manager instead of the database, but for some reason that still didn't work and was forced to add ed.Regen() after your transaction.

Hope that helps
I've use the AutoCAD 2009. I will try call the ed.Regen() in Monday, when I'll come in the office.

Thank you.