ILoveMadoka 发表于 2022-7-6 08:33:29

添加错误检查或条件

我有一个小应用程序(在这里的帮助下)我拼凑了起来。
我不知道如何添加错误检查(或条件),这样就不会出现未找到项的滚动错误。
 
此外,我认为一些项目可以合并为我的(ssget“x”)
但我也不清楚这是怎么回事。
 
请求调整我的代码或指导。
 
警告:我还是个新手。。。
 
这是我的代码:
 
(DEFUN C:SW1 ()
(Prompt "SolidWorks DWG Cleanup:")

(setq TEST (tblsearch "LAYER" "Dim"))
    (if (= TEST nil)
            (progn
               (Command "-layer" "n" "Dim" "C" "Red" "Dim" "")
            )
    )

(setq ss1 (ssget "x" '((0 . "INSERT")(2 . "*SW_CENTERMARKSYMBOL_*"))))
(Command "Erase" SS1 "")
(setq ss2 (ssget "x" '((0 . "INSERT")(2 . "*SW_NOTE_1*"))))
(Command "Erase" SS2 "")
(setq ss3 (ssget "x" '((0 . "INSERT")(2 . "*SW_TABLEANNOTATION_0*"))))
(Command "Erase" SS3 "")

(Command "-style" "Romans" "romans" "" "0.9" "" "" "n" "n")
(princ "\nRomans Text Style Created:")

(command "dimstyle" "r" "standard")
(command "dimtxsty" "romans")
(Command "dimstyle" "s" "standard" "y" )

(if (setq ss (ssget "x" '((0 . "DIMENSION"))))
   (repeat
   (setq i (sslength ss))
      (setq sset (ssname ss (setq i (1- i))))
      (if (not (eq (cdr (assoc 3 (setq e (entget sset)))) "Standard"))
      (entmod (subst (cons 3 "Standard") (assoc 3 e) e))
      )
   )
   (princ)
)

(setq ss4 (ssget "x" '((0 . "DIMENSION"))))
(Command "_CHANGE" SS4 "" "P" "LA" "DIM" "C" "BYLAYER" "")
(Command "_LAYER" "C" "GREEN" "X50" "C" "yellow" "TEXT" "")

(setq ss5 (ssget "x" '((6 . "PHANTOM"))))
(Command "_CHANGE" SS5 "" "P" "LA" "X25" "C" "BYLAYER" "LT" "PHANTOM" "")

(setq ss6 (ssget "x" '((6 . "HIDDEN"))))
(Command "_CHANGE" SS6 "" "P" "LA" "X25" "C" "BYLAYER" "LT" "HIDDEN" "")

(setq ss7 (ssget "x" '((6 . "DASHED"))))
(Command "_CHANGE" SS7 "" "P" "LA" "X25" "C" "BYLAYER" "LT" "DASHED" "")

(princ))

 
谢谢大家。。。

JohnM 发表于 2022-7-6 09:52:03

只是一些快速的想法来帮助你开始(但不是测试)
您会发现,您将编写的大多数代码都是错误检查
当你编写一段代码时,你应该考虑它可能崩溃或引起问题的所有方式。
通常情况下,可以使用if语句进行验证,但这完全取决于您正在尝试做什么。
 
总的错误陷阱总是一件值得学习的事情,因此当程序崩溃时,所有设置都会返回到原来的位置。
这个网站上有很多错误陷阱的例子,只要搜索一下就可以了
 
我希望这有帮助
 
 
 

(DEFUN C:SW1 ()
(Prompt "SolidWorks DWG Cleanup:")
(setq TEST (tblsearch "LAYER" "Dim"))
    (if (= TEST nil)
            (progn
               (Command "-layer" "n" "Dim" "C" "Red" "Dim" "")
            )
    )
(setq ss1 (ssget "x" '((0 . "INSERT")(2 . "*SW_CENTERMARKSYMBOL_*,*SW_NOTE_1*,*SW_TABLEANNOTATION_0*"))))
(if ss1
(Command "Erase" SS1 "")
);_if

;;;(setq ss2 (ssget "x" '((0 . "INSERT")(2 . "*SW_NOTE_1*"))))
;;;(Command "Erase" SS2 "")
;;;(setq ss3 (ssget "x" '((0 . "INSERT")(2 . "*SW_TABLEANNOTATION_0*"))))
;;;(Command "Erase" SS3 "")
;;; before you make a style use tblsearch to chaeck if it is there

(Command "-style" "Romans" "romans" "" "0.9" "" "" "n" "n")
(princ "\nRomans Text Style Created:")
;; also check for the below items
(command "dimstyle" "r" "standard")
(command "dimtxsty" "romans")
(Command "dimstyle" "s" "standard" "y" )
;****this is a good code usinf the if to error trap
(if (setq ss (ssget "x" '((0 . "DIMENSION"))))
   (repeat
   (setq i (sslength ss))
      (setq sset (ssname ss (setq i (1- i))))
      (if (not (eq (cdr (assoc 3 (setq e (entget sset)))) "Standard"))
      (entmod (subst (cons 3 "Standard") (assoc 3 e) e))
      )
   )
   (princ)
)
(setq ss4 (ssget "x" '((0 . "DIMENSION"))))
(if ss4 ;_us if to check if ss is good
(progn
(Command "_CHANGE" SS4 "" "P" "LA" "DIM" "C" "BYLAYER" "")
(Command "_LAYER" "C" "GREEN" "X50" "C" "yellow" "TEXT" "")
);_progn
);_if
(setq ss5 (ssget "x" '((6 . "PHANTOM,HIDDEN,DASHED"))))
(if ss5
(Command "_CHANGE" SS5 "" "P" "LA" "X25" "C" "BYLAYER" "LT" "PHANTOM" "")
);_if
;;;(setq ss6 (ssget "x" '((6 . "HIDDEN"))))
;;;(Command "_CHANGE" SS6 "" "P" "LA" "X25" "C" "BYLAYER" "LT" "HIDDEN" "")
;;;
;;;(setq ss7 (ssget "x" '((6 . "DASHED"))))
;;;(Command "_CHANGE" SS7 "" "P" "LA" "X25" "C" "BYLAYER" "LT" "DASHED" "")
(princ)
);_defun
页: [1]
查看完整版本: 添加错误检查或条件