明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 505|回复: 5

[函数] 求助 求教这两个函数冲突问题 先用WZ函数然后再用FA函数会报错 选择第一行:; ...

[复制链接]
发表于 2025-6-23 11:29:42 | 显示全部楼层 |阅读模式
本帖最后由 小王在学lisp 于 2025-6-23 11:36 编辑

(defun setupLayer (layerName / )
  (if (not (tblsearch "LAYER" layerName))
    (command "_.-LAYER" "_M" layerName "_C" "1" "" "")
  )
)

(defun is-close (a b tolerance / )
  (<= (abs (- a b)) tolerance)
)

(defun processCircles (text1 diameter-rules  TEXT66 / ss old-cmdEcho old-osmode count mark-count sorted-mark-count result final-result text1 pt tolerance H6)
  (setq tolerance 0.003) ; 设置容差值为0.003


  (setq H6 (* (getvar "dimscale") 2.5))


  (setq ss (ssget "_" '((0 . "CIRCLE"))))
  (if ss
    (progn
      (setq old-cmdEcho (getvar "cmdecho"))
      (setvar "cmdecho" 0)
      (setq old-osmode (getvar "osmode"))
      (setvar "osmode" 0)
      (setq count 0)
      (setq mark-count '())

      (repeat (sslength ss)
        (setq ent (ssname ss count))
        (setq rad (cdr (assoc 40 (entget ent))))
        (if rad
          (progn
            (setq diam (* 2 rad))
            (setq center (cdr (assoc 10 (entget ent))))
            (setq angle (/ pi 4))
            (setq new-point (list (+ (car center) (* (cos angle) (+ rad 0.5)))
                                  (+ (cadr center) (* (sin angle) (+ rad 0.5)))))
            (setupLayer "TEXT")
            
            ; 修改标记查找逻辑,使用容差值比较
            (setq mark nil)
            (setq description "")
            (foreach rule diameter-rules
              (if (is-close diam (car rule) tolerance)
                (setq mark (cadr rule)
                      description (caddr rule))
              )
            )
            
            (if (not mark) (setq mark "")) ; 如果没有找到标记,设为空字符串
            (princ (strcat "\nProcessing diameter: " (rtos diam 3 3) ", Mark: " mark)) ; 调试信息
            (if (not (equal mark ""))
              (progn
                (if (not (assoc mark mark-count))
                  (setq mark-count (cons (list mark 1 diam description) mark-count))
                  (progn
                    (setq existing (assoc mark mark-count))
                    (setq new-count (1+ (nth 1 existing)))
                    (setq mark-count (subst (list mark new-count diam description) existing mark-count))
                  )
                )
                (command "_.-text" new-point H6 "0" mark)
              )
            )
          )
        )
        (setq count (1+ count))
      )
      (setvar "osmode" old-osmode)
      (setvar "cmdecho" old-cmdEcho)
      (princ "\n标注已完成!\n")

      (setq sorted-mark-count (vl-sort mark-count '(lambda (a b) (< (car a) (car b)))))
      (setq result '())
      (foreach pair sorted-mark-count
        (setq result (cons (list (car pair) (nth 1 pair) (nth 2 pair) (nth 3 pair)) result))
      )
      (setq final-result (reverse result))
      (princ "\n标记、数量和直径列表:\n")
      (foreach item final-result
        (princ (strcat (car item) " -" (itoa (cadr item)) " -%%c" (rtos (caddr item) 2 3) "\n"))
      )
      (princ)

      ; 生成NOTES文本
     ; (setq text1 "NOTES:\n\n")
      
      ; 根据标记添加相应的文本,包含数量
      (foreach item final-result
        (setq mark (car item))
        (setq cnt (cadr item))
        (setq diam (caddr item))
        (setq desc (cadddr item))
        (setq text1 (strcat text1 mark ":" (itoa cnt) "-" desc "\n"))
      )
      
      ; 添加材质和圆角信息
      (setq text1 (strcat text1 text66))
      
      (vl-load-com)
      (setq pt (getpoint "\n请选择插入点:"))
      (command "_.MTEXT" pt "H" H6 "W" 0 text1 "")
    )
    (princ "\nNo circles selected.")
  )
  (princ)
)



(defun c:WZ ()
   (processCircles
   
     ""    ; 开头
    '(


      (3.00 "A" "%%C3销定孔加引导鱼眼")
      (4.00 "B" "%%C4销定孔加引导鱼眼")
      (8.00 "E" "%%C8销定孔加引导鱼眼")
      (2.50 "F" "M3 THR")
      (3.30 "G" "M4 THR")
      (4.20 "H" "M5 THR")
      (5.00 "I" "M6 THR")
      (6.75 "J" "M8 THR")
      (2.90 "K" "M2.5CB THR")
      (3.40 "L" "M3CB THR")
      (4.50 "M" "M4CB THR")
      (5.50 "N" "M5CB THR")
      (6.60 "O" "M6CB THR")
      (9.00"" "M8CB THR")


     )

  ""  ;结尾
  )
)



(defun c:fa (/ ca p p1 p2 x y ob ob1 ob2)
  (setvar "osmode" 32)
  (setvar "cmdecho" 0)
  (command "ucs" "w")
  (setq        l1   (car (entsel "Select fist line:"))
        tab1 (entget l1)
        l1p1 (cdr (assoc 10 tab1))
        l1p2 (cdr (assoc 11 tab1))
        ang1 (atof (angtos (angle l1p1 l1p2) 0 0))
        l2   (car (entsel "Select second line:"))
        tab2 (entget l2)
        l2p1 (cdr (assoc 10 tab2))
        l2p2 (cdr (assoc 11 tab2))
        ang2 (atof (angtos (angle l2p1 l2p2) 0 0))
        p    (inters l1p1 l1p2 l2p1 l2p2 nil)
        px   (car p)
        py   (cadr p)

  ) ;_ end of setq
  (if (or (= ang1 180) (= ang1 0))
    (setq vy  (cadr l2p1)
          hx  (car l1p1)
          hx1 (car l1p2)
    ) ;_ end of setq
    (setq vy  (cadr l1p1)
          hx  (car l2p1)
          hx1 (car l2p2)
    ) ;_ end of setq
  )                                        ;end if
  (setq ca (rtos (/ (min (abs (- px hx1)) (abs (- px hx))) 2)))
  (setvar "osmode" 0)
  (command "color" "red")

  (if (and (> hx px) (< vy py))
    (progn
      (command "line" p (strcat "@" ca ",0") "")
      (command "line" p (strcat "@0,-" ca) "")
    ) ;_ end of progn
  ) ;_ end of if
  (if (and (> hx px) (> vy py))
    (progn
      (command "line" p (strcat "@" ca ",0") "")
      (command "line" p (strcat "@0," ca) "")
    ) ;_ end of progn
  )                                        ;end if
  (if (and (< hx px) (< vy py))
    (progn
      (command "line" p (strcat "@-" ca ",0") "")
      (command "line" p (strcat "@0,-" ca) "")
    ) ;_ end of progn
  )                                        ;end if
  (if (and (< hx px) (> vy py))
    (progn
      (command "line" p (strcat "@-" ca ",0") "")
      (command "line" p (strcat "@0," ca) "")
    ) ;_ end of progn
  )                                        ;end if

  (command "color" "bylayer")
  (setvar "osmode" 679)
  (setvar "cmdecho" 1)
  (princ)
) ;_ end of defun

先用WZ函数然后再用FA函数会报错 选择第一行:; 错误: 函数错误: 0.785398

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

点评

行能把angle作变量?  发表于 2025-6-24 11:29
回复

使用道具 举报

发表于 2025-6-23 17:57:59 | 显示全部楼层
(defun c:fa ( / ang1 ang2 ca hx hx1 l1 l1p1 l1p2 l2 l2p1 l2p2 ll p px py tab1 tab2 vy)
  (setvar "osmode" 32)
  (setvar "cmdecho" 0)
  (command "ucs" "w")
  (if (setq l1 (entsel "Select fist line:"))
   (progn
  (setq l1 (car ll)
        tab1 (entget l1)
        l1p1 (cdr (assoc 10 tab1))
        l1p2 (cdr (assoc 11 tab1))
        ang1 (atof (angtos (angle l1p1 l1p2) 0 0))
        l2   (car (entsel "Select second line:"))
        tab2 (entget l2)
        l2p1 (cdr (assoc 10 tab2))
        l2p2 (cdr (assoc 11 tab2))
        ang2 (atof (angtos (angle l2p1 l2p2) 0 0))
        p    (inters l1p1 l1p2 l2p1 l2p2 nil)
        px   (car p)
        py   (cadr p)

  ) ;_ end of setq
  (if (or (= ang1 180) (= ang1 0))
    (setq vy  (cadr l2p1)
          hx  (car l1p1)
          hx1 (car l1p2)
    ) ;_ end of setq
    (setq vy  (cadr l1p1)
          hx  (car l2p1)
          hx1 (car l2p2)
    ) ;_ end of setq
  )                                        ;end if
  (setq ca (rtos (/ (min (abs (- px hx1)) (abs (- px hx))) 2)))
  (setvar "osmode" 0)
  (command "color" "red")

  (if (and (> hx px) (< vy py))
    (progn
      (command "line" p (strcat "@" ca ",0") "")
      (command "line" p (strcat "@0,-" ca) "")
    ) ;_ end of progn
  ) ;_ end of if
  (if (and (> hx px) (> vy py))
    (progn
      (command "line" p (strcat "@" ca ",0") "")
      (command "line" p (strcat "@0," ca) "")
    ) ;_ end of progn
  )                                        ;end if
  (if (and (< hx px) (< vy py))
    (progn
      (command "line" p (strcat "@-" ca ",0") "")
      (command "line" p (strcat "@0,-" ca) "")
    ) ;_ end of progn
  )                                        ;end if
  (if (and (< hx px) (> vy py))
    (progn
      (command "line" p (strcat "@-" ca ",0") "")
      (command "line" p (strcat "@0," ca) "")
    ) ;_ end of progn
  )                                        ;end if

  (command "color" "bylayer")
  (setvar "osmode" 679)
  (setvar "cmdecho" 1)
  ))
  (princ)
) ;_ end of defun
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-6-24 08:11:26 | 显示全部楼层
437271963 发表于 2025-6-23 17:57
(defun c:fa ( / ang1 ang2 ca hx hx1 l1 l1p1 l1p2 l2 l2p1 l2p2 ll p px py tab1 tab2 vy)
  (setvar "o ...


命令: FA
Select fist line:; 错误: 参数类型错误: lentityp nil

命令: *取消*

命令:  FA Select fist line:; 错误: 参数类型错误: lentityp nil    出现新的错误了
回复 支持 反对

使用道具 举报

发表于 2025-6-24 15:15:58 | 显示全部楼层
还能 (setq angle (/ pi 4)) ?
多看点相关的书吧!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-6-24 16:20:06 | 显示全部楼层
xyp1964 发表于 2025-6-24 15:15
还能 (setq angle (/ pi 4)) ?
多看点相关的书吧!

懂了,感谢佬指教
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-8-12 09:25 , Processed in 0.178836 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表