Author Topic: How to merge 2 attribute-values to 1 combined value?  (Read 500 times)

0 Members and 1 Guest are viewing this topic.

bilanšikur

  • Newt
  • Posts: 82
How to merge 2 attribute-values to 1 combined value?
« on: February 12, 2024, 08:06:11 AM »

Hello all,

I have this idea to automate something that is now done manually. Allthough I searched I have not found what I am looking for except https://www.cadtutor.net/forum/topic/62546-multiple-default-attribute-values-transfered-into-1-target/#comment-516219, what got me thinking.

Here is my problem: I have 500+ blocks in a drawing, these blocks have like 15 names and 5 attribute values. I want to read out certain attributes from specified blocks and then put it back combined into another attribuet "TAG-COMBINED". Maybe an example is better:

Block names to be used:
'("LP015" "LP016" "LP017" "LP018" "LP019" "LP20")

Attributes to be used:
'("TAG1"  "TAG2")

Destination TAG:  "TAG-COMBINED"

Block  "LP015" is in the drawing (say 24 times), the first insertion has attribute  "TAG1" containing the value "A" and the atribute  "TAG2" containing "98". The following blocks of the same name have different values. The attribute value of "TAG-COMBINED" is empty or maybe there is already someting in it.

After the command the attribute value of  "TAG-COMBINED" is "A98" for the first block "LP015" in the drawing.

Sorry for being unclear but I find difficulties explaining this better.

