Could not load file or assembly 'Acdbmgd, Version=22.0.0.0

Could not load file or assembly 'Acdbmgd, Version=22.0.0.0

Anonymous
Not applicable
2,013 Views
7 Replies
Message 1 of 8

Could not load file or assembly 'Acdbmgd, Version=22.0.0.0

Anonymous
Not applicable

I get following exception when I debug the code below, can anyone help what is the reason? I am not able to find solution in the forum.

 

csharp.JPG

 

I have following dependencies:

 

csharp2.JPG

 code:

 

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

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Point3d point1 = new Point3d(0, 0, 0);
string blkN = "C:/E_Cable.dwg";

InsertBlock(point1, blkN);
}


public static void InsertBlock(Point3d insPt, string blockName)
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
using (var tr = db.TransactionManager.StartTransaction())
{
// check if the block table already has the 'blockName'" block
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(blockName))
{
try
{
// search for a dwg file named 'blockName' in AutoCAD search paths
var filename = HostApplicationServices.Current.FindFile(blockName + ".dwg", db, FindFileHint.Default);
// add the dwg model space as 'blockName' block definition in the current database block table
using (var sourceDb = new Database(false, true))
{
sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, true, "");
db.Insert(blockName, sourceDb, true);
}
}
catch
{
ed.WriteMessage($"\nBlock '{blockName}' not found.");
return;
}
}

// create a new block reference
using (var br = new BlockReference(insPt, bt[blockName]))
{
var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
space.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
}
tr.Commit();
}
}

}
}

0 Likes
Accepted solutions (1)
2,014 Views
7 Replies
Replies (7)
Message 2 of 8

norman.yuan
Mentor
Mentor

The AutoCAD .NET reference assemblies (accoremgd/acdbmgd/acmgd.dll) CANNOT be used in stand-alone EXE application. They can only be used in .NET framework class library DLL project and loaded into AutoCAD via AutoCAD command "NETLOAD" or AutoCAD's auto-loading configuration.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable

This is .cs file. My goal is to load the dll file from successful debug to autocad using netload. What am I missing here?

0 Likes
Message 4 of 8

Anonymous
Not applicable

Ah makes sense. Thanks

0 Likes
Message 5 of 8

Anonymous
Not applicable

@norman.yuan 

 

I fixed my project as you suggested but now when I run the dll file in autocad using netload my command doesn't show up. I have autocad 2018, and VS2019. Below is my code:

 

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

namespace Pranav_
{
public class Class1
{

[CommandMethod("InsBlockPranav")]
public void InsertBlock()
{
Point3d insPt = new Point3d(0, 0, 0);
string blockName = "C:/Users/E_Cable.dwg";

var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
using (var tr = db.TransactionManager.StartTransaction())
{
// check if the block table already has the 'blockName'" block
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(blockName))
{
try
{
// search for a dwg file named 'blockName' in AutoCAD search paths
var filename = HostApplicationServices.Current.FindFile(blockName + ".dwg", db, FindFileHint.Default);
// add the dwg model space as 'blockName' block definition in the current database block table
using (var sourceDb = new Database(false, true))
{
sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, true, "");
db.Insert(blockName, sourceDb, true);
}
}
catch
{
ed.WriteMessage($"\nBlock '{blockName}' not found.");
return;
}
}

// create a new block reference
using (var br = new BlockReference(insPt, bt[blockName]))
{
var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
space.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
}
tr.Commit();
}
}

}
}

0 Likes
Message 6 of 8

norman.yuan
Mentor
Mentor
Accepted solution

Well, it would be better if you clearly described what exactly occurred after you "netloaded" your DLL and entered your command ("InsBlockPranav"), than simply said "...dosn't show up": what did you see at command line: nothing? or message, something like "Unknown command, Press F1 for help"? 

 

I assume you got "unknown command" error, which usually is the result of these possible issues:

 

1. Wrong version of .NET framework. With Acad2018, you can use 4.6x (or 4.5x, I cannot remember) to 4.8x.

2. Wrong version of AutoCAD managed assembly references (accoremgd/acdbmgd/acmdg.dll). Where the reference DLLs you use are from?

3. You need to make sure the Acad refernce assemblies in your project's Copy Local property is set to False (i.e. if you find the 3 references DLLs end up in the same folder as your custom DLL, the Copy Local property is true, which is wrong, especially, if the referenced DLLs are from ObjectARX SDK).

 

The most likely reason of "unknown command" error from Acad .NET newbie's code is 3.

 

BTW, assume you eventually get your command working (I strongly recommend you try your very first command with simpliest code, i.e. "Hello world" type of code), the code in your first command method has numeral errors and would not work. Once you get your command recognized, you might, or might not, be able step the code through in debugging to find out what are wrong in the code, depending on your AutoCAD programming knowledge. Hint: you cannot use a block file name (with path!) as block name.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 8

Anonymous
Not applicable

I installed .net framework 4.8 and now it works. It was not giving me any errors in autocad after netload on .net framework 5.0. and autocad 2018. thanks again

0 Likes
Message 8 of 8

norman.yuan
Mentor
Mentor

AutoCAD uses .NET Framework, which ends with 4.8 as its very last version and is not part of .NET 5 (and soon coming .NET 6).

Norman Yuan

Drive CAD With Code

EESignature