need help to how to set attribute value of specified bloc in specified layout?

need help to how to set attribute value of specified bloc in specified layout?

mduvalAA6EJ
Enthusiast Enthusiast
456 Views
6 Replies
Message 1 of 7

need help to how to set attribute value of specified bloc in specified layout?

mduvalAA6EJ
Enthusiast
Enthusiast

how to set attribute value of specified bloc in specified layout

 

block name= SSREVISION

attribute tag name of block1= 1_NO

new attribute value= 1

layout=name is layout 1 but is position 1 in layout list in autocad

 

note the block are joint in this message

 

i try but autocad write this message Command: REVUP
; error: bad argument type: VLA-OBJECT nil

 

i dont understand all i try and i search... can someone could help?

 

i tried this:

;; Set Attribute Value - Lee Mac
;; Sets the value of the first attribute with the given tag found within the block, if present.
;; blk - [vla] VLA Block Reference Object
;; tag - [str] Attribute TagString
;; val - [str] Attribute Value
;; Returns: [str] Attribute value if successful, else nil.

(defun LM:vl-setattributevalue ( blk tag val )
(setq tag (strcase tag))
(vl-some
'(lambda ( att )
(if (= tag (strcase (vla-get-tagstring att)))
(progn (vla-put-textstring att val) val)
)
)
(vlax-invoke blk 'getattributes)
)
)

