向所有打开的文件添加元素

Add element to all open files

我已经使用 AutoLISP/CAD 一段时间了,现在我想为所有打开的文件添加一个带有我名字的标签。我已经设法遍历所有打开的文件,但文本命令只在第一个文件中运行。我认为它太快了,CAD 无法正常工作,所以我添加了延迟,但它没有用。我已经检查了所有打开的文件并且变量是共享和同步的。顺便说一下,每个打开的文件都会添加一次文本,但只会在第一个文件中添加。

这是我的代码:

(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)
    )
  )

在这么短的时间内解释起来有点复杂,但是: 每个 drawig 都有自己的 "namespace" 可能是错误的词,但没关系。 当您 运行 命令时,它 运行 仅在活动绘图中,但是当您更改活动绘图时,您丢失了活动的 lisp 例程。 所以激活绘图是不够的。 更好的方法是通过操作模型对象来绘制。例如像这样:

(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))
)