Author Topic: Edit constant attributes  (Read 1876 times)

0 Members and 1 Guest are viewing this topic.

Cawaugh

  • Guest
Edit constant attributes
« on: January 18, 2013, 11:13:08 AM »
I am trying to convert this piece of code to handle constant attributes. So far I have had no luck.
The process should be the same except getting and modifying a constant attrib instead of a non-constant.
Any help will be greatly appreciated. If you want to get in contact with me offline, my email is charles.waugh@wmeng.com.
Thanks for any help you may be able to give to me on this.  :-)

Code - Auto/Visual Lisp: [Select]
  1. ;; -----------------------------------------------------------------------------
  2. ;; Function: ATTRIB_REPLACE_INCREMENTAL
  3. ;; Purpose : Modifies attribute values according to params:
  4. ;; Params  : BNAME:  Block name(s), can be wildcard
  5. ;;           TAG:    Attribute tag, should be wildcard
  6. ;;           ACTION: String describing what to do (Add or Delete)
  7. ;;           VALUE:  List of Values to insert/delete/whatever
  8. ;; Local   : ss, ssl: Selection set, length
  9. ;;           #:       Loop counter
  10. ;;           en, eg:  Entity name, (entget) list
  11. ;;           modlist: List of (entget) lists for attributes which match TAG
  12. ;;           vallist: List of values to place in attribute slots
  13. ;; Uses    : (naus_aso) (naus_strplace)
  14. ;; Returns : Number of blocks processed (but not necessarily modified)
  15. ;; -----------------------------------------------------------------------------
  16. ;; codelist looks like ("US001" "US253")
  17.  
  18. (attrib_replace_incremental "3M-BORDER-E" "3MA#" "Add" codelist)
  19.  
  20. (defun attrib_replace_incremental (BNAME TAG ACTION VALUE
  21.                                         / ss ssl # en eg modlist vallist)
  22.   (setq
  23.     ss (ssget "X" (list '(0 . "INSERT") (cons 2 BNAME)))
  24.     ssl (if ss (sslength ss) 0.0)
  25.     # 0.0
  26.   )
  27.   (while (< # ssl)
  28.     (setq
  29.       en (ssname ss #)
  30.       modlist nil
  31.       vallist (if (= ACTION "Add") VALUE nil)
  32.     )
  33.     (while (setq en (entnext en))
  34.       (setq eg (entget en))
  35.       (if (and (= "ATTRIB" (naus_aso 0 eg)) (wcmatch (naus_aso 2 eg) TAG))
  36.         (progn
  37.           (setq modlist (cons eg modlist)) ; Add to list of potential code slots
  38.           (if (/= (naus_aso 1 eg) "")
  39.             (setq vallist (cons (naus_aso 1 eg) vallist)) ; Add to list of codes
  40.           )
  41.         )
  42.       )
  43.     )
  44.  
  45.     (setq cnt 1)
  46.     (if (= ACTION "Delete")
  47.         (foreach item value
  48.          (progn
  49.           (setq vallist (naus_list_delall item vallist))
  50.           (setq cnt (+ cnt 1))
  51.          );end progn
  52.         );foreach
  53.     );end if (= ACTION "Delete")
  54.  
  55.     (if modlist
  56.       (progn
  57.         (setq
  58.           modlist (reverse modlist)
  59.           vallist (naus_list_unique vallist) ; Remove duplicates
  60.           vallist ; Place in ascending order
  61.            (naus_sort
  62.              vallist
  63.              '(lambda (X Y)
  64.                 (if (= (atof X) (atof Y)) (< X Y) (< (atof X) (atof Y)))
  65.               )
  66.            )
  67.         )
  68.             ;(strcat "\nWARNING: no room for code " (car vallist) ", discarded.")
  69.         (if (> (length vallist) (length modlist)) ; If more values than slots
  70.              (progn
  71.                 (princ (strcat "\nWARNING: Room for only 6 Access code!"))
  72.                 (princ (strcat "\nDelete old or no longer used codes, then add in new codes!"))
  73.         (exit)
  74.                 ;(setq vallist (cdr vallist)) ; Reduce list of values
  75.              );end progn
  76.         );end if
  77.         (foreach eg modlist ; For each code slotmc
  78.  
  79.           (if (setq val (car vallist)) ; If there are values left,
  80.             (entmod (subst (cons 1 val) (assoc 1 eg) eg)) ; insert value, else
  81.             (entmod (subst (cons 1 "") (assoc 1 eg) eg))  ; remove existing value
  82.           )
  83.  
  84.           (setq vallist (cdr vallist)) ; Reduce list of values
  85.         )
  86.  
  87.         (entupd (ssname ss #))
  88.  
  89.       )
  90.     )
  91.     (setq # (1+ #))
  92.   )
  93.   ssl ; Return number of blocks processed
  94. )
« Last Edit: January 18, 2013, 06:03:07 PM by CAB »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Edit constant attributes
« Reply #1 on: January 18, 2013, 06:21:29 PM »
I assume there is no error just no change?
Nothing jumps out.
Could you post a DWG of a test Block / Insert?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Edit constant attributes
« Reply #2 on: January 18, 2013, 06:30:22 PM »
I am trying to convert this piece of code to handle constant attributes. So far I have had no luck.
The process should be the same except getting and modifying a constant attrib instead of a non-constant.

You cannot modify the value of a constant attribute reference [ATTRIB] (hence the name); you will need to modify the value held by the attribute definition [ATTDEF] of the constant attribute by iterating over the entities contained within the block definition.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Edit constant attributes
« Reply #3 on: January 18, 2013, 06:49:40 PM »
I am trying to convert this piece of code to handle constant attributes. So far I have had no luck.
The process should be the same except getting and modifying a constant attrib instead of a non-constant.

You cannot modify the value of a constant attribute reference [ATTRIB] (hence the name); you will need to modify the value held by the attribute definition [ATTDEF] of the constant attribute by iterating over the entities contained within the block definition.

A simple example to clarify my point:

Code - Auto/Visual Lisp: [Select]
  1. (defun _changeconstantattrib ( blk tag val / ent enx rtn )
  2.     (if (setq ent (tblobjname "BLOCK" blk)) ;; Does the block exist?
  3.         (while (setq ent (entnext ent))
  4.             (if
  5.                 (and
  6.                     (= "ATTDEF" (cdr (assoc 0 (setq enx (entget ent))))) ;; is it an ATTDEF?
  7.                     (= 2 (logand 2 (cdr (assoc 70 enx)))) ;; is the ATTDEF constant?
  8.                     (= (strcase tag) (strcase (cdr (assoc 2 enx)))) ;; is it our tag?
  9.                 )
  10.                 (setq rtn (entmod (subst (cons 1 val) (assoc 1 enx) enx)))
  11.             )
  12.         )
  13.     )
  14.     (and rtn)
  15. )

Code - Auto/Visual Lisp: [Select]
  1. (_changeconstantattrib <block-name> <attribute-tag> <new-value>)

Since the block definition is being modified, a REGEN is required to reflect changes to all block references.
« Last Edit: January 18, 2013, 06:53:34 PM by Lee Mac »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Edit constant attributes
« Reply #4 on: January 20, 2013, 11:54:10 PM »
Yep, a constant attrib is noting different from a normal piece of text inside the block. I don't think the OP's idea would ever work with constant attribs. Incrementing one would update all to the same number. Why do you need to have this attribute set as constant?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Cawaugh

  • Guest
Re: Edit constant attributes
« Reply #5 on: January 21, 2013, 04:43:26 PM »
Lee, it works. I did modified a little for my program but it came out great.
Just missing that line of thought for constants. Thanks again!!

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Edit constant attributes
« Reply #6 on: January 21, 2013, 05:52:51 PM »
You're welcome!  :-)