Jump to content

read field expression of an attribute


aleair

Recommended Posts

Hi all,

 

I have a block-reference with attribute-references.

 

One of these references is a calculated field that gives the area of a polyline.

 

This is its calculation expression:

%%).Area \f "%lu2">%

 

where 2129748024 is the ID of the linked polyline.

 

I'd like to get the expression above in order to extract the object id number (2129748024). My aim is to get the id number and use it to insert a new attribute into the block that will have a calculated field corresponding to the length of the same polyline.

 

I posted this issue on www.vbaexpress.com without solutions yet.

 

Thanks in advance.

Link to comment
Share on other sites

This code is is kinda dirty and tricky you have to vanish it

(sorry for the bad formatting)


<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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...