明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 695|回复: 5

[讨论] 为任意物体添加圆圈,非常好的函数,可惜括号出问题了

[复制链接]
发表于 2023-6-8 11:57 | 显示全部楼层 |阅读模式
本帖最后由 meja 于 2023-6-8 12:35 编辑

源码来自augi的论坛,不知道为何本地无法运行,请坛友清除BUG

  1. (vl-load-com)

  2. (defun c:cisel ( / *error* *adoc* 5px gr f ff k r n p pl ci cix s ss )

  3. (defun *error* ( msg )
  4.    (if (entget ci)
  5.      (entdel ci)
  6.    )
  7.    (vla-endundomark *adoc*)
  8.    (if msg
  9.      (prompt msg)
  10.    )
  11.    (princ)
  12. )

  13. (setq *adoc* (vla-get-activedocument (vlax-get-acad-object)))
  14. (vla-startundomark *adoc*)
  15. (setq 5px (* 5 (/ (getvar 'viewsize) (cadr (getvar 'screensize)))))
  16. (prompt "\n"-" reduce circle; "+" increase circle; left mouse click - brush down, second click - brush up; right mouse click - deselecting mode\nESC - finish")
  17. (while (or (= 5 (car (setq gr (grread t 5 1)))) (= 2 (car gr)) (= 3 (car gr)) (= 11 (car gr)) (= 25 (car gr)))
  18.    (if ci
  19.      (setq cix (entget ci))
  20.    )
  21.    (cond
  22.      ( (= (car gr) 3)
  23.        (if (null f)
  24.          (setq f t ff nil)
  25.          (setq f nil ff nil)
  26.        )
  27.      )
  28.      ( (or (= (car gr) 11) (= (car gr) 25))
  29.        (if (null ff)
  30.          (setq ff t f nil)
  31.          (setq ff nil f nil)
  32.        )
  33.      )
  34.    )
  35.    (if (null k)
  36.      (setq k 3)
  37.    )
  38.    (cond
  39.      ( (equal gr (list 2 (ascii "+")))
  40.        (setq k (1+ k))
  41.      )
  42.      ( (equal gr (list 2 (ascii "-")))
  43.        (setq k (1- k))
  44.        (if (zerop k)
  45.          (setq k 1)
  46.        )
  47.      )
  48.    )
  49.    (setq r (* k 5px))
  50.    (if (and ci (or (equal gr (list 2 (ascii "+"))) (equal gr (list 2 (ascii "-")))))
  51.      (setq cix (subst (cons 40 r) (assoc 40 cix) cix))
  52.    )
  53.    (setq n -1)
  54.    (if (= (car gr) 5)
  55.      (progn
  56.        (repeat 36
  57.          (setq p (polar (cadr gr) (* (setq n (1+ n)) (/ pi 18)) r))
  58.          (setq pl (cons p pl))
  59.        )
  60.        (if (null ci)
  61.          (setq ci (entmakex (list '(0 . "CIRCLE") (cons 10 (cadr gr)) (cons 40 r) '(62 . 2))))
  62.          (setq cix (subst (cons 10 (cadr gr)) (assoc 10 cix) cix))
  63.        )
  64.      )
  65.    )
  66.    (if pl
  67.      (progn
  68.        (if (setq s (ssget "_CP" pl))
  69.          (if ci
  70.            (ssdel ci s)
  71.          )
  72.          (setq s (ssadd))
  73.        )
  74.        (if (/= (sslength s) 0)
  75.          (progn
  76.            (setq cix (subst (cons 62 1) (assoc 62 cix) cix))
  77.            (cond
  78.              ( f
  79.                (setq ss (acet-ss-union (list s ss)))
  80.              )
  81.              ( ff
  82.                (setq ss (acet-ss-remove s ss))
  83.              )
  84.            )
  85.          )
  86.          (setq cix (subst (cons 62 2) (assoc 62 cix) cix))
  87.        )
  88.      )
  89.    )
  90.    (cond
  91.      ( f
  92.        (setq cix (subst (cons 62 (+ (cdr (assoc 62 cix)) 2)) (assoc 62 cix) cix))
  93.      )
  94.      ( ff
  95.        (setq cix (subst (cons 62 (+ (cdr (assoc 62 cix)) 4)) (assoc 62 cix) cix))
  96.      )
  97.    )
  98.    (if (and cix (not (equal cix (entget ci) 1e-))
  99.      (entupd (cdr (assoc -1 (entmod cix))))
  100.    )
  101.    (if ss
  102.      (sssetfirst nil ss)
  103.    )
  104.    (setq pl nil)
  105. )
  106. (*error* nil)
  107. )
  108.    (princ)
  109. )


 楼主| 发表于 2023-6-10 19:58 | 显示全部楼层
