Add element to all open files

344 views Asked by At

I've been using AutoLISP/CAD for a while and now I want to add a label with my name to all the open files. I've managed to iterate over all the open files, but the text command only runs in the first file. I thought it was too quick for CAD to work properly, so I added delays but it didn't work. I've checked in all the open files and the variables are shared and synced. By the way, the text is added once per open file, but only in the first one.

Here is my code:

(defun c:labeling()
  (vl-load-com)
  (setq docs (vla-get-documents (vlax-get-acad-object)))
  (setq top (vla-get-count docs))
  (setq p1 (list 10 -10))                   
  (setq p2 (list 95 -15))
  (setq p3 (list 12 -14))
  (setq c 0)
  (vl-propagate 'docs)
  (vl-propagate 'top)
  (vl-propagate 'p1)
  (vl-propagate 'p2)
  (vl-propagate 'p3)
  (vl-propagate 'c)
  (while (< c top)
    (vla-activate (vla-item docs c))
    (command "_rectang" p1 p2)
    (command "delay" 500)
    (command "_text" p3 "3" 0 "My name - year" "" nil)
    ;(print c)
    (setq c (+ c 1))
    (vl-propagate 'c)
    (command "delay" 1000)
    )
  )
1

There are 1 answers

2
CAD Developer On BEST ANSWER

It's a bit complicated to explain in such short time I have but: Each drawig has his own "namespace" probably it's wrong word, but nevermind. When You run command it runs only in active drawing, but when You change active drawing You lost active lisp routine. So it's not enought to activate drawing. Better way is to draw by manipulate model object. for example like this:

(defun c:labeling()
  (vl-load-com)
  (setq docs (vla-get-documents (vlax-get-acad-object)))
  (setq top (vla-get-count docs))
  (setq p1 (list 10 -10))
  (setq p2 (list 95 -15))
  (setq p3 (list 12 -14))
  (setq c 0)
  (vlax-for ThisDoc docs 
    (setq Space (vlax-get-property ThisDoc 'ModelSpace ) )
    (Rectangle Space p1 p2 )
    (setq txt (vlax-invoke-method Space 'AddText "My name - year" (vlax-3d-point p3 ) 3  ))
    (setq c (+ c 1))
   )
)


(defun Rectangle ( Space P1 P2 / lpts pts poly ) 
    (setq lpts (append p1 (list 0 ) (list (car p1 ) (cadr p2 ) 0 ) p2 (list 0 ) (list (car P2)  (cadr p1) 0 ) ) )
    (setq pts (L2v lpts vlax-vbDouble ) )
    (setq poly(vlax-invoke-method Space 'AddPolyline pts ) )
    (vlax-put-property poly 'Closed :vlax-true )
    poly
)

(defun L2v(lista typ / NObj SelObjArray iCount iList SelObjArrayVar)
    ;|
    vlax-vbInteger (2)      Integer 
    vlax-vbLong (3)         Long integer 
    vlax-vbSingle (4)       Single-precision floating-point number 
    vlax-vbDouble (5)       Double-precision floating-point number 
    vlax-vbString (8)       String 
    vlax-vbBoolean (11)     Boolean 
    vlax-vbVariant (12)     Variant
|;

  (setq NObj (length lista)
        SelObjArray (vlax-make-safearray typ (cons 0 (1- NObj) ))
        iCount 0)
 (repeat NObj     
 (vlax-safearray-put-element SelObjArray iCount (nth iCount lista))
 (setq iCount (1+ iCount))
  )
  (setq SelObjArrayVar (vlax-make-variant SelObjArray))
)