ahyin 发表于 2022-7-6 09:25:01

无法运行命令

如果在循环中运行AutoCAD命令,是否可以计算在此循环中失败的次数并将其存储到变量中?
 
非常感谢。

irneb 发表于 2022-7-6 09:33:10

嗯,这个想法似乎是可能的。在循环开始之前,您需要将一个变量初始化为0,然后在if语句中简单地递增它。
 
尽管“问题”可能是:“如何测试不成功?”一种可能的方法是启动命令反应器:vlr命令失败。然后,不用循环中的if和增量,而是在循环之前启动反应器。然后,反应堆回调将检查计数变量是否分配了某个值(即非零),如果是,则增加该值。当然,在循环之后,你需要移除反应堆。

ahyin 发表于 2022-7-6 09:35:36

 
谢谢你的建议。

ahyin 发表于 2022-7-6 09:38:19

 
正如你所说,我可以这样写代码吗?
 

(defun c:tmp (/ ctr tte sset item r )
(setq sset (ssget "_a" (LIST '(0 . "*polyline") (cons 410 (getvar "CTAB")))))
(setq ctr 0 tte 0 r 0)
(repeat (sslength sset )
    (setq item (ssname sset ctr))

(if (/= sset 0)
   (porgn
   (vl-cmdf "explode" item "")
   (princ "All objects exploded")
   )
   (progn
   (setq r (1+ r))
);end if

(if (/= r 0)
      (alert (strcat "total"(itoa r) "nos of polyline can't explode"))
       (princ "All polyline exploded")
    );end if
);end if


);end defun

CHLUCFENG 发表于 2022-7-6 09:42:22

一个快速通知:
 

...
(if (/= sset 0)
   (progn
   (vl-cmdf "explode" item "")
   (princ "All objects exploded")
   )
...

CHLUCFENG 发表于 2022-7-6 09:46:37

另一个快速调整。。。
 
这分解了我在屏幕上放置的所有多段线:
 

(defun c:tmp (/ ctr sset item r) ; tte
(setq
   sset (ssget "_a"
(LIST '(0 . "*polyline") (cons 410 (getvar "CTAB")))
)
)
(setq ctr 0 r 0)    ; tte 0)
(repeat (sslength sset)
   (setq item (ssname sset ctr))
   (if (/= sset 0)
   (progn
      (vl-cmdf "_.explode" item "")
       (princ "\nThis object exploded")
   )   ;end progn
;;   (progn
      (setq r (1+ r))
;;   )   ;end progn
    )   ;end if
(setq ctr (1+ ctr))
)   ;end repeat
(if (/= r 0)
   (alert
   (strcat "\nTotal" (itoa r) "nos of polyline can't explode")
   )
   (princ "\nAll polyline exploded")
)   ;end if
(princ) ; exit quietly
)   ;end defun


 
开始一行,使代码更易于阅读,结束(princ)使代码不会两次返回最终消息。

ahyin 发表于 2022-7-6 09:54:14

 
谢谢你的帮助,但是当我尝试绘制没有多段线的图形时。它无法运行。它无法返回不能分解的多段线数。

irneb 发表于 2022-7-6 09:55:06

不幸的是,您想要做的事情不会产生任何错误,因为Explode命令在尝试爆炸之前会进行检查。
 
只是一个小的替代方案:与其将每个都发送到explode命令,不如检查它是否可以通过其ActiveX对象进行分解:
然而,该代码甚至会在锁定层上分解多段线。如果需要坚持使用锁定层上的代码,则需要更复杂的代码:
(vl-load-com)

(defun c:ExpPolys (/ ss eo err lck res LckLay)
;; Firsth get all the locked layer names
(vlax-for eo (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
   (if (= (vla-get-Lock eo) :vlax-true)
   (setq LckLay (cons (vla-get-Name eo) LckLay))
   )
)
;; Next select and initialize variables
(if (ssget "_a" (LIST '(0 . "*polyline") (cons 410 (getvar "CTAB"))))
   (progn
   (setq err 0
         lck 0
         ss(vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-acad-object)))
   )
   ;; Step through all items in the selection set
   (vlax-for eo ss
       ;; Check if on locked layer
       (if (vl-position (vla-get-Layer eo) LckLay)
         (setq lck (1+ lck)) ;If locked increment lck
         ;; Else attempt to explode
         (if (vl-catch-all-error-p (setq res (vl-catch-all-apply 'vla-Explode (list eo))))
         (setq err (1+ err)) ;If failed increment err
         )
       )
   )
   (princ (strcat "\nOff "
                  (itoa (vla-get-Count ss))
                  " polylines, "
                  (itoa lck)
                  " were on locked layers and "
                  (itoa err)
                  " couldn't be exploded."
            )
   )
   ;; Ensure selection set is cleared from RAM
   (vla-Delete ss)
   (vlax-release-object ss)
   (setq ss nil)
   (gc)
   )
   (princ "\nNo polylines to explode.")
)
(princ)
)

CHLUCFENG 发表于 2022-7-6 10:00:02

干得好irneb。

ahyin 发表于 2022-7-6 10:05:22

 
非常感谢!
页: [1] 2
查看完整版本: 无法运行命令