Author Topic: Help: Adding Prefix to Selected Attribute Text  (Read 1036 times)

0 Members and 1 Guest are viewing this topic.

AVCAD

  • Newt
  • Posts: 29
Help: Adding Prefix to Selected Attribute Text
« on: January 19, 2024, 12:15:59 PM »
Hi all,

I found this Lisp from Lee Mac on a separate forum, and it works but it changes all of the attributes where I only want to change the selected attribute or even if its one attribute by the TAG name.

The Tag I need to be changed is called D_ID

This way when i select the text/block it adds the prefix i want to this attribute only.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / as el en i ss str typ )
  2.  
  3.  (initget "Prefix Suffix")
  4.  (setq typ (cond ((getkword "\nAdd Prefix or Suffix? [Prefix/Suffix] <Prefix>: ")) ("Prefix")))
  5.  (setq str (getstring t (strcat typ " to Add: ")))
  6.  
  7.  (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))
  8.      (repeat (setq i (sslength ss))
  9.          (setq en (ssname ss (setq i (1- i))))
  10.          (while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en)))))))
  11.              (setq as (cdr (assoc 1 el)))
  12.              (if (eq "Prefix" typ)
  13.                  (if (not (wcmatch as (strcat str "*")))
  14.                      (entmod (subst (cons 1 (strcat str as)) (assoc 1 el) el))
  15.                  )
  16.                  (if (not (wcmatch as (strcat "*" str)))
  17.                      (entmod (subst (cons 1 (strcat as str)) (assoc 1 el) el))
  18.                  )
  19.              )
  20.          )
  21.      )
  22.  )
  23.  (princ)
  24. )
  25.  

I found this code here: https://www.cadtutor.net/forum/topic/32354-adding-prefix-or-suffix-to-block-attribute/

Thanks in advance!


edit Kerry : [ code=cadlisp-7 ]
« Last Edit: January 19, 2024, 08:37:46 PM by kdub_nz »

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #1 on: January 19, 2024, 12:34:59 PM »
Hey, that code is using the RRB_Default Method I've been preaching all these years. Nice!

