[结贴]插入外部属性块,无法获得属性的值
本帖最后由 沼泽里的鱼 于 2012-10-26 17:35 编辑开发了个程序,想实现如下功能,但是遇到问题,请各位帮助分析诊断一下,谢谢!
方法:选取外部文件,读取里面的属性块并插入,获取其默认属性值,显示在指定的位置。
问题:块插入正常,但是属性值无法获取,如:一个块中有个属性Tag=“工号”,textString=“0123”,通过程序插入后,能查看和编辑该属性,但是值为"",也即没有将定义好的默认属性值带入插入的图形中去。
代码如下:
private static bool InsertBlock(string strBlockName, string strFilePath)
{
Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
using (Database db = new Database(false, true))
{
db.ReadDwgFile(strFilePath, System.IO.FileShare.Read, true, null);
using (Transaction trm = db.TransactionManager.StartTransaction())
{
BlockTable sourceBlkTbl = (BlockTable)trm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
if (!sourceBlkTbl.Has(strBlockName))
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 91");
return false;
}
ObjectIdCollection blockIds = new ObjectIdCollection();
blockIds.Add(sourceBlkTbl);
IdMapping IdMap = new IdMapping();
db.WblockCloneObjects(blockIds, doc.Database.BlockTableId, IdMap, DuplicateRecordCloning.Replace, false);
trm.Commit();
}
}
tr.Commit();
}
}
return true;
}
public static ObjectId InsertBlock(string strFilePath, string strBlockName, Point3d insertPt, Double scale)
{
ObjectId objId = new ObjectId();
Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
if (strBlockName.CompareTo("") == 0 || strFilePath.CompareTo("") == 0)
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 118!");
return objId;
}
if (!System.IO.File.Exists(strFilePath))
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("没有找到外部引用文件!");
doc.Editor.WriteMessage(string.Format("\nInput file :{0} no existed !", strFilePath));
return objId;
}
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
if (!bt.Has(strBlockName))
{
if (!InsertBlock(strBlockName, strFilePath))
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 137");
return objId;
}
}
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
using (BlockReference newBlkRef = new BlockReference(insertPt, bt))
{
newBlkRef.ScaleFactors = new Scale3d(scale);
objId = btr.AppendEntity(newBlkRef);
#region 加入默认属性
BlockTableRecord btrBlock = tr.GetObject(bt, 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)));//复制属性
newBlkRef.AttributeCollection.AppendAttribute(attref);//附着属性参照
tr.AddNewlyCreatedDBObject(attref, true);
attref = null;
}
}
}
#endregion
tr.AddNewlyCreatedDBObject(newBlkRef, true);
}
tr.Commit();
//tr.Dispose();
}
return objId;
}
textString=“0123”是存在于块参考中,而从外部文件搬进来的是块记录
感谢回复。怎么才能获得textString=“0123”呢? textString=“0123”只是其中一个块参考BlockReference的值,若有多个块参考,则或许另有textString=“0000”;
你若是要把块参考搬进来,则把块参考的ObjectId放进blockIds,如blockIds.Add(sourceBlkTbl);
如你所说,在块定义的时候必须要给默认属性值,这样在其它图形中插入该块的时候就能获取到该属性值。 不错,很好
页:
[1]