Tuesday, October 5, 2010

Layer App

There are several ways to develop custom applications but now that we have talked about setting up the C#.net IDE let's develop an app! This simple little app will create AutoCAD layers. We will develop and build the app in this post and in a future post we will look at different UI options for providing it to the designers on the network.


Here is the code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;


namespace SLDTools
{
    public class LayerCreate
    {
        // Creates layer if one does not already exist
        public void layerCreate(string layerName,
                                short layerColor,
                                string layerLineType,
                                string layerDescription)
        {
            Document doc =
              Application.DocumentManager.MdiActiveDocument;
            Database db =
              doc.Database;
            Editor ed =
              doc.Editor;
            // Start a transaction
            using (Transaction acTrans =
                   db.TransactionManager.StartTransaction())
            {
                // Open the layer table for read
                LayerTable acLyrTbl;
                acLyrTbl = acTrans.GetObject(
                             db.LayerTableId,
                             OpenMode.ForRead)
                           as LayerTable;
                // Open the linetype table for read
                LinetypeTable acLineTypeTbl;
                acLineTypeTbl = acTrans.GetObject(
                                  db.LinetypeTableId,
                                  OpenMode.ForRead)
                                as LinetypeTable;
                LayerTableRecord acLyrTblRec;
                // If layer does not exist then create it
                if (acLyrTbl.Has(layerName) == false)
                {
                    acLyrTblRec = new LayerTableRecord();
                    // Assign ACI color and layer name
                    acLyrTblRec.Color = Color.FromColorIndex(
                                          ColorMethod.ByAci,
                                          layerColor);
                    acLyrTblRec.Name = layerName;
                    // Upgrade the layer table for write
                    acLyrTbl.UpgradeOpen();
                    // Append the new layer to the layer table
                    // and the transaction
                    acLyrTbl.Add(acLyrTblRec);
                    // Load linetype
                    if (acLineTypeTbl.Has(layerLineType) == false)
                    {
                        db.LoadLineTypeFile(
                          layerLineType, "acad.lin");
                    }
                    // Assign linetype
                    if (acLineTypeTbl.Has(layerLineType) == true)
                    {
                        acLyrTblRec.LinetypeObjectId =
                          acLineTypeTbl[layerLineType];
                    }
                    // Add layer description
                    acLyrTblRec.Description = layerDescription;
                    // Add new layer
                    acTrans.AddNewlyCreatedDBObject(
                      acLyrTblRec, true);
                }
                else
                {
                    acLyrTblRec = acTrans.GetObject(
                                    acLyrTbl[layerName],
                                    OpenMode.ForRead)
                                    as LayerTableRecord;
                }
                acTrans.Commit();
                acTrans.Dispose();
            }
        }
        // Define command that will be executed
        // from AutoCAD command line
        [CommandMethod("SLDLAYER")]
        public void sldLayer()
        {
            layerCreate("SLD_Example",
                        154,
                        "Continuous",
                        "Layer created for SLD blog post");
        }
    }
}


The above code can be expanded to either create several layers at the same time (i.e. to create a standard layer set within a drawing) or to create several individual layers (one at a time). The defined command "SLDLAYER" will be the command executed from the AutoCAD command line. Let's test it:


1. Enter the code in the new project we created in our last post and compile the dll.

The compiled dll file will now be in your Visual Studio project path...\bin\release folder.


2. Open AutoCAD and execute NETLOAD at the command prompt.


3. Browse to the dll and select Open.


4. The command defined in our custom layer app is now loaded into AutoCAD so we can execute SLDLAYER...and after doing so, we have our new layer!

Note: our streamlined environment that we have created so far provides the flexibility to incorporate scripts, custom lisp routines, dll files, and more without having to modify client machines. This app can be added by adding the NETLOAD function in the startup mnl file. We will look closer at this in the next post.