I digress. If you're picking attributes one at a time I really like a simple attribute editor program a friend wrote. I use it all the time. I wish I saved the link to the original post but it was a very long time ago and I may even have modified the code (very slightly if at all). So, if you want to just pick the attribute and want a simple dialog to change the text, this is a great utility.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:at ( / _ExeFooAtPoint _Foo _EditAttribObject _MakeDCL _Main )
  2.  
  3.     ;;  © Michael Puckett.
  4.  
  5.     (defun _ExeFooAtPoint ( foo pmt hilite / _Hilite result key value done enames )
  6.  
  7.         ;;  ----------------------------------------
  8.         ;;  foo is an arbitrary function that takes
  9.         ;;  one argument, a point. What is does we
  10.         ;;  care not. It is up to foo to ensure it
  11.         ;;  addresses any and all potential errors.
  12.         ;;  ----------------------------------------
  13.         ;;  pmt is a prompt string
  14.         ;;  ----------------------------------------
  15.         ;;  hilight is an entity type match string,
  16.         ;;  e.g. "ATTRIB". The function will hilite
  17.         ;;  matching entities as it goes.
  18.         ;;  ----------------------------------------
  19.  
  20.         (defun _Hilite ( point hilite / ename )
  21.             ;;  uses local global enames
  22.             (cond
  23.                 (   (and
  24.                         (setq ename (car (nentselp point)))
  25.                         (not (equal ename (car enames)))
  26.                         (wcmatch
  27.                             (cdr (assoc 0 (entget ename)))
  28.                             hilite
  29.                         )
  30.                     )
  31.                     (redraw ename 3)
  32.                     (setq enames (cons ename enames))
  33.                 )
  34.                 (   ename
  35.                     (foreach ename (cdr enames) (redraw ename 4))
  36.                     (setq enames (list ename))
  37.                 )
  38.                 (   t
  39.                     (foreach ename enames (redraw ename 4))
  40.                     (setq enames nil)
  41.                 )
  42.             )
  43.         )
  44.  
  45.         (setq hilite (strcase hilite))
  46.  
  47.         (princ pmt)
  48.  
  49.         (while (not done)
  50.             ;;  stay in the loop until the
  51.             ;;  user presses <enter> or <esc>
  52.             (vl-catch-all-apply
  53.                '(lambda ( )
  54.                     (setq
  55.                         result nil
  56.                         result (grread t 13 2)
  57.                     )
  58.                 )
  59.             )
  60.             (setq
  61.                 key   (car result)
  62.                 value (cadr result)
  63.             )
  64.             (cond
  65.                 ;;  user pressed <enter> or <esc> exit quietly
  66.                 ((member result '(nil (2 13))) (setq done t value nil))
  67.                 ;;  user hit right mouse button, consider done
  68.                 ((eq 25 key)(setq done t value nil))
  69.                 ;;  user picked a point, let's roll
  70.                 ((eq 3 key) (foo value))
  71.                 ((eq 5 key) (_Hilite value hilite))
  72.             )
  73.         )
  74.  
  75.         (foreach ename enames (redraw ename 4))
  76.  
  77.     )
  78.  
  79.     (defun _Foo ( point / ename data )
  80.         (if (setq ename (car (nentselp point)))
  81.             (if (setq data (entget ename))
  82.                 (if (eq "ATTRIB" (cdr (assoc 0 data)))
  83.                     (_EditAttrib (vlax-ename->vla-object ename))
  84.                     (princ "<not an attribute>")
  85.                 )
  86.                 (princ "<no data for entity>")
  87.  
  88.             )
  89.             (princ "<no entity at point>")
  90.         )
  91.     )
  92.  
  93.     (defun _EditAttrib ( attribObject / _Text _Cancel _Accept text )
  94.  
  95.         ;;  note, uses local global dclid
  96.  
  97.         (defun _Text ( value )
  98.             ;;  update local global
  99.             (setq text value)
  100.         )
  101.  
  102.         (defun _Cancel ( )
  103.             ;;  just bail
  104.             (done_dialog)
  105.         )
  106.  
  107.         (defun _Accept ( )
  108.             ;;  uses local glabal text
  109.             (vla-put-textstring
  110.                 attribObject
  111.                 text
  112.             )
  113.             (done_dialog)
  114.         )
  115.  
  116.         (new_dialog "EdAtt" dclid)
  117.  
  118.         (set_tile "title"
  119.             (strcat "Edit attribute <"
  120.                 (vla-get-tagstring attribObject)
  121.                 ">"
  122.             )
  123.         )
  124.  
  125.         (set_tile "text"
  126.             (setq text
  127.                 (vla-get-textstring attribObject)
  128.             )
  129.         )
  130.  
  131.         (action_tile "text" "(_Text $value)")
  132.         (action_tile "cancel" "(_Cancel)")
  133.         (action_tile "accept" "(_Accept)")
  134.         (start_dialog)
  135.  
  136.     )
  137.  
  138.     (defun _MakeDCL ( / fname handle )
  139.  
  140.         (setq
  141.             fname  (vl-filename-mktemp "1.dcl")
  142.             handle (open fname "w")
  143.         )
  144.  
  145.         (mapcar '(lambda (x) (write-line x handle))
  146.            '(
  147.                 "EdAtt : dialog {"
  148.                 "    label = \"Edit Attribute <TagString>\";"
  149.                 "    key = \"title\";"
  150.                 "    initial_focus = \"text\";"
  151.                 "    spacer;"
  152.                 "    :   edit_box {"
  153.                 "        label = \"Attribute value:\";"
  154.                 "        key = \"text\";"
  155.                 "        edit_width = 20;"
  156.                 "        edit_limit = 2048;"
  157.                 "        allow_accept = true;"
  158.                 "    }"
  159.                 "    spacer;"
  160.                 "    ok_cancel;"
  161.                 "    spacer;"
  162.                 "}"
  163.             )
  164.         )
  165.  
  166.         (close handle)
  167.  
  168.         fname
  169.  
  170.     )
  171.  
  172.     (defun _Main ( / dclname dclid )
  173.         (cond
  174.             (   (setq dclid
  175.                     (load_dialog
  176.                         (setq dclname (_MakeDCL))
  177.                     )
  178.                 )
  179.                 (_ExeFooAtPoint _Foo "Pick an attribute <esc|enter to end>: " "ATTRIB")
  180.                 (unload_dialog dclid)
  181.                 (vl-file-delete dclname)
  182.             )
  183.         )
  184.         (princ)
  185.     )
  186.  
  187.     (_Main)
  188.  
  189. )
  190.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

