Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Update Block Attribute with variable

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
CAD_user85
2031 Views, 9 Replies

Update Block Attribute with variable

Dear Autocad / Lisp Users,

 

Could somebody please help me.

 

I would like to create a lisp that does the following:

Update a titleblock attribute with a $variable.

 

The titleblockname is variable / not static.

The attribute is not variable / Static, DW_SCALE

The titleblock is always places on the same layer, BL$4----

The (custom)-plotscale is stored in the $var "gvpsc".

 

I've tried to ajust the following lisp code. But I couldn't extract the part I need.

The problem is, the code checks all the layouts and updates all the titlblocks one by one.

Truely a good code. 

The only problem is, I don't have multiple layouts and I have a variable titleblock name.

 

Here is to code i've tried to convert to something I could use.

(defun C:UPD-DPC () 
  (setvar "cmdecho" 0)

  (setq blkname "DPC-STEMPEL")			;(getstring "\nEnter the name of block to update:  "))
  
(setq att1 "DW_DATE")					;(strcase(getstring "\nEnter the name of the attribute to change: ")))
  (setq newatt (strcase(getstring "\nEnter new date: ")))
  (update blkname att1 newatt)
  
  (setq blkname "DPC-STEMPEL")
  (setq att1 "DW_DESCRIPTION")
  (setq newatt (strcase(getstring "\nEnter new description: ")))
  (update blkname att1 newatt)

  (command "_zoom" "all")
  (princ)
  (princ)
);ends routine

(defun Update (Blockname attrname attrval)
  (setq I 0)
  (setq sset (ssget "x" (list (cons 2 blkname))))
  (if (= sset nil)
    (setq l 0)
    (setq l (sslength sset))
  )
  (repeat l
    (setq stop 1)
    (setq name (ssname sset I))
    (while (= stop 1)
      (setq name1 (entget (entnext name)))
      (setq ATT (cdr (assoc 2 name1)))
      (if (= att att1)(setq go 1))
      (setq name (cdr (assoc -1 name1)))  
                        
      (while (= go 1)
        (setq d (assoc 1 name1))
        (setq d2 (cons 1 newatt))
        (setq d1 (subst d2 d name1))
        (entmod d1)
        (setq go nil)
        (setq stop nil)
      )
    )
    (setq i (1+ i))
  )
  (princ)
  (princ)
);ends routine

 

Anny suggestions are welcome. 

Thanks in advance.

 

Greets,

Michel

 

 

9 REPLIES 9
Message 2 of 10
pbejse
in reply to: CAD_user85


@weedseed85 wrote:

 

The only problem is, I don't have multiple layouts and I have a variable titleblock name.

 

Here is to code i've tried to convert to something I could use.Anny suggestions are welcome. 

Thanks in advance.

 

Greets,

Michel 

 


Would you rather be prompted for a BLOCK name/Attribute TAG/ Attribute string value?

 

IF not :

 

