[分享]插入外部文件中块的C#实现方法
////插入块private static bool InsertBlock(string strBlockName, string strFilePath)
{
if (strBlockName.CompareTo("") == 0 || strFilePath.CompareTo("") == 0)
{
return false;
}
Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
if (!System.IO.File.Exists(strFilePath))
{
CGcEditor.ShowMessage(string.Format("\nInput file :{0} no existed !", strFilePath));
return false;
}
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
using (Database db = new Database(false, true))
{
db.ReadDwgFile(strFilePath, System.IO.FileShare.Read, true, null);
Autodesk.AutoCAD.DatabaseServices.TransactionManager trMag = db.TransactionManager;
using (Transaction trm = trMag.StartTransaction())
{
BlockTable sourceBlkTbl = (BlockTable)trm.GetObject(
db.BlockTableId, OpenMode.ForRead, false);
if (! sourceBlkTbl.Has(strBlockName))
{
return false;
}
ObjectIdCollection blockIds = new ObjectIdCollection();
blockIds.Add(sourceBlkTbl);
IdMapping IdMap = new IdMapping();
db.WblockCloneObjects(blockIds, doc.Database.BlockTableId, IdMap, DuplicateRecordCloning.Replace, false);
}
}
tr.Commit();
}
return true;
}
public static ObjectId InsertBlock(string strFilePath, string strBlockName, Point3d insertPt)
{
ObjectId objId = new ObjectId();
if (strBlockName.CompareTo("") == 0 || strFilePath.CompareTo("") == 0)
{
return objId;
}
if (!System.IO.File.Exists(strFilePath))
{
CGcEditor.ShowMessage(string.Format("\nInput file :{0} no existed !", strFilePath));
return objId;
}
Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
if (! bt.Has(strBlockName))
{
if (! InsertBlock(strBlockName, strFilePath))
{
return objId;
}
}
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
using (BlockReference newBlkRef = new BlockReference(insertPt, bt))
{
//Matrix3d mat3d = Matrix3d.Rotation(dbAngle, Vector3d.ZAxis, insertPt);
//bref.TransformBy(mat3d);
objId = btr.AppendEntity(newBlkRef);
tr.AddNewlyCreatedDBObject(newBlkRef, true);
}
tr.Commit();
tr.Dispose();
}
return objId;
} 本帖最后由 作者 于 2010-10-26 22:45:34 编辑
如果原块有属性,则这种方法插入的块参照中属性值会丢掉,改为:
public static ObjectId m_InsertBlockRef(ObjectId blockId, Point3d insertPt)
{
ObjectId blockRefId = ObjectId.Null;
using (Transaction tr = mCommands.m_db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = tr.GetObject(mCommands.m_db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
BlockReference bref = new BlockReference(insertPt, blockId);//新建块参照,注意此时无属性参照
blockRefId = btr.AppendEntity(bref);//先加入数据库块表记录中
#region 加入默认属性
BlockTableRecord btrBlock = tr.GetObject(blockId, OpenMode.ForRead) as BlockTableRecord;
if (btrBlock.HasAttributeDefinitions)
{
foreach (ObjectId id in btrBlock)
{
DBObject ent = tr.GetObject(id, OpenMode.ForRead) as DBObject;
if (ent is AttributeDefinition)
{
AttributeDefinition attdef = ent as AttributeDefinition;//取得块的属性定义
AttributeReference attref = new AttributeReference();//新建属性参照
attref.SetPropertiesFrom(attdef);
attref.SetAttributeFromBlock(attdef, Matrix3d.Displacement(btrBlock.Origin.GetVectorTo(insertPt)));//复制属性
bref.AttributeCollection.AppendAttribute(attref);//附着属性参照
tr.AddNewlyCreatedDBObject(attref, true);
}
}
}
#endregion
tr.AddNewlyCreatedDBObject(bref,true);
tr.Commit();
}
return blockRefId;
}
}
学习了,一定要顶 好东西 顶啊!!!
有这样肯分享的高手我们这样的小兵才有希望升级。致敬一下!! 楼主辛苦了,正好不知道怎么插入外来块!!!! 这个好,最近正在研究使用C#进行cad二次开发,多谢! 楼主,不知道这些块是怎么影响块的图形的呢? mkhsj929 发表于 2010-10-26 21:25 static/image/common/back.gif
如果原块有属性,则这种方法插入的块参照中属性值会丢掉,改为:
学习学习 ,不错、、、、、、、 mkhsj929 发表于 2010-10-26 21:25如果原块有属性,则这种方法插入的块参照中属性值会丢掉,改为:
但是按这种方法,为什么最后属性的注记的位置始终不是自己想要的结果?
页:
[1]