AVCAD

  • Newt
  • Posts: 29
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #2 on: January 19, 2024, 02:36:55 PM »
Thanks JohnK.

I like this program but really what i need is something that is predefined just click the text and it adds the prefix to the text there already.

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #3 on: January 19, 2024, 03:33:56 PM »
Thanks JohnK.

I like this program but really what i need is something that is predefined just click the text and it adds the prefix to the text there already.

How long is a string?
I'm sure that can be done but you're being a little vague about what you want. For example, what do you want as a prefix, how many blocks (would this have to happen to all the instances in the whole drawing), etc.. So please take some time and define your problem so people can help you.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

AVCAD

  • Newt
  • Posts: 29
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #4 on: January 19, 2024, 05:07:24 PM »
Sorry,

I would like to select as many blocks as needed...one, 2 a window...

The block names a all the same which i dont think would matter anyways and the TAG's are all the same D_ID

what I want is to select the blocks and have the prefix be EXISTING and it gets added to all of the tag D_ID 's it finds

I found the below and modified it a little to make it work. seems to be working good right now

Code - Auto/Visual Lisp: [Select]
  1. (defun c:attfix (/ as el en i n pat ss str tag typ i2 ss2 en2 el2 as2 pat2 str2 tag2 n2)
  2.  ;; Change tag to suit
  3.  (setq tag "D_ID")
  4.  (initget "Prefix Suffix")
  5.  (if (and (setq typ (cond ((getkword "\nAdd Suffix or Prefix  <Prefix>: "))
  6.                    ("Prefix")
  7.              )
  8.    )
  9.    (/= "" (setq str (getstring t (strcat typ " to Add: "))))
  10.    (setq ss (ssget ":L" '((0 . "INSERT") (66 . 1))))     
  11.      )
  12.    (repeat (setq i (sslength ss))
  13.      (setq en (ssname ss (setq i (1- i))))
  14.      (while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en)))))))
  15. (setq as  (cdr (assoc 1 el))
  16.       pat (cond ((eq "Prefix" typ) (strcat str as))
  17.                 ((strcat as str))
  18.           )
  19.       n   (vl-string-search (strcase str) (strcase as))
  20. )
  21. (if (and (= tag (cdr (assoc 2 el)))
  22.          (or (null n)
  23.              (not (wcmatch (strcase as)
  24.                            (strcase (cond ((= 0 n) (strcat str "*"))
  25.                                           ((strcat "*" str))
  26.                                     )
  27.                            )
  28.                   )
  29.              )
  30.          )
  31.     )
  32.   (entmod (subst (cons 1 pat) (assoc 1 el) el))
  33. )
  34.      )
  35.    )
  36.  )
  37. (setq tag2 "D_MODEL")
  38.  (initget "Y N")
  39.  (if (and (setq typ2 (cond ((getkword "\nExisting Model?  <Y>: "))
  40.                    ("Y")
  41.              )
  42.    )
  43. (IF (= typ2 "Y")
  44.   (setq str2 "//")
  45.   )
  46.      )
  47.    (repeat (setq i2 (sslength ss))
  48.      (setq en2 (ssname ss (setq i2 (1- i2))))
  49.      (while (eq "ATTRIB" (cdr (assoc 0 (setq el2 (entget (setq en2 (entnext en2)))))))
  50. (setq as2  (cdr (assoc 1 el2))
  51.       pat2 (cond        ((eq "Prefix" typ) (strcat str2 as2))
  52.                 ((strcat as2 str2))
  53.           )
  54.       n2          (vl-string-search (strcase str2) (strcase as2))
  55. )
  56. (if (and (= tag2 (cdr (assoc 2 el2)))
  57.          (or (null n2)
  58.              (not (wcmatch (strcase as2)
  59.                            (strcase (cond ((= 0 n2) (strcat str2 "*"))
  60.                                           ((strcat "*" str2))
  61.                                     )
  62.                            )
  63.                   )
  64.              )
  65.          )
  66.     )
  67.   (entmod (subst (cons 1 pat2) (assoc 1 el2) el2))
  68. )
  69.      )
  70.    )
  71.  )
  72.  (princ)
  73. )
  74.  

