MexicanCustard 发表于 2015-6-11 09:53:16

获取外部参照中实体的最佳方式

我遇到了一个问题,我需要选择图形中具有特定扩展数据的所有实体。通常我会这样做:    Dim entities = New objectid collection()。
Dim typed values As typed value()= { New typed value(DXF code,ExtendedDataRegAppName," AppName")} 。
Dim selection filter = New selection filter(typed values)。
Dim结果为PromptSelectionResult = Active,editor . SelectAll(selection filter)。

如果结果,Status = PromptStatus,那好吧。
entities = New objectid collection(结果,value . get objectid())。
End If但是,这不会获取外部参照或块中的任何嵌套实体,实现这一目标的最佳方式是什么?是检查块引用,然后分解项目,清洗并重复吗?有没有人已经完成了这一步的样板代码?这似乎是一件需要做很多的事情,**编辑**我需要在没有任何用户交互的情况下完成此操作,否则我将只使用其中一个GetNestedEntity例程。
**** Hidden Message *****

kaefer 发表于 2015-6-11 13:11:54

你可以使用编辑器。选择添加的事件,并通过BlockReferences筛选包含您要查找的扩展数据的enitites。

kaefer 发表于 2015-6-11 14:06:17

以下是我想出的解决方案,只需获取包含所需 XData 的模型空间中每个实体的唯一 objectId。 我为VB道歉,但我被要求用它来工作。 我每天晚上回家,在热水淋浴中擦洗自己,只是为了在事后摆脱它的臭味。 开个玩笑,但我确实认为C#是一种更优雅的语言。Private Function GetXDataEntities() As ObjectIdCollection。

Dim entitiesId = New ObjectIdCollection()。
Dim database = Active.Database。

尝试。
Using transaction = Active.Database.TransactionManager.StartTransaction()。
Dim blockTable = TryCast(transaction.GetObject(database.BlockTableId, OpenMode.ForRead), BlockTable)。
如果 blockTable 不是 Nothing,则。
Dim modelspace = TryCast(transaction.GetObject(blockTable(BlockTableRecord.ModelSpace), OpenMode.ForRead), BlockTableRecord)。
如果模型空间不是虚无的,那么。
EntityWalker(modelspace, entitiesId, transaction)。
如果结束。
如果结束。
结束使用。

将异常捕获为系统,异常。
Log.Logger.Error(异常,“GetLineEntities: Error getting entity objectId”)。
结束试用。

返回实体 Id。

结束功能。

Private Sub EntityWalker(blockTableRecord As BlockTableRecord, ByRef entitiesId as ObjectIdCollection, transaction As Transaction)。

尝试。
Dim rxc As RXClass = RXClass.GetClass(GetType(BlockReference))。
对于每个实体 Id 作为 BlockTable 中的 ObjectId 记录。
Dim entity = TryCast(transaction.GetObject(entityId, OpenMode.ForRead, False, True), Entity)。
如果实体不是无,则。
如果 entityId.ObjectClass = rxc 则。
Dim blockReference = TryCast(transaction.GetObject(entityId, OpenMode.ForRead), BlockReference)。
如果块引用不是没有,则。
Dim blockTableRecord2 = TryCast(transaction.GetObject(blockReference.BlockTableRecord, OpenMode.ForRead, False, True), BlockTableRecord)。
如果 blockTableRecord2 Is Not Nothing,则。
EntityWalker(blockTableRecord2, entitiesId, transaction)。
如果结束。
如果结束。
还。
如果 XData.CheckXDataExists(实体,_AppName) 则。
如果不是实体 Id.包含(实体.ObjectId) 则。
entitiesId.Add(entity.对象 Id)。
如果结束。
如果结束。
如果结束。
如果结束。
下一个。

将异常捕获为异常。
Log.Logger.Error(异常,“EntityWalker: Error getting entity objectId”)。
结束试用。

结束子。

kaefer 发表于 2015-6-11 15:13:13

我希望使用 vb.net 不会对程序逻辑产生不利影响。是只有我,还是两次打开相同的 ObjectId,并且还在 RXClass 和 .net 对象类型上执行基本相同的测试?
您的代码可能会受益于 TypeOf Operator (Visual Basic)

kaefer 发表于 2015-6-11 15:31:49

我注意到在我发布代码后,我已经更改了TryCast并删除了事务,以便像这样将实体直接转换为BlockReference。If entityId,ObjectClass = rxc Then。
Dim block reference = direct cast(entity,BlockReference)。
Dim block table record 2 = try cast(事务,GetObject(blockReference,BlockTableRecord,OpenMode。ForRead,False,True),BlockTableRecord)。
...我将检查运算符的类型,我的所有代码肯定会从代码审查中受益,事实上,我每天晚上都花很多时间复习代码,学习程序课程,尽可能多的学习。我以前没有受过训练,所以我尽我所能把事情做好。。
页: [1]
查看完整版本: 获取外部参照中实体的最佳方式