Insert specific block at a POSITION parameter in another block

Insert specific block at a POSITION parameter in another block

cksE6SE5
Participant Participant
326 Views
3 Replies
Message 1 of 4

Insert specific block at a POSITION parameter in another block

cksE6SE5
Participant
Participant

Hi all!

 

I need a LISP command to automatically insert a "Position number" block at a point called "POSDATA" in my conveyor block. I tried using the following LISP, but it just doesn't work:

 

(defun c:POS (/  valid_blocks f ss i e data dpp len)
  
;;(setq valid_blocks '("CONV-480-B190""));;
(setq f (lambda (p) (vlax-get e p)))

(if 
      (setq ss (ssget '((0 . "INSERT")(2 . "`*U*,CONV-*"))))
      
  	(repeat (setq i (sslength ss))
          (setq e (vlax-ename->vla-object (ssname ss (Setq i (1- i)))))
          (setq data (mapcar 'f  '("Insertionpoint" "Rotation" "EffectiveName" "XScaleFactor")))

          (if (and
                ;;(member (caddr data) valid_blocks)
		(vl-string-search "CONV-" (caddr data))
                (setq dpp (mapcar '(lambda (e) (mapcar 'f
                                      '("PropertyName" "Value" )))
                                    (vlax-invoke e 'GetDynamicBlockProperties)))
                (> (setq len (cadr (Assoc "Posdata" dpp))) 0)
                )
		(vlax-invoke
		  (vlax-get (vla-get-ActiveLayout
		              (vla-get-activedocument
		                (vlax-get-acad-object)
		              )
		            )
		            'Block
		  )
		  'InsertBlock
		  (polar (car data)
                         (+ (if (minusp (last data)) pi 0.0) (Cadr data))
                         len)
		  "POSITION2"	;; block needs to be in drawing to execute command
		  1 1 1 0) ;; <-- block rotation 0
		)
            )
          )
  (princ)
  )

 

I don't have any experience in coding at all, so I don't know what to do other than replacing names and values in the code. Just for info, the conveyor blocks might vary in name, but they all start with "CONV-", eg.:

 

CONV-480-B180

CONV-580-B380

CONV-680-B90

and so on...

 

Attached example drawing with the conveyor block and the position block, and a description of what it is supposed to look like.

 

If any questions, I'll be glad to elaborate 🙂 thanks in advance

0 Likes
327 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant

Seems ok this way. 

 

(defun c:POS (/  valid_blocks f ss i e data dpp lenx leny)
  
  ;;(setq valid_blocks '("CONV-480-B190""));;
  (setq f (lambda (p) (vlax-get e p)))
  
  (if
    (setq ss (ssget '((0 . "INSERT")(2 . "`*U*,CONV-*"))))
    
    (repeat (setq i (sslength ss))
      (setq e (vlax-ename->vla-object (ssname ss (Setq i (1- i)))))
      (setq data (mapcar 'f  '("Insertionpoint" "Rotation" "EffectiveName" "XScaleFactor" "YScaleFactor")))
      
      (if (and
	    ;;(member (caddr data) valid_blocks)
	    (vl-string-search "CONV-" (caddr data))
	    (setq dpp (mapcar '(lambda (e) (mapcar 'f
						   '("PropertyName" "Value" )))
			      (vlax-invoke e 'GetDynamicBlockProperties)))
	    (setq lenx (cadr (Assoc "POSDATA X" dpp)))
	    (setq leny (cadr (Assoc "POSDATA Y" dpp)))
	    )
	
	(vlax-invoke
	  (vlax-get (vla-get-ActiveLayout
		      (vla-get-activedocument
			(vlax-get-acad-object)
			)
		      )
		    'Block
		    )
	  'InsertBlock
	  (polar (polar (car data)
			(+ (if (minusp (cadddr data)) pi 0.0) (Cadr data))
			lenx)
		 (+ (if (minusp (last data)) pi 0.0) (+ (* 0.5 pi) (Cadr data)))
		 leny)
	  "POSITION2"	;; block needs to be in drawing to execute command
	  1 1 1 0) ;; <-- block rotation 0
	)
      )
    )
  (princ)
  )

 

0 Likes
Message 3 of 4

cksE6SE5
Participant
Participant

Seems to work as intended - thank you very much 🙂

0 Likes
Message 4 of 4

cksE6SE5
Participant
Participant

I noticed one minor issue (at least I hope it's minor). When I Rotate3D or 3DR one of my conveyors, the position-block doesn't adhere correctly to the snap point, but rather places itself several meters out in the open... Any ways to fix this?Captureconv.JPG

0 Likes