edit Kerry : [ code=cadlisp-7 ]
« Last Edit: January 19, 2024, 08:36:52 PM by kdub_nz »

AVCAD

  • Newt
  • Posts: 29
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #5 on: January 26, 2024, 03:50:12 PM »
Hello again,

I had a request after I got this to work to make all the items that were changed be put on a different layer

I sort of got it to work....but it changes all of the selected blocks even if the block was not changed... I know why....its because I am using "previous" as the selection method. I thought it would only select the blocks that got changed but it is not doing that.

Is there away of doing this? only putting the items that were changed on the layer instead of everything I selected? Just easier to use a Crossing window to select instead of selecting each object

Thanks in advance again!

Code Below:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:attfix (/ as el en i n pat ss str tag typ i2 ss2 en2 el2 as2 pat2 str2 tag2 n2)
  2.  ;; Change tag to suit
  3.  (setq tag "D_ID")
  4.  (initget "Prefix Suffix")
  5.  (if (and (setq typ (cond ((getkword "\nAdd Suffix or Prefix  <Prefix>: "))
  6.                    ("Prefix")
  7.              )
  8.    )
  9.    (/= "" (setq str (getstring t (strcat typ " to Add: "))))
  10.    (setq ss (ssget ":L" '((0 . "INSERT") (66 . 1))))     
  11.      )
  12.    (repeat (setq i (sslength ss))
  13.      (setq en (ssname ss (setq i (1- i))))
  14.      (while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en)))))))
  15. (setq as  (cdr (assoc 1 el))
  16.       pat (cond ((eq "Prefix" typ) (strcat str as))
  17.                 ((strcat as str))
  18.           )
  19.       n   (vl-string-search (strcase str) (strcase as))
  20. )
  21. (if (and (= tag (cdr (assoc 2 el)))
  22.          (or (null n)
  23.              (not (wcmatch (strcase as)
  24.                            (strcase (cond ((= 0 n) (strcat str "*"))
  25.                                           ((strcat "*" str))
  26.                                     )
  27.                            )
  28.                   )
  29.              )
  30.          )
  31.     )
  32.   (entmod (subst (cons 1 pat) (assoc 1 el) el))
  33. )
  34.      )
  35.    )
  36.  )
  37. (setq tag2 "D_MODEL")
  38.  (initget "Y N")
  39.  (if (and (setq typ2 (cond ((getkword "\nExisting Model?  <Y>: "))
  40.                    ("Y")
  41.              )
  42.    )
  43. (IF (= typ2 "Y")
  44.   (setq str2 "//")
  45.   )
  46.      )
  47.    (repeat (setq i2 (sslength ss))
  48.      (setq en2 (ssname ss (setq i2 (1- i2))))
  49.      (while (eq "ATTRIB" (cdr (assoc 0 (setq el2 (entget (setq en2 (entnext en2)))))))
  50. (setq as2  (cdr (assoc 1 el2))
  51.       pat2 (cond        ((eq "Prefix" typ) (strcat str2 as2))
  52.                 ((strcat as2 str2))
  53.           )
  54.       n2          (vl-string-search (strcase str2) (strcase as2))
  55. )
  56. (if (and (= tag2 (cdr (assoc 2 el2)))
  57.          (or (null n2)
  58.              (not (wcmatch (strcase as2)
  59.                            (strcase (cond ((= 0 n2) (strcat str2 "*"))
  60.                                           ((strcat "*" str2))
  61.                                     )
  62.                            )
  63.                   )
  64.              )
  65.          )
  66.     )
  67.   (entmod (subst (cons 1 pat2) (assoc 1 el2) el2))
  68. )
  69.      )
  70.    )
  71.  )
  72. (setq curlay (getvar "clayer"))
  73. (command "-layer" "m" "1_AV-LINE-OFE" "c" "251" "" "")
  74. (setvar "clayer" "1_AV-LINE-OFE")
  75. (command "chprop" "P" "" "la" "1_AV-LINE-OFE" "")
  76. (setvar "clayer" curlay)
  77.  (princ)
  78. )
  79.  (princ "\nAttribute Prefix tool has been loaded: command = ATTFIX ")
  80.  

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #6 on: January 26, 2024, 05:23:41 PM »
Perhaps make a list and add the entity ( block inserts ) as you modify it.

