Customization of AutoCAD Electrical using C# and ObjectARX


AutoCAD Electrical is a vertical application developed by Autodesk on the top of AutoCAD for electrical, electronics, and mechtronics industry.  AutoCAD Electrical provides with more than 2,000 schematic symbols. You can access list of symbols from this link on autodesk server  A simple, icon-menu-driven system for inserting electrical, pneumatic, hydraulic, and P&ID devices is provided, allowing user to quickly build standards-based control designs by simple pick and place.


These are “smart” symbols in many ways, one of which is that wiring automatically breaks and connects to them. Autodesk has published a very good white paper on advantages AutoCAD Electrical  which can be access hereI found them to be relevant and therefore listing them below for quick overview :

1. Thousands of Standards-Based Schematic Symbols 
2. Automatic Wire Numbering and Component Tagging

3. Real-Time Error Checking

4. Real-Time Coil and Contact Cross-Referencing

5. Electrical-Specific Drafting Features
6. Automatically Create PLC I/O Drawings from Spreadsheets

7. Create Smart Panel Layout Drawings
8. Automatic Update of BOM Table

9. Share Drawings with Customers and Suppliers and Track Their Changes

10. Reuse Existing Drawings

Customization :

ObjectARX API can be used to customize AutoCAD Electrical. ObjectARX (AutoCAD Runtime Extension) is an API for customizing and extending AutoCAD.  The ObjectARX SDK is published by Autodesk and freely available under license from Autodesk. The ObjectARX SDK consists primarily of  libraries that can be used to build Windows application or dynamic load library (dll) that can be loaded into the AutoCAD process and interact directly with the AutoCAD application. One significant difference to note is ObjectARX modules use the file extensions .arx and .dbx instead of the more common .dll. This is many times a major hurdle for first time developer on ObjectARX platform.

Autodesk provides software development kit (SDK) such as ObjectARX 2013 wizard which works seamlessly with visual studio. Following five steps can create customize applications for AutoCAD Electrical using C# :

  1. Open Visual studio 10.0
  2. Go to File Menu->New Project->Visual C#->Class Library->Enter name of Add In->Click OK
  3. Go to Properties->Select Debug menu->Enter path of AutoCAD Electrical exe in "External Program" tab
  4. Right Click on project Name->select Add References ->select Browse>C:ObjectARX2013
    ->Inc. ->select “AcMgd”, “AcDbMgd”->open.
  5. Run the Project.

Let us try to understand how using ObjectARX API you can modify your AutoCAD Electrical document. Following code deletes the required group entities from the document. First you need to include required AutoCAD services to call the respective API. Once you access the document from the application rest of the process to access database, groups and entities is easy.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;

public void delete Group ()
{
Editor ed =.Application.DocumentManager.MdiActiveDocument.Editor;
Document doc = Application.DocumentManager.MdiActiveDocument;

Database db = doc.Database;

DBDictionary groupDic = trans.GetObject (db.GroupDictionaryId, OpenMode.ForWrite) as DBDictionary;

foreach (DBDictionaryEntry groupId in groupDic)
{
      Group group = trans.GetObject(groupId.Value, OpenMode.ForWrite) as Group;
      ed.WriteMessage ("Earased group :-" + group.Name + "n");

      if(group.NumEntities! = 0)
      {
          ObjectId [] id = group.GetAllEntityIds ();
          for (int i = 0; i < group.NumEntities; i++)
          {
              Entity ent = (Entity) trans.GetObject (id[i], OpenMode.ForWrite);
              ent.Erase (true);
          }
      }
}

Using following code you can create a block using ObjectARX programming. In AuotCAD Electrical most of the symbols are created using blocks. You can also create custom symbol using your own blocks.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;

public ObjectId createBlock(string blockname)
{

ObjectId blkid = ObjectId. Null;

try
{
     Database db = HostApplicationServices.WorkingDatabase;
     using (Transaction tr = db.TransactionManager.StartTransaction ()){
     BlockTable bt = (BlockTable) tr.GetObject (db.BlockTableId, OpenMode.ForWrite, false);
     if (bt.Has(blockname)) {
         blkid = bt[blockname];
     }
     else {
         BlockTableRecord btr = new BlockTableRecord ();
         btr.Name = blockname;
         btr.Explodable = true;

         btr.Origin = new Point3d(0, 0, 0);
         btr.Units = UnitsValue.Inches;
         btr.BlockScaling = BlockScaling.Any;
         Line line = new Line (new Point3d (-2, -0.4, 0), new Point3d (2, -0.4, 0));
         blkid = bt.Add (btr);

         Application.SetSystemVariable ("CECOLOR", "ByLayer");
         tr.AddNewlyCreatedDBObject (btr, true);
     }
     tr.Commit();
}
}

catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
     Application.ShowAlertDialog ("ERROR: " + "n" + ex.Message + "nSOURCE: " + ex.StackTrace);
}
return blkid;
}

Using following C# code and ObjectARX API you can access block attributes.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;

public static void getAttributesTest()
{
     Document doc = DocumentManager.MdiActiveDocument;
     Database db = doc.Database;
     Editor ed = doc.Editor;
     StringBuilder sb = new StringBuilder();
     using (DocumentLock docloc = doc.LockDocument())
     {
          using (Transaction tr = db.TransactionManager.StartTransaction ())
          {
               try
                    {
                         BlockTable bt = db.BlockTableId.GetObject (OpenMode.ForRead) as BlockTable;
                         foreach (ObjectId adId in bt)
                         {
                              BlockTableRecord btrec = (BlockTableRecord) tr.GetObject (adId, OpenMode.ForRead);
                              foreach (ObjectId eid in btrec)
                              {
                                   DBObject obj = (Entity) tr.GetObject(eid, OpenMode.ForWrite);
                                   if (obj is AttributeDefinition)
                                   {
                                        AttributeDefinition atdef = obj as AttributeDefinition;
                                        if (atdef.Constant)
                                        {
                                             sb.AppendLine(atdef.TextString);
                                        }
                                   }
                              }
                         }
                         tr.Commit();
                    }
              catch (Autodesk.AutoCAD.Runtime.Exception ex)
              {
                  ed.WriteMessage ("n" + ex.Message + "n" + ex.StackTrace);
              }
     }
}

CCTech provides services to design automate AutoCAD Electrical or developing customize application. For more information about our CAD customization services contact Sandip Jadhav


 

The Author

{module [313]}