Unable to load an assembly properly.

Unable to load an assembly properly.

Jeremiah.Dillashaw
Contributor Contributor
970 Views
8 Replies
Message 1 of 9

Unable to load an assembly properly.

Jeremiah.Dillashaw
Contributor
Contributor

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();
                }
            }
        }
    }
}

 

0 Likes
Accepted solutions (2)
971 Views
8 Replies
Replies (8)
Message 2 of 9

cuongtk2
Advocate
Advocate
Using...
[assembly: CommandClass(typeof(DrawObjects.DrawObject)]
namespace DrawObjects
{class DrawObject
...
}
}
0 Likes
Message 3 of 9

Jeff_M
Consultant
Consultant

I created a new project/solution, added the 3 references, copied your code into the new class, built solution, started C3D, netload assembly, answered to always load, tried drawline command, line created without issue. So it is not your code...do you have anything else in the project?

 

What version of AutoCAD? Which Visual Studio? What .NET Framework is the project using?

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 4 of 9

Jeremiah.Dillashaw
Contributor
Contributor

Thank you @cuongtk2 . I tried your suggestion as best as i know how, and i still had the same result of "unknown command".

 

Below is just the top few lines of the code, in case i didn't follow your instruction correctly.

 

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

[assembly: CommandClass(typeof(DrawObjects.DrawObject))]
namespace DrawObjects
{
    class DrawObject
    {

//... didn't want to dump it all in again, nothing below here changed

 

0 Likes
Message 5 of 9

Jeremiah.Dillashaw
Contributor
Contributor

Hi @Jeff_M , i'm using ACAD 2023, VS 2022 & .net 6.0

 

There is nothing else in the visual studio project, if that's what you mean. The class and methods are all wrapped up in one just for size and exercise purposes--i'm guessing.

 

I'm going through a C# and AutoCAD programming with .NET course on Udemy. I have reached out to the instructor, i just figured it'd be a good idea to cast the net wide 🙂 Thanks for responding.

0 Likes
Message 6 of 9

Jeremiah.Dillashaw
Contributor
Contributor

I looked in my acad.exe.config file. There's some supported runtime stuff in there, i don't know if this is related to .net runtime, but it has some "useLegacyV2Runtime..." thing in it.

 

Is it possible one of my other main ACAD addins changed to this when it installed? I use Microvellum in almost all my daily work with ACAD.

I'm just guessing of course. Here's the config file contents though:

 

<configuration>
 <startup useLegacyV2RuntimeActivationPolicy="true">
   <supportedRuntime version="v4.0"/>
   <supportedRuntime version="v2.0.50727" />
 </startup>
 <runtime>        
   <generatePublisherEvidence enabled="false"/>    
 </runtime>
</configuration>

 

0 Likes
Message 7 of 9

Jeff_M
Consultant
Consultant
Accepted solution

@Jeremiah.Dillashaw your project must be a .NET Framework not a .NET 6.0 assembly. I'm on my phone so cannot post an image, sorry.

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 8 of 9

norman.yuan
Mentor
Mentor
Accepted solution

You CANNOT use .NET Core (.NET5/6/7). AutoCAD .NET API project can only work with .NET Framework (since you use Acad2023, thus .NET Framework 4.8x)!

 

So, make sure you choose your VS2022 project type correctly: Class Library (.NET Framework), not Class Library (which creates .NET Core class library)

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 9 of 9

Jeremiah.Dillashaw
Contributor
Contributor

Thank you very much guys. It's all straitened out now 🙂 Everything is working as expected, once i got the correct project type selected. I appreciate your help!

0 Likes