Thanks for any help.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #1 on: February 12, 2024, 11:43:01 AM »
Quick & dirty -
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / a i l s v w x )
  2.     (if (setq s (ssget "_:L" '((0 . "INSERT")(66 . 1))))
  3.         (repeat (setq i (sslength s))
  4.             (setq i (1- i)
  5.                   a (entnext (ssname s i))
  6.                   x (entget a)
  7.                   l nil
  8.             )
  9.             (while (= "ATTRIB" (cdr (assoc 0 x)))
  10.                 (setq l (cons (cons (strcase (cdr (assoc 2 x))) x) l)
  11.                       a (entnext a)
  12.                       x (entget  a)
  13.                 )
  14.             )
  15.             (if (and (setq v (cdr (assoc 1 (cdr (assoc "TAG1" l)))))
  16.                      (setq w (cdr (assoc 1 (cdr (assoc "TAG2" l)))))
  17.                      (setq x (cdr (assoc "TAG-COMBINED" l)))
  18.                 )
  19.                 (if (entmod (subst (cons 1 (strcat v w)) (assoc 1 x) x))
  20.                     (entupd (cdr (assoc -1 x)))
  21.                 )
  22.             )
  23.         )
  24.     )
  25.     (princ)
  26. )

bilanšikur

  • Newt
  • Posts: 82
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #2 on: February 13, 2024, 05:25:44 AM »
Thank you Lee for this 'quick & dirty' code. It works exactly as I hoped for. The code is far from dirty, it is clean due to te recursive patterns which I defenitely need to study. Very good, thank you again.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #3 on: February 13, 2024, 10:48:32 AM »
Just for curiosity :
What if "TAG-COMBINED" attribute don't exist and you'll have to create it on the fly... How would you do it?
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

kozmos

  • Newt
  • Posts: 114
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #4 on: February 14, 2024, 08:25:20 PM »
can create combined attdef in blockeditor and directly use tag1 and tag2 as PlaceHolder for the content. then after attsync, the new combined attribute will have content of tag1 and tag2, no coding is needed.
KozMos Inc.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #5 on: February 16, 2024, 08:58:51 AM »
can create combined attdef in blockeditor and directly use tag1 and tag2 as PlaceHolder for the content. then after attsync, the new combined attribute will have content of tag1 and tag2, no coding is needed.

That is slow process, so the code is needed... Further more, process should be preformed only on those blocks that are selected/preselected - not all with the same name - attsync isn't needed and adding attribute that don't exist should be perfomed only in space of reference of block - not definition... So it is more likely that that isn't possible, but that is the challenge to make it possible...

[EDIT]
If it isn't possible, like I tried with attsync, consider putting wipeout on those you wish to be invisible on top over block - not inside and over tag-combined...[/EDIT]
« Last Edit: February 16, 2024, 09:26:48 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #6 on: February 16, 2024, 10:25:20 AM »
Here is what I thought and without wipeouts, based on Lee's example :

Code - Auto/Visual Lisp: [Select]
  1. ;;; by Lee Mac
  2. ;;; mod. M.R.
  3. (defun c:tag-combined ( / a e i k l s v w x )
  4.     (if (setq s (ssget "_:L" '((0 . "INSERT")(66 . 1)))
  5.              e (ssget "_A" '((0 . "INSERT")(66 . 1)))
  6.         )
  7.         (progn
  8.             (setq k 0)
  9.             (repeat 2
  10.                 (setq k (1+ k))
  11.                 (repeat (setq i (sslength (if (= k 1) s e)))
  12.                     (setq i (1- i)
  13.                           a (entnext (ssname (if (= k 1) s e) i))
  14.                           x (entget a)
  15.                           l nil
  16.                     )
  17.                     (if (= k 1)
  18.                         (ssdel (ssname s i) e)
  19.                     )
  20.                     (while (= "ATTRIB" (cdr (assoc 0 x)))
  21.                         (setq l (cons (cons (strcase (cdr (assoc 2 x))) x) l)
  22.                               a (entnext a)
  23.                               x (entget  a)
  24.                         )
  25.                     )
  26.                     (if (and (setq v (cdr (assoc 1 (cdr (assoc "TAG1" l)))))
  27.                              (setq w (cdr (assoc 1 (cdr (assoc "TAG2" l)))))
  28.                              (setq x (cdr (assoc "TAG-COMBINED" l)))
  29.                         )
  30.                         (if (entmod (subst (cons 1 (if (= k 1) (strcat v w) "")) (assoc 1 x) x))
  31.                             (entupd (cdr (assoc -1 x)))
  32.                         )
  33.                     )
  34.                 )
  35.             )
  36.         )
  37.     )
  38.     (princ)
  39. )
  40.  

Code - Auto/Visual Lisp: [Select]
  1. ;;; by Lee Mac
  2. ;;; mod. M.R.
  3. (defun c:tag-combined-invisible ( / a i l s v w x )
  4.     (if (setq s (ssget "_:L" '((0 . "INSERT")(66 . 1))))
  5.         (repeat (setq i (sslength s))
  6.             (setq i (1- i)
  7.                   a (entnext (ssname s i))
  8.                   x (entget a)
  9.                   l nil
  10.             )
  11.             (while (= "ATTRIB" (cdr (assoc 0 x)))
  12.                 (setq l (cons (cons (strcase (cdr (assoc 2 x))) x) l)
  13.                       a (entnext a)
  14.                       x (entget  a)
  15.                 )
  16.             )
  17.             (if (and (setq v (cdr (assoc 1 (cdr (assoc "TAG1" l)))))
  18.                      (setq w (cdr (assoc 1 (cdr (assoc "TAG2" l)))))
  19.                      (setq x (cdr (assoc "TAG-COMBINED" l)))
  20.                 )
  21.                 (if (entmod (subst (cons 1 "") (assoc 1 x) x))
  22.                     (entupd (cdr (assoc -1 x)))
  23.                 )
  24.             )
  25.         )
  26.     )
  27.     (princ)
  28. )
  29.  
« Last Edit: February 16, 2024, 01:09:13 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #7 on: February 16, 2024, 12:19:02 PM »
I've tested again Lee's version... It turns out that that's all that is needed... That firstly posted code and my last one, for returning back if user made mistake...

Sorry for my late intervention, but now I am satisfied as it's all clear in my head...  :idiot2:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BIGAL

  • Swamp Rat
  • Posts: 1408
  • 40 + years of using Autocad
Re: How to merge 2 attribute-values to 1 combined value?
« Reply #8 on: February 17, 2024, 07:00:39 PM »
Just a comment if the attributes creation order is att1 att2 att3 then do not need attribute names as a VL get attributes will return the 3 attributes in that order, (nth 0 atts) (nth 1 atts)(nth 2 atts) even if there is say 5 attributes it does not matter. You can say do add 3 + 5 update 2 in terms of attribute order.

Another example that was done for a post.
Code: [Select]
; add 2 attributes and put the value as a field into the 3rd attribute.
; BY AlanH

(defun c:test ( / obj lst x )
(setq oldatt (getvar 'attdia))
(setvar 'attdia 0)
(command "-insert" "c" (getpoint "\npick point") 1 1 0 (getstring "\nEnter Att1 ") (getstring "\nEnter Att2 ")  "-")
(setq obj (vlax-ename->vla-object (entlast)))
(setq lst '())
(foreach att (vlax-invoke obj 'getattributes)
(princ  "\n")
(setq lst (cons  (strcat "%<\\AcObjProp Object(%<\\_ObjId "
(vlax-invoke-method (vla-get-Utility  (vla-get-activedocument (vlax-get-acad-object))) 'GetObjectIdString att :vlax-false)
">%).Textstring>%"
 ) lst ))
)
(setq str nil)
(setq x (length lst))
(setq str (strcat "%<\\AcExpr "
(nth (setq  x (- x 1)) lst) " + "
(nth (setq  x (- x 1)) lst) " + "
(nth (setq  x (- x 1)) lst) ">%"
)
)
(setq x 1 y 4)
(foreach att(vlax-invoke obj 'getattributes)
(if (= x y)
(Vla-put-textstring att str)
)
(setq x (+ x 1))
)
(setvar 'attdia oldatt)
(princ)
)
(c:test)
« Last Edit: February 17, 2024, 07:04:56 PM by BIGAL »
A man who never made a mistake never made anything