明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: 依然小小鸟

[提问] 任意曲线转多段线程序!

[复制链接]
发表于 2025-11-4 10:49:11 | 显示全部楼层
ET函数可以直接获取拟合点集合,直接连接点就是模拟曲线
尽量确保曲线有一部分屏幕可见,如果曲线可见的缩放太小,误差会比较大
(acet-list-remove-adjacent-dups
      (acet-geom-object-point-list
        crv
        (* 0.375 (acet-geom-pixel-unit))
      )
    )
回复 支持 反对

使用道具 举报

发表于 2025-11-6 19:12:45 | 显示全部楼层
感谢各位老师的指引
回复 支持 反对

使用道具 举报

发表于 2025-11-6 22:28:19 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-11-7 09:33:43 | 显示全部楼层
Sring65 发表于 2025-11-6 22:28
http://bbs.mjtd.com/thread-192143-1-1.html 试试这个的回复

跟我的不相关呢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-11-8 10:34:25 | 显示全部楼层
414249149 发表于 2025-10-17 18:09
http://bbs.mjtd.com/thread-191046-1-1.html

这个帖子里面程序的命令是啥呢 我用这个命令  CESPL2PL  用不了呢
回复 支持 反对

使用道具 举报

发表于 2025-11-12 17:05:05 | 显示全部楼层
本帖最后由 Sring65 于 2025-11-12 17:06 编辑


  1. (defun c:转化为多段线
  2.        (/ acadDoc ssg i pts ptmrg e lwPts tol entlist olst entl)
  3.   (defun *error* (msg)
  4.     (vla-endundomark acadDoc)
  5.     (if        (not
  6.           (wcmatch (strcase msg t) "*break *cancel* *exit* *取消*")
  7.         )
  8.       (princ (strcat "\n运行错误: " msg))
  9.     )
  10.     (princ)
  11.   )
  12.   (defun tan (x)
  13.     (/ (sin x) (cos x))
  14.   )
  15.   (setq acadDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  16.   (vla-StartUndoMark acadDoc)
  17.   (setq ssg (ssget '((0 . "CIRCLE,ARC,ELLIPSE,LINE,SPLINE,*POLYLINE"))))
  18.   (setq i -1)
  19.   (while (setq e (ssname ssg (setq i (1+ i))))
  20.     (setq lwPts (转化为多段线 e))
  21.     (if        (not (CheckPtLstclockwisep (mapcar 'car lwPts)))
  22.       (setq lwPts (lwplineReverse lwPts))
  23.     )
  24.     (setq pts (cons lwPts pts))
  25.   )
  26.   (setq tol 1)                                ;允许误差
  27.   (setq ptmrg (MergePline pts tol))  
  28.   (setq entlist (mapcar 'entmakeLWPOLYLINE ptmrg))
  29.   (vla-endundomark acadDoc)
  30.   (princ)
  31. )
  32. (defun 转化为多段线 (e / ename lst)
  33.   (setq ename (cdr (assoc 0 (entget e))))
  34.   (setq        lst
  35.          (cond ((= ename "CIRCLE") (CircleToBulgePolyline e))
  36.                ((= ename "ARC") (ArcToBulgePolyline e))
  37.                ((= ename "ELLIPSE") (ellipseToBulgePolyline e))
  38.                ((= ename "LINE") (LineToBulgePolyline e))
  39.                ((= ename "SPLINE") (SPLINEToBulgePolyline e))
  40.                ((wcmatch ename "*POLYLINE") (POLYLINEToBulgePolyline e))
  41.          )
  42.   )
  43.   (if lst
  44.     (PLlstremoveRepeat lst)
  45.   )
  46. )
  47.   (defun PLlstremoveRepeat (lst / i res p1 p2 p3 b c1 c2)
  48.     (setq i 1)
  49.     (setq p1 (car lst))
  50.     (setq p2 (cadr lst))
  51.     (while (setq p3 (nth (setq i (1+ i)) lst))
  52.       (cond ((and (= 0 (cadr p1)) (= 0 (cadr p2)))
  53.              (if (equal (getangles (car p1) (car p2) (car p3)) pi 1e-8)
  54.                nil
  55.                (setq res (appenda res p1)
  56.                      p1         p2
  57.                )
  58.              )
  59.             )
  60.             ((and (/= 0 (cadr p1)) (/= 0 (cadr p2)))
  61.               (setq c1 (getCircleCenterByPtsBulge
  62.                          (car p1)
  63.                          (car p2)
  64.                          (cadr p1)
  65.                        )
  66.               )
  67.               (setq c2 (getCircleCenterByPtsBulge
  68.                          (car p2)
  69.                          (car p3)
  70.                          (cadr p2)
  71.                        )
  72.               )
  73.               (if (equal c1 c2 1e-8)
  74.                 (progn
  75.                   (if (> (cadr p1) 0)
  76.                     (setq b (getangles (car p1) c1 (car p3)))
  77.                     (setq b (- (getangles (car p3) c1 (car p1))))
  78.                   )
  79.                   (setq p1 (Put-IndexValue p1 1 (tan (* 0.25 b))))
  80.                 )
  81.                 (setq res (appenda res p1)
  82.                       p1  p2
  83.                 )
  84.               )
  85.             )
  86.             (t
  87.              (setq res (appenda res p1)
  88.                    p1  p2
  89.              )
  90.             )
  91.       )
  92.       (setq p2 p3)
  93.     )
  94.     (setq res (append res (list p1 p2)))
  95.   )

  96. (defun Put-IndexValue (lst n va / i)
  97.   (setq i -1)
  98.   (mapcar
  99.     '(lambda (a)
  100.        (if (= n (setq i (1+ i)))
  101.          va
  102.          a
  103.        )
  104.      )
  105.     lst
  106.   )
  107. )
  108. (defun vlaOffsetObjs (objs len / lst)
  109.   (setq objs (ename->object objs))
  110.   (setq        lst (if        (= (type objs) 'LIST)
  111.               (apply
  112.                 'append
  113.                 (mapcar        '(lambda (a)
  114.                            (safearray->List (vla-Offset a len))
  115.                          )
  116.                         objs
  117.                 )
  118.               )
  119.               (safearray->List (vla-Offset objs len))
  120.             )
  121.   )
  122.   (vl-remove-if
  123.     'null
  124.     (mapcar '(lambda (e)
  125.                (if (vlax-erased-p e)
  126.                  nil
  127.                  e
  128.                )
  129.              )
  130.             lst
  131.     )
  132.   )
  133. )
  134. (defun safearray->List (s / i lst l e)
  135.   (if (= 'variant (type s))
  136.     (setq s (vlax-variant-value s))
  137.   )
  138.   (setq i -1)
  139.   (setq l (vlax-safearray-get-u-bound s 1))
  140.   (while (<= (setq i (1+ i)) l)
  141.     (setq e (vlax-safearray-get-element s i))
  142.     (setq lst (cons e lst))
  143.   )
  144.   lst
  145. )
  146. (defun entmakeLWPOLYLINE (pts / e)
  147.   (setq e (entlast))
  148.   (entmake
  149.     (append
  150.       (list
  151.         '(0 . "LWPOLYLINE")
  152.         '(100 . "AcDbEntity")
  153.         '(100 . "AcDbPolyline")
  154.         (cons 90 (length pts))                ; 点的数量
  155.                                         ; 闭合标志
  156.         (if (equal (caar pts) (car (last pts)) 1e-8)
  157.           (cons 70 1)
  158.           (cons 70 0)
  159.         )
  160.       )
  161.       (apply 'append
  162.              (mapcar
  163.                '(lambda        (a)                ; 这里加上了单引号
  164.                   (list        (cons 10 (car a))
  165.                         (cons 42 (cadr a))
  166.                   )
  167.                 )                        ; 每个点和 bulge
  168.                pts
  169.              )
  170.       )
  171.     )
  172.   )
  173.   (entnext e)
  174. )
  175. (defun CircleToBulgePolyline (ent     /              edata   center  radius
  176.                               ang     points  bulge   i              pt1
  177.                               pt2     points
  178.                              )
  179.   (setq edata (entget ent))
  180.   (setq center (cdr (assoc 10 edata)))
  181.   (setq radius (cdr (assoc 40 edata)))
  182.   (setq bulge (tan (/ pi 4)))                ; tan(45°) = 1.0
  183.   (setq points '())
  184.   (setq i 0)
  185.   (while (< i 3)
  186.     (setq pt (polar center (* i pi) radius))
  187.     (setq points (append points (list (list pt bulge))))
  188.     (setq i (1+ i))
  189.   )
  190.   points
  191. )
  192. (defun tan (x)
  193.   (/ (sin x) (cos x))
  194. )
  195. (defun POLYLINEToBulgePolyline (ent / pts p ptsOut i)
  196.   (setq        pts (vl-remove-if
  197.               'null
  198.               (mapcar
  199.                 '(lambda (x)
  200.                    (cond ((= (car x) 10) (cdr x))
  201.                          ((= (car x) 42) (cdr x))
  202.                    )
  203.                  )
  204.                 (entget ent)
  205.               )
  206.             )
  207.   )
  208.   (if (/= 'LIST (type (cadr pts)))
  209.     (progn (setq i -2)
  210.            (while (setq p (nth (setq i (+ 2 i)) pts))
  211.              (setq ptsOut (append ptsOut (list (list p (nth (1+ i) pts)))))
  212.            )
  213.     )
  214.     pts
  215.   )
  216. )
  217. (defun getspPolyParamlist (obj pms pme / a1 a2 a3 pmc gx)
  218.   (setq pmc (* 0.5 (+ pms pme)))
  219.   (setq gx 0.05)
  220.   (if (EQUAL pms pmc gx)
  221.     (list pms pme)
  222.     (progn
  223.       (setq a1 (angle '(0 0) (vlax-curve-getFirstDeriv obj pms)))
  224.       (setq a2 (angle '(0 0) (vlax-curve-getFirstDeriv obj pmc)))
  225.       (setq a3 (angle '(0 0) (vlax-curve-getFirstDeriv obj pme)))
  226.       (if (and (or (equal a1 a2 gx) (equal (abs (- a1 a2)) pi2 gx))
  227.                (or (equal a3 a2 gx) (equal (abs (- a2 a3)) pi2 gx))
  228.           )
  229.         (list pms pme)
  230.         (append        (getspPolyParamlist obj pms pmc)
  231.                 (cdr (getspPolyParamlist obj pmc pme))
  232.         )
  233.       )
  234.     )
  235.   )
  236. )
  237. (defun SPLINEToBulgePolyline (ent / pts p ptsOut i p1 p2 p3)
  238.   (setq pi2 (+ pi pi))
  239.   (setq        mlst (getspPolyParamlist
  240.                ent
  241.                (vlax-curve-getStartParam ent)
  242.                (vlax-curve-getEndParam ent)
  243.              )
  244.   )
  245.   (setq        mlst
  246.          (mapcar
  247.            '(lambda (a b)
  248.               (setq p1 (vlax-curve-getPointAtParam ent a))
  249.               (setq p2 (vlax-curve-getPointAtParam ent (* 0.5 (+ a b))))
  250.               (setq p3 (vlax-curve-getPointAtParam ent b))
  251.               (if (setq pc (LM:3pcircle p1 p2 p3))
  252.                 (progn (setq a (getangles p1 pc p3))
  253.                        (if (> a pi)
  254.                          (setq a (- a pi pi))
  255.                        )
  256.                        (list p1 (tan (/ a 4)))
  257.                 )
  258.                 (list p1 0)
  259.               )
  260.             )
  261.            mlst
  262.            (append (cdr mlst) (list (vlax-curve-getEndParam ent)))
  263.          )
  264.   )  
  265.   (if (vlax-curve-isClosed ent)
  266.     (setq mlst (append mlst (list (list (vlax-curve-getPointAtParam ent 0) 0))))
  267.   )
  268.   mlst
  269. )
  270. (defun LineToBulgePolyline (ent / edata s e)
  271.   (setq edata (entget ent))
  272.   (list        (list (cdr (assoc 10 edata)) 0)
  273.         (list (cdr (assoc 11 edata)) 0)
  274.   )
  275. )
  276. (defun ArcToBulgePolyline (ent            /             edata    startPt  endPt
  277.                            center   radius   startAng endAng   bulge
  278.                            segments angleDiff              pts      i
  279.                            pt
  280.                           )
  281.   (setq edata (entget ent))
  282.   ;; 获取弧线的起点、终点、圆心、半径、角度
  283.   (setq center (cdr (assoc 10 edata)))        ; 圆心
  284.   (setq radius (cdr (assoc 40 edata)))        ; 半径
  285.   (setq startAng (cdr (assoc 50 edata))) ; 起始角度
  286.   (setq endAng (cdr (assoc 51 edata)))        ; 结束角度


  287.   (setq        angleDiff (if (< endAng startAng)
  288.                     (- (+ endAng (* 2 pi)) startAng)
  289.                     (- endAng startAng)
  290.                   )
  291.   )
  292.   (setq bulge (tan (/ angleDiff 4)))
  293.   (list        (list (polar center startAng radius) bulge)
  294.         (list (polar center endAng radius) bulge)
  295.   )
  296. )
  297. (defun ellipseToBulgePolyline (ent    /             isMirr edata  center
  298.                                ang    a             b            s           e
  299.                                n      theta  delta  points i
  300.                                pts    isMirr
  301.                               )
  302.   (setq edata (entget ent))
  303.   ;; 获取弧线的起点、终点、圆心、半径、角度
  304.   (setq center (cdr (assoc 10 edata)))        ; 圆心
  305.   (setq ang (angle '(0 0 0) (cdr (assoc 11 edata)))) ;旋转角度
  306.   (setq a (distance '(0 0 0) (cdr (assoc 11 edata)))) ; 半径
  307.   (setq b (* a (cdr (assoc 40 edata))))
  308.   (setq s (cdr (assoc 41 edata)))        ; 起始角度
  309.   (setq e (cdr (assoc 42 edata)))        ; 结束角度
  310.   (setq n 64)
  311.   (setq isMirr (< (caddr (cdr (assoc 210 edata))) 0.0))
  312.   (if (> s e)
  313.     (setq e (+ e pi pi))
  314.   )
  315.   (setq theta 0)                        ; 初始化角度
  316.   (setq delta (/ (* 2 pi) n))                ; 计算每个增量的角度
  317.   (setq points '())                        ; 存储点的列表
  318.   (setq
  319.     points (cons (list (list (* a (cos s)) (* b (sin s))) s) points)
  320.   )
  321.                                         ; 将点添加到列表
  322.   (setq i -1)
  323.   (while (< (setq i (1+ i)) n)
  324.     (if        (> theta s)
  325.       (setq points
  326.              (cons (list (list (* a (cos theta)) (* b (sin theta))) theta)
  327.                    points
  328.              )
  329.       )                                        ; 将点添加到列表
  330.     )                                        ; 增加角度
  331.     (if        (> (setq theta (+ theta delta)) e)
  332.       (setq i n)
  333.     )
  334.   )
  335.   (setq
  336.     points (cons (list (list (* a (cos e)) (* b (sin e))) e) points)
  337.   )
  338.                                         ; 将点添加到列表
  339.   (mapcar
  340.     '(lambda (x y)                        ; 这里加上了单引号
  341.        (list (ellipsePointRotate '(0 0) center (car x) ang isMirr)
  342.              (if isMirr
  343.                (- (get-ellipse-Bulge a b y x))
  344.                (get-ellipse-Bulge a b y x)
  345.              )
  346.        )
  347.      )                                        ; 每个点和 bulge
  348.     points
  349.     (append (cdr points) (list (car points)))
  350.   )
  351. )
  352. ;;;判断椭圆是否镜像
  353. (defun is-ellipse-mirrored (ent)
  354.   (if (and ent (= (cdr (assoc 0 (entget ent))) "ELLIPSE"))
  355.     (if        (< (caddr (cdr (assoc 210 (entget ent)))) 0.0) ; Z方向为负
  356.       T                                        ; 是镜像的
  357.       nil                                ; 不是镜像的
  358.     )
  359.   )
  360. )
  361. ;;;判断是否顺时针
  362. (defun CheckPtLstclockwisep (lst / l2)
  363.   (defun calo2A        (i j)
  364.     (- (* (car i) (cadr j)) (* (car j) (cadr i)))
  365.   )
  366.   (setq l2 (append (cdr lst) (list (car lst))))
  367.   (< (apply '+ (mapcar 'calo2A lst l2)) 1e-8)
  368. )

  369. (defun get-ellipse-Bulge (a b x y / c p0 s e pc)
  370.   (setq c (* 0.5 (+ (cadr y) (cadr x))))
  371.   (setq pc (list (* a (cos c)) (* b (sin c))))
  372.   (if (setq p0 (LM:3pcircle (car x) pc (car y)))
  373.     (progn
  374.       (setq s (angle p0 (car x)))
  375.       (setq e (angle p0 (car y)))
  376.       (if (< e s)
  377.         (setq e (+ e pi pi))
  378.       )
  379.       (tan (* -0.25 (- e s)))
  380.     )
  381.     0
  382.   )
  383. )
  384. (defun ellipsePointRotate (p1 P2 Pm ang isMirr / a)
  385.   (if isMirr
  386.     (setq a (- ang (angle p1 pm)))
  387.     (setq a (+ ang (angle p1 pm)))
  388.   )
  389.   (mapcar '+ p2 (polar p1 a (distance p1 pm)))
  390. )
  391. (defun LM:3pcircle (pt1 pt2 pt3 / a b c d)
  392.   (setq        pt2 (mapcar '- pt2 pt1)
  393.         pt3 (mapcar '- pt3 pt1)
  394.         a   (* 2.0
  395.                (- (* (car pt2) (cadr pt3)) (* (cadr pt2) (car pt3)))
  396.             )
  397.         b   (distance '(0.0 0.0) pt2)
  398.         c   (distance '(0.0 0.0) pt3)
  399.         b   (* b b)
  400.         c   (* c c)
  401.   )
  402.   (if (/= a 0)
  403.     (mapcar '+
  404.             pt1
  405.             (list
  406.               (/ (- (* (cadr pt3) b) (* (cadr pt2) c)) a)
  407.               (/ (- (* (car pt2) c) (* (car pt3) b)) a)
  408.               0
  409.             )
  410.     )
  411.   )
  412. )
  413. (defun sortByAngle-i (pt0 pt1 ptxlst / angb anga ang0)
  414.   (vl-sort-i ptxlst
  415.              '(lambda (a b)
  416.                 (< (getAngles pt0 pt1 a) (getAngles pt0 pt1 b))
  417.               )
  418.   )
  419. )
  420. (defun sortByAngle (pt0 pt1 ptxlst / angb anga ang0)
  421.   (vl-sort ptxlst
  422.            '(lambda (a b)
  423.               (< (getAngles pt0 pt1 a) (getAngles pt0 pt1 b))
  424.             )
  425.   )
  426. )
  427. (defun sortByDistance (p ptxlst)
  428.   (vl-sort ptxlst
  429.            '(lambda (a b) (< (distance p a) (distance p b)))
  430.   )
  431. )
  432. (defun sortByDistance-i        (p ptxlst)
  433.   (vl-sort-i ptxlst
  434.              '(lambda (a b) (< (distance p a) (distance p b)))
  435.   )
  436. )
  437. ;;;中心点
  438. (defun getPtsCenter (pts / l _f)
  439.   (setq l (length pts))
  440.   (defun _f (a) (/ a l))
  441.   (setq pts (apply 'mapcar (cons '+ pts)))
  442.   (mapcar '_f pts)
  443. )
  444. (defun MergePline (all-lines tol / n1 rs)
  445.   (while all-lines
  446.     (setq n1 (car all-lines))
  447.     (setq all-lines (cdr all-lines))
  448.     (setq rs (cons (MergePlineNear n1 tol) rs))
  449.   )
  450.   rs
  451. )
  452. (defun MergeOutPline (all-lines tol / n1 rs r1 r2 pts ps si tmp)
  453.   (if all-lines
  454.     (progn
  455.       (setq pts (mapcar '(lambda (a) (mapcar 'car a)) all-lines))
  456.       (setq pts (mapcar 'getPtsCenter pts))
  457.       (setq ptn (mapcar '- (apply 'mapcar (cons 'min pts)) '(1 1 1)))
  458.       (setq si (sortByDistance-i ptn pts))
  459.       (setq n1 (nth (car si) all-lines))
  460.       (setq all-lines (vl-remove n1 all-lines))
  461.       (setq tmp all-lines)
  462.       (setq r1 (list (MergePlineNear n1 tol)))
  463.       (setq all-lines tmp)
  464.       (setq r2 (list (MergePlineNear (lwplineReverse n1) tol)))
  465.       (if (> (length (car r2)) (length (car r1)))
  466.         r2
  467.         r1
  468.       )
  469.     )
  470.   )
  471. )
  472. (defun getPtNth2 (s r / p ret)
  473.   (if r
  474.     (setq s (reverse s))
  475.   )
  476.   (setq p (caar s))
  477.   (setq ret (caar (setq s (cdr s))))
  478.   (while (and (equals p ret 1e-6)
  479.               (setq s (cdr s))
  480.          )
  481.     (setq ret (caar s))
  482.   )
  483.   ret
  484. )
  485. ;;查找点附近的
  486. (defun MergeNextPtNear
  487.                        (lst p0 ps tol clk-p / d        x l res        pts i n1 n0
  488.                         chklp ptmin ptn)
  489.   (setq d tol)
  490.   (mapcar
  491.     '(lambda (x)
  492.        (setq l (distance ps (caar x)))
  493.        (if (and (< l tol) (or (< l d) (equal d l 1e-6)))
  494.          (if (equal d l 1e-6)
  495.            (setq res (cons (list x nil) res)
  496.                  pts (cons (getPtNth2 x nil) pts)
  497.            )
  498.            (setq d   l
  499.                  res (list (list x nil))
  500.                  pts (list (getPtNth2 x nil))
  501.            )
  502.          )
  503.        )
  504.        (setq l (distance ps (car (last x))))
  505.        (if (and (< l tol) (or (< l d) (equal d l 1e-6)))
  506.          (if (equal d l 1e-6)
  507.            (setq res (cons (list x t) res)
  508.                  pts (cons (getPtNth2 x t) pts)
  509.            )
  510.            (setq d   l
  511.                  res (list (list x t))
  512.                  pts (list (getPtNth2 x t))
  513.            )
  514.          )
  515.        )
  516.      )
  517.     lst
  518.   )
  519.   (if (> (length pts) 1)
  520.     (progn
  521.       (setq ptsr (sortByAngle-i p0 ps pts))
  522.       (if clk-p
  523.         (setq res (nth (last ptsr) res))
  524.         (setq res (nth (car ptsr) res))
  525.       )
  526.     )
  527.     (setq res (car res))
  528.   )
  529.   res                                        ;返回相邻列表,和是否反向
  530. )
  531. ;;;查找相邻
  532. (defun MergePlineNear
  533.        (frst tol / res _f r lastf chkpt ptsr clk-p p rList)
  534.   (defun _f (ps / res)
  535.     (if        (setq res (MergeNextPtNear all-lines chkpt ps tol clk-p))
  536.       (progn
  537.         (setq all-lines (vl-remove (car res) all-lines))
  538.         (if (cadr res)
  539.           (lwplineReverse (car res))
  540.           (car res)
  541.         )
  542.       )
  543.     )
  544.   )
  545.   (while (and
  546.            (setq p (caar frst))
  547.            (not (member p rList))
  548.            (not (< (distance p (car (last frst))) tol))
  549.            (setq chkpt (getPtNth2 frst nil))
  550.            (setq r (_f p))
  551.          )                                ;检查起点
  552.     (setq rList (cons p rList))

  553.     (if        (equals (caar r) p 1e-6)
  554.       (setq r (lwplineReverse r)
  555.             r (vl-remove (last r) r)
  556.       )
  557.       (setq r (lwplineReverse r))
  558.     )
  559.     (setq frst (append r frst))
  560.   )
  561.   (setq clk-p t)
  562.   (while (and
  563.            (setq p (car (last frst)))
  564.            (not (member p rList))
  565.            (not (< (distance (caar frst) p) tol))
  566.            (setq chkpt (getPtNth2 frst t))
  567.            (setq r (_f p))
  568.          )                                ;检查终点位置
  569.     (setq rList (cons p rList))
  570.     (if        (equals (caar r) p 1e-6)
  571.       (setq frst (vl-remove (last frst) frst))
  572.     )
  573.     (setq frst (append frst r))
  574.   )
  575.   (if (and (< (distance (caar frst) (car (last frst))) tol)
  576.            (not (equal (caar frst) (car (last frst)) 1e-6))
  577.       )
  578.     (setq frst (cons (last frst) frst))
  579.   )
  580.   frst
  581. )
  582. (defun lwplineReverse (pts)
  583.   (setq pts (Reverse pts))
  584.   (mapcar
  585.     '(lambda (a b) (list (car a) (- (cadr b))))
  586.     pts
  587.     (append (cdr pts) (list (car pts)))
  588.   )
  589. )

  590. ;;;三维点集转点集合
  591. (defun Arr3dToPtlst (tmp / lst)
  592.   (while tmp
  593.     (setq lst (appenda lst (list (car tmp) (cadr tmp) (caddr tmp)))
  594.           tmp (cdddr tmp)
  595.     )
  596.   )
  597.   lst
  598. )
  599. (defun Arr2dToPtlst (tmp / lst)
  600.   (while tmp
  601.     (setq lst (appenda lst (list (car tmp) (cadr tmp) 0))
  602.           tmp (cddr tmp)
  603.     )
  604.   )
  605.   lst
  606. )

  607. (defun getCircleCenterByPtsBulge (pt1 pt2 bulge / ptc x1 x2 y1 y2 b)
  608.   (setq        x1  (car pt1)
  609.         y1  (cadr pt1)
  610.         x2  (car pt2)
  611.         y2  (cadr pt2)
  612.         b   (* 0.5 (- (/ 1 bulge) bulge))
  613.         ptc (list (* 0.5 (+ x1 x2 (- (* b (- y2 y1)))))
  614.                   (* 0.5 (+ y1 y2 (* b (- x2 x1))))
  615.                   0
  616.             )
  617.   )
  618. )
  619. (defun getAngles (pt1 pt2 pt3 / ang a1 a2)
  620.   (if (or (equal pt2 pt1 1e-6) (equal pt2 pt3 1e-6))
  621.     (+ pi pi)
  622.     (progn
  623.       (setq ang (- (Angle pt2 pt3) (Angle pt2 pt1)))
  624.       (if (< ang 0)
  625.         (setq ang (+ ang pi pi))
  626.       )
  627.       (if (equal ang 0 1e-6)
  628.         (+ pi pi)
  629.         ang
  630.       )
  631.     )
  632.   )
  633. )
  634. (defun Equals (a b p)
  635.   (vl-every '(lambda (x y) (equal x y p)) a b)
  636. )
  637. (defun checkPtInArc (ptx ptCircleCenter p1 p2 / r x angx ang1 ang2)
  638.   (if (not (And (Equals p1 p2 1e-6) (Equals ptx p2)))
  639.     (progn
  640.       (setq r (Distance ptx ptCircleCenter))
  641.       (setq x (Distance p1 ptCircleCenter))
  642.       (If (Equal r x 1e-6)
  643.         (progn
  644.           (setq        angx (getAngles p1 ptCircleCenter ptx)
  645.                 ang1 (getAngles p1 ptCircleCenter p2)
  646.           )
  647.           (if (> ang1 angx)
  648.             t
  649.             nil
  650.           )
  651.         )
  652.       )
  653.     )
  654.   )
  655. )
  656. (defun checkPtInPtlst (pt pts)
  657.   (equal (getangles (car pts) pt (cadr pts)) pi 1e-6)
  658. )
  659. (defun ArraySort (sortIdx lst)
  660.   (mapcar '(lambda (n) (nth n lst)) (VL-SORT-I sortIdx '<))
  661. )
  662. (defun BulgeFromArc (ps pe pc bulge / a)
  663.   (setq a (getangles ps pc pe))
  664.   (if (> bulge 0)
  665.     (tan (* 0.25 a))
  666.     (tan (* 0.25 (- a pi pi)))
  667.   )
  668. )
  669. (defun set3dPtZBy2Pt (pt ptr pte / le lt zr ze z)
  670.   (setq        zr (caddr ptr)
  671.         ze (caddr pte)
  672.   )
  673.   (if (and (= zr 0) (= 0 pte))
  674.     (list (car pt) (cadr pt) 0)
  675.     (progn
  676.       (setq le (Distance ptr pte)
  677.             lt (Distance ptr pt)
  678.       )
  679.       (If (/= le 0)
  680.         (setq z (- zr (* lt (/ (- zr ze) le))))
  681.         (setq z zr)
  682.       )
  683.       (list (car pt) (cadr pt) z)
  684.     )
  685.   )
  686. )
  687. (defun getPLCenterPt (pt1 pt2 b / pt)
  688.   (setq pt (mapcar '* '(0.5 0.5 0.5) (mapcar '+ pt1 pt2)))
  689.   (If (Equals pt pt1 1e-6)
  690.     pt
  691.     (Polar pt
  692.            (- (Angle pt1 pt2) (* 0.5 pi))
  693.            (* (Distance pt1 pt2) 0.5 b)
  694.     )
  695.   )
  696. )
  697. (defun appenda (lst a)
  698.   (append lst (list a))
  699. )
  700. (defun ptTo3D (pt)
  701.   (if (= (type (car pt)) 'LIST)
  702.     (mapcar 'ptTo3D pt)
  703.     (if        (car pt)
  704.       (list (car pt)
  705.             (cadr pt)
  706.             (if        (caddr pt)
  707.               (caddr pt)
  708.               0
  709.             )
  710.       )
  711.       nil
  712.     )
  713.   )
  714. )
  715. (defun ptTo2D (pt)
  716.   (if (= (type pt) 'LIST)
  717.     (if        (= (type (car pt)) 'LIST)
  718.       (mapcar 'ptTo2D pt)
  719.       (list (car pt) (cadr pt))
  720.     )
  721.     (progn pt)
  722.   )
  723. )

  724. (defun Plpts2DToArr (pts / l points)
  725.   (setq pts (apply 'append (mapcar 'ptto2d pts)))
  726.   (setq l (cons 0 (1- (length pts))))
  727.   (setq points (vlax-make-safearray vlax-vbDouble l))
  728.   (vlax-safearray-fill points pts)
  729. )
  730. (defun Plpts3DToArr (pts / l points)
  731.   (setq pts (apply 'append (mapcar 'ptto3d pts)))
  732.   (setq l (cons 0 (1- (length pts))))
  733.   (setq points (vlax-make-safearray vlax-vbDouble l))
  734.   (vlax-safearray-fill points pts)
  735. )

  736. (defun ename->object (s / e lst i _f)
  737.   (defun _f (e)
  738.     (if        (= 'ENAME (type e))
  739.       (vlax-ename->vla-object e)
  740.       e
  741.     )
  742.   )
  743.   (setq i -1)
  744.   (if (= (type s) 'PICKSET)
  745.     (while (setq e (ssname s (setq i (1+ i))))
  746.       (setq lst (cons (_f e) lst))
  747.     )
  748.     (if        (= (type s) 'LIST)
  749.       (mapcar 'ename->object s)
  750.       (_f s)
  751.     )
  752.   )
  753. )
  754. (defun cutPolylineByObjs (ssg / i j tmp e f ptsArr objlst)
  755.   (setq i -1)
  756.   (setq objlst (ename->object ssg))
  757.   (setq i -1)
  758.   (while (setq e (nth (setq i (1+ i)) objlst))
  759.     (setq j i)
  760.     (while (setq f (nth (setq j (1+ j)) objlst))
  761.       (if (setq tmp (vlax-invoke e 'intersectwith f acextendnone))
  762.         (setq ptsArr (append ptsArr tmp))
  763.       )
  764.     )
  765.   )  
  766.   (if (> (Length ptsArr) 0)
  767.     (progn
  768.       (setq ptsArr (Arr3dToPtlst ptsArr))
  769.       (apply 'append
  770.              (mapcar '(lambda (e) (cutPolyline e ptsArr))
  771.                      objlst
  772.              )
  773.       )
  774.     )
  775.   )
  776. )
  777. (defun cutPolyline (obj               breakPtlst /             ptsArr
  778.                     jn               EntityName StartWidth i
  779.                     j               L          Crdnj             Crdn
  780.                     notDeleteObj          ptxbulge   flg_Bg
  781.                     ix               ptIx          flg_Wd     ib
  782.                     Ent               plineLength             ptxEndbulge
  783.                     widthxS    widthxE          EndWidth   widthx
  784.                     ptxLst     distAng          ang12             ptxLstDist
  785.                     isInArc    ptCircleCenter             bulge
  786.                     cpobj      p1          p2             ptr
  787.                     pte               ptx          ptc             Coordinates
  788.                     newCrdnt   Crdnt0          CrdntLst   bulgeLst
  789.                     newBulges  widthLst          newWidths  dpz
  790.                     tmp               bCadCutCheckIn             CrdnCar
  791.                    )
  792.   (setq obj (ename->object obj))
  793.   (if (> (Length breakPtlst))
  794.     (progn
  795.       (setq ptsArr breakPtlst)
  796.       (setq EntityName
  797.              (vla-get-ObjectName obj)
  798.       )
  799.       (if (= EntityName "AcDbPolyline")
  800.         (setq Coordinates (Arr2dToPtlst (vlax-get obj 'Coordinates)))
  801.         (setq Coordinates (Arr3dToPtlst (vlax-get obj 'Coordinates)))
  802.       )
  803.       (setq jn 0)
  804.       (If (= (vla-get-closed obj) :vlax-true)
  805.         (if (not (Equal        (car Coordinates)
  806.                         (last Coordinates)
  807.                         1e-6
  808.                  )
  809.             )
  810.           (setq        Coordinates
  811.                  (append Coordinates (list (car Coordinates)))
  812.           )
  813.         )
  814.         (setq ptr (car Coordinates)
  815.               pte (last Coordinates)
  816.         )
  817.       )
  818.       (setq i -1)
  819.       (while (and (setq p1 (nth (setq i (1+ i)) Coordinates))
  820.                   (setq p2 (nth (1+ i) Coordinates))
  821.              )
  822.         (If (not (Equal p1 p2 1e-6))
  823.           (progn
  824.             (setq bulge (vla-GetBulge obj i))
  825.             (vla-GetWidth obj i 'StartWidth 'EndWidth)
  826.             (If        (/= StartWidth EndWidth)
  827.               (setq flg_Wd t)
  828.             )
  829.             (If        (/= bulge 0)
  830.               (progn
  831.                 (setq flg_Bg t)
  832.                 (setq ptCircleCenter
  833.                        (getCircleCenterByPtsBulge p1 p2 bulge)
  834.                 )
  835.                 (setq j (1- jn))
  836.                 ;;检查每个交点 是否在多段线节点线段上
  837.                 (while (setq ptx (nth (setq j (1+ j)) ptsArr))
  838.                   (If (> bulge 0)
  839.                     (setq
  840.                       isInArc (checkPtInArc ptx ptCircleCenter p1 p2)
  841.                     )
  842.                     (setq
  843.                       isInArc (checkPtInArc ptx ptCircleCenter p2 p1)
  844.                     )
  845.                   )
  846.                                         ;判断交点是否在弧线段上
  847.                   (If (and isInArc (not (Equals ptx p1 1e-6)))
  848.                     (progn
  849.                       (setq ptxLst (append ptxLst (list ptx)))
  850.                       (if (> bulge 0)
  851.                         (setq distAng (getAngles p1 ptCircleCenter ptx)
  852.                               ang12   (getAngles p1 ptCircleCenter p2)
  853.                         )
  854.                         (setq distAng (getAngles ptx ptCircleCenter p1)
  855.                               ang12   (getAngles p2 ptCircleCenter p1)
  856.                         )
  857.                       )
  858.                       (setq tmp (/ distAng ang12))
  859.                       (setq ptxLstDist (appenda ptxLstDist tmp))
  860.                     )
  861.                   )
  862.                 )
  863.               )
  864.               (progn
  865.                 (setq j (1- jn))        ;检查每个交点 是否在多段线节点线段上
  866.                 (while (setq ptx (nth (setq j (1+ j)) ptsArr))
  867.                   (If (checkPtInPtlst ptx (list p1 p2))
  868.                                         ;判断交点是否在线段上
  869.                     (setq ptxLst     (appenda ptxLst ptx)
  870.                           dpz             (/ (Distance p1 ptx) (Distance p1 p2))
  871.                           ptxLstDist (appenda ptxLstDist dpz)
  872.                     )
  873.                   )
  874.                 )
  875.               )
  876.             )
  877.             (If        (> (Length ptxLst) 0)        ;是否存在交点
  878.               (progn
  879.                 (setq ptxLst (ArraySort ptxLstDist ptxLst))
  880.                 (If
  881.                   (and (= bulge 0)
  882.                        (= StartWidth EndWidth)
  883.                   )
  884.                    (progn
  885.                      (setq tmp (list p1 bulge StartWidth EndWidth))
  886.                      (setq newCrdnt (appenda newCrdnt tmp))
  887.                      (ForEach ptx ptxLst
  888.                        (setq tmp (list ptx bulge StartWidth EndWidth))
  889.                        (setq newCrdnt (appenda newCrdnt tmp))
  890.                        (setq CrdntLst (appenda CrdntLst newCrdnt))
  891.                        (setq tmp (list ptx bulge StartWidth EndWidth))
  892.                        (setq newCrdnt (list tmp))
  893.                      )
  894.                    )
  895.                    (progn
  896.                      (setq ptxEndbulge
  897.                             0
  898.                            ptxbulge 0
  899.                            widthxS StartWidth
  900.                            ptx p1
  901.                            ix -1
  902.                      )
  903.                      (while (setq ptIx (nth (setq ix (1+ ix)) ptxLst))
  904.                        (setq ptxbulge
  905.                               (BulgeFromArc
  906.                                 ptx
  907.                                 ptIx
  908.                                 ptCircleCenter
  909.                                 bulge
  910.                               )
  911.                        )
  912.                        (setq
  913.                          widthxE
  914.                           (- StartWidth
  915.                              (*        (nth ix ptxLstDist)
  916.                                 (- StartWidth EndWidth)
  917.                              )
  918.                           )
  919.                        )
  920.                        (setq tmp (list ptx ptxbulge widthxS widthxE))
  921.                        (setq newCrdnt (appenda newCrdnt tmp))
  922.                                         ;存入作为起点
  923.                        (setq ptx ptIx)
  924.                        (If (= EntityName "AcDb2DPolyline")
  925.                          (setq ptx (set3dPtZBy2Pt ptx p1 p2))
  926.                        )
  927.                        (setq tmp (list ptx 0 0 0))
  928.                        (setq newCrdnt (appenda newCrdnt tmp))
  929.                                         ;交点存入作为终点
  930.                        (setq CrdntLst (appenda CrdntLst newCrdnt))
  931.                        (Setq newCrdnt nil)
  932.                        (setq widthxS widthxE)
  933.                        (setq ptxbulge
  934.                               (BulgeFromArc
  935.                                 ptx
  936.                                 p2
  937.                                 ptCircleCenter
  938.                                 bulge
  939.                               )
  940.                        )
  941.                        (setq tmp (list ptx ptxbulge widthxS EndWidth))
  942.                        (setq newCrdnt (appenda newCrdnt tmp))
  943.                                         ;px存入作为起点
  944.                      )
  945.                    )
  946.                 )
  947.                 (Setq ptxLstDist nil)
  948.                 (Setq ptxLst nil)
  949.               )
  950.               (setq tmp             (list p1 bulge StartWidth EndWidth)
  951.                     newCrdnt (appenda newCrdnt tmp)
  952.               )                                ;将点1存如数组
  953.             )
  954.           )
  955.         )
  956.       )
  957.       ;;多段线判断结束
  958.       (If (> (Length CrdntLst) 0)
  959.         (progn
  960.           (if (= (vla-get-closed obj) :vlax-true)
  961.                                         ;判断闭合保持闭合线收尾相连
  962.             (setq CrdntLst
  963.                    (cons (append newCrdnt (car CrdntLst))
  964.                          (cdr CrdntLst)
  965.                    )
  966.             )
  967.             (setq tmp           (list pte 0 0 0) ;添加末尾点
  968.                   newCrdnt (appenda newCrdnt tmp)
  969.                   CrdntLst (appenda CrdntLst newCrdnt)
  970.             )
  971.           )
  972.           CrdntLst
  973.         )
  974.       )
  975.     )
  976.   )
  977. )

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-11-13 11:13:40 | 显示全部楼层

你这个程序加载后的 命令是啥 我不懂lisp  看不懂
回复 支持 反对

使用道具 举报

发表于 2025-11-14 13:16:31 | 显示全部楼层
这个应用场景挺多的,不错不错
回复 支持 反对

使用道具 举报

 楼主| 发表于 13 小时前 | 显示全部楼层
搞好了 已经
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-3 22:25 , Processed in 0.182485 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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