After you've processed the selection set iterate the list and change the layer of each block insert changed.

. . or change the Layer as soon as you identify the block is a candidate for changing.

just a side note.
It would make the code easier to decipher if you refactored it into mltiple functions that each has reduced functionality.
That way it would be easier for determine what is happening, and easier to check. ( and change )
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

AVCAD

  • Newt
  • Posts: 29
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #7 on: January 29, 2024, 08:42:05 AM »
Thanks for the response Kdub,

however I am not a coder, I can modify some code and can sort of read it when its in front of me but I am not good at writing it and everything you just said was like a jet flying at 30,000ft and I am at like 6 ft.

The code supplied was code I found on and modified it to work for what I wanted but like I said the request now is to have the blocks that were modified (only blocks with D_ID and D_model attribute tags) to be reselected or what ever the method is to then be put on a different layer.

What happens right now is that all of the blocks selected will be put on the layer but that's not what I want I just want the changed blocks to go to the new layer.

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #8 on: January 29, 2024, 09:16:36 PM »
Just a suggestion if you use Nentsel you can pick an attribute and return its tag name, like wise you can use the pick point to get the block name also.

Code: [Select]
(cdr (assoc 2 (entget (car (nentsel "\nPick attribute ")))))

A man who never made a mistake never made anything

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #9 on: January 29, 2024, 09:36:10 PM »
I think this is what you want . . could be improved or enhanced.

Don't be scared by the amount of code.
Most of it is from my generic library.

WTF happened to the formatting ??  This fixed the file https://www.browserling.com/tools/tabs-to-spaces

