Add string to attribute with field

Add string to attribute with field

msarqui
Collaborator Collaborator
1,594 Views
8 Replies
Message 1 of 9

Add string to attribute with field

msarqui
Collaborator
Collaborator

Hi guys,


May I have some help please?
I am trying to add a new string to a block attribute that has a field.
Please, see the attached file.
I would like to select the attribute and the routine will automatically add the "/301"


Thanks
Marcelo

0 Likes
Accepted solutions (1)
1,595 Views
8 Replies
Replies (8)
Message 2 of 9

braudpat
Mentor
Mentor

 

Hello

 

SORRY I am not a programmer so I can't improve the routine "AddStr" which does the job on ALL the attributes

Maybe this routine can help you ?

Or better somebody can add a question on which Attribute to update ?

 

Regards, Patrice

 

 

;; 
;; http://www.cadtutor.net/forum/showthread.php?61373-Adding-Prefix-or-Suffix-to-block-attribute
;; 
;; Routine: AddStr - Add Prefix/Suffix to ALL Attributes - From David BETHEL
;; 
;; Adding Prefix/Suffix to ALL attributes of the Blocks 
;; 

(defun c:AddStr (/ atype ns ss en ed an ad av nv)
  (initget 1 "Suffix Prefix")
  (setq atype (getkword "\nAdd Suffix or Prefix (S/P) :   "))

  (initget 1)
  (setq ns (getstring t "\nString To Add :  "))

  (and (setq ss (ssget '((0 . "INSERT")(66 . 1))))
       (while (setq en (ssname ss 0))
              (setq ed (entget en)
                    an (entnext en)
                    ad (entget an))
              (while (/= "SEQEND" (cdr (assoc 0 ad)))
                     (setq av (cdr (assoc 1 ad))
                           nv (if (= atype "Prefix")
                                  (strcat ns av)
                                  (strcat av ns)))
                     (entmod (subst (cons 1 nv) (assoc 1 ad) ad))
                     (setq an (entnext an)
                           ad (entget an)))
              (entupd en)
              (ssdel en ss)))
(prin1)
)

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 3 of 9

msarqui
Collaborator
Collaborator

Hi Patrice,

 

Thanks for repply but this will not work for me because I would like to keep the existing field.

0 Likes
Message 4 of 9

braudpat
Mentor
Mentor
Hello

Sorry but for me, this routine Addstr is OK
It runs on all attributes...

Regards, Patrice
Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 5 of 9

hmsilva
Mentor
Mentor
Accepted solution

Hi Marcelo,

to get the 'FIELDCODE' from TEXT or MTEXT, it is easy, we can get the 'FIELDCODE' property from TEXT or MTEXT as a vla-object.

 

(defun c:demo ( / hnd obj)
   (if (and (setq hnd (car (nentsel "\Select a Field to add \"\/301\": ")))
            (setq obj (vlax-ename->vla-object hnd))
            (vlax-method-applicable-p obj 'FIELDCODE)
            (vlax-write-enabled-p obj)
       )
      (vla-put-textstring obj (strcat (vlax-invoke-method obj 'FIELDCODE) "\/301"))
      (prompt "\nNot a field, or method not applicable... ")
   )
   (princ)
)

 

 

But to get the 'FIELDCODE' from an ATRRIB it is not easy, we need to step thru dictionaries and get the FIELD, then get all 360's pairs, long fieldcodes have multiple 360's pairs, get the ObjectID and put everything together in one 'FIELDCODE' string.

Sorry for my rough explanation, but English is not my native language...

The following quick and dirty 'demo' was mostly borrowed from Tlindell's 'fieldtotext', and it's just to show you one way to access the 'FIELDCODE' from an ATRRIB...

 

 ; http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/getting-the-field-code-of-text-strings/m-p/1459283#M193818
 ; From 'fieldtotext' by Tlindell
(defun c:demo (/ ent entid fld Counter fldNo fldList obj tmp subFldTxt subFldOid)
   (if (and (setq ent (car (nentsel "\Select a Field to add \"\/301\": ")))
            (setq entid (entget ent))
            (wcmatch (cdr (assoc 0 entid)) "MTEXT,TEXT,ATTRIB")
            (assoc 360 entid)
            (assoc 360 (entget (cdr (assoc 360 entid))))
            (setq obj (vlax-ename->vla-object ent))
            (vlax-write-enabled-p obj)
       )
      (progn
         (setq fld     (entget (cdr (assoc 360 (entget (cdr (assoc 360 (entget (cdr (assoc 360 entid)))))))))
               txt     (cdr (assoc 2 fld))
               cntr    0
               fldNo   (cdr (assoc 90 fld))
               fldList (vl-remove-if-not '(lambda (x) (= (car x) 360)) fld)
         )
         (while (< cntr fldNo)
            (setq tmp       (strcat "\\_FldIdx " (itoa cntr))
                  subFldTxt (cdr (assoc 2 (entget (cdr (nth cntr fldList)))))
            )
            (if (assoc 331 (entget (cdr (nth cntr fldList))))
               (progn
                  (setq subFldOid (strcat "ObjId " (itoa (vla-get-ObjectID (vlax-ename->vla-object (cdr (assoc 331 (entget (cdr (nth cntr fldList))))))))))
                  (setq subFldTxt (vl-string-subst subFldOid "ObjIdx 0" subFldTxt))
               )
            )
            (setq txt (vl-string-subst subFldTxt tmp txt))
            (setq cntr (+ cntr 1))
         )
         (vla-put-textstring obj (strcat txt "\/301"))
      )
      (prompt "\nNot a field... ")
   )
   (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 6 of 9

msarqui
Collaborator
Collaborator

Amazing Henrique!

Now I can continue to develop my routine from here.

Many thanks

Marcelo

0 Likes
Message 7 of 9

hmsilva
Mentor
Mentor

@msarqui wrote:

Amazing Henrique!

Now I can continue to develop my routine from here.

Many thanks

Marcelo


You're welcome, Marcelo!
Glad I could help

Henrique

EESignature

0 Likes
Message 8 of 9

3wood
Advisor
Advisor

@hmsilva wrote:

... 

But to get the 'FIELDCODE' from an ATRRIB it is not easy, we need to step thru dictionaries and get the FIELD, then get all 360's pairs, long fieldcodes have multiple 360's pairs, get the ObjectID and put everything together in one 'FIELDCODE' string.

Sorry for my rough explanation, but English is not my native language...

The following quick and dirty 'demo' was mostly borrowed from Tlindell's 'fieldtotext', and it's just to show you one way to access the 'FIELDCODE' from an ATRRIB...


It becomes more complicated when the attribute field contains of nested fields. The code doesn't work in that case.

0 Likes
Message 9 of 9

hmsilva
Mentor
Mentor

@3wood wrote:
It becomes more complicated when the attribute field contains of nested fields. The code doesn't work in that case.

Yes, it becomes more complicated...

 

Henrique

EESignature

0 Likes