Author Topic: Layouts  (Read 4125 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: [Select]
//
// KDUB
// Codehimbelonga kwb@theSwamp 20070112
// For AC2007: MSVS2005

#region usingdeclarations

using System;

//// Assembly acdbmgd .. ObjectDBX.NET Managed Wrapper
//// Assembly acmgd .. Autocad.NET Managed Wrapper
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
#endregion

#region usingAliases
// Forms : MessageBox,
using System.Windows.Forms;
// : DictionaryEntry,
using System.Collections;
//
using System.Collections.Generic;
// For StringCollection
using System.Collections.Specialized;
// For Plotting
using Autodesk.AutoCAD.PlottingServices;
//
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;

#endregion


[assembly: AcRx.CommandClass(typeof(kdubTestStuff.kdubTestClass))]

namespace kdubTestStuff
{
    /// <summary>
    /// Summary for kdubTestStuff
    /// </summary>
    public class kdubTestClass : AcRx.IExtensionApplication
    {
        /// <summary>
        /// ---------------------------------------------------------------------------------------------
        /// </summary>

        [AcRx.CommandMethod("kbLAL")]
        static public void
            listAllLayouts()
        {
            AcDb.Database db = HostApplicationServices.WorkingDatabase;           
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                using (AcDb.Transaction
                    tr = db.TransactionManager.StartTransaction())
                {
                     AcDb.LayoutManager layoutMgr = AcDb.LayoutManager.Current;
                     ed.WriteMessage("LayoutManager.CurrentLayout = " + layoutMgr.CurrentLayout + "\n");
                     ed.WriteMessage("LayoutManager.Count = " + layoutMgr.LayoutCount.ToString() + "\n");

                     using (AcDb.DBDictionary
                         layoutDict = (AcDb.DBDictionary)tr.GetObject(db.LayoutDictionaryId, AcDb.OpenMode.ForRead))
                     {
                        foreach (DictionaryEntry layoutEntry in layoutDict)
                        {
                            using (AcDb.Layout
                                layoutObj = (AcDb.Layout)tr.GetObject((AcDb.ObjectId)(layoutEntry.Value), AcDb.OpenMode.ForRead))
                            {
                                ed.WriteMessage("Layout.LayoutName = " + layoutObj.LayoutName + "\n");
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// ---------------------------------------------------------------------------------------------
        /// </summary>
        public void
            Initialize()
        {
            //Let the user know what's happening while loading your stuff 
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nLoading 'listAllLayouts' application ...");
            ed.WriteMessage("\nConcept Code For AC2007, MSVS2005 :: kwb@theSwamp 20070112 ...");
            ed.WriteMessage("\nUse kbLAL at command line to List all Current Layout Names\n");

        }
        public void
            Terminate()
        {
            //Nothing to see here yet, move along !

        }
    }
}

... and the obligatory piccy .. :-)
« Last Edit: January 12, 2007, 02:46:17 AM by Kerry Brown »
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 »
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<--

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6980
  • 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 »

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6980
  • 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?

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.
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.
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: [Select]
       
        [AcRx.CommandMethod("kbPSL")]
        static public void
            ListPlotStylesForLayouts()
        {
            AcDb.Database db = HostApplicationServices.WorkingDatabase;           
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            //           
            try
            {
                using (AcDb.Transaction tr = db.TransactionManager.StartTransaction())
                {
                    using (AcDb.DBDictionary layoutDict = (AcDb.DBDictionary)tr.GetObject
                                             (db.LayoutDictionaryId, AcDb.OpenMode.ForRead))
                    {
                        foreach (DictionaryEntry layoutEntry in layoutDict)
                        {
                            using (AcDb.Layout  layoutObj = (AcDb.Layout)tr.GetObject
                                                ((AcDb.ObjectId)(layoutEntry.Value),AcDb.OpenMode.ForRead))
                            {
                                string strStyleSheet = layoutObj.CurrentStyleSheet.ToString();
                                if (strStyleSheet.Length == 0)
                                {
                                    ed.WriteMessage("Layout.LayoutName = " + layoutObj.LayoutName
                                        + ", StyleSheet = " + "nada - tough luck" + "\n");
                                }
                                else
                                {
                                    ed.WriteMessage("Layout.LayoutName = " + layoutObj.LayoutName
                                        + ", StyleSheet = " + strStyleSheet + "\n");
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


and replace the  Initialize() Method with this ..
Code: [Select]
        public void
            Initialize()
        {
            //Let the user know what's happening while loading your stuff 
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
           
            ed.WriteMessage("\nConcept Code For AC2007, MSVS2005 :: kwb@theSwamp 20070112 ...");
            ed.WriteMessage("\nUse kbLAL at command line to List all Current Layout Names\n");
            ed.WriteMessage("\nUse kbPSL at command line to List PlotStyles For Layouts \n");
        }
and the piccy ...
« Last Edit: January 12, 2007, 08:07:38 AM by Kerry Brown »
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: [Select]
//// COM Interop References
//// AutoCAD 2007 Type Library
//// AXDBLib AutoCAD/ObjectDBX Common 17.0 Type Library
using AcInt = Autodesk.AutoCAD.Interop;
using AcIntC = Autodesk.AutoCAD.Interop.Common;

Add this to the main Class ..
Code: [Select]
        [AcRx.CommandMethod("kbPDN")]
        static public void GetPlotDeviceNames()
        {
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            // Use the COM ActiveX interface to extract the devices from the activeLayout
            //
            AcInt.AcadApplication appCOM = (AcInt.AcadApplication)AcadApp.AcadApplication;           
            AcIntC.AcadLayout activeLayout = appCOM.ActiveDocument.ActiveLayout;
           
            // generic System.Object
           Object plotDevices = activeLayout.GetPlotDeviceNames();
            // Array of device Names
           Array plotDeviceArray = (Array)plotDevices;
           
           //
           ed.WriteMessage("\nPlot Device List for :"  + activeLayout.Name) ;
           foreach (string nameStr in plotDeviceArray) 
           {
               ed.WriteMessage("\n " + nameStr);
           }           
        }

And the Piccy

« Last Edit: January 13, 2007, 01:13:00 AM by Kerry Brown »
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: [Select]
        [AcRx.CommandMethod("kbPDNN")]
        static public void PlotDeviceNamesNet()
        {
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nPlot Device List In Net :"  );
            foreach (AcPS.PlotConfigInfo pci in AcPS.PlotConfigManager.Devices )
            {
                ed.WriteMessage("\n " + pci.DeviceName );
            }
        }
and the piccy .. same result !
« Last Edit: January 13, 2007, 01:43:30 AM by Kerry Brown »
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: [Select]
        [AcRx.CommandMethod("kbPDN-1")]
        static public void PlotDeviceNamesNet_1()
        {
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nPlot Device List In Net :");

            AcPS.PlotConfigInfoCollection plotDevices = AcPS.PlotConfigManager.Devices ;

            // use a bit of the NET generics
            //
            List<string>  plotDeviceList = new List<string>();

            //// lets build a list [collection]to use later ...
           
            foreach (AcPS.PlotConfigInfo pci in plotDevices )
            {
                plotDeviceList.Add(pci.DeviceName);
            }

            // Print it out just to proove it ..
            foreach (string deviceName in plotDeviceList)
            {
                ed.WriteMessage("\n " + deviceName);
            }
        }
The debug Locals for the built List ..
.. and the command line piccy ..
« Last Edit: January 13, 2007, 02:56:31 AM by Kerry Brown »
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<--

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6980
  • 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

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6980
  • 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

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 !
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 !  :-)


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<--