插入的文件是块,怎么调整为文件
本帖最后由 k1239752201 于 2021-12-1 09:59 编辑以下代码是插入一个外部cad文件,但是插入进去的是一个块,怎么调整为插入文件
public void insertBlockFunc(string blockName, string layerName, bool isShow, bool isCover)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
// 指定块的插入位置
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
//提示起点
pPtOpts.Message = "\n请选择插入点: ";
pPtRes = doc.Editor.GetPoint(pPtOpts);
Point3d point = pPtRes.Value;
// 新建块文件数据库对象 读取块
using (Database blockDb = new Database(false, true))
{
// 读取外部块 dwg
blockDb.ReadDwgFile(blockName, System.IO.FileShare.Read, true, null);
// 关闭文件
blockDb.CloseInput(true);
// 先锁定文档 多文档的情况需要先锁定
using (DocumentLock lockDoc = doc.LockDocument())
{
// 新建图层id
ObjectId layId = ObjectId.Null;
// 开启事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 指定插入块后的新名称
string newName = System.IO.Path.GetFileNameWithoutExtension(blockName);
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if ((bt.Has(newName)) && isCover)
{
// 插入块 insert it as a new block
ObjectId idBtr = doc.Database.Insert(newName, blockDb, false);
if (isShow)
{
BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
if (!lt.Has("layerName"))
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "layerName";
layId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
curBtr.LayoutId = layId;
using (BlockReference blockRef = new BlockReference(point, idBtr))
{
curBtr.AppendEntity(blockRef);
trans.AddNewlyCreatedDBObject(blockRef, true);
}
}
}
else if (!(bt.Has(newName)))
{
// 插入块 insert it as a new block
ObjectId idBtr = doc.Database.Insert(newName, blockDb, false);
if (isShow)
{
BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
if (!lt.Has("layerName"))
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "layerName";
layId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
curBtr.LayoutId = layId;
using (BlockReference blockRef = new BlockReference(point, idBtr))
{
curBtr.AppendEntity(blockRef);
trans.AddNewlyCreatedDBObject(blockRef, true);
}
}
}
trans.Commit();
}
}
}
}
主要步骤
Dim ID As ObjectId = Doc.Database.AttachXref("x:\xxxxx.dwg", "新的名称")
Dim Ref As New BlockReference(Point3d.Origin, ID)
ModelSpace.AppendEntity(Ref)
Trans.AddNewlyCreatedDBObject(Ref, True)
页:
[1]