how to insert and edit attributes of a block

how to insert and edit attributes of a block

Anonymous
Not applicable
1,220 Views
8 Replies
Message 1 of 9

how to insert and edit attributes of a block

Anonymous
Not applicable

Hello. I would like to create a lisp routine to insert a block in specific coordinates and edit the attributes of this block. Do you have any exemples to help me in this task? Thanks.

0 Likes
Accepted solutions (1)
1,221 Views
8 Replies
Replies (8)
Message 2 of 9

john.uhden
Mentor
Mentor

Take a look at the TransAtts.lsp posted here.

It should give you plenty of ideas.

John F. Uhden

Message 3 of 9

pbejse
Mentor
Mentor

Here's another way, [ Not as complete as Johns offering ]

 

(defun c:demo  (/ _Insert&AddValue str p)
(defun _Insert&AddValue  (bname p attvalue / blk)
    (setq blk  (vlax-invoke
                     (vlax-get
                           (vla-get-ActiveLayout
                                 (vla-get-activedocument
                                       (vlax-get-acad-object)))
                           'Block)
                     'InsertBlock p
                     bname 1 1 1 0))
    (mapcar '(lambda (a b)
                   (vla-put-textstring a b))
            (vlax-invoke blk 'Getattributes)
            attvalue)
    (princ)
    )
      (_Insert&AddValue
            "YourBlockname"
            "Yourcoordinates"
            '("list of values" "for your attribute"));<-- order of attribute prompt
      (princ)
      )

HTH

Message 4 of 9

Anonymous
Not applicable

Hello. I've tried to use your code, but I am a beginner in lisp taks, so guess what ... I was not succesful. I am sending a simple block as example, so supposing that I want to insert it at coordinates x=100 and y=12, with the attribute NUMBER=01 and CODE=9887, how can I indicate these number in the lisp. I've tried to perform it using your example but I really do not have enough knolegment with autolisps to make it work. I think I am forgeting something. Could you please help me ?

0 Likes
Message 5 of 9

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Hello. I've tried to use your code, but I am a beginner in lisp taks, so guess what ... I was not succesful. I am sending a simple block as example, so supposing that I want to insert it at coordinates x=100 and y=12, with the attribute NUMBER=01 and CODE=9887, how can I indicate these number in the lisp. I've tried to perform it using your example but I really do not have enough knolegment with autolisps to make it work. I think I am forgeting something. Could you please help me ?


Of course, we will help you jean_rossini

 

Keep in mind that this is a demonstration code, 

 

 

 

(defun c:demo  (/ _Insert&AddValue str p)
(defun _Insert&AddValue  (bname p attvalue / blk)
    (setq blk  (vlax-invoke
                     (vlax-get
                           (vla-get-ActiveLayout
                                 (vla-get-activedocument
                                       (vlax-get-acad-object)))
                           'Block)
                     'InsertBlock p
                     bname 1 1 1 0))
    (mapcar '(lambda (a b)
                   (vla-put-textstring a b))
            (vlax-invoke blk 'Getattributes)
            attvalue)
    (princ)
    )
      (_Insert&AddValue
            "dt"  (list 100.00 12.00 0.0) (list "9887" "01"))
      (princ)
      )

 

If you are planning to use for real, it may look something like this

 

(defun c:demo2  (/ _Insert&AddValue str p)
 
(defun _Insert&AddValue  (bname p attvalue / blk)
    (setq blk  (vlax-invoke
                     (vlax-get
                           (vla-get-ActiveLayout
                                 (vla-get-activedocument
                                       (vlax-get-acad-object)))
                           'Block)
                     'InsertBlock p
                     bname 1 1 1 0))
    (mapcar '(lambda (a b)
                   (vla-put-textstring a b))
            (vlax-invoke blk 'Getattributes)
            attvalue)
    (princ)
    )

;;;			DATA			;;;
(setq blockname "dt")
(setq pointsforinsertion '(	(129.341 88.4354 0.0)
			(109.854 115.512 0.0)
		        (75.4478 96.6498 0.0)
		        (71.1851 134.375 0.0)
		        (33.4296 95.1286 0.0)))

(setq code 	'("123" "75675" "687" "2345" "okidoki"))
(setq counter  '("01""02" "03" "04" "05"))

;;;		   END of DATA			;;;

;;; Pass the value to _Insert&AddValue function	;;;
 
(mapcar '(lambda (x y z)
               (_Insert&AddValue blockname x (list y z)))
 			pointsforinsertion
 			code
 			counter)
 
;;;		 END of function		;;;
(princ)
 
 )

HTH

 

Message 6 of 9

Anonymous
Not applicable

Hello. The code demo2 is exactly what I was looking for, I am really grateful for your help. I intend to use your code (demo2) together with my code gdt, but I have one last question. The code gdt reads a txt file that contains the attributes for the block dt (NUMBER, CODE and coordinates x and y). I've created four lists to insert these data according to your code demo2, however I was not sucessful. I think I am inserting data in a wrong format, but I really don't know how to solve it. Could you please help me again? 

(defun c:gdt ()
  (setq NOME (getfiled "Open file" "" "TXT" (+ 2 4)))

    (setq FD (open NOME "r")); open the file "data"
		  
  		  
	    (while (setq REG (read-line FD)); it reads the file line by line


			  (setq a (substr REG 1 2)); in the file: initial position 1, length 2
			  (setq a1 (read a)); it is the attribute NUMBER 

			  (setq nox (substr REG 4 10)); in the file: initial position 4, length 10
			  (setq nox1 (read nox)); it is the x coordinate

			  (setq noy (substr REG 15 10)); in the file: initial position 15, length 10
			  (setq noy1 (read noy)); it is the y coordinate
    
	   		  (setq CD (substr REG 27 4)); in the file: initial position 27, length 4
	                  (setq CD1 (read CD)); it is the attribute CODE


;; list creation 
		  	  (setq counter (append counter (list a1))); NUMBER list
		      (setq coordx (append coordx (list nox1))); x coordinates list
		      (setq coordy (append coordy (list noy1))); y coordinates list
	          (setq code (append code (list CD1))); CODE list


;; just printing to evaluate 
	          (princ counter) (terpri)
	          (princ coordx) (terpri)
		  (princ coordy) (terpri)
	          (princ code) (terpri)
;;;;

		); while

	(close FD)
);defun
0 Likes
Message 7 of 9

pbejse
Mentor
Mentor

@Anonymous wrote:

... Could you please help me again? 


Of course, we'll help you.

 

 

I would suggest you include a check for existence of block name and data

 

 

(if
      (and
	(setq NOME (getfiled "Open file" "" "TXT" (+ 2 4)))
        (tblsearch "BLOCK" (setq blockname "DT"))
        )
      (progn
            (setq FD (open NOME "r"))
            ......

            (close FD)
      
	(mapcar '(lambda (x y z a)
	               (_Insert&AddValue
	                     blockname
	                     (list x y 0.0)
	                     (list (itoa z) (itoa a)))
	               ) coordx  coordy code counter)
            );progn
      )
)

 

 

You may want to convert the items in code and counter list to a string before passing them  to _Insert&AddValue  unless of course yourre planning to use the values for something else that requires a number

 

You can also do it this way.

 

.... 
(setq CD1 (read CD)) ; it is the attribute CODE (_Insert&AddValue blockname (list nox1 noy1 0.0) (list (itoa CD1) (itoa a1))) ); while (close FD)
);progn

or even

 

...
(_Insert&AddValue blockname (list (read nox) (read noy) 0.0) (list (itoa (read CD)) (itoa (read a))))
...

You know what i mean...

 

 

Good luck jean_rossini 

 

 

 

Message 8 of 9

Anonymous
Not applicable

It works perfectly. Thanks for all the support and advices, I've learned a bunch of new approaches with your codes. I am really grateful. 

0 Likes
Message 9 of 9

pbejse
Mentor
Mentor

@Anonymous wrote:

It works perfectly. Thanks for all the support and advices, I've learned a bunch of new approaches with your codes. I am really grateful. 


You are welcome jean_rossini,

 

We're happy to help.

 

0 Likes