作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试以编程方式将 block 从预先存在的图形插入到当前正在运行插件的图形中。为此,我在 C#.NET 窗体上有一个按钮调用以下方法
public void MakeAndInsertObject() //Method to add all windows and doors templates to drawing database for use later
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Stores the active document
Editor ed = doc.Editor; //Stores the document's editor
Database dtb = ed.Document.Database; //Stores the database from the editor
Transaction tr = dtb.TransactionManager.StartTransaction(); //Start a transaction with the document's database
DocumentLock docLock = doc.LockDocument();
using (tr)
using (docLock)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(dtb.CurrentSpaceId, OpenMode.ForWrite); //Opens the block table record so you can write to it
BlockTableRecord newBlockDef = new BlockTableRecord(); //Creates a new record in the block table
BlockTable blockTable = (BlockTable)tr.GetObject(dtb.BlockTableId, OpenMode.ForWrite); //Opens the block table so it can be written to
//Pointing new block to correct drawing file
newBlockDef.Name = "Door";
newBlockDef.PathName = "C:/Users/Administrator/Documents/All Code/clearspan-autocad-tools-development/Templates/locks/DOOR.dwg";
blockTable.Add(newBlockDef); //Adds the block table record to the block table
BlockReference newBlock = new BlockReference(new Point3d(0, 0, 0), newBlockDef.ObjectId); //Insert a block reference with the newly created block
btr.AppendEntity(newBlock); //Inserts the block into the current space (Model or Paper) via the block table record
//Updates the Transaction with all new database objects
tr.AddNewlyCreatedDBObject(newBlockDef, true);
tr.AddNewlyCreatedDBObject(newBlock, true);
tr.Commit(); //Applies all changes made as part of the current transaction
tr.Dispose();
}
}
代码完全执行,但我的 DOOR.dwg 文件中的 block 没有出现在位置 (0, 0, 0),我不知道为什么没有出现
最佳答案
我从 Keen Walmsley 中截取了一个片段并将其编辑为不那么繁琐且更清晰。这是非常基本的。您应该阅读 Keen 的一些东西,这些东西非常好,而且他的笔记非常具有描述性。请参阅下面的代码了解用法。
public void ImportBlocks()
{
DocumentCollection dm =
Application.DocumentManager;
Editor ed = dm.MdiActiveDocument.Editor;
Database destDb = dm.MdiActiveDocument.Database;
Database sourceDb = new Database(false, true);
try
{
// Get name of DWG from which to copy blocks
var sourceFileName = ed.GetString("\nEnter the path of the source drawing: ");
// Read the DWG into a side database
sourceDb.ReadDwgFile(sourceFileName.StringResult, System.IO.FileShare.Read, true, "");
// Create a variable to store the list of block identifiers
ObjectIdCollection blockIds = new ObjectIdCollection();
var tm = sourceDb.TransactionManager;
using (var myT = tm.StartOpenCloseTransaction())
{
// Open the block table
BlockTable bt = (BlockTable)myT.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);
// Check each block in the block table
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr =
(BlockTableRecord)myT.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
var mapping = new IdMapping();
sourceDb.WblockCloneObjects(blockIds,
destDb.BlockTableId,
mapping,
DuplicateRecordCloning.Replace,
false);
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\nError during copy: " + ex.Message);
}
sourceDb.Dispose();
}
出于好奇,如果有人开发了用于 AutoCAD 的 python API,您会使用它吗?或者试一试?
关于c# - 将预先存在的 AutoCAD 图形插入当前图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868288/
问了这个问题How to reach CSS zen? ,我现在明白我遇到的问题大多与定位有关。我发现一些文章说 CSS 作为布局系统并不总是足够好。 http://echochamber.me/vi
我是一名优秀的程序员,十分优秀!