Insert block and check attribute of blocks previously inserted

Insert block and check attribute of blocks previously inserted

luizhcastanho
Contributor Contributor
456 Views
6 Replies
Message 1 of 7

Insert block and check attribute of blocks previously inserted

luizhcastanho
Contributor
Contributor

I need to insert blocks, but first I need to check if there are blocks with the same attributes and, if so, I need to copy one of the attributes.

 

Attributes:
ID
COMP
GAUGE
QTY

 

If the COMP and BITOLA are the same as those of another previously inserted block, then I need to copy the ID, otherwise ID + 1.

 

;contar blocos
(setq block_list
		(list 
			"Negativo"
		)
	)
	(setq ss (ssget "X" (list (cons 0 "INSERT") (cons 410 "Model"))))
	(foreach block_name block_list
		(setq i -1 cnt 0)
		(while (< (setq i (1+ i)) (sslength ss))
			(setq bo (vlax-ename->vla-object (ssname ss i)))
			(if (= block_name (vlax-get bo 'EffectiveName)) (setq cnt (1+ cnt)))
		)
		(setq env_name (strcat "BLOCK_COUNT_" block_name))
		(setenv env_name (itoa cnt))
	)
	(princ "\n")
	(command "_REGEN")
	(princ)

 

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

ВeekeeCZ
Consultant
Consultant

try this if it helps...

 

(defun :getid (block comp bitola id / sel lst x)
  
  (if (and (setq sel (ssget "X" (list '(0 . "INSERT") '(410 . "Model"))))
	   (setq lst (mapcar 'cadr (ssnamex sel)))
	   (vl-some '(lambda (blk)
		       (and (not (vl-catch-all-error-p (setq x (vl-catch-all-apply 'getpropertyvalue (list blk "BlockTableRecord/Name")))))
			    (= block x)
			    (not (vl-catch-all-error-p (setq x (vl-catch-all-apply 'getpropertyvalue (list blk "COMP")))))
			    (= comp x)
			    (not (vl-catch-all-error-p (setq x (vl-catch-all-apply 'getpropertyvalue (list blk "BITOLA")))))
			    (= bitola x)
			    (not (vl-catch-all-error-p (setq x (vl-catch-all-apply 'getpropertyvalue (list blk "ID")))))))
		    lst)
	   )
    x
    (strcat (substr id 1 1) (1+ (atoi (substr id 2))))))

 

0 Likes
Message 3 of 7

luizhcastanho
Contributor
Contributor

Sorry, but I don't understand how to implement your function. I need it to check the COMP and BITOLA attributes of previous blocks and return ID.

 

Example I have list of attributes

 

("ID" COMP BITOLA QTD)

 

("N1" 120 6.3 12) ("N2" 150 6.3 5) ("N3" 200 8 10) ("N4" 220 6.3 7) 

 

If the next block has COMP=150 and BITOLA=6.3, its ID should "N2"

 

next block has COMP=200 and BITOLA=8, its ID should "N3"

 

next block has COMP=300 and BITOLA=8, then ID should "N5"

0 Likes
Message 4 of 7

ВeekeeCZ
Consultant
Consultant

Sorry man, can't help you. I don't understand what you need, you don't understand the code. Seeing this and other threads of yours.. Nothing good can possibly come from this. You need to learn the language better.

Message 5 of 7

Sea-Haven
Mentor
Mentor

If I understand correctly, you are counting the blocks as you insert, the 6th "N2" then the 7th and so on. 

You could bury away inside the dwg a look up in some way so when block is added a check of 150 6.3 so its a "N2" so add 1 to current stored value. I have used Ldata and works well for this type of thing. A couple of problems how many "NX" values ? And how many sizes to look up "150 6.3" etc. 

 

 

(vlax-ldata-put "LUIZ" "N1" 0)
(vlax-ldata-put "LUIZ" "N2" 0)
(vlax-ldata-put "LUIZ" "N3" 0)

(setq num (vlax-ldata-get "LUIZ" "N1")) ; gets the last number add 1 then use put to update.

 

 

So supply some more answers.

0 Likes
Message 6 of 7

luizhcastanho
Contributor
Contributor

I'm using this loop to check previously inserted blocks.

 

		(setq conid 0)
		(setq tt (ssget "X" (list (cons 0 "INSERT") (cons 410 "Model"))))
		(setq y -1)
		(while (< (setq y (1+ y)) (sslength tt))
			(setq bo (vlax-ename->vla-object (ssname tt y)))
			(if (= "Vigota hor." (vlax-get bo 'EffectiveName))
					(if (and (= bitola (getpropertyvalue (ssname tt y) "BITOLA"))   (= (rtos compng 2 0) (getpropertyvalue (ssname tt y) "COMP"))  )
						(progn
					  	(setq idex (getpropertyvalue (ssname tt y) "ID"))
				 		(setq numex (getpropertyvalue (ssname tt y) "NUM"))
						(setq conid 1)
					
						)
					)
			)
		)

 

If you already have a block with the attributes corresponding to the one I am inserted, copy the ID.

 

(getpropertyvalue (ssname tt y)

 

 This part has an error, I can't get the ename correctly.

In an isolated test I use (entlast) and it works, but in the main lisp I need to change the call.

 

0 Likes
Message 7 of 7

luizhcastanho
Contributor
Contributor
Accepted solution

If it is useful to anyone

 

  		(if (/= tt nil)
		(while (< (setq yy (1+ yy)) (sslength tt))
		  
			(setq bo (vlax-ename->vla-object (ssname tt yy)))		  
			(if (= "Negativo" (vlax-get bo 'EffectiveName))
				(progn
						(if (and (= bitola (getpropertyvalue (ssname tt yy) "BITOLA"))   (= (rtos compng 2 0) (getpropertyvalue (ssname tt yy) "COMP"))  )   
							(progn
						 	(setq conid 1)
					  		(setq idex (getpropertyvalue (ssname tt yy) "ID"))
				 			(setq numex (getpropertyvalue (ssname tt yy) "NUM"))
							)
						)
			  	)
			)
		)
		);fecha if nil
0 Likes