Author Topic: LISP to edit NESTED attribute BLOCK‏  (Read 7234 times)

0 Members and 1 Guest are viewing this topic.

dannypd

  • Guest
LISP to edit NESTED attribute BLOCK‏
« on: November 27, 2011, 04:51:29 AM »
hi
I have many drawing with one nested block attributes. Is it possible to change the Nested attribute block values without exploding it? attached
Your help would be infinitely appreciated.
Thanks in advance

Regards,

Danny

below routine runs fine with attribute fine. but how do we modify an nested attribute?

(defun c:CREV (/ change-one-att sel idx n entname )
(defun change-one-att (tag val / blk elist next next_data);
(setq blk entname)
(setq elist (entget blk))
(setq next blk)
(while (setq next (entnext next))
(setq next_data (entget next))
(if (= tag (cdr (assoc 2 next_data)))
(progn
(entmod (subst (cons 1 val) (assoc 1 next_data) next_data))
(entupd blk)
)
)
)
(princ)
)

(setq sel (ssget '((0 . "INSERT"))))
;(command "explode" sel)
(if sel
(progn
(setq idx 0)
(setq n (sslength sel))
(repeat n
(setq entname (ssname sel idx))
(change-one-att "REV1" "A")
(change-one-att "REVDATE1" "01/12/2011")
(change-one-att "REVDESC1" "FOR  REVIEW")
(change-one-att "APP1" "MAL")
(change-one-att "DATE" "01/12/2011")
(setq idx (1+ idx))
);repeat
);if
);progn
(princ)
);;eof


Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: LISP to edit NESTED attribute BLOCK‏
« Reply #1 on: November 27, 2011, 06:34:46 AM »
The drawing that you have uploaded is not attributed block , so do not test the code on because it would not work , try to make another block that contains the same tag strings that you have included within your post to change .

Code: [Select]
(defun c:TesT (/ blks i sn n e)
;;; Tharwat 27. Nov. 2011 ;;;
  (if (setq blks (ssget '((0 . "INSERT") (66 . 1))))
    (repeat (setq i (sslength blks))
      (setq sn (ssname blks (setq i (1- i))))
      (setq n (entnext sn))
      (while
(not
  (eq (cdr (assoc 0 (setq e (entget n))))
      "SEQEND"
  )
)
(if (eq (cdr (assoc 0 e)) "ATTRIB")
   (cond ((eq (cdr (assoc 2 e)) "REV1")
  (entmod (subst (cons 1 "A") (assoc 1 e) e))
)
((eq (cdr (assoc 2 e)) "REVDATE1")
  (entmod (subst (cons 1 "01/12/2011") (assoc 1 e) e))
)
((eq (cdr (assoc 2 e)) "REVDESC1")
  (entmod (subst (cons 1 "FOR  REVIEW") (assoc 1 e) e))
)
((eq (cdr (assoc 2 e)) "APP1")
  (entmod (subst (cons 1 "MAL") (assoc 1 e) e))
)
((eq (cdr (assoc 2 e)) "DATE")
  (entmod (subst (cons 1 "01/12/2011") (assoc 1 e) e))
)
   )
)
(setq n (entnext n))
      )
    )
  )
  (princ)
)


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: LISP to edit NESTED attribute BLOCK‏
« Reply #2 on: November 27, 2011, 08:41:52 AM »
The drawing that you have uploaded is not attributed block

You missed the key word: 'NESTED'.

dannypd

  • Guest
Re: LISP to edit NESTED attribute BLOCK‏
« Reply #3 on: November 27, 2011, 10:22:19 AM »
Thanks for the reply. Is it possible to change the attributes value without exploding or burst the nested block.
Regards,
Danny

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: LISP to edit NESTED attribute BLOCK‏
« Reply #4 on: November 27, 2011, 10:52:54 AM »
Thanks for the reply. Is it possible to change the attributes value without exploding or burst the nested block.

Try something like this:

Code: [Select]
(defun c:nAttEdit ( / _UpdateNestedAttributes data ent )

    (setq data
       '(
            ("REV1"     . "A")
            ("REVDATE1" . "01/12/2011")
            ("REVDESC1" . "FOR  REVIEW")
            ("APP1"     . "MAL")
            ("DATE"     . "01/12/2011")
        )
    )

    (defun _UpdateNestedAttributes ( parent data / obj )
        (if (setq obj (tblobjname "BLOCK" parent))
            (while (setq obj (entnext obj))
                (if
                    (and
                        (eq "INSERT" (cdr (assoc 0 (entget obj))))
                        (= 1 (cdr (assoc 66 (entget obj))))
                    )
                    (LM:SetAttributeValues obj data)
                )
            )
        )
    )

    (while
        (progn (setvar 'ERRNO 0) (setq ent (car (entsel "\nSelect Block: ")))
            (cond
                (   (= 7 (getvar 'ERRNO))
                    (princ "\nMissed, try again.")
                )
                (   (eq 'ENAME (type ent))
                    (if (eq "INSERT" (cdr (assoc 0 (entget ent))))
                        (_UpdateNestedAttributes (cdr (assoc 2 (entget ent))) data)
                        (princ "\nSelected Object is not a Block.")
                    )
                )
            )
        )
    )
   
    (command "_.regen")
    (princ)
)


;; Set Attribute Values  -  Lee Mac
;; Sets the block attributes whose tags are found in the supplied
;; association list to their associated values.

(defun LM:SetAttributeValues ( block lst / elist item )
    (if
        (eq "ATTRIB"
            (cdr
                (assoc 0
                    (setq elist
                        (entget (setq block (entnext block)))
                    )
                )
            )
        )
        (if (setq item (assoc (strcase (cdr (assoc 2 elist))) lst))
            (progn
                (if (setq elist (entmod (subst (cons 1 (cdr item)) (assoc 1 elist) elist)))
                    (entupd (cdr (assoc -1 elist)))
                )
                (LM:SetAttributeValues block lst)
            )
            (LM:SetAttributeValues block lst)
        )
    )
)

Change the list at the top of the code to suit your tags / values.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: LISP to edit NESTED attribute BLOCK‏
« Reply #5 on: November 27, 2011, 12:47:52 PM »
You missed the key word: 'NESTED'.

You're right (my bad habit of fast reading)  :ugly:

This seems to work for me ..

Code: [Select]
(defun c:Test (/ lst ss i sset nme Blockdefinition atts)
  (vl-load-com)
;;; Tharwat 27. Nov. 2011 ;;;
  (cond ((not acdoc)
         (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
        )
  )
  (setq lst '())
  (if (setq ss (ssget "_:L" '((0 . "INSERT"))))
    (repeat
      (setq i (sslength ss))
       (setq sset (ssname ss (setq i (1- i))))
       (setq nme (cdr (assoc 2 (entget sset))))
       (if (not (member nme lst))
         (progn
           (setq lst (cons nme lst))
           (setq Blockdefinition (vla-item (vla-get-blocks acdoc) nme))
           (vlax-for block Blockdefinition
             (if (and
                   (eq :vlax-false (vla-get-isxref Blockdefinition))
                   (eq :vlax-false (vla-get-islayout Blockdefinition))
                 )
               (vlax-for nested Blockdefinition
                 (if
                   (and
                     (eq (vla-get-objectname nested)
                         "AcDbBlockReference"
                     )
                     (vla-get-hasattributes nested)
                   )
                    (foreach atts (vlax-invoke nested "getattributes")
                      (cond (
                             (eq (vla-get-tagstring atts) "REV1")
                             (vla-put-textstring atts "A")
                            )
                            (
                             (eq (vla-get-tagstring atts) "REVDATE1")
                             (vla-put-textstring atts "01/12/2011")
                            )
                            (
                             (eq (vla-get-tagstring atts) "REVDESC1")
                             (vla-put-textstring atts "FOR  REVIEW")
                            )
                            (
                             (eq (vla-get-tagstring atts) "APP1")
                             (vla-put-textstring atts "MAL")
                            )
                            (
                             (eq (vla-get-tagstring atts) "DATE")
                             (vla-put-textstring atts "01/12/2011")
                            )
                      )
                    )
                 )
               )
             )
           )
         )
       )
    )
    (princ)
  )
  (vla-regen acdoc acActiveViewport)
  (princ)
)


dannypd

  • Guest
Re: LISP to edit NESTED attribute BLOCK‏
« Reply #6 on: November 27, 2011, 09:48:40 PM »
Thanks a lot for taking your valuable time to do this routine.. both rouines are working fine...
Regards
Danny

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: LISP to edit NESTED attribute BLOCK‏
« Reply #7 on: November 27, 2011, 11:19:35 PM »
Really glad to hear that .  ;-)