(defun setAttrib ( blkname att val / ss vla-obj)
(setq ss (ssget "X" (list (cons 2 blkname)(cons 0 "INSERT")(cons 410 (getvar 'ctab)))))
(LM:vl-setattributevalue vla-obj att val)
)

(defun c:REVUP ()
(setAttrib "SSREVISION" "1_NO" "1")
)

 

thank you very much

0 Likes
457 Views
6 Replies
Replies (6)
Message 2 of 7

Sea-Haven
Mentor
Mentor

If you look at this line

(setq ss (ssget "X" (list (cons 2 blkname)(cons 0 "INSERT")(cons 410 (getvar 'ctab))))) the (getvar 'ctab) is doing just that getting a selection set from the "Current layout" this includes "Model". So you need to set which layout you want to search, simplest (setvar 'ctab "yourlayoutname") before the ss. If the block is in multiple layouts can loop through all layouts running the code.

Message 3 of 7

CADaSchtroumpf
Advisor
Advisor

This isn't (getvar 'ctab) but (getvar "ctab")

If you want change in all layouts remove simply (cons 410 (getvar "ctab"))without, (ssget "_x") has selected all objects in drawing and in all layouts, make a loop foreach entitie

 

(defun setAttrib ( blkname att val / ss vla-obj)
(setq ss (ssget "_X" (list (cons 0 "INSERT")(cons 66 1)(cons 2 blkname))))
(repeat (setq n (sslength ss))
(setq vla-obj (vlax-ename->vla-object (ssname ss (setq n (1- n)))))
(LM:vl-setattributevalue vla-obj att val)
)
)

 

Message 4 of 7

mduvalAA6EJ
Enthusiast
Enthusiast

how to do this with :layorder function if i want to modify the block in layout 1 example?

thanks 🙂 

 

; pbejse https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/layout-order/td-p/3749964
(defun :Layorder (/ order)
(vlax-for lay (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
(setq order (cons (list (vla-get-name lay) (vla-get-taborder lay)) order)))
(mapcar 'car (cdr (vl-sort order '(lambda (j k) (< (cadr j) (cadr k)))))))

 

;; Set Attribute Value - Lee Mac
;; Sets the value of the first attribute with the given tag found within the block, if present.
;; blk - [vla] VLA Block Reference Object
;; tag - [str] Attribute TagString
;; val - [str] Attribute Value
;; Returns: [str] Attribute value if successful, else nil.

(defun LM:vl-setattributevalue ( blk tag val )

;(setq blk "SSREVISION")
;(setq tag "1_NO")

(setq tag (strcase tag))
(vl-some
'(lambda ( att )
(if (= tag (strcase (vla-get-tagstring att)))
(progn (vla-put-textstring att val) val)
)
)
(vlax-invoke blk 'getattributes)
)
)

(defun setAttrib ( blkname att val / ss vla-obj)


(setq lays (:Layorder))
(setq lay (______ lays "1")


(if (setq ss (ssget "X" (list (cons 2 blkname)(cons 0 "INSERT")(cons 410 (lay)))))
(progn
(setq vla-obj (vlax-ename->vla-object (ssname ss 0)))
(if (not (LM:vl-setattributevalue vla-obj att val))
(prompt "\nAttribute not present")
)
)
(prompt "\nBlock not found")
)
(princ)
)


(defun c:REVUP ()
(setAttrib "SSREVISION" "1_NO" "1")

0 Likes
Message 5 of 7

CADaSchtroumpf
Advisor
Advisor

I add a function in your code

(vl-load-com)
; str2lst
;; Transforme un chaine avec séparateur en liste de chaines
;;
;; Arguments
;; str : la chaine à transformer en liste
;; sep : le séparateur
;;
;; Exemples
;; (str2lst "a b c" " ") -> ("a" "b" "c")
;; (str2lst "1,2,3" ",") -> ("1" "2" "3")
(defun str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
    (cons
      (substr str 1 pos)
      (str2lst (substr str (+ (strlen sep) pos 1)) sep)
    )
    (list str)
  )
)
;; ListBox (gile)
;; Boite de dialogue permettant un ou plusieurs choix dans une liste
;;
;; Arguments
;; title : le titre de la boite de dialogue (chaîne)
;; msg ; message (chaîne), "" ou nil pour aucun
;; keylab : une liste d'association du type ((key1 . label1) (key2 . label2) ...)
;; flag : 0 = liste déroulante
;; 1 = liste choix unique
;; 2 = liste choix multipes
;;
;; Retour : la clé de l'option (flag = 0 ou 1) ou la liste des clés des options (flag = 2)
;;
;; Exemple d'utilisation
;; (listbox "Présentation" "Choisir une présentation" (mapcar 'cons (layoutlist) (layoutlist)) 1)
(defun ListBox (title msg keylab flag / tmp file dcl_id choice)
  (setq
    tmp (vl-filename-mktemp "tmp.dcl")
    file (open tmp "w")
  )
  (write-line
    (strcat "ListBox:dialog{label=\"" title "\";")
    file
  )
  (if (and msg (/= msg ""))
    (write-line (strcat ":text{label=\"" msg "\";}") file)
  )
  (write-line
    (cond
      ((= 0 flag) "spacer;:popup_list{key=\"lst\";")
      ((= 1 flag) "spacer;:list_box{key=\"lst\";")
      (T "spacer;:list_box{key=\"lst\";multiple_select=true;")
    )
    file
  )
  (write-line "}spacer;ok_cancel;}" file)
  (close file)
  (setq dcl_id (load_dialog tmp))
  (if (not (new_dialog "ListBox" dcl_id))
    (exit)
  )
  (start_list "lst")
  (mapcar 'add_list (mapcar 'cdr keylab))
  (end_list)
  (action_tile
    "accept"
    "(or (= (get_tile \"lst\") \"\")
      (if (= 2 flag)
        (progn
          (foreach n (str2lst (get_tile \"lst\") \" \")
            (setq choice (cons (nth (atoi n) (mapcar 'car keylab)) choice))
          )
          (setq choice (reverse choice))
        )
        (setq choice (nth (atoi (get_tile \"lst\")) (mapcar 'car keylab)))
      )
    )
    (done_dialog)"
  )
  (start_dialog)
  (unload_dialog dcl_id)
  (vl-file-delete tmp)
  choice
)
(defun LM:vl-setattributevalue ( blk tag val )
  (setq tag (strcase tag))
  (vl-some
    '(lambda ( att )
      (if (= tag (strcase (vla-get-tagstring att)))
        (progn (vla-put-textstring att val) val)
      )
    )
    (vlax-invoke blk 'getattributes)
  )
)
(defun setAttrib ( blkname att val / ss vla-obj)
  (setq layout (listbox "Layouts" "Select a layout" (mapcar 'cons (append '("Model") (layoutlist)) (append '("Model") (layoutlist))) 1))
  (setq ss (ssget "_X" (list (cons 0 "INSERT")(cons 410 layout)(cons 66 1)(cons 2 blkname))))
  (repeat (setq n (sslength ss))
    (setq vla-obj (vlax-ename->vla-object (ssname ss (setq n (1- n)))))
    (LM:vl-setattributevalue vla-obj att val)
  )
)
(defun c:REVUP ()
(setAttrib "SSREVISION" "1_NO" "1")
)
0 Likes
Message 6 of 7

Sea-Haven
Mentor
Mentor

Both work  (getvar 'ctab) but (getvar "ctab")

 

(getvar 'ctab)
"Model"

(getvar "ctab")
"Model"
0 Likes
Message 7 of 7

CADaSchtroumpf
Advisor
Advisor

@Sea-Haven 

It's true, it works. I had never seen this notation before.
I should have tried before I said ...

0 Likes