Author Topic: SetOrMakeLAYOUT and Zoom Ext  (Read 2654 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
SetOrMakeLAYOUT and Zoom Ext
« on: May 15, 2017, 12:41:05 AM »
Ref:
https://forums.autodesk.com/t5/net/set-new-layout-to-zoom-extents-in-c/m-p/7081775#U7081775

Posting here for better code pane quality

Proof of Concept code.

Code - C#: [Select]
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using CadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  6.  
  7. [assembly: CommandClass(typeof(KdubServices.Test0315.MyCommands))]
  8.  
  9. namespace KdubServices.Test0315
  10. {
  11.     public class MyCommands
  12.     {
  13.         [CommandMethod("kb_SetOrMakeLAYOUT")]
  14.         public void kb_SetOrMakeLAYOUT()
  15.         {
  16.             var doc = CadApp.DocumentManager.MdiActiveDocument;
  17.             var db = doc.Database;
  18.  
  19.             var pso = new PromptStringOptions("\nEnter the layout name: ");
  20.             var pr = doc.Editor.GetString(pso);
  21.             if (pr.Status != PromptStatus.OK)   // <<<< added
  22.                 return;
  23.  
  24.             var name = pr.StringResult;  
  25.  
  26.  
  27.             using (var tr = db.TransactionManager.StartTransaction()){
  28.                 // Cheating here :
  29.                 // using both LayoutManager AND Dictionary
  30.  
  31.                 // Reference the Layout Manager
  32.                 var lmc = LayoutManager.Current;
  33.  
  34.                 // Get the layout dictionary of the current database
  35.                 var layouts = (DBDictionary) tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
  36.  
  37.                 // Get OR Create Layout
  38.                 var layoutId = layouts.Contains(name) ? layouts.GetAt(name) : lmc.CreateLayout(name);
  39.  
  40.                 // Set the named Layout current ( Active )
  41.                 lmc.CurrentLayout = name;
  42.  
  43.                 // Open the layout
  44.                 var layout = (Layout) tr.GetObject(layoutId, OpenMode.ForRead);
  45.  
  46.                 // Output layout info fot demo:
  47.                 doc.Editor.WriteMessage($"\nTab Order: {layout.TabOrder}" +
  48.                                         $"\nTab Selected: {layout.TabSelected}" +
  49.                                         $"\nBlock Table Record ID: {layout.BlockTableRecordId}"
  50.                 );
  51.  
  52.                 // Zoom to the extents of the Layout
  53.                 doc.SendStringToExecute("._zoom _extents ", true, false, false);
  54.                 tr.Commit();
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60.  
« Last Edit: May 15, 2017, 01:06:03 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: SetOrMakeLAYOUT and Zoom Ext
« Reply #1 on: May 15, 2017, 12:58:21 AM »
Nice kdub, SendStringToExecute was my first thought as well. It feels like a hack, but it works... :)

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: SetOrMakeLAYOUT and Zoom Ext
« Reply #2 on: May 15, 2017, 01:05:42 AM »
Nice kdub, SendStringToExecute was my first thought as well. It feels like a hack, but it works... :)

Thanks Tim.

:) It is a hack, yes ... but valid


//----

added PromptStatus check:

Code - C#: [Select]
  1.             if (pr.Status != PromptStatus.OK)   // <<<< added
  2.                 return;
  3.  
  4.             var name = pr.StringResult;    
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: SetOrMakeLAYOUT and Zoom Ext
« Reply #3 on: May 15, 2017, 01:27:49 AM »

There is a way to have the zoom command ignored by the 'last command' feature in AutoCAD

... but I can't recall what it is.

anyone ?? ...
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: SetOrMakeLAYOUT and Zoom Ext
« Reply #4 on: May 26, 2017, 12:16:59 AM »
A bit off topic sorry but I have found .SendStringToExecute  to work better in one case than ed.command.
I like the 2017 rectangular revcloud and .SendStringToExecute   will let me use that whereas the same string wont with ed.command.
Then I can begincommand that to use the correct layer and endcommand it to add the date and note to the title block. It's like ed.command is only using 2015 cad