Skip to content

Instantly share code, notes, and snippets.

@tonicanada
Last active August 16, 2022 00:05
Show Gist options
  • Save tonicanada/7eab060b4a47c9fa81eb7af2deef9dcc to your computer and use it in GitHub Desktop.
Save tonicanada/7eab060b4a47c9fa81eb7af2deef9dcc to your computer and use it in GitHub Desktop.
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[assembly: CommandClass(typeof(MyFirstCadPlugin.Class1))]
namespace MyFirstCadPlugin
{
public class Class1
{
[CommandMethod("hello")]
public void HelloCommand()
{
// Here we connect to the active AutoCAD Document and Database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Here we create a Transaction in the current Database
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Access to the Model Blocktable for write
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForWrite) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create a Circle with its center in the coordinates origin and radius = 50.
Circle myCircle = new Circle();
myCircle.Center = new Point3d(0, 0, 0);
myCircle.Radius = 50;
// Create text with "Hello!" in the middle of the circle
DBText myText = new DBText();
myText.SetDatabaseDefaults();
myText.Height = 20;
myText.TextString = "Hello!";
myText.Justify = AttachmentPoint.MiddleCenter;
// Append Circle and Text to the Blocktable record and Database
acBlkTblRec.AppendEntity(myCircle);
acTrans.AddNewlyCreatedDBObject(myCircle, true);
acBlkTblRec.AppendEntity(myText);
acTrans.AddNewlyCreatedDBObject(myText, true);
// Finish Transaction
acTrans.Commit();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment