Message 1 of 9
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello all.
I've taken the plunge to learn C# programming, and am having some trouble to get my first assembly to load properly.
When i run NETLOAD, it either seems to work just fine. That is, there isn't anything returned to the command line (but i'm not sure if it should say the .dll was loaded properly), or once it said "unable to load ...address..DrawObjects.dll"
If i don't get the latter error message, and try to type in the command method, it just says "unknown command".
Is there anything in the code below that is obviously wrong?
I'm using AutoCAD 2023, and have the accoremgd, Acdbmgd & Acmgd assemblies set to not copy local in VisualStudio.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
namespace DrawObjects
{
public class DrawObject
{
[CommandMethod("DRAWCIRCLE")]
public void DrawCircle()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
doc.Editor.WriteMessage("Drawing a circle.");
BlockTable bt;
bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr;
btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// specify the circle's parameters
Point3d centerPt = new Point3d(100, 100, 0);
double circleRad = 100.0;
using (Circle circle = new Circle())
{
circle.Radius = circleRad;
circle.Center = centerPt;
circle.Layer = "ELEV_LIGHT";
btr.AppendEntity(circle);
trans.AddNewlyCreatedDBObject(circle, true);
}
trans.Commit();
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage("Error encountered: " + ex.Message);
trans.Abort();
}
}
}
[CommandMethod("DRAWMTEXT")]
public void DrawMText()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor edt = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
edt.WriteMessage("Drawing MText Exercise!");
BlockTable bt;
bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr;
btr = trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
// specify the Mtext parameters
string txt = "Hello AutoCAD, from cSharp.";
Point3d insPt = new Point3d(200, 200, 0);
using (MText mtx = new MText())
{
mtx.Contents = txt;
mtx.Location = insPt;
btr.AppendEntity(mtx);
trans.AddNewlyCreatedDBObject(mtx, true);
}
trans.Commit();
}
catch (System.Exception ex)
{
edt.WriteMessage("Error encountered: " + ex.Message);
trans.Abort();
}
}
}
[CommandMethod("DRAWLINE")]
public void DrawLine()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor edt = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt;
bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr;
btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// send a message to the user
edt.WriteMessage("\nDrawing a Line object");
Point3d pt1 = new Point3d(0, 0, 0);
Point3d pt2 = new Point3d(100, 100, 0);
Line ln = new Line(pt1, pt2);
ln.ColorIndex = 8;
btr.AppendEntity(ln);
trans.AddNewlyCreatedDBObject(ln, true);
trans.Commit();
}
catch (System.Exception ex)
{
edt.WriteMessage("Error was encountered: " + ex.Message);
trans.Abort();
}
}
}
}
}
Solved! Go to Solution.