- 积分
- 379
- 明经币
- 个
- 注册时间
- 2012-6-5
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 沼泽里的鱼 于 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[strBlockName]);
- 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[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- using (BlockReference newBlkRef = new BlockReference(insertPt, bt[strBlockName]))
- {
- newBlkRef.ScaleFactors = new Scale3d(scale);
- objId = btr.AppendEntity(newBlkRef);
- #region 加入默认属性
- BlockTableRecord btrBlock = tr.GetObject(bt[strBlockName], 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;
- }
|
|