- 积分
- 31765
- 明经币
- 个
- 注册时间
- 2005-5-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2018-10-18 22:23:57
|
显示全部楼层
Attribute prompt from AttributeReference ?
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using AcRx = Autodesk.AutoCAD.Runtime;
namespace Autodesk.AutoCAD.DatabaseServices
{
public static class ExtensionMethods
{
static RXClass attDefClass = RXClass.GetClass(typeof(AttributeDefinition));
public static void SynchronizeAttributes(this BlockTableRecord target)
{
if (target == null)
throw new ArgumentNullException("target");
Transaction tr = target.Database.TransactionManager.TopTransaction;
if (tr == null)
throw new AcRx.Exception(ErrorStatus.NoActiveTransactions);
List<AttributeDefinition> attDefs = target.GetAttributeDefinitions();
foreach (ObjectId id in target.GetBlockReferenceIds(true, false))
{
BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForWrite);
br.ResetAttributes(attDefs, tr);
}
if (target.IsDynamicBlock)
{
target.UpdateAnonymousBlocks();
foreach (ObjectId id in target.GetAnonymousBlockIds())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
attDefs = btr.GetAttributeDefinitions();
foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
{
BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForWrite);
br.ResetAttributes(attDefs, tr);
}
}
}
}
public static List<AttributeDefinition> GetAttributeDefinitions(this BlockTableRecord target)
{
if (target == null)
throw new ArgumentNullException("target");
Transaction tr = target.Database.TransactionManager.TopTransaction;
if (tr == null)
throw new AcRx.Exception(ErrorStatus.NoActiveTransactions);
List<AttributeDefinition> attDefs = new List<AttributeDefinition>();
foreach (ObjectId id in target)
{
if (id.ObjectClass == attDefClass)
{
AttributeDefinition attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
attDefs.Add(attDef);
}
}
return attDefs;
}
private static void ResetAttributes(this BlockReference br, List<AttributeDefinition> attDefs, Transaction tr)
{
Dictionary<string, string> attValues = new Dictionary<string, string>();
foreach (ObjectId id in br.AttributeCollection)
{
if (!id.IsErased)
{
AttributeReference attRef = (AttributeReference)tr.GetObject(id, OpenMode.ForWrite);
if (!attValues.ContainsKey(attRef.Tag))
{
attValues.Add(attRef.Tag,
attRef.IsMTextAttribute ? attRef.MTextAttribute.Contents : attRef.TextString);
}
attRef.Erase();
}
}
foreach (AttributeDefinition attDef in attDefs)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
if (attDef.Constant)
{
attRef.TextString = attDef.IsMTextAttributeDefinition ?
attDef.MTextAttributeDefinition.Contents :
attDef.TextString;
}
else if (attValues.ContainsKey(attRef.Tag))
{
attRef.TextString = attValues[attRef.Tag];
}
br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
} |
|