Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I’m trying to write a C# program which will insert a block or text in a drawing without opening the Autocad application. I’m ok to run the Autocad application as a background process.

What I have tried:

I’m actually getting an error
System.InvalidProgramException occurred
HResult= 0x8013153A Common Language Runtime detected an invalid program.
Source=<cannot evaluate="" the="" exception="" source="">
StackTrace: at Autodesk.AutoCAD.DatabaseServices.Database.get_TransactionManager()

While running this below piece of code

Database sourceDb = new Database(false, true); //Temporary database to hold data for block we want to import
try
{
sourceDb.ReadDwgFile(dwgPath, System.IO.FileShare.Read, true, ""); //Read the DWG into a side database
ObjectIdCollection blockIds = new ObjectIdCollection(); // Create a variable to store the list of block identifiers

Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = sourceDb.TransactionManager; using (Transaction myT = tm.StartTransaction())
{
// Open the block table
BlockTable bt = (BlockTable)tm.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);

// Check each block in the block table
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)tm.GetObject(btrId, OpenMode.ForRead, false);
// Only add named & non-layout blocks to the copy list
if (!btr.IsAnonymous && !btr.IsLayout)
{
blockIds.Add(btrId);
}
btr.Dispose();
}
}
// Copy blocks from source to destination database
//IdMapping mapping = new IdMapping(); sourceDb.WblockCloneObjects(blockIds, _database.BlockTableId, mapping, DuplicateRecordCloning.Replace, false); _editor.WriteMessage("\nCopied " + blockIds.Count.ToString() + " block definitions from " + blockToImport + " to the current drawing.");
}
Posted
Updated 20-Mar-19 5:48am

You usually get something like this when you try to mix 64- and 32-bit code in the same process.

It could be that your AutoCAD installation is 64-bit and your app is compiled as x86 (32-bit) or "Prefer 32-bit" is enabled. Look in your Project Properties and set the target to x64 and try it again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900