Author Topic: Layouts  (Read 6594 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Layouts
« on: January 12, 2007, 02:05:14 AM »
I've started to play with Layouts .. heading towards Plotting [expect this to be a long path ]


Anyone want to give this a run .. ?
The routine is designed to display the Names of All Layouts defined in the Document.

The initialiser may be of interest too.
Code - C#: [Select]
  1. //
  2. // KDUB
  3. // Codehimbelonga kwb@theSwamp 20070112
  4. // For AC2007: MSVS2005
  5.  
  6. #region usingdeclarations
  7.  
  8. using System;
  9.  
  10. //// Assembly acdbmgd .. ObjectDBX.NET Managed Wrapper
  11. //// Assembly acmgd .. Autocad.NET Managed Wrapper
  12. using Autodesk.AutoCAD.ApplicationServices;
  13. using Autodesk.AutoCAD.DatabaseServices;
  14. using Autodesk.AutoCAD.EditorInput;
  15. using Autodesk.AutoCAD.Geometry;
  16. using Autodesk.AutoCAD.Runtime;
  17. using Autodesk.AutoCAD.Colors;
  18. #endregion
  19.  
  20. #region usingAliases
  21. // Forms : MessageBox,
  22. using System.Windows.Forms;
  23. // : DictionaryEntry,
  24. using System.Collections;
  25. //
  26. using System.Collections.Generic;
  27. // For StringCollection
  28. using System.Collections.Specialized;
  29. // For Plotting
  30. using Autodesk.AutoCAD.PlottingServices;
  31. //
  32. using AcDb = Autodesk.AutoCAD.DatabaseServices;
  33. using AcEd = Autodesk.AutoCAD.EditorInput;
  34. using AcRx = Autodesk.AutoCAD.Runtime;
  35. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  36. using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
  37.  
  38. #endregion
  39.  
  40.  
  41. [assembly: AcRx.CommandClass(typeof(kdubTestStuff.kdubTestClass))]
  42.  
  43. namespace kdubTestStuff
  44. {
  45.     /// <summary>
  46.     /// Summary for kdubTestStuff
  47.     /// </summary>
  48.     public class kdubTestClass : AcRx.IExtensionApplication
  49.     {
  50.         /// <summary>
  51.         /// ---------------------------------------------------------------------------------------------
  52.         /// </summary>
  53.  
  54.         [AcRx.CommandMethod("kbLAL")]
  55.         static public void
  56.             listAllLayouts()
  57.         {
  58.             AcDb.Database db = HostApplicationServices.WorkingDatabase;            
  59.             AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  60.             try
  61.             {
  62.                 using (AcDb.Transaction
  63.                     tr = db.TransactionManager.StartTransaction())
  64.                 {
  65.                      AcDb.LayoutManager layoutMgr = AcDb.LayoutManager.Current;
  66.                      ed.WriteMessage("LayoutManager.CurrentLayout = " + layoutMgr.CurrentLayout + "\n");
  67.                      ed.WriteMessage("LayoutManager.Count = " + layoutMgr.LayoutCount.ToString() + "\n");
  68.  
  69.                      using (AcDb.DBDictionary
  70.                          layoutDict = (AcDb.DBDictionary)tr.GetObject(db.LayoutDictionaryId, AcDb.OpenMode.ForRead))
  71.                      {
  72.                         foreach (DictionaryEntry layoutEntry in layoutDict)
  73.                         {
  74.                             using (AcDb.Layout
  75.                                 layoutObj = (AcDb.Layout)tr.GetObject((AcDb.ObjectId)(layoutEntry.Value), AcDb.OpenMode.ForRead))
  76.                             {
  77.                                 ed.WriteMessage("Layout.LayoutName = " + layoutObj.LayoutName + "\n");
  78.                             }
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.             catch (System.Exception ex)
  84.             {
  85.                 MessageBox.Show(ex.ToString());
  86.             }
  87.         }
  88.         /// <summary>
  89.         /// ---------------------------------------------------------------------------------------------
  90.         /// </summary>
  91.         public void
  92.             Initialize()
  93.         {
  94.             //Let the user know what's happening while loading your stuff  
  95.             AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  96.             ed.WriteMessage("\nLoading 'listAllLayouts' application ...");
  97.             ed.WriteMessage("\nConcept Code For AC2007, MSVS2005 :: kwb@theSwamp 20070112 ...");
  98.             ed.WriteMessage("\nUse kbLAL at command line to List all Current Layout Names\n");
  99.  
  100.         }
  101.         public void
  102.             Terminate()
  103.         {
  104.             //Nothing to see here yet, move along !
  105.  
  106.         }
  107.     }
  108. }

... and the obligatory piccy .. :-)
« Last Edit: March 12, 2023, 03:25:00 PM by kdub »
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #1 on: January 12, 2007, 02:22:09 AM »
.. and these are the Locals after the first run through the foreach loop.

I'm finding that the Locals, the Browser/Reflection is helping me make a pretty good guess regarding whats going on.
« Last Edit: January 12, 2007, 02:30:52 AM by Kerry Brown »
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Mr. Bean

  • Retired
  • Needs a day job
  • Posts: 7791
  • AKA Daniel
Re: Layouts
« Reply #2 on: January 12, 2007, 02:49:16 AM »
Nice! works great here

I like this ...
Code: [Select]
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;

Hope you don’t mind me borrowing stuff  :-)

edit

And I do like the initialiser I’ve never seen this
« Last Edit: January 12, 2007, 02:52:56 AM by Danielm103 »
Retired

Mr. Bean

  • Retired
  • Needs a day job
  • Posts: 7791
  • AKA Daniel
Re: Layouts
« Reply #3 on: January 12, 2007, 02:56:17 AM »
layoutMgr does not need to be disposed because it’s not a "new" LayoutManager right?
Retired

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #4 on: January 12, 2007, 02:58:06 AM »
It's a good learner Dan.

Re the 'borrowing'  ... whats round comes around .. I notices the concept on one of the chinese sites ages ago .. just starting to make use of it sometimes. I thought it may help me learn where everything comes from without increasing the keystroke count too much.
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #5 on: January 12, 2007, 03:03:34 AM »
layoutMgr does not need to be disposed because it’s not a "new" LayoutManager right?

Thats my understanding ... but I'd be happy to be corrected.

And I do like the initialiser I’ve never seen this

Yeah, I became tired of forgetting the command Name I need to type to get test stuff to run  ;-)

When I use more that one callable method especially .. I just add an extra line to the Initialiser for each command.
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #6 on: January 12, 2007, 07:39:04 AM »
Next one .. needs some refining ..

If anyone can see anything that looks questionable or could do with a rev' please yell.  :|

add this Method to the same class as the previous ..
Code - C#: [Select]
  1.        
  2.         [AcRx.CommandMethod("kbPSL")]
  3.         static public void
  4.             ListPlotStylesForLayouts()
  5.         {
  6.             AcDb.Database db = HostApplicationServices.WorkingDatabase;            
  7.             AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  8.             //            
  9.             try
  10.             {
  11.                 using (AcDb.Transaction tr = db.TransactionManager.StartTransaction())
  12.                 {
  13.                     using (AcDb.DBDictionary layoutDict = (AcDb.DBDictionary)tr.GetObject
  14.                                              (db.LayoutDictionaryId, AcDb.OpenMode.ForRead))
  15.                     {
  16.                         foreach (DictionaryEntry layoutEntry in layoutDict)
  17.                         {
  18.                             using (AcDb.Layout  layoutObj = (AcDb.Layout)tr.GetObject
  19.                                                 ((AcDb.ObjectId)(layoutEntry.Value),AcDb.OpenMode.ForRead))
  20.                             {
  21.                                 string strStyleSheet = layoutObj.CurrentStyleSheet.ToString();
  22.                                 if (strStyleSheet.Length == 0)
  23.                                 {
  24.                                     ed.WriteMessage("Layout.LayoutName = " + layoutObj.LayoutName
  25.                                         + ", StyleSheet = " + "nada - tough luck" + "\n");
  26.                                 }
  27.                                 else
  28.                                 {
  29.                                     ed.WriteMessage("Layout.LayoutName = " + layoutObj.LayoutName
  30.                                         + ", StyleSheet = " + strStyleSheet + "\n");
  31.                                 }
  32.                             }
  33.                         }
  34.                     }
  35.                 }
  36.             }
  37.             catch (System.Exception ex)
  38.             {
  39.                 MessageBox.Show(ex.ToString());
  40.             }
  41.         }


and replace the  Initialize() Method with this ..
Code - C#: [Select]
  1.         public void
  2.             Initialize()
  3.         {
  4.             //Let the user know what's happening while loading your stuff  
  5.             AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  6.            
  7.             ed.WriteMessage("\nConcept Code For AC2007, MSVS2005 :: kwb@theSwamp 20070112 ...");
  8.             ed.WriteMessage("\nUse kbLAL at command line to List all Current Layout Names\n");
  9.             ed.WriteMessage("\nUse kbPSL at command line to List PlotStyles For Layouts \n");
  10.         }
and the piccy ...
« Last Edit: March 12, 2023, 03:25:52 PM by kdub »
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #7 on: January 13, 2007, 01:09:49 AM »
Next one :
Device Names for the Active Layout using COM ActiveX in C# [ no apologies ;-) ]

Add this to the usings and reference the COM Dll's..
Code - C#: [Select]
  1. //// COM Interop References
  2. //// AutoCAD 2007 Type Library
  3. //// AXDBLib AutoCAD/ObjectDBX Common 17.0 Type Library
  4. using AcInt = Autodesk.AutoCAD.Interop;
  5. using AcIntC = Autodesk.AutoCAD.Interop.Common;
  6.  

Add this to the main Class ..
Code - C#: [Select]
  1.         [AcRx.CommandMethod("kbPDN")]
  2.         static public void GetPlotDeviceNames()
  3.         {
  4.             AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  5.  
  6.             // Use the COM ActiveX interface to extract the devices from the activeLayout
  7.             //
  8.             AcInt.AcadApplication appCOM = (AcInt.AcadApplication)AcadApp.AcadApplication;            
  9.             AcIntC.AcadLayout activeLayout = appCOM.ActiveDocument.ActiveLayout;
  10.            
  11.             // generic System.Object
  12.            Object plotDevices = activeLayout.GetPlotDeviceNames();
  13.             // Array of device Names
  14.            Array plotDeviceArray = (Array)plotDevices;
  15.            
  16.            //
  17.            ed.WriteMessage("\nPlot Device List for :"  + activeLayout.Name) ;
  18.            foreach (string nameStr in plotDeviceArray)  
  19.            {
  20.                ed.WriteMessage("\n " + nameStr);
  21.            }            
  22.         }
  23.  

And the Piccy

« Last Edit: March 12, 2023, 03:28:35 PM by kdub »
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #8 on: January 13, 2007, 01:41:54 AM »
.. and the same from the managed API using PlottingServices ..

Add this to the usings ;
Code: [Select]
using AcPS = Autodesk.AutoCAD.PlottingServices;
Then add this to the Class
Code - C#: [Select]
  1.         [AcRx.CommandMethod("kbPDNN")]
  2.         static public void PlotDeviceNamesNet()
  3.         {
  4.             AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  5.             ed.WriteMessage("\nPlot Device List In Net :"  );
  6.             foreach (AcPS.PlotConfigInfo pci in AcPS.PlotConfigManager.Devices )
  7.             {
  8.                 ed.WriteMessage("\n " + pci.DeviceName );
  9.             }
  10.         }
and the piccy .. same result !
« Last Edit: March 12, 2023, 03:27:44 PM by kdub »
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #9 on: January 13, 2007, 02:52:20 AM »
This one was fun !! .. had a play with a generics list ..
not sure if it's correct [best way etc] , but it works !!!

Code - C#: [Select]
  1.         [AcRx.CommandMethod("kbPDN-1")]
  2.         static public void PlotDeviceNamesNet_1()
  3.         {
  4.             AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  5.             ed.WriteMessage("\nPlot Device List In Net :");
  6.  
  7.             AcPS.PlotConfigInfoCollection plotDevices = AcPS.PlotConfigManager.Devices ;
  8.  
  9.             // use a bit of the NET generics
  10.             //
  11.             List<string>  plotDeviceList = new List<string>();
  12.  
  13.             //// lets build a list [collection]to use later ...
  14.            
  15.             foreach (AcPS.PlotConfigInfo pci in plotDevices )
  16.             {
  17.                 plotDeviceList.Add(pci.DeviceName);
  18.             }
  19.  
  20.             // Print it out just to proove it ..
  21.             foreach (string deviceName in plotDeviceList)
  22.             {
  23.                 ed.WriteMessage("\n " + deviceName);
  24.             }
  25.         }
  26.  
The debug Locals for the built List ..
.. and the command line piccy ..
« Last Edit: March 12, 2023, 03:26:49 PM by kdub »
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Mr. Bean

  • Retired
  • Needs a day job
  • Posts: 7791
  • AKA Daniel
Re: Layouts
« Reply #10 on: January 13, 2007, 02:52:37 AM »
Oh yea managed API version is much cleaner. I almost forgot all about that ActiveX stuff  :-o
Retired

Mr. Bean

  • Retired
  • Needs a day job
  • Posts: 7791
  • AKA Daniel
Re: Layouts
« Reply #11 on: January 13, 2007, 02:56:18 AM »
.. had a play with a generics list ..

Ha! I was just doing the same thing!!! List<string>  plotDeviceList = new List<string>();
That’s funny
Retired

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #12 on: January 13, 2007, 02:58:58 AM »
Great minds think alike, fools never differ  .. etc ... ;-)

Today has been a good day Dan !
kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #13 on: January 13, 2007, 03:07:54 AM »
Just a thought ..

If we can get a couple of people adding working Methods to this thread it won't take long before we start to understand the Plotting and Layouts Namespaces.  :|

... simple single purpose snippets will me fine, thank you !  :-)


kdub in one timeline.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<-

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 1643
  • class keyThumper<T>:ILazy<T>
Re: Layouts
« Reply #14 on: March 11, 2023, 03:33:43 PM »

Jeeeze , time flies !!
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.