Quantcast
Channel: Dotnet/C# – Xinyustudio
Viewing all articles
Browse latest Browse all 204

Developing AutoCAD addin/plugin using C#

$
0
0
  • In Visual Studio .NET, create a class library solution and project.

NewProject 

  • Add Reference from the Project menu or Solution Explorer, browse to the \inc or \incx64 directory of the ObjectARX SDK and select acdbmgd.dll and acmgd.dll.
  • Create a new class or rename the auto-created class

RenameClass 

  • In the main class file, add the namespaces you will use.

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

  • Add a C# function

HelloWorld

Note the attributes are important and must be used, otherwise the command will not be fired.

  • In AutoCAD command prompt, type “netload”, then browse to the C# Library assembly, and click OK.
  • In AutoCAD command prompt, type the function name, in our case, it is  “CreateIt”

RunIt

 

Now add some more meaningful code:

public static void CreatePorousObject() 
{
      Point3d center=new Point3d(9.0, 3.0, 0.0);
      Vector3d normal=new Vector3d(0.0, 0.0, 1.0);
      Circle pCirc = new Circle(center, normal, 2.0);

      Database acadDB = HostApplicationServices.WorkingDatabase;
      Autodesk.AutoCAD.DatabaseServices.TransactionManager acadTransMgr =acadDB.TransactionManager;
      Transaction acadTrans = acadTransMgr.StartTransaction();
      BlockTableRecord acadBTR = (BlockTableRecord)acadTrans.GetObject(acadDB.CurrentSpaceId,OpenMode.ForWrite);
      acadBTR.AppendEntity(pCirc);
      acadTrans.AddNewlyCreatedDBObject(pCirc, true);
      acadTrans.Commit();
}
Now re-compile and run, you will see a circle drawn in AutoCAD. 

Posted in CAD, Dotnet/C#

Viewing all articles
Browse latest Browse all 204

Trending Articles