从块中获取属性的简单方法
我正在做我的第一个成年VB.net项目,它是一个vba脚本的重写,该脚本从块中收集某些属性值并将它们作为BOM制成表格。我正在努力从块引用中获取属性值——我被一个问题弄得更复杂,虽然大多数属性是恒定的,但在某些情况下它们不是,所以我真的需要收集所有属性并按标签排序。
我的VBA方法使用了
BlockRef.Getattributes
和
BlockRef.GetConstantAttributes
方法,它将我需要的一切都放在数据结构中。
我的VB获取属性的方法因是否满足要求而异:对于非恒定属性,我使用
blockref.AttributeCollection
语句,然后给我一个包含属性的结构,非常像VBA
但是对于常量属性,我目前必须转到块定义,并遍历内容,隔离属性定义并检查常量属性是否为真
这是两种非常不同的方法,并且有点让我的编程魔力感到不安-有一种技术可以用于两种类型的属性吗?
任何想法、提示或代码片段都将不胜感激...
G
**** Hidden Message ***** blockref。AttributeCollection将返回与BlockReference关联的所有AttributeReferences,包括任何标记为常量的。 对不起,我不会说VB 。
使用系统;。
使用系统,集合,泛型;。
使用系统,Linq。
使用系统,文本;。
使用Autodesk,AutoCAD . Runtime。
。
命名空间Autodesk,AutoCAD.DatabaseServices。
{。
公共静态类AcDbExtensionMethods。
{。
静态RXClass attDefClass = RXClass,GetClass(type of(attribute definition));。
。
//要求调用时事务(不是OpenCloseTransaction)处于活动状态:。
//返回常量属性为的所有AttributeDefinitions的枚举。
// true,以及附着到块引用的所有AttributeReferences。。
。
公共静态IEnumerableget attributes(this block reference block ref)。
{。
事务tr = blockRef,get transaction();。
block table record btr =(block table record)block ref,DynamicBlockTableRecord . GetObject(OpenMode,for read);。
if( btr,HasAttributeDefinitions )。
{。
foreach(block ref中的ObjectId id,AttributeCollection )。
{。
收益率(AttributeReference) tr,GetObject( id,OpenMode。for read);。
}。
foreach(btr中的ObjectId id)。
{。
如果(id,ObjectClass == attDefClass )。
{。
attribute definition attdef =(attribute definition)trGetObject( id,OpenMode。for read);。
if( attdef,常数)。
yield返回attdef。
}。
}。
}。
}。
。
//需要活动事务(不是OpenCloseTransaction)。
//返回一个字典,其值为常量AttributeDefinitions。
//或AttributeReferences,键入它们的标记:。
。
公共静态字典 GetAttributeDictionary(this block reference block ref)。
{。
返回blockref,GetAttributes(),ToDictionary( a => GetTag( a),StringComparer。普通案件);。
}。
。
公共静态事务GetTransaction(此DBObject obj )。
{。
if( obj,Database == null )。
抛出新的ArgumentException("无数据库");。
事务tr = obj,database . transaction manager . top transaction;。
if( tr == null )。
抛出新的InvalidOperationException("无活动事务");。
返回tr;。
}。
。
静态字符串GetTag( DBText dbtext )。
{。
attribute definition attdef = db text as attribute definition;。
if( attdef!= null )。
返回属性,标签;。
attribute reference attref = db text as attribute reference;。
if( attref!= null )。
返回attref,标签;。
throw new ArgumentException("需要AttributeDefintion或attribute reference ");。
}。
}。
}。
。
页:
[1]