public void WblockCloneObjects(
ObjectIdCollection identifiers,
ObjectId id,
IdMapping mapping,
Autodesk.AutoCAD.DatabaseServices.DuplicateRecordCloning cloning,
[MarshalAs(UnmanagedType.U1)] bool deferTranslation
);
其中各个参数的意义如下:
以下分别通过两种方式(拷贝实体和拷贝块表记录)实现了用A图纸中的图元替换B图纸中的块表记录,快速实现图框模板定制。
//拷贝实体
public void CloneEntity()
{
try
{
string defaultPath = new DirectoryInfo(typeof(CommandDetail).Assembly.Location).Parent.FullName;
string stdBlkPath = Path.Combine(Path.Combine(defaultPath, "config"), "B.dwg");
string titlePath = Path.Combine(Path.Combine(defaultPath, "config"), "A.dwg");
Database titleDb = new Database(false, true);
titleDb.ReadDwgFile(titlePath, System.IO.FileShare.Read, true, null);
titleDb.CloseInput(true);
Database stdBlkDb = new Database(false, true);
stdBlkDb.ReadDwgFile(stdBlkPath, System.IO.FileShare.ReadWrite, true, null);
stdBlkDb.CloseInput(true);
Point3d rbPos = new Point3d(titleDb.Extmax.X, titleDb.Extmin.Y, 0);
Matrix3d mt = Matrix3d.Displacement(rbPos.GetVectorTo(Point3d.Origin));
ObjectIdCollection entIds = new ObjectIdCollection();
using (Transaction trans = titleDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(titleDb.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId entId in btr)
{
Entity ent = trans.GetObject(entId, OpenMode.ForRead) as Entity;
if (ent != null)
{
ent.UpgradeOpen();
ent.TransformBy(mt);
ent.Layer = "TK1";
ent.DowngradeOpen();
entIds.Add(entId);
}
}
}
ObjectId titleBtrId = ObjectId.Null;
using (Transaction trans = stdBlkDb.TransactionManager.StartTransaction())
{
BlockTable stdBt = (BlockTable)trans.GetObject(stdBlkDb.BlockTableId, OpenMode.ForWrite, false);
BlockTableRecord stdBtr = trans.GetObject(stdBt[""], OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId id in stdBtr)
{
Entity ent= trans.GetObject(id,OpenMode.ForWrite) as Entity;
if (ent != null)
{
//清空块表记录中的图元
ent.Erase();
}
}
titleBtrId = stdBtr.Id;
trans.Commit();
}
var mappling = new IdMapping();
titleDb.WblockCloneObjects(entIds, titleBtrId, mappling, DuplicateRecordCloning.Replace, false);
stdBlkDb.SaveAs(stdBlkPath, DwgVersion.AC1024);
titleDb.Dispose();
stdBlkDb.Dispose();
}
catch (System.Exception ex)
{
throw new System.Exception(ex.Message);
}
}
//拷贝块表记录
public void CloneBlockTableRecord()
{
try
{
string defaultPath = new DirectoryInfo(typeof(CommandDetail).Assembly.Location).Parent.FullName;
string stdBlkPath = Path.Combine(Path.Combine(defaultPath, "config"), "B.dwg");
string titlePath = Path.Combine(Path.Combine(defaultPath, "config"), "A.dwg");
Database titleDb = new Database(false, true);
titleDb.ReadDwgFile(titlePath, System.IO.FileShare.Read, true, null);
titleDb.CloseInput(true);
Database stdBlkDb = new Database(false, true);
stdBlkDb.ReadDwgFile(stdBlkPath, System.IO.FileShare.ReadWrite, true, null);
stdBlkDb.CloseInput(true);
Point3d rbPos = new Point3d(titleDb.Extmax.X, titleDb.Extmin.Y, 0);
Matrix3d mt = Matrix3d.Displacement(rbPos.GetVectorTo(Point3d.Origin));
string csBtrName = "";
List<string> titleBtrNames = new List<string> { "", ""};
ObjectIdCollection blkTbRecIds = new ObjectIdCollection();
List<Entity> cEnts = new List<Entity>();
using (Transaction trans = titleDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(titleDb.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId entId in btr)
{
Entity ent=trans.GetObject(entId, OpenMode.ForRead) as Entity;
if (ent!=null)
{
ent.UpgradeOpen();
ent.TransformBy(mt);
ent.Layer = "TK1";
ent.DowngradeOpen();
cEnts.Add(ent);
}
}
bt.UpgradeOpen();//切换块表为写的状态
foreach (string titleBtrName in titleBtrNames)
{
//创建一个BlockTableRecord类的对象,表示所要创建的块
BlockTableRecord titleBtr = new BlockTableRecord();
titleBtr.Name = titleBtrName;
//将列表中的实体加入到新建的BlockTableRecord对象
cEnts.ForEach(ent => titleBtr.AppendEntity(ent.Clone() as Entity));
ObjectId titleBtrId = bt.Add(titleBtr);//在块表中加入blockName块
blkTbRecIds.Add(titleBtrId);
titleDb.TransactionManager.AddNewlyCreatedDBObject(titleBtr, true);//通知事务处理
}
bt.DowngradeOpen();//为了安全,将块表状态改为读
}
using (Transaction trans = stdBlkDb.TransactionManager.StartTransaction())
{
BlockTable stdBt = (BlockTable)trans.GetObject(stdBlkDb.BlockTableId, OpenMode.ForWrite, false);
foreach (ObjectId btrId in stdBt)
{
BlockTableRecord stdBtr = trans.GetObject(btrId, OpenMode.ForWrite) as BlockTableRecord;
if (stdBtr != null && stdBtr.Name == csBtrName)
{
stdBtr.Erase();
}
}
trans.Commit();
}
var mappling = new IdMapping();
titleDb.WblockCloneObjects(blkTbRecIds, stdBlkDb.BlockTableId, mappling, DuplicateRecordCloning.Replace, false);
stdBlkDb.SaveAs(stdBlkPath,DwgVersion.AC1024);
titleDb.Dispose();
stdBlkDb.Dispose();
}
catch (System.Exception ex)
{
throw new System.Exception(ex.Message);
}
}