无法运行


  1. (defun pickset:to-list (ss)

  2.   (if ss (vl-remove-if-not (quote p:enamep)
  3.       (mapcar (quote cadr)
  4.         (ssnamex ss)))
  5.     nil))

  6. (defun p:vla-listp (lst)

  7.   (apply (quote and)
  8.     (mapcar (quote vlap)
  9.       lst)))

  10. (defun p:ename-listp (lst)
  11.   
  12.   (apply (quote and)
  13.     (mapcar (quote enamep)
  14.       lst)))

  15. (defun entity:getbox (ent offset / lst obj p1 p2 p3 p4)

  16.   (if (= (quote pickset)
  17.       (type ent))
  18.     (pickset:getbox ent offset)
  19.     (progn (setq obj (vlax-ename->vla-object ent))
  20.       (vla-getboundingbox obj (quote p1)
  21.         (quote p3))
  22.       (setq p1 (vlax-safearray->list p1)
  23.         p3 (vlax-safearray->list p3))
  24.       (if (= "SPLINE"
  25.           (cdr (assoc 0 (entget ent))))
  26.         (progn (setq lst (mapcar (quote (lambda (a b)
  27.                   (vlax-curve-getclosestpointtoprojection ent a b t)))
  28.               (list p1 (list (car p1)
  29.                   (cadr p3)
  30.                   (caddr p1))
  31.                 p3 (list (car p3)
  32.                   (cadr p1)
  33.                   (caddr p1)))
  34.               (quote ((1.0 0 0)
  35.                   (0 -1.0 0)
  36.                   (-1.0 0 0)
  37.                   (0 1.0 0)))))
  38.           (setq p1 (apply (quote mapcar)
  39.               (cons (quote min)
  40.                 lst))
  41.             p3 (apply (quote mapcar)
  42.               (cons (quote max)
  43.                 lst)))))
  44.       (if (or (not offset)
  45.           (equal offset 0 0.0001))
  46.         (list p1 p3)
  47.         (list (list:- p1 (list offset offset 0))
  48.           (list:+ p3 (list offset offset 0)))))))



  49. (defun pickset:getbox (ss offset / ptlist)

  50.   (cond ((= (quote pickset)
  51.         (type ss))
  52.       (setq ss (pickset:to-list ss)))
  53.     ((= (quote ename)
  54.         (type ss))
  55.       (setq ss (list ss)))
  56.     ((p:vla-listp ss)
  57.       (setq ss (mapcar (quote o2e)
  58.           ss))))
  59.   (if (p:ename-listp ss)
  60.     (progn (setq ptlist (apply (quote append)
  61.           (mapcar (quote (lambda (x)
  62.                 (entity:getbox x offset)))
  63.             ss)))
  64.       (list (apply (quote mapcar)
  65.           (cons (quote min)
  66.             ptlist))
  67.         (apply (quote mapcar)
  68.           (cons (quote max)
  69.             ptlist))))
  70.     (progn (@:log "ERROR"
  71.         "parameter is NOT pickset")
  72.       nil)))

  73. (defun point:mid (pt1 pt2)
  74.     "求两点 pt1 pt2 的中点"
  75.     "中点坐标"
  76.     (mapcar (quote (lambda (x y)
  77.                 (* 0.5 (+ x y))))
  78.         pt1 pt2))

  79. (defun entity:make-circle (pts-cen num-rad)
  80.   "创建圆.如果圆心是点的列表或半径是数值的列表,可以同时创建多个圆"
  81.   "Ename"
  82.   "(entity:make-circle (list (getpoint)(getpoint))
  83.     '(3 5))"
  84.   (cond ((and (= (quote point)
  85.           (type-of pts-cen))
  86.         (numberp num-rad))
  87.       (entmakex (list (quote (0 . "circle"))
  88.           (quote (100 . "AcDbEntity"))
  89.           (quote (100 . "AcDbCircle"))
  90.           (cons 10 pts-cen)
  91.           (cons 40 num-rad))))
  92.     ((and (listp pts-cen)
  93.         (apply (quote and)
  94.           (mapcar (function (lambda (x)
  95.                 (= (quote point)
  96.                   (type-of x))))
  97.             pts-cen)))
  98.       (foreach pt pts-cen (entity:make-circle pt num-rad)))
  99.     ((and (listp num-rad)
  100.         (apply (quote and)
  101.           (mapcar (quote numberp)
  102.             num-rad)))
  103.       (foreach rad num-rad (entity:make-circle pts-cen rad)))))

  104. (defun c:sscircle ( )
  105.   "将选择的图形用圆圈住"
  106.   ;; 选择图形
  107.   (setq ss (ssget ))
  108.   ;; 获取图形的包围盒
  109.   (setq box (pickset:getbox ss 0))
  110.   ;; 用包围盒的中心点与 中心点到角点为半径画圆
  111.   (entity:make-circle
  112.     (setq c (point:mid (car box)(cadr box)))
  113.     ((distance c (car box)))
  114.   ))




回复 支持 1 反对 0

使用道具 举报

发表于 2023-6-9 11:24 | 显示全部楼层
本帖最后由 vitalgg 于 2023-6-10 21:11 编辑

  1. (defun c:sscircle ()
  2.   "将选择的图形用圆圈住"
  3.   ;; 选择图形
  4.   (setq ss (ssget ))
  5.   ;; 获取图形的包围盒
  6.   (setq box (pickset:getbox ss 0))
  7.   ;; 用包围盒的中心点与 中心点到角点为半径画圆
  8.   (entity:make-circle
  9.     (setq c (point:mid (car box)(cadr box)))
  10.     (distance c (car box))
  11.   ))



本帖子中包含更多资源

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

x
 楼主| 发表于 2023-6-10 19:30 | 显示全部楼层
发表于 2023-6-10 21:11 | 显示全部楼层

(distance c (car box))

这一句多了一层括号。
 楼主| 发表于 2023-6-16 08:36 | 显示全部楼层
还是运行不起来
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-1 03:39 , Processed in 0.216906 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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