Professional Documents
Culture Documents
Help - Walkthrough - Working With AutoCAD and As API - Autodesk
Help - Walkthrough - Working With AutoCAD and As API - Autodesk
Since Advance Steel runs on top of AutoCAD, the AutoCAD API can be used in conjunction with the Advance
Steel API in an Advance Steel plugin. Common uses of the AutoCAD API from an Advance Steel plugin
include:
This tutorial will look at several examples. For more information on the AutoCAD API, see the online help
for the AutoCAD Managed .NET API (https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-C3F3C736-40CF-
44A0-9210-55F6A939B6F2) .
AcCoreMgd
AcDbMgd
AcMgd
Create a new class called WorkingWithAutoCADAPI and add the following code:
using Autodesk.AdvanceSteel.CADAccess;
using Autodesk.AdvanceSteel.DocumentManagement;
using Autodesk.AdvanceSteel.Modelling;
using Autodesk.AdvanceSteel.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Windows.Forms;
namespace WorkingWithAutoCADandASAPI
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 1/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
{
class WorkingWithAutoCADAPI
[CommandMethodAttribute("TEST_GROUP", "WorkingWithAutoCADAPI",
"WorkingWithAutoCADAPI",
CommandFlags.Modal |
CommandFlags.UsePickSet | CommandFlags.Redraw)]
da.Commit();
Note that this tutorial will require 4 using statements for AutoCAD API namespaces. The AutoCAD API has
several class names in common with Advance Steel, such as ObjectId. This requires disambiguation in the
code. Rather than including the namespace in front of the class name, we can use a 'using alias directive'
at the top of the file to create a shorter name for one of the classes. For this tutorial, we will use three
using alias directives as shown in the rewritten code below.
using Autodesk.AdvanceSteel.CADAccess;
using Autodesk.AdvanceSteel.DocumentManagement;
using Autodesk.AdvanceSteel.Modelling;
using Autodesk.AdvanceSteel.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System.Windows.Forms;
using Autodesk.AutoCAD.Geometry;
namespace WorkingWithAutoCADandASAPI
class WorkingWithAutoCADAPI
[CommandMethodAttribute("TEST_GROUP", "WorkingWithAutoCADAPI",
"WorkingWithAutoCADAPI",
CommandFlags.Modal |
CommandFlags.UsePickSet | CommandFlags.Redraw)]
da.Commit();
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 2/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
Add the method below to the WorkingWithAutoCADAPI class. This method uses a crossing window to find
objects within a rectangular area. It then checks if any of them are straight beams and returns he first
straight beam it finds.
PromptSelectionResult acSSPrompt;
Editor ed =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
acSSPrompt = ed.SelectCrossingWindow(new Point3d(0, 0, 0), new Point3d(1000, 0, 0));
if (acSSPrompt.Status == PromptStatus.OK)
//Iterate through the selected objects, and check if we have a straight beam
ASObjectId idDbObject =
if (obj.Type() == FilerObject.eObjectType.kStraightBeam)
break;
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 3/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
return beam;
In order to successfully work with Advance Steel and AutoCAD API it is necessary to understand which
entity is required. Whenever you want AutoCAD to act on an object you need to use the "representation" id,
while in the cases when you want the AdvanceSteel API to act on the same object the "internal" id must be
used. Whenever object ids are obtained by calling methods, Advance Steel methods always return ids of
the "internal" objects, while AutoCAD API calls always return ids of the "representation" objects. The
Advance Steel API has methods to go from one type of id to the other.
int nExplodedEnts = 0;
DocumentCollection docs =
Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;
Autodesk.AutoCAD.DatabaseServices.Transaction trans =
currDoc.TransactionManager.StartTransaction();
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 4/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
DBObject objBeam = trans.GetObject(acadIdBeam,
Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
if (objBeam is Autodesk.AutoCAD.DatabaseServices.Entity)
ent.Explode(explodedEnts);
nExplodedEnts = explodedEnts.Count;
trans.Commit();
return nExplodedEnts;
Note that the method above first converts the Advance Steel id to an AutoCAD id since we want the
AutoCAD Explode method to act on the entity.
Now return to the original Create() method for the WorkingWithAutoCADAPI and call our two methods -
one to get an Advance Steel straight beam and one to explode it.
if (beam != null)
ExplodeBeam(beam);
In this example, we will create the AutoCAD 3d solid with code, but it could also be created manually. Add
the following method to the class file to create a wedge.
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 5/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
ObjectId ret;
// Get the current document and database, and start an AutoCAD transaction
ACDocument acDoc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
BlockTable acBlkTbl;
acBlkTblRec.Name = "wedge";
acSol3D.SetDatabaseDefaults();
acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
Point3d.Origin));
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acSol3D);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
acTrans.Commit();
ret = acBlkTblRec.Id;
return ret;
Note that the method returns an ObjectId for the BlockTableRecord. We will use this for our special part,
after converting it to an Advance Steel ObjectId. Add the following method to create a new special part.
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 6/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
specialPart.WriteToDb();
if (beam != null)
ExplodeBeam(beam);
CreateSpecialPart();
da.Commit();
Command Reactors
Sometimes it is useful to respond to a command in AutoCAD. An Advance Steel plugin can subscribe to the
CommandEnded event to be notified with a command has completed so some action can be taken. In this
example, we will create a method that looks for when a new beam has been added to the Advance Steel
model and checks to make sure all beams (which would include the new one) meet some minimum beam
length (and issue a warning if one does not).
if (e.GlobalCommandName == "ASTM4CRBEAMBYCLASS")
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 7/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
try
ASObjectId[] ids;
filter.AppendAcceptedClass(FilerObject.eObjectType.kStraightBeam);
if (obj != null)
catch(System.Exception ex)
MessageBox.Show(ex.Message);
Note that in this method, it checks the GlobalCommandName to see if it is ASTM4CRBEAMBYCLASS, the
command indicating that a new Advance Steel beam has been created. The command name may be one
created by another plugin, such as CREATEFEATURES, or any built in command from Advance Steel or
AutoCAD. If you do not know the name of the command you want to create a reactor for, simply create a
command reactor method similar to above and place a breakpoint in the method to check the value of
e.GlobalCommandName when the command is triggered at the end of the desired command.
Since this method will be called when a command has ended and is not going to be within the scope of the
transaction started in the Create() method, it is necessary for CurrentDocument_CommandEnded to have
it's own transaction, as well as calls to lock and unlock the current document.
To have your command reactor called, it must be registered with the current document. Add the following
code to the WorkingWithAutoCADAPI class to register the CurrentDocument_CommandEnded method.
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 8/9
11/10/21, 4:30 PM Help | Walkthrough: Working with AutoCAD and AS API | Autodesk
DocumentCollection docs =
Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;
currDoc.CommandEnded += CurrentDocument_CommandEnded;
if (beam != null)
ExplodeBeam(beam);
CreateSpecialPart();
AddCommandEndedReactor();
Except where otherwise noted, this work is licensed under a Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported License. Please see the Autodesk Creative Commons FAQ
for more information.
© 2021 Autodesk Inc. All rights reserved
https://help.autodesk.com/view/ADSTPR/2021/ENU/?guid=GUID-0AFEFD2B-EED4-4F6A-8F97-BA0814E99643 9/9