本文介绍了插入块或文本的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个C#程序,它将在不打开Autocad应用程序的情况下在图形中插入块或文本。我可以将Autocad应用程序作为后台进程运行。



我尝试了什么:



我实际上收到错误

发生System.InvalidProgramException

HResult = 0x8013153A公共语言运行时检测到无效程序。

Source =< can not evaluation ==exception =source =>

StackTrace:at Autodesk.AutoCAD.DatabaseServices.Database.get_TransactionManager()



在下面运行这段代码



数据库sourceDb = new数据库(false,true); //用于保存我们要导入的块的数据的临时数据库

尝试

{

sourceDb.ReadDwgFile(dwgPath,System.IO.FileShare 。读,真,); //将DWG读入边数据库

ObjectIdCollection blockIds = new ObjectIdCollection(); //创建一个变量来存储块标识符列表



Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = sourceDb.TransactionManager; using(Transaction myT = tm.StartTransaction())

{

//打开块表

BlockTable bt =(BlockTable)tm。 GetObject(sourceDb.BlockTableId,OpenMode.ForRead,false);



//检查块表中的每个块

foreach(ObjectId btrId在bt)

{

BlockTableRecord btr =(BlockTableRecord)tm.GetObject(btrId,OpenMode.ForRead,false);

//仅限添加命名&非布局块到复制列表

if(!btr.IsAnonymous&&!btr.IsLayout)

{

blockIds。添加(btrId);

}

btr.Dispose();

}

}

//将块从源数据库复制到目标数据库

// IdMapping mapping = new IdMapping(); sourceDb.WblockCloneObjects(blockIds,_database.BlockTableId,mapping,DuplicateRecordCloning.Replace,false); _editor.WriteMessage(\ nCopied+ blockIds.Count.ToString()+块定义从+ blockToImport +到当前图纸。);

}

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.");
}

推荐答案



这篇关于插入块或文本的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 16:25