fengjunchina 发表于 2007-1-17 01:30:00

[求助]画中心线问题!

<p>从网上下载了一个画中心线的LISP程序</p><p>帮忙分析一下为什么画椭圆中心线的方向是反的。用在AUTOCAD R14英文版下。源程序如下:</p><p><br/>;;&nbsp;&nbsp; 本程序为绘制中心线程序<br/>;;&nbsp;&nbsp; 执行命令:cline<br/>;;<br/>;;确定中心线延伸长度<br/>;(defun adddist (/ defdist)<br/>&nbsp;; (if dist<br/>&nbsp;&nbsp; ; (progn<br/>&nbsp;&nbsp;&nbsp; ;&nbsp; (princ "\n请输入延伸长度&lt;")<br/>&nbsp;&nbsp;&nbsp; ;&nbsp; (setq defdist (princ dist))<br/>&nbsp;&nbsp;&nbsp;&nbsp; ; (setq dist (getdist "&gt;:"))<br/>&nbsp;&nbsp;&nbsp; ;)<br/>&nbsp;&nbsp;&nbsp; ;(progn<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;(setq defdist 5)</p><p>&nbsp;&nbsp;&nbsp;&nbsp; ; (setq dist (getdist "\n请输入延伸长度&lt;5&gt;:"))<br/>&nbsp;&nbsp; ; )<br/>&nbsp;; )<br/>&nbsp;; (if (= dist nil)<br/>&nbsp; ;&nbsp; (setq dist defdist)<br/>&nbsp;; )<br/>&nbsp;; dist<br/>;)</p><p>;;确定中心线延伸长度<br/>(defun adddist (/ dist scale)<br/>(setq SCALE (getvar "DIMSCALE"))<br/>&nbsp;&nbsp; (setq DIST (* 3.5 SCALE))<br/>)</p><p>;画圆中心线<br/>(defun circle_cl (en1 / ss i j ent ents ptc r pts adist)<br/>&nbsp; (setq ss (ssget '((0 . "Circle"))))<br/>&nbsp; (setq i 0)<br/>&nbsp; (setq adist(adddist))<br/>&nbsp; (repeat (sslength ss)<br/>&nbsp; (setq ent (ssname ss i))<br/>&nbsp;&nbsp;&nbsp; (setq ents (entget ent))<br/>&nbsp;&nbsp;&nbsp; (setq ptc (cdr (assoc 10 ents))<br/>&nbsp;&nbsp; r (cdr (assoc 40 ents))<br/>&nbsp;&nbsp; j 0<br/>&nbsp;&nbsp; pts'())<br/>&nbsp;&nbsp;&nbsp; (repeat 4<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq pts (append pts (list (polar ptc (* j (/ pi 2)) (+ r adist)))))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq j (1+ j))<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (command "_.line" (nth 0 pts) (nth 2 pts) ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "_.line" (nth 1 pts) (nth 3 pts) "")<br/>&nbsp;&nbsp;&nbsp; (setq i (1+ i))<br/>&nbsp; )<br/>&nbsp; (princ)<br/>&nbsp; <br/>)</p><p><br/>;画弧中心线<br/>(defun arc_cl (en1 / ed1 pto rad ang1 ang2 pto1 pt1 pt2 pto2 objline dd adist)<br/>&nbsp; (setq ed1 (entget en1))<br/>&nbsp; (setq&nbsp;pto&nbsp; (cdr (assoc 10 ed1))<br/>&nbsp;rad&nbsp; (cdr (assoc 40 ed1))<br/>&nbsp;ang1 (cdr (assoc 50 ed1))<br/>&nbsp;ang2 (cdr (assoc 51 ed1))<br/>&nbsp; )<br/>&nbsp; (if (&gt; ang1 ang2)<br/>&nbsp;&nbsp;&nbsp; (setq ang2(+ ang2 (* PI 2)))<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp; (setq ang (/ (+ ang1 ang2)2))<br/>&nbsp;&nbsp; (setq pto1 (polar pto ang rad))<br/>&nbsp; (setq pt1 (polar pto ang1 rad))<br/>&nbsp; (setq pt2 (polar pto ang2 rad))<br/>&nbsp; (setq pto2 (inters pto pto1 pt1 pt2 nil))<br/>&nbsp;(command "_line" pto1 pto2 "")<br/>&nbsp; (setq&nbsp;objline&nbsp;(entlast)<br/>&nbsp;dd&nbsp;(distance pto1 pto2)<br/>&nbsp; )<br/>&nbsp; (setq adist (adddist))<br/>&nbsp; (command "_lengthen"<br/>&nbsp;&nbsp;&nbsp; "t"<br/>&nbsp;&nbsp;&nbsp; (+ dd adist)<br/>&nbsp;&nbsp;&nbsp; (cons objline (list pto1))<br/>&nbsp;&nbsp;&nbsp; ""<br/>&nbsp; )<br/>&nbsp; (command "_lengthen"<br/>&nbsp;&nbsp;&nbsp; "t"<br/>&nbsp;&nbsp;&nbsp; (+ dd (* adist 2))<br/>&nbsp;&nbsp;&nbsp; (cons objline (list pto2))<br/>&nbsp;&nbsp;&nbsp; ""<br/>&nbsp; )</p><p><br/>)</p><p><br/>;画直线中心线<br/>(defun line_cl (en1 / en2 )<br/>&nbsp; (setq en2 (car (entsel "\n请选择另外的直线&lt;只绘制单直线中心线&gt;:")))<br/>&nbsp; (if en2<br/>&nbsp;&nbsp;&nbsp; (progn<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (while (/= "LINE" (cdr (assoc 0 (entget en2))))<br/>&nbsp;(setq en2 (car (entsel "\n请重新选择另外的直线:")))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (dline_cl en1 en2)<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (sline_cl en1)<br/>&nbsp; )<br/>)</p><p><br/>(defun dline_cl&nbsp;(en1 en2 /&nbsp;&nbsp; ed1&nbsp;&nbsp;&nbsp;&nbsp; ed2&nbsp;&nbsp;&nbsp;&nbsp; pta1&nbsp;&nbsp;&nbsp; pta2&nbsp;&nbsp;&nbsp; ptb1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptb2&nbsp;&nbsp;&nbsp; pto&nbsp;&nbsp;&nbsp;&nbsp; ptoo&nbsp;&nbsp;&nbsp; pto1&nbsp;&nbsp;&nbsp; pto2&nbsp;&nbsp;&nbsp; anga<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; angb&nbsp;&nbsp;&nbsp; ango&nbsp;&nbsp;&nbsp; objline dd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adist)<br/>&nbsp; (setq&nbsp;ed1 (entget en1)<br/>&nbsp;ed2 (entget en2)<br/>&nbsp; )<br/>&nbsp; (setq&nbsp;pta1 (cdr (assoc 10 ed1))<br/>&nbsp;pta2 (cdr (assoc 11 ed1))<br/>&nbsp;ptb1 (cdr (assoc 10 ed2))<br/>&nbsp;ptb2 (cdr (assoc 11 ed2))<br/>&nbsp; )</p><p>&nbsp; (if (setq pto (inters pta1 pta2 ptb1 ptb2 nil)) ;if 2<br/>&nbsp;&nbsp;&nbsp; (if&nbsp;(inters pta1 pta2 ptb1 ptb2)&nbsp;;if 3<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (princ "\n两线相交,退出")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (progn<br/>&nbsp;(setq anga (angle pto pta1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; angb (angle pto ptb1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ango (/ (+ anga angb) 2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptoo (polar pto ango 1)<br/>&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;;setq<br/>&nbsp;(if (inters pta1 ptb1 pta2 ptb2) ;if 4<br/>&nbsp;&nbsp; (setq&nbsp;pto1 (inters pta1 ptb2 pto ptoo nil)<br/>&nbsp;&nbsp;pto2 (inters pta2 ptb1 pto ptoo nil)<br/>&nbsp;&nbsp; )<br/>&nbsp;&nbsp; (setq&nbsp;pto1 (inters pta1 ptb1 pto ptoo nil)<br/>&nbsp;&nbsp;pto2 (inters pta2 ptb2 pto ptoo nil)<br/>&nbsp;&nbsp; )<br/>&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;;if 4<br/>&nbsp;(command "_line" pto1 pto2 "")<br/>&nbsp;(setq objline (entlast)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (distance pto1 pto2)<br/>&nbsp;)<br/>&nbsp;(setq adist (adddist))<br/>&nbsp;(command "_lengthen"<br/>&nbsp;&nbsp; "t"<br/>&nbsp;&nbsp; (+ dd adist)<br/>&nbsp;&nbsp; (cons objline (list pto1))<br/>&nbsp;&nbsp; ""<br/>&nbsp;)<br/>&nbsp;(command "_lengthen"<br/>&nbsp;&nbsp; "t"<br/>&nbsp;&nbsp; (+ dd (* adist 2))<br/>&nbsp;&nbsp; (cons objline (list pto2))<br/>&nbsp;&nbsp; ""<br/>&nbsp;)</p><p><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;progn<br/>&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;if 3<br/>&nbsp;&nbsp;&nbsp; (progn<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (arxload "geomcal.arx" nil)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (inters pta1 ptb1 pta2 ptb2)&nbsp;;if 4<br/>&nbsp;(progn<br/>&nbsp;&nbsp; (setq&nbsp;pto1 (c:cal "(pta1 + ptb2) / 2")<br/>&nbsp;&nbsp;pto2 (c:cal "(pta2 + ptb1) / 2")<br/>&nbsp;&nbsp; )<br/>&nbsp;&nbsp; (command "_line"<br/>&nbsp;&nbsp;&nbsp;&nbsp; pto1<br/>&nbsp;&nbsp;&nbsp;&nbsp; pto2<br/>&nbsp;&nbsp;&nbsp;&nbsp; ""<br/>&nbsp;&nbsp; )<br/>&nbsp;)<br/>&nbsp;(progn<br/>&nbsp;&nbsp; (setq&nbsp;pto1 (c:cal "(pta1+ptb1)/2")<br/>&nbsp;&nbsp;pto2 (c:cal "(pta2+ptb2)/2")<br/>&nbsp;&nbsp; )<br/>&nbsp;&nbsp; (command "_line"<br/>&nbsp;&nbsp;&nbsp;&nbsp; pto1<br/>&nbsp;&nbsp;&nbsp;&nbsp; pto2<br/>&nbsp;&nbsp;&nbsp;&nbsp; ""<br/>&nbsp;&nbsp; )<br/>&nbsp;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (arxunload "geomcal.arx" nil)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq objline (entlast)<br/>&nbsp;&nbsp;&nbsp;&nbsp; dd&nbsp;&nbsp;&nbsp;&nbsp; (distance pto1 pto2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq adist (adddist))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (command "_lengthen"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "t"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (+ dd adist)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (cons objline (list pto1))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (command "_lengthen"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "t"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (+ dd (* adist 2))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (cons objline (list pto2))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )</p><p>&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;px<br/>&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;if 2<br/>)</p><p><br/>(defun sline_cl&nbsp;(en1 / ed1 pta1 pta2 pto anga ango adist pto1 pto2)<br/>&nbsp; (arxload "geomcal.arx" nil)<br/>&nbsp; (setq ed1 (entget en1))<br/>&nbsp; (setq&nbsp;pta1 (cdr (assoc 10 ed1))<br/>&nbsp;pta2 (cdr (assoc 11 ed1))</p><p>&nbsp; )<br/>&nbsp; (setq pto (c:cal "(pta1 + pta2) / 2"))<br/>&nbsp; (setq anga (angle pto pta1))<br/>&nbsp; (setq ango (+ anga (/&nbsp; PI 2)))<br/>&nbsp; (setq adist (adddist))<br/>&nbsp; (setq pto1 (polar pto ango adist))<br/>&nbsp; (setq pto2 (polar pto ango (- adist)))<br/>&nbsp; (command "_line" pto1 pto2 "")<br/>&nbsp; (arxunload "geomcal.arx" nil)<br/>)</p><p>;画椭圆中心线<br/>(defun ellipse_cl (en1 / ss pc px rad rad1 d p1 p2 ri adist ns n lline)<br/>&nbsp; (setq ss (ssget '((0 . "Ellipse"))))</p><p>(setq ns (sslength ss) n 0)<br/>&nbsp; (<br/>&nbsp;while (&lt; n ns)</p><p>(setq adist(adddist))<br/>(setq en1 (ssname ss n))&nbsp; <br/>(setq pc (dxf 10 en1))<br/>(<br/>&nbsp;&nbsp;&nbsp; setq px (dxf 11 en1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; px (list (+ (car pc)(car px))(+ (cadr pc)(cadr px)) 0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rad (distance pc px)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d (/ (fix (* rad adist)) 100.0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p1 (polar pc (angle pc px)(+ rad d))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p2 (polar pc (angle px pc)(+ rad d))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rad1 (* (dxf 40 en1) rad)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ri (/ (+ rad1 d)(+ rad d))<br/>) ;end setq</p><p>(if p2 (command "line" p1 p2 ""))<br/>&nbsp;&nbsp;&nbsp; (setq lline (entlast))<br/>&nbsp;&nbsp;&nbsp; (command "copy" (entlast) "" pc pc "rotate" (entlast) "" pc 90 "")<br/>&nbsp;&nbsp;&nbsp; (<br/>if (= (dxf 0 en1) "ELLIPSE") (command "scale" lline "" pc ri )<br/>);end if<br/>&nbsp;<br/>&nbsp;&nbsp;&nbsp; (setq n (1+ n))<br/>&nbsp; )<br/>&nbsp; (princ)<br/>&nbsp; <br/>)</p><p><br/>(defun dxf (code ename)(cdr (assoc code (entget ename))))</p><p>;主程序 <br/>(defun c:cline (/ oldlayer oldecho oldsnap oldortho en1 )<br/>&nbsp; (setq oldlayer (getvar "clayer"))<br/>&nbsp; (setq oldecho (getvar "cmdecho"))<br/>&nbsp; (setq oldsnap (getvar "osmode"))<br/>&nbsp; (setq oldortho (getvar "orthomode"))<br/>&nbsp; ;(setvar "clayer" "Center")<br/>&nbsp; (setvar "cmdecho" 0)<br/>&nbsp; (setvar "osmode" 0)<br/>&nbsp; (setvar "orthomode" 0)<br/>&nbsp; (setq en1 (car (entsel "\n请选择直线、圆、圆弧或椭圆&lt;退出&gt;:")))<br/>&nbsp; (if en1<br/>&nbsp;&nbsp;&nbsp; (cond<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((= "LINE" (cdr (assoc 0 (entget en1)))) (line_cl en1))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((= "ARC" (cdr (assoc 0 (entget en1)))) (arc_cl en1))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((= "CIRCLE" (cdr (assoc 0 (entget en1)))) (circle_cl en1))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((= "ELLIPSE" (cdr (assoc 0 (entget en1)))) (ellipse_cl en1))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (t (princ "\n所选对象不符合要求,程序退出"))<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (princ "\n未选择对象,程序退出")<br/>&nbsp; )<br/>&nbsp; (setvar "cmdecho" oldecho)<br/>&nbsp; (setvar "osmode" oldsnap)<br/>&nbsp; (setvar "orthomode" oldortho)<br/>&nbsp; (setvar "clayer" oldlayer)<br/>&nbsp; (princ)<br/>)</p><p></p>

fengjunchina 发表于 2007-1-17 01:31:00

另外哪位大哥能否给这个程序再加个画矩形中心线的程序。在网上一直找不到这样的程序!先谢谢了。

fengjunchina 发表于 2007-1-24 13:21:00

顶上去,求各位老大帮帮忙呀。感谢你们了。

bobby 发表于 2007-1-24 16:28:00

<p>我更改了ellipse_cl函数,你试度吧。</p><p>(defun ellipse_cl (en1 / ss pc px rad rad1 d p1 p2 ri adist ns n lline)<br/>&nbsp; (setq ss (ssget '((0 . "Ellipse"))))</p><p>&nbsp; (setq&nbsp;ns (sslength ss)<br/>&nbsp;n&nbsp; 0<br/>&nbsp; )<br/>&nbsp; (<br/>&nbsp;&nbsp; while (&lt; n ns)</p><p>&nbsp;&nbsp;&nbsp; (setq adist (adddist))<br/>&nbsp;&nbsp;&nbsp; (setq en1 (ssname ss n))<br/>&nbsp;&nbsp;&nbsp; (setq pc (dxf 10 en1))<br/>&nbsp;&nbsp;&nbsp; (setq px&nbsp;&nbsp; (dxf 11 en1)<br/>&nbsp;&nbsp; px&nbsp;&nbsp; (list (+ (car pc) (car px)) (+ (cadr pc) (cadr px)) 0)<br/>&nbsp;&nbsp; rad&nbsp; (distance pc px)<br/>&nbsp;&nbsp; d&nbsp;&nbsp;&nbsp; (/ (fix (* rad adist)) 100.0)<br/>&nbsp;&nbsp; p1&nbsp;&nbsp; (polar pc (angle pc px) (+ rad d))<br/>&nbsp;&nbsp; p2&nbsp;&nbsp; (polar pc (angle px pc) (+ rad d))<br/>&nbsp;&nbsp; rad1 (* (dxf 40 en1) rad)<br/>&nbsp;&nbsp; ri&nbsp;&nbsp; (/ (+ rad1 d) (+ rad d))<br/>&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;end setq</p><p>&nbsp;&nbsp;&nbsp; (if&nbsp;p2<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (command "line" p1 p2 "")<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (command "copy"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (entlast)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pc<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pc<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "rotate"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (entlast)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pc<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 90<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (setq lline (entlast))<br/>&nbsp;&nbsp;&nbsp; (if&nbsp;(= (dxf 0 en1) "ELLIPSE")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (command "scale" lline "" pc ri)<br/>&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;end if</p><p>&nbsp;&nbsp;&nbsp; (setq n (1+ n))<br/>&nbsp; )<br/>&nbsp; (princ)</p><p>)</p>

fengjunchina 发表于 2007-1-27 19:50:00

<p>非常感谢,画椭圆中心线问题已经解决.太感谢你了,就是现在还没有画矩形中心线的程序.</p>

ygrzz 发表于 2007-1-31 03:39:00

不知是哪位大侠编的,也忘了从哪找到的,可对矩形画中心线<br/>(defun c:CL ()<br/>  (vl-load-com)<br/>  (setq ent0 (car (entsel "\n 请选择直线、圆、圆弧、椭圆和矩形:")))<br/>  (setq ent1 (entget ent0))<br/>  (setq name (cdr (assoc 0 ent1)))<br/>  (setq os (getvar "osmode"))<br/>  (setq cmd (getvar "CMDECHO"))<br/>  (setvar "cmdecho" 0)<br/>  (setvar "OSMODE" 0)<br/>;;;所选物体是直线<br/>  (cond    ((= name "LINE")<br/>     (setq ent2 (entget (car (entsel "\n 请选择另一条直线:"))))<br/>     (setq point10 (cdr (assoc 10 ent1))<br/>           point11 (cdr (assoc 11 ent1))<br/>           point20 (cdr (assoc 10 ent2))<br/>           point21 (cdr (assoc 11 ent2))<br/>     ) ;_求两直线2个端点的坐标<br/>     (setq ang1 (angle point10 point11)<br/>           ang2 (angle point20 point21)<br/>     )<br/>     (if (or (equal (abs (- ang1 ang2)) pi 0.0001)<br/>         (= (- ang1 ang2) 0)<br/>         )<br/>       (progn<br/>         (setq obj (vlax-Ename-&gt;vla-object ent0))<br/>         (setq p0 (vlax-curve-getClosestPointTo obj point20))<br/>         (setq inter-point<br/>            (mapcar '(lambda (a b) (/ (+ a b) 2))<br/>                p0<br/>                point20<br/>            )<br/>         )<br/>         (print inter-point)<br/>         (command "offset" "T" ent0 inter-point "")<br/>       )<br/>       (progn<br/>         (setq inter-point (inters point10 point11 point20 point21 nil)) ;_求得两直线的交点<br/>         (setq p0 (polar inter-point (angle inter-point point10) 10)<br/>           p1 (polar inter-point (angle inter-point point20) 10)<br/>         ) ;_求2个虚拟点<br/><br/>                    ;直线端点离交点最近判断<br/>         (if (&gt; (distance inter-point point10)<br/>            (distance inter-point point11)<br/>         )<br/>           (setq p10 point11<br/>             p11 point10<br/>           )<br/>           (setq p10 point10<br/>             p11 point11<br/>           )<br/>         )<br/>         (if (&gt; (distance inter-point point20)<br/>            (distance inter-point point21)<br/>         )<br/>           (setq p20 point21<br/>             p21 point20<br/>           )<br/>           (setq p20 point20<br/>             p21 point21<br/>           )<br/>         )<br/>                    ;求角平分线上的虚拟点<br/>         (setq inter-point0<br/>            (mapcar '(lambda (a b) (/ (+ a b) 2)) p0 p1)<br/>         )<br/>         (setq p00 (inters p10 p20 inter-point inter-point0 nil)<br/>           p01 (inters p11 p21 inter-point inter-point0 nil)<br/>         )<br/>         (command "LINE" p00 p01 "")<br/>       )<br/>     )<br/>    )<br/>;;;所选物体是圆<br/>    ((or<br/>       (= name "CIRCLE")<br/>       (= name "ARC")<br/>     )<br/>     (setq point (cdr (assoc 10 ent1)))<br/>     (setq r (cdr (assoc 40 ent1)))<br/>     (setq l (+ r 2.0))<br/>     (setq x (car point)<br/>           y (cadr point)<br/>     )<br/>     (setq point10 (list (- x l) y))<br/>     (setq point11 (list (+ x l) y))<br/>     (setq point20 (list x (+ y l)))<br/>     (setq point21 (list x (- y l)))<br/><br/><br/>     (command "line" point10 point11 "")<br/>     (command "line" point20 point21 "")<br/><br/><br/>    )<br/><br/>;;;所选物体是圆弧<br/>    ;;((= name "ARC")<br/>    ;;    )<br/><br/>;;;所选物体是椭圆<br/>    ((= name "ELLIPSE")<br/>     (setq obj (vlax-Ename-&gt;Vla-Object ent0))<br/>     (setq center-point (vlax-safearray-&gt;list<br/>                  (vlax-variant-value (vla-get-Center obj))<br/>                )<br/>           StartPoint   (vlax-safearray-&gt;list<br/>                  (vlax-variant-value (vla-get-StartPoint obj))<br/>                )<br/>           MajorRadius  (vla-get-MajorRadius obj)<br/>           MinorRadius  (vla-get-MinorRadius obj)<br/>     )<br/>     (setq ang1 (angle center-point startpoint)<br/>           ang2 (+ ang1 (* pi 0.5))<br/>     )<br/>     (setq point10 (polar center-point ang1 (+ MajorRadius 2.0))<br/>           point11 (polar center-point<br/>                  (+ ang1 pi)<br/>                  (+ MajorRadius 2.0)<br/>               )<br/>           point20 (polar center-point ang2 (+ MinorRadius 2.0))<br/>           point21 (polar center-point<br/>                  (+ ang2 pi)<br/>                  (+ MinorRadius 2.0)<br/>               )<br/>     )<br/>     (command "LINE" point10 point11 "")<br/>     (command "LINE" point20 point21 "")<br/>    )<br/><br/>;;;所选物体是矩形    <br/>    ((= name "LWPOLYLINE")<br/>     (setq obj (vlax-Ename-&gt;Vla-Object ent0))<br/>     (setq point (vlax-safearray-&gt;list<br/>               (vlax-variant-value (vla-get-Coordinates obj))<br/>             )<br/>     )<br/>     (if (= (length point) 8)<br/>       (progn<br/><br/>         (setq p1 (list (nth 0 point) (nth 1 point))<br/>           p2 (list (nth 2 point) (nth 3 point))<br/>           p3 (list (nth 4 point) (nth 5 point))<br/>           p4 (list (nth 6 point) (nth 7 point))<br/>         )<br/>         (setq center-point    (mapcar    '(lambda (a b) (/ (+ a b) 2))<br/>                    p1<br/>                    p3<br/>                )<br/>           ang1        (angle p1 p2)<br/>           ang2        (angle p1 p4)<br/>           d1        (distance p1 p2)<br/>           d2        (distance p1 p4)<br/>         )<br/>         (setq point10 (polar center-point ang1 (+ (* d1 0.5) 2.0))<br/>           point11 (polar center-point<br/>                  (+ ang1 pi)<br/>                  (+ (* d1 0.5) 2.0)<br/>               )<br/>           point20 (polar center-point ang2 (+ (* d2 0.5) 2.0))<br/>           point21 (polar center-point<br/>                  (+ ang2 pi)<br/>                  (+ (* d2 0.5) 2.0)<br/>               )<br/>         )<br/>         (command "LINE" point10 point11 "")<br/>         (command "LINE" point20 point21 "")<br/>       )<br/>       (princ "\n所选物体不是矩形,请重新选择!")<br/>     )<br/>    )<br/>    (T<br/>     (princ<br/>       "\n 所选物体不属于直线、圆、圆弧、椭圆和矩形之内,请重新选择!"<br/>     )<br/>    )<br/>  )<br/>  (setvar "OSMODE" os)<br/>  (setvar "cmdecho" cmd)<br/>  (princ)<br/>)<br/>

usestone 发表于 2007-3-8 00:42:00

<p><strong><font face="Verdana" color="#61b713">太好了,我以前看到网站上有关于这一编程的流程思路,可惜一直没找到作者的源代码,</font></strong></p><p><strong><font face="Verdana" color="#61b713">ygrzz发了,今天让我遇到了,太好了,谢谢</font></strong></p>

长枫间谍 发表于 2014-10-23 21:17:51

可否再改进一下!颜色控制选择红色,线型控制选择中心线!就完美了!

库里 发表于 2023-5-20 17:39:53

谢谢分享,非常感谢
页: [1]
查看完整版本: [求助]画中心线问题!