I want to contribute the following lisp code which retrieves the filed code from a text or attribute. I know we have FieldCode method in VBA for texts for this purpose but there is no equivalent for attributes. This code works for both and save the output to USERS1 variable so it can be used in a VBA macro. You can also use "fldtxt" variable in other AutoLisp codes.There is also a sample AutoLISP function "TFC" and VBA macros to utilize the GetFieldCode function in VBA. I am a newbie in AutoLISP programming so you may find the code not very efficient. I appreciate any comment.
;Start of AutoLISP code:
(defun GetFieldCode (handle / tmp fld fldCounter subFldTxt fldNo fldList)
(setq tmp (entget (handent handle))) ; get the entity
(princ "\n")
(setq fld
(entget
(cdr
(assoc
'360
(entget (cdr (assoc '360 (entget (cdr (assoc '360 tmp)))))
; get the field object
)
)
)
)
)
(setq fldtxt (cdr (assoc '2 fld))) ; get the field pattern string
(setq fldCounter 0)
(setq fldNo (cdr (assoc '90 fld))) ; number of fields
(setq fldList (vl-remove-if 'null (mapcar '(lambda (a) (if (= (car a) 360) a)) fld))) ; filter list of field entities
(while (< fldCounter fldNo) ; loop to the number of fields
(setq tmp (strcat "\\_FldIdx " (itoa fldCounter)))
; part of the field string to be replaced
(setq subFldTxt
(cdr (assoc '2 (entget (cdr (nth fldCounter fldList)))))
) ; get the actual field string for each sub field
(setq fldtxt (vl-string-subst subFldTxt tmp fldtxt))
; and replace it in the text string
(setq fldCounter (1+ fldCounter))
)
(setvar "USERS1" fldtxt) ; save the output field code to USERS1 system variable
(princ)
)
(defun C:TFC() ; Test GetFieldCode function
(setq hnd (cdr (assoc '5 (entget (car (nentsel)))))) ; gettign the entity handle
(GetFieldCode hnd)
(princ fldtxt)
(princ)
)
;Endt of AutoLISP code:
;Start of VBA macros
Private Function GetFieldCode(ByVal objHandle As String) As String
On Error GoTo ErrorHandler
Dim cmd As String
ThisDrawing.SetVariable "CMDECHO", 0
cmd = "(GetFieldCode """ & objHandle & """) "
ThisDrawing.SendCommand cmd
GetFieldCode = ThisDrawing.GetVariable("USERS1")
ErrorHandler:
If Err Then
Debug.Print "GetFieldCode: " & Err.Description
GetFieldCode = ""
Err.Clear
End If
ThisDrawing.SetVariable "CMDECHO", 1
End Function
Sub testGetFieldCode()
On Error GoTo ErrorHandler
Dim Object As AcadObject
Dim PickedPoint As Variant, TransMatrix As Variant, ContextData As Variant
ThisDrawing.Utility.GetSubEntity Object, PickedPoint, TransMatrix, ContextData
Debug.Print Object.TextString
Debug.Print GetFieldCode(Object.handle)
ErrorHandler:
If Err Then
Debug.Print Err.Description
End If
End Sub
;End of VBA macros