(setq sset (ssget "x" (list '(0 . "INSERT")'(66 . 1)
                              '(8  . "YourLayername")
                              (cons 410 (getvar 'ctab)))))

 

(8 . "YourLayername) ---> Attribute block at specific layer  [Static]

 

(cons 410 (getvar 'ctab)) ----> Target objects ONLY on current layout tab 

 

Is that enough info for you?

 

EDIT: would you want this to work only for "DW_SCALE" tag ?

 

Message 3 of 10
CAD_user85
in reply to: pbejse

Dear pbejse,

 

Autolisp is still something i'm trying to learn. with the help of kind people like you and google,

I think I will get it eventualy.

 

The problem with most code / scripts, is that when you add / ajust / delete parts of code, 99% of the time

some part of the code will not be working...

 

My knowlage is to limited to read and interpeted the code, so i can ajust it.

 

Getting a block or object from a certain layer, I can do.

Altho your method is a more easy.

Thank you.

(setq sset (ssget "x" (list '(0 . "INSERT")'(66 . 1)'(8  . "BL$4----")(cons 410 (getvar 'ctab)))))

 

The only thing left to do now is, delete the loop part, without killing the rest of the code.

Could you perhabs lend me a hand with that?

 

Anny help will be appreciated.

 

p.s. Yess I would verry mucht like it to work only for the DW_SCALE.

 

Greets,

Michel

 

 

 

Message 4 of 10
pbejse
in reply to: CAD_user85


@weedseed85 wrote:

Dear pbejse,@pbejse

 

Autolisp is still something i'm trying to learn. with the help of kind people like you and google,

I think I will get it eventualy.

 

The problem with most code / scripts, is that when you add / ajust / delete parts of code, 99% of the time

some part of the code will not be working...

 

My knowlage is to limited to read and interpeted the code, so i can ajust it.

 

Getting a block or object from a certain layer, I can do.

Altho your method is a more easy.

Thank you.

(setq sset (ssget "x" (list '(0 . "INSERT")'(66 . 1)'(8  . "BL$4----")(cons 410 (getvar 'ctab)))))

 

The only thing left to do now is, delete the loop part, without killing the rest of the code.

Could you perhabs lend me a hand with that?

 

Anny help will be appreciated.

 

p.s. Yess I would verry mucht like it to work only for the DW_SCALE.

 

Greets,

Miche

 


Ok, one more question. what of the variable gvpsc where is this stored? About the loop thingy, will there be more than one titleblock on one layout tab? do you want the routine to stop if in case tehre are more than one?

 

 

Message 5 of 10
CAD_user85
in reply to: CAD_user85

(defun C:updateblk () 
  (setvar "cmdecho" 0)
  (vpsc)
  (setq att1 (getvar "gvpsc")
  (setq blkname (ssget "x" (list '(0 . "INSERT")'(66 . 1)'(8  . "BL$4----")(cons 410 (getvar 'ctab)))))
  ;(setq blkname "DPC-STEMPEL")			;(getstring "\nEnter the name of block to update:  "))
  
  (setq att1 "DW_SCALE")				;(strcase(getstring "\nEnter the name of the attribute to change: ")))
  (setq newatt (strcase(getstring "\nEnter new date: ")))
  
  
  (update blkname att1 newatt)
  
  (setq blkname "DPC-STEMPEL")
  (setq att1 "DW_DESCRIPTION")
  (setq newatt (strcase(getstring "\nEnter new description: ")))
  (update blkname att1 newatt)

  (command "_zoom" "all")
  (princ)
  (princ)
);ends routine

(defun Update (Blockname attrname attrval)
  (setq I 0)
  (setq sset (ssget "x" (list (cons 2 blkname))))
  (if (= sset nil)
    (setq l 0)
    (setq l (sslength sset))
  )
  (repeat l
    (setq stop 1)
    (setq name (ssname sset I))
    (while (= stop 1)
      (setq name1 (entget (entnext name)))
      (setq ATT (cdr (assoc 2 name1)))
      (if (= att att1)(setq go 1))
      (setq name (cdr (assoc -1 name1)))  
                        
      (while (= go 1)
        (setq d (assoc 1 name1))
        (setq d2 (cons 1 newatt))
        (setq d1 (subst d2 d name1))
        (entmod d1)
        (setq go nil)
        (setq stop nil)
      )
    )
    (setq i (1+ i))
  )
  (princ)
  (princ)
);ends routine

(defun vpsc ()	;/ a ts n vpsf th index b1 b c d b2)  		; defines function and variables
  (setq ss (ssget "X" (list '(0 . "Viewport")(cons 69 2))))		; defines selection set '(cons 69 1)(cons 410 "*") 
  (setq n (sslength ss))                                   	 	; gets number of items selected
  (setq index 0)                                           	 	; sets counter for loop to 0
  (repeat n                                               	 	; begin loop for number of items
    (setq ent (ssname ss index))                 				; get the entity name
    (setq entnm (entget (ssname ss index)))             	    ; get the entity name
	;(princ entnm)												; show ent data
	(setq index (+ index 1))                          		   	; adds to counter
	(setq entyp (assoc 0 entnm))                      	     	; pulls out entity type 
    (if (= "VIEWPORT" (cdr entyp))                          	; checks for text entity
		(progn                                          		; begins actual program 
			(setq vpnme (cdr (assoc 410 entnm)))              	; gets viewport name
			;(princ "\nViewportname = ")
			;(princ vpnme)
			(setq data (entget ent '("ACAD*")))
 
			(setq cvhgt  (acet-dxf 41 DATA))  					; viewport height
			(setq cvsize (cdr (getx 6 data)))    				; viewsize from extended data
			;(strcat "\n1:" (rtos (/ cvsize cvhgt) 2 0))			; Print output, 1: scale
			(setq gvpsc (rtos (/ cvsize cvhgt) 2 0))			; Place output into $var "gvpsc"
		)														; end progn
    )															; ends if
	)															; ends loop
  

I've tried to create something out of it

Unfortunaly it didn't work..

 

 

 

 

 

Message 6 of 10
pbejse
in reply to: CAD_user85


@weedseed85 wrote:
I've tried to create something out of it

Unfortunaly it didn't work..

 

 


I dont have the system variable gvpsc on my ACAD

 

Try this:

 

(defun C:UPD-DPC (/ I sset l stop name name1 Att go d d1 d2)
  (if (and (setq att1 (getvar "gvpsc"))
	   (setq I 0
		 sset (ssget "x"
			     (list '(0 . "INSERT")
				   '(66 . 1)
				   '(8  . "BL$4----")
				   (cons 410 (getvar 'ctab))
			     )
		      )
	   )
      )
    (repeat (setq l (sslength sset))
      (setq stop 1)
      (setq name (ssname sset I))
      (while (= stop 1)
	(setq name1 (entget (entnext name)))
	(setq ATT (cdr (assoc 2 name1)))
	(Setq go (if (= att "DW_SCALE")
		   1
		 )
	)
	(setq name (cdr (assoc -1 name1)))

	(while (= go 1)
	  (setq d (assoc 1 name1))
	  (setq d2 (cons 1 att1))
	  (setq d1 (subst d2 d name1))
	  (entmod d1)
	  (setq go nil)
	  (setq stop nil)
	)
      )
      (setq i (1+ i))
    )
  )
  (princ)
)

 

Message 7 of 10
CAD_user85
in reply to: pbejse

😞 

Unfortunaly, nothing.

 

The $var, gvpsc isn't a system variable.

It's a declared variable.

 

This is one of the last command lines of the "VPSC" lisproutine*

"(setq gvpsc (rtos (/ cvsize cvhgt) 2 0))" 

 

It returns the calculated viewportscale, (size / height)

 

I've added the ajusted code below.

 

*The below part of the code, is a chuck of code, extracted from the VPSCALE.lsp lisproutine provided by Expresstools

It does nothing more then calculate the vpscale.

 

 

(defun C:UPD-DPC (/ I sset l stop name name1 Att go d d1 d2)

(setq ss (ssget "X" (list '(0 . "Viewport")(cons 69 2))))		; defines selection set '(cons 69 1)(cons 410 "*") 
  (setq n (sslength ss))                                   	 	; gets number of items selected
  (setq index 0)                                           	 	; sets counter for loop to 0
  (repeat n                                               	 	; begin loop for number of items
    (setq ent (ssname ss index))                 				; get the entity name
    (setq entnm (entget (ssname ss index)))             	    ; get the entity name
	;(princ entnm)												; show ent data
	(setq index (+ index 1))                          		   	; adds to counter
	(setq entyp (assoc 0 entnm))                      	     	; pulls out entity type 
    (if (= "VIEWPORT" (cdr entyp))                          	; checks for text entity
		(progn                                          		; begins actual program 
			(setq vpnme (cdr (assoc 410 entnm)))              	; gets viewport name
			;(princ "\nViewportname = ")
			;(princ vpnme)
			(setq data (entget ent '("ACAD*")))
 
			(setq cvhgt  (acet-dxf 41 DATA))  					; viewport height
			(setq cvsize (cdr (getx 6 data)))    				; viewsize from extended data
			;(strcat "\n1:" (rtos (/ cvsize cvhgt) 2 0))			; Print output, 1: scale
			(setq gvpsc (rtos (/ cvsize cvhgt) 2 0))			; Place output into $var "gvpsc"
		)														; end progn
    )															; ends if
	)															; ends loop
  


  (if (and (setq att1 (getvar "gvpsc"))
	   (setq I 0
		 sset (ssget "x"
			     (list '(0 . "INSERT")
				   '(66 . 1)
				   '(8  . "BL$4----")
				   (cons 410 (getvar 'ctab))
			     )
		      )
	   )
      )
    (repeat (setq l (sslength sset))
      (setq stop 1)
      (setq name (ssname sset I))
      (while (= stop 1)
	(setq name1 (entget (entnext name)))
	(setq ATT (cdr (assoc 2 name1)))
	(Setq go (if (= att "DW_SCALE")
		   1
		 )
	)
	(setq name (cdr (assoc -1 name1)))

	(while (= go 1)
	  (setq d (assoc 1 name1))
	  (setq d2 (cons 1 att1))
	  (setq d1 (subst d2 d name1))
	  (entmod d1)
	  (setq go nil)
	  (setq stop nil)
	)
      )
      (setq i (1+ i))
    )
  )
  (princ)
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Chuck of code extracte from the vpscale.lsp (expresstools)
(defun vpsc () ;/ a ts n vpsf th index b1 b c d b2) ; defines function and variables (setq ss (ssget "X" (list '(0 . "Viewport")(cons 69 2)))) ; defines selection set '(cons 69 1)(cons 410 "*") (setq n (sslength ss)) ; gets number of items selected (setq index 0) ; sets counter for loop to 0 (repeat n ; begin loop for number of items (setq ent (ssname ss index)) ; get the entity name (setq entnm (entget (ssname ss index))) ; get the entity name ;(princ entnm) ; show ent data (setq index (+ index 1)) ; adds to counter (setq entyp (assoc 0 entnm)) ; pulls out entity type (if (= "VIEWPORT" (cdr entyp)) ; checks for text entity (progn ; begins actual program (setq vpnme (cdr (assoc 410 entnm))) ; gets viewport name ;(princ "\nViewportname = ") ;(princ vpnme) (setq data (entget ent '("ACAD*"))) (setq cvhgt (acet-dxf 41 DATA)) ; viewport height (setq cvsize (cdr (getx 6 data))) ; viewsize from extended data ;(strcat "\n1:" (rtos (/ cvsize cvhgt) 2 0)) ; Print output, 1: scale (setq gvpsc (rtos (/ cvsize cvhgt) 2 0)) ; Place output into $var "gvpsc" ) ; end progn ) ; ends if ) ; ends loop (defun getx (n data) (nth n (cdadr (assoc -3 data))) )

 

 

 

 

Message 8 of 10
pbejse
in reply to: CAD_user85

I see now. Theres something odd about that vpsc code you posted, this (cons 69 2). anyhoo

 

<NOT TESTED>

 

(defun C:UPD-DPC (/ I sset l stop name name1 Att go d d1 d2)
  (if (and 
	   (setq att1 (vpsc T))
	   (setq I 0
		 sset (ssget "x"
			     (list '(0 . "INSERT")
				   '(66 . 1)
				   '(8  . "BL$4----")
				   (cons 410 (getvar 'ctab))
			     )
		      )
	   )
      )
    (repeat (setq l (sslength sset))
      (setq stop 1)
      (setq name (ssname sset I))
      (while (= stop 1)
	(setq name1 (entget (entnext name)))
	(setq ATT (cdr (assoc 2 name1)))
	(Setq go (if (= att "DW_SCALE")
		   1
		 )
	)
	(setq name (cdr (assoc -1 name1)))

	(while (= go 1)
	  (setq d (assoc 1 name1))
	  (setq d2 (cons 1 att1))
	  (setq d1 (subst d2 d name1))
	  (entmod d1)
	  (setq go nil)
	  (setq stop nil)
	)
      )
      (setq i (1+ i))
    )
  )
  (princ)
)
(defun getx (n data)
  (nth n (cdadr (assoc -3 data)))
)  
  (defun vpsc (flg / ss n index ent entnm entyp vpnme data cvhgt cvsize gvpsc)
    (if	(setq ss (ssget	"X"
			(list '(0 . "Viewport")
			      (cons 69 2)
			      (cons 410
				    (if	flg
				      (getvar 'Ctab)
				      "*"
				    )
			      )
			)
		 )
	)
      (progn
	(setq n (sslength ss))
	(setq index 0)
	(repeat	n
	  (setq ent (ssname ss index))
	  (setq entnm (entget (ssname ss index)))

	  (setq index (+ index 1))
	  (setq entyp (assoc 0 entnm))
	  (if (= "VIEWPORT" (cdr entyp))
	    (progn
	      (setq vpnme (cdr (assoc 410 entnm)))
	      (setq data (entget ent '("ACAD*")))
	      (setq cvhgt (acet-dxf 41 DATA))
	      (setq cvsize (cdr (getx 6 data)))
	      (setq gvpsc (rtos (/ cvsize cvhgt) 2 0))
	    )
	  )
	)
      )
    )
    gvpsc
  )

 

vpsc sub function re-define to accept flag for ALL layout tabs or just the current layout.  T for current layout only nil for ALL

 

Cant vouch for he result of vpsc function, need to test it on an actual drawing which meets the condtion 

 

HTH

 

Message 9 of 10
CAD_user85
in reply to: pbejse

Dear pbejse,

 

Thank you verry, verry much for that last lisp.

It works perfectly!

 

Thank you!

 

Greets Michel

Message 10 of 10
pbejse
in reply to: CAD_user85

You are welcome. Glad to be of service.

 

Cheers weedseed85

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost