|
发表于 2018-10-18 22:23:00
|
显示全部楼层
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 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 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 attDefs = new List();
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 attDefs, Transaction tr)
{
Dictionary attValues = new Dictionary();
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);
}
}
}
} |
|