aleair 发表于 2022-7-6 22:45:54

at的读取字段表达式

大家好,
 
我有一个带有属性引用的块引用。
 
其中一个参考是计算字段,该字段给出了多段线的面积。
 
这是其计算表达式:
%%).区域\f“%lu2”>%
 
其中2129748024是链接多段线的ID。
 
为了提取对象id号(2129748024),我想得到上面的表达式。我的目标是获取id号,并使用它将新属性插入到块中,该块将具有对应于同一多段线长度的计算字段。
 
我在www.vbaexpress上发布了这个问题。com还没有解决方案。
 
提前谢谢。

fixo 发表于 2022-7-7 00:35:29

这段代码有点脏,很棘手,你必须把它抹掉
(抱歉格式错误)


<CommandMethod("foo")> _
       Public Sub
GetFieldFromAttribute()

Dim doc As Document =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

Dim db As Database =
doc.Database

Dim ed As Editor = doc.Editor


Try

Dim tab As String =
Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("ctab").ToString

Dim blockname As String = ed.GetString(vbLf & "Enter blockname:
").StringResult

Dim opts As PromptSelectionOptions = New
PromptSelectionOptions

opts.SingleOnly =
True

opts.MessageForRemoval = vbLf & "Select block
only"

opts.MessageForAdding = vbLf & "Select single
block"

Dim filt As SelectionFilter = New SelectionFilter({New TypedValue(0, "insert"),
New TypedValue(2, blockname), New TypedValue(410,
tab)})

Dim psr As PromptSelectionResult = ed.GetSelection(opts,
filt)

If psr.Status <> PromptStatus.OK Then Exit Sub


If psr.Value.Count <> 1 Then Exit Sub


Dim id As ObjectId = psr.Value.GetObjectIds(0)


If id.IsNull Then Exit Sub


Using tr As Transaction = db.TransactionManager.StartTransaction()


Dim obj As DBObject = tr.GetObject(psr.Value.GetObjectIds(0), OpenMode.ForRead)


If TypeOf obj Is BlockReference Then


Dim bref As BlockReference = CType(tr.GetObject(id, OpenMode.ForRead),
BlockReference)

Dim attcoll As AttributeCollection =
bref.AttributeCollection

Dim att As
AttributeReference

For Each aid As ObjectId In
attcoll

Dim aobj As DBObject = tr.GetObject(aid,
OpenMode.ForRead)

att = TryCast(DirectCast(aobj, AttributeReference),
AttributeReference)

If att Is Nothing Then Exit
Sub

If att.Tag.Equals("LENGTH", StringComparison.CurrentCultureIgnoreCase)
Then

If att.HasFields
Then

Dim fld As Field = tr.GetObject(att.GetField(),
OpenMode.ForRead)

Dim code As String =
fld.GetFieldCode

Dim pos1 As Int32 =
code.IndexOf("_ObjId")

Dim pos2 As Integer =
code.IndexOf(">%")

Dim leng1 As Integer =
"_ObjId".Length

Dim leng2 As Integer =
">%".Length

Dim idstr As String = code.Substring(pos1 + leng1 + 1, pos2 - (pos1 + leng1) -
1)


MsgBox("ID from field code: " & idstr)


Dim objId As ObjectId = New ObjectId(New IntPtr(Convert.ToInt32(idstr)))


Dim fieldchild As DBObject = tr.GetObject(objId,
OpenMode.ForRead)

If TypeOf fieldchild Is Polyline
Then

Dim poly As Polyline = DirectCast(fieldchild,
Polyline)

MsgBox("Compare:" & vbLf & "Return from attribute: " &
att.TextString & vbLf & "Return from Polyline: " &
poly.Area.ToString)

End
If

Exit
For

End
If

End If


Next

End
If

End Using
         Catch ex
As
Autodesk.AutoCAD.Runtime.Exception

ed.WriteMessage(ex.Message + vbLf +
ex.StackTrace)

End Try
       End Sub
页: [1]
查看完整版本: at的读取字段表达式