Main Code
Code - Auto/Visual Lisp: [Select]
  1. (defun c:att_prefix  (/ *error* default-tag curlay ss i blockinsert
  2.            attributelist current-value dotlist
  3.           )
  4.    ;; simple error trap
  5.    (defun *error* (msg)
  6.     (if (null (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))    
  7.      (progn (princ (strcat "\nxError: " msg)) (vl-bt))
  8.     )
  9.     (princ)
  10.    )
  11.    ;;------
  12.    (princ
  13.     "If selected blockInsert has D_ID and D_MODEL Attribute tag ' ' "
  14.    )
  15.    (princ "\n . . Prefix D_ID Tag and change Layer to 1_AV-LINE-OFE")
  16.    (setq default-tag "D_ID")
  17.    (setq curlay (getvar "clayer"))
  18.    (command "-layer" "m" "1_AV-LINE-OFE" "c" "251" "" "")
  19.    (setvar "clayer" curlay)
  20.    ;;
  21.    (setq *attprefix* (kdub:getstring "blockinsertter Prefix "
  22.                    *attprefix*
  23.                    1
  24.                    nil
  25.            )
  26.    )
  27.    (if (not  (and *attprefix*
  28.          (setq ss (ssget ":L" '((0 . "INSERT") (66 . 1))))
  29.       )
  30.      )
  31.     quit
  32.    )
  33.    ;;
  34.    (repeat (setq i (sslength ss))
  35.     (setq blockinsert (ssname ss (setq i (1- i))))
  36.     (if
  37.      (and (setq attributelist (kdub:getattributelist blockinsert))
  38.         (setq
  39.          current-value (cdr (assoc default-tag attributelist))
  40.         )
  41.         (assoc "D_MODEL" attributelist)
  42.      ) (progn
  43.         (setq  dotlist
  44.             (list  (cons default-tag
  45.                   (strcat *attprefix* current-value)
  46.               )
  47.             )
  48.         )
  49.         (kdub:set-prefixed-attributes  blockinsert
  50.                       default-tag
  51.                       dotlist
  52.         )
  53.         (command "chprop" blockinsert "" "la" "1_AV-LINE-OFE" "")
  54.        )
  55.     )
  56.    )
  57.    (princ)
  58. )
  59.    "\nAttribute Prefix tool has beblockinsert loaded: command = ATT_ATTFIX "
  60. )
  61.  

Library Code
Either pre-load this or add it to the main file.
This code is 20+ years matured, so best not change it :)
Code - Auto/Visual Lisp: [Select]
  1. ;;========================================================================
  2. ;| #lib.
  3. kdub:getString (<Promptmsg><Default><InitBit><AllowSpaces>)
  4. kwb 20021103
  5. Revised Library : kwb 20051031
  6. 20051101 kwb : ESC test added.
  7. Build 2.0 :
  8.  
  9. (SETQ tmpVal (kdub:getString "Lot Description" nil (+ 1 ) T))
  10. (SETQ tmpVal (kdub:getString "Name" "Me" (+ 1 ) Nil))
  11. (SETQ tmpVal (kdub:getString nil nil nil Nil))
  12. |;
  13.  
  14. (defun kdub:getstring (promptmsg              ; The prompt string.
  15.              default                ; Value to return if response is <blockinsertter>
  16.              initbit                ; Initget bit
  17.              allowspaces              ; spaces Flag < T or nil )
  18.                               ;
  19.              / returnvalue
  20.             )
  21.    ;;------------------------------
  22.    (or initbit (setq initbit 0))
  23.    ;;------------------------------
  24.    (setq promptmsg (strcat "\n"
  25.                (cond (promptmsg)
  26.                  ("Specify String Value")
  27.                )
  28.            )
  29.    )
  30.    ;;------------------------------
  31.    (if (and default (= (type default) 'str) (/= default ""))
  32.     (progn (setq
  33.         promptmsg (strcat "\n" promptmsg " << " default " >>: ")
  34.        )
  35.           (setq returnvalue (vl-catch-all-apply
  36.                      'getstring
  37.                      (list allowspaces promptmsg)
  38.                     )
  39.           )
  40.          )
  41.         ;; ESC was pressed.
  42.         (setq returnvalue nil
  43.             default nil
  44.         )
  45.        )
  46.        (setq returnvalue (if (= returnvalue "")
  47.                   default
  48.                   returnvalue
  49.                  )
  50.        )
  51.     )
  52.     ;; Else no default, so don't accept blockinsertTER or SPACEBAR
  53.     ;;
  54.     (progn
  55.      (setq promptmsg (strcat "\n" promptmsg ": "))
  56.      (if (= initbit 1)
  57.       (while (= ""
  58.             (setq  returnvalue  (vl-catch-all-apply
  59.                        'getstring
  60.                        (list allowspaces promptmsg)
  61.                     )
  62.             )
  63.            )
  64.       )
  65.       ;;
  66.       (setq returnvalue (vl-catch-all-apply
  67.                  'getstring
  68.                  (list allowspaces promptmsg)
  69.                 )
  70.       )
  71.      )
  72.      (if (vl-catch-all-error-p returnvalue)
  73.       ;; ESC was pressed.
  74.       (setq returnvalue nil)
  75.      )
  76.     )
  77.    )
  78.    ;;------------------------------
  79.    returnvalue
  80. )
  81. ;;========================================================================
  82. ;;;------------------------------------------------------------------
  83. ;;;------------------------------------------------------------------
  84. ;;;
  85.  
  86. (defun getattributes (objselection)
  87.    (if (= (type objselection) 'ename)
  88.     (setq objselection (vlax-ename->vla-object objselection))
  89.    )
  90.    (if (= (vla-get-hasattributes objselection) :vlax-true)
  91.     (mapcar '(lambda (x)
  92.           (cons (vla-get-tagstring x) (vla-get-textstring x))
  93.          )
  94.         (vlax-safearray->list
  95.          (variant-value (vla-getattributes objselection))
  96.         )
  97.     )
  98.    )
  99. )
  100.  
  101. ;;;------------------------------------------------------------------
  102. ;;;------------------------------------------------------------------
  103. ;;;
  104. ;| KDUB:GetAttributeList
  105.     < BlockRef > [VLA-OBJECT]
  106.    
  107.    Description: Read all attribute values from a block.
  108.    
  109.    Return [Typ]:
  110.      ==> [Dot-list ] '(("tagstring1" . "textstring1")...) [LIST]
  111.      or ==>nil
  112.    Remarks:
  113.      N/a
  114.   Dependencies:
  115.      N/a
  116.    revised kdub 2003 for ac2004|;
  117. |                              ;
  118.  
  119.  
  120. (defun kdub:getattributelist (blockref / catchit returnval)
  121.    (if (= (type blockref) 'ename)
  122.     (setq blockref (vlax-ename->vla-object blockref))
  123.    )
  124.    (if (vl-catch-all-error-p (setq catchit
  125.                    (vl-catch-all-apply 'vlax-invoke
  126.                              (list blockref 'getattributes)
  127.                    )
  128.                )
  129.      )
  130.     ;; else
  131.     (setq  returnval (mapcar '(lambda (attref)
  132.                   (cons  (vla-get-tagstring attref)
  133.                     (vla-get-textstring attref)
  134.                   )
  135.                  )
  136.                 catchit
  137.             )
  138.     )
  139.    )
  140.    returnval
  141. )
  142. ;;;------------------------------------------------------------------
  143. ;;;------------------------------------------------------------------
  144. ;;;
  145. ;| KDUB:KDUB:SetAttributes
  146.     < BlockRef > [VLA-OBJECT]
  147.      < DotList  > [Dot-list  ] '(("tagstring1" . "textstring1")...)
  148.      
  149.    Description: Set attribute values to a block.
  150.    
  151.    Return [Typ]:    
  152.      ==>nil
  153.      
  154.    Remarks:
  155.      N/a
  156.   Dependencies:
  157.      N/a  
  158.   revised kdub 2003 for ac2004|;
  159. |                              ;
  160. ;;;------------------------------------------------------------------
  161. ;;;------------------------------------------------------------------
  162. ;;;
  163. (defun kdub:setattributes (blockref dotlist / catchit attval)
  164.    (if (= (type blockref) 'ename)
  165.     (setq blockref (vlax-ename->vla-object blockref))
  166.    )
  167.    (if (vl-catch-all-error-p (setq catchit
  168.                    (vl-catch-all-apply 'vlax-invoke
  169.                              (list blockref 'getattributes)
  170.                    )
  171.                )
  172.      )
  173.     ;; else
  174.     (progn
  175.      (mapcar '(lambda (attref)
  176.            (if (setq
  177.               attval (cdr  (assoc (vla-get-tagstring attref)
  178.                          dotlist
  179.                     )
  180.                    )
  181.              )
  182.             (vla-put-textstring attref attval)
  183.            )
  184.           )
  185.          catchit
  186.      )
  187.      (vla-update blockref)
  188.     )
  189.    )
  190.    (princ)
  191. )
  192. ;;;------------------------------------------------------------------
  193. ;;;------------------------------------------------------------------
  194.  

Regards,
« Last Edit: January 30, 2024, 12:14:53 AM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

AVCAD

  • Newt
  • Posts: 29
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #10 on: January 30, 2024, 07:22:16 PM »
30,000ft...you just went to 100,000....lol Thanks for this. I am just seeing it now so tomorrow I will have to test it out.

Will let you know.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #11 on: January 30, 2024, 07:45:46 PM »
Consider the Library functions as black-box functions, you don't 'need' to know how thay work . . . just what to feed it and what irt returns.

I find that the main code can be kept a lot quieter coding this way.
Most times I extract code from the main into local well-named functions.
You'd be surprised how much simpler the code looks a couple months later.

It also makes the main a lot easier to think about.

Regards,
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

AVCAD

  • Newt
  • Posts: 29
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #12 on: February 22, 2024, 12:29:19 PM »
So finally found some time to test this out and I am getting this error after it allows me to select objects

Code - Auto/Visual Lisp: [Select]
  1. xError: no function definition: KDUB:SET-PREFIXED-ATTRIBUTES
  2. Backtrace:
  3. [0.55] (VL-BT)
  4. [1.51] (*ERROR* "no function definition: KDUB:SET-PREFIXED-ATTRIBUTES")
  5. [2.46] (_call-err-hook #<SUBR @000001b88436ac50 *ERROR*> "no function definition: KDUB:SET-PREFIXED-ATTRIBUTES")
  6. [3.40] (sys-error "no function definition: KDUB:SET-PREFIXED-ATTRIBUTES")
  7. :ERROR-BREAK.35 nil
  8. [4.32] (#<SUBR @000001b8820d8b88 null-fun-hk> <Entity name: 1b8fd6af980> "D_ID" (("D_ID" . "OFE4K60 HDMI RX")))
  9. [5.26] (KDUB:SET-PREFIXED-ATTRIBUTES <Entity name: 1b8fd6af980> "D_ID" (("D_ID" . "OFE4K60 HDMI RX")))
  10. [6.19] (C:ATT_PREFIX)
  11. [7.15] (#<SUBR @000001b88436f250 -rts_top->)
  12. [8.12] (#<SUBR @000001b8820d8700 veval-str-body> "(C:ATT_PREFIX)" T #<FILE internal>)
  13. :CALLBACK-ENTRY.6 (:CALLBACK-ENTRY)
  14. :ARQ-SUBR-CALLBACK.3 (nil 0)
  15.  

any idea?

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: Help: Adding Prefix to Selected Attribute Text
« Reply #13 on: February 22, 2024, 03:29:19 PM »
Consider the Library functions as black-box functions, you don't 'need' to know how thay work . . . just what to feed it and what irt returns.

I find that the main code can be kept a lot quieter coding this way.
Most times I extract code from the main into local well-named functions.
You'd be surprised how much simpler the code looks a couple months later.

It also makes the main a lot easier to think about.

Regards,

Kdubb - you're missing a function you call in the main routine here:
Code - Auto/Visual Lisp: [Select]
  1. (kdub:set-prefixed-attributes  blockinsert
  2.              default-tag
  3.              dotlist
  4. )
  5.  
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt