请让我也来凑热闹:)我没有编过这个,只能好好学习了
具体帖子可见:
http://www.theswamp.org/index.php?topic=1804.0;all
首先
CAB根据楼主的要求发了两个现成的
1.这个不知道作者,精度还可以

- (defun c:sptrace (/ ent spline cur pl end keep)
- (setq cur nil)
- (setq pl '((0 . "LWPOLYLINE")
- (100 . "AcDbEntity")
- (67 . 0)
- (8 . "0")
- (100 . "AcDbPolyline")
- (90 . 7)
- (70 . 0)
- (43 . 0.0)
- (38 . 0.0)
- (39 . 0.0)
- )
- )
- (setq end '(210 0.0 0.0 1.0));define polyline group codes
- (while (not (progn (princ "\rSelect Spline: ") ;select spline to convert
- (setq spline (ssget ":s" '((0 . "SPLINE"))))
- )
- )
- )
- (initget "Yes No")
- (setq keep (getkword "Keep Original Spline [Yes/No]: "));keep original line or not
- (setq spline (ssname spline 0)); get ename
- (if (/= keep "No")
- (progn
- (entmake (entget spline));copy spline
- (setq spline (entlast));get ename of new spline
- )
- )
- (setq ent spline); copy ename to new variable
- (command "splinedit" ent "refine" "elevate" 26 "x" "x");add extra control points
- (setq ent (entget ent)); get data for spline
- (setq pl (subst (assoc 8 ent) (assoc 8 pl) pl)); set polylines layer to same as spline
- (if (= (rem (cdr (assoc 70 ent)) 2) 1);is spline closed
- (setq pl (subst (cons 70 1) (assoc 70 pl) pl));set polyline closed
- (setq pl (subst (cons 70 0) (assoc 70 pl) pl));set polyline open
- )
- (repeat (length ent);loop
- (progn
- (if (eq (car (car ent)) 10);get control point data
- (setq cur (append cur (list (car ent))))
- )
- (setq ent (cdr ent));get next element in list
- )
- )
- (setq pl (subst (cons 90 (length cur)) (assoc 90 pl) pl));set number of points in polyline
- (repeat (length cur);loop
- (progn;add polyline point data
- (setq pl
- (append
- pl
- (list (car cur) (cons 40 0.0) (cons 41 0.0) (cons 42 0.0))
- )
- )
- (setq cur (cdr cur));get next element in list
- )
- )
- (setq pl (append pl (list end)));add normal vector to polyline data
- (entmake pl);make polyline
- (entdel (cdr (assoc -1 (entget spline))));entdel spline, original or copy
- (princ);exit quietly
- )
2.Mauricio Ferman的,精度一般,可能是方法问题

- ;Convert SPLINES to PLINES - Mauricio Ferman??2001
- (defun c:spl2pl (/ splines plinetype osmode i spl ed codepair)
- (if
- (setq splines (ssget (list (cons 0 "spline"))))
- (progn
- (if
- (zerop (setq plinetype (getvar "plinetype")))
- (setvar "plinetype" 1)
- ) ;if
- (setq osmode (getvar "osmode"))
- (setvar "osmode" 0)
- (setq i 0)
- (while
- (setq spl (ssname splines i))
- (setq i (1+ i)
- ed (entget spl)
- ) ;setq
- (command ".pline")
- (foreach
- codepair
- ed
- (if
- (= 10 (car codepair))
- (command (cdr codepair))
- ) ;if
- ) ;foreach
- (command "")
- (command ".pedit" "l" "s" "")
- ) ;while
- (if plinetype
- (setvar "plinetype" plinetype)
- )
- (setvar "osmode" osmode)
- ) ;progn
- ) ;if
- (princ)
- ) ;defun
3.Columbia(也是theswamp一位高手)写了一个子函数,我稍微加了一个运行函数test和一个绘制函数
这个直接有误差控制,可以模拟的很好

- ;;add this rountine to draw
- (defun c:test (/ a ptlst)
- (setq a (car (entsel))
- tol 10
- )
- (setq ptlst (spline->pline a tol))
- (make_pline ptlst 2)
- )
- (vl-load-com)
- ;Spline->Pline
- (defun Spline->Pline (oSpline tol / 1stPoint dist fullLength lastPoint plinePoints point)
- (setq 1stPoint (vlax-curve-getPointAtParam oSpline (vlax-curve-getStartParam oSpline))
- lastPoint (vlax-curve-getPointAtParam oSpline (vlax-curve-getEndParam oSpline))
- fullLength (vlax-curve-getdistatparam oSpline (vlax-curve-getendparam oSpline))
- dist 0
- plinePoints (list 1stPoint)
- )
- (while (< (setq dist (+ dist tol)) fullLength)
- (if (setq point (vlax-curve-getPointAtDist oSpline dist))
- (setq plinePoints (append plinePoints (list point)))
- )
- )
- (setq plinePoints (append plinePoints (list lastpoint)))
- ;;; Use the variable plinePoints (which is a list of vertex points)
- ;;; and whatever method you like to use for adding a polyline
- ;;; to the drawing.
- )
- ;;;makepline
- (defun make_pline(lst color)
- (entmakex
- (append
- (list
- '(0 . "LWPOLYLINE")
- '(100 . "AcDbEntity")
- '(100 . "AcDbPolyline")
- (cons 90 (length lst))
- '(70 . 0)
- (cons 62 color)
- ) ;_ list
- (mapcar '(lambda (x) (cons 10 x)) lst)
- ) ;_ append
- )
- )
4.CAB在COLUMBIA的基础上进行一些批量处理,会删除原spline的
第一个用COMMAND

- (defun c:spl2pl(/ usrlay usrosm usrplw ent pdist idx ss )
- (setq usrosm (getvar "osmode"))
- (setq usrplw (getvar "plinewid"))
- (setvar "plinewid" 0 )
- (setvar "osmode" 0 )
- (setq pdist 36) ; distance between points on new pline
- (if (setq ss (ssget '((0 . "SPLINE")))); get all splines
- (progn
- (setq idx (sslength ss))
- (while (>= (setq idx (1- idx)) 0)
- (setq ent (ssname ss idx))
- (if (setq plist (Spline->Pline ent pdist))
- (progn
- (command "._pline" )
- (mapcar '(lambda (x) (command x)) plist)
- (command "")
- (command "._change" "_Last" "" "_P" "_LA" (cdr(assoc 8(entget ent)))"")
- (entdel ent)
- ) ; progn
- ); endif
- ) ; while
- ) ; progn
- (prompt "\nNo splines in drawing.")
- ) ;endif
- (setvar "osmode" usrosm)
- (setvar "plinewid" usrplw)
- (princ)
- );defun
第二个用ENTMAKE

- (defun c:spl2pl(/ usrlay usrosm usrplw ent pdist idx ss pl plist pentlst)
- (setq pdist 36) ; distance between points on new pline
- (setq pentlst '((0 . "LWPOLYLINE")
- (100 . "AcDbEntity")
- (67 . 0)
- (8 . "0")
- (100 . "AcDbPolyline")
- (90 . 7)
- (70 . 0)
- (43 . 0.0)
- (38 . 0.0)
- (39 . 0.0)
- )
- )
- (setq end '(210 0.0 0.0 1.0));define polyline group codes
- (if (setq ss (ssget '((0 . "SPLINE")))); get all splines
- (progn
- (setq idx (sslength ss))
- (while (>= (setq idx (1- idx)) 0)
- (setq ent (ssname ss idx)
- pl pentlst)
- (if (setq plist (Spline->Pline ent pdist))
- (progn
- (foreach x plist
- (setq pl (append pl
- (list (cons 10 (list (car x)(cadr x)))
- (cons 40 0.0)
- (cons 41 0.0)
- (cons 42 0.0))))
- )
- ;;set number of points in polyline
- (setq pl (append
- (subst (cons 90 (length plist)) (assoc 90 pl) pl)
- pl
- (list end))
- )
- ;; update layer
- (setq pl (subst (assoc 8 (entget ent)) (assoc 8 pl) pl))
- (entmake pl);make polyline
- (entdel ent)
- ) ; progn
- ); endif
- ) ; while
- ) ; progn
- (prompt "\nNo splines in drawing.")
- ) ;endif
- (princ)
- )
从这个学习中看到,spline2pline的处理方法
caddog和eachy兄贴的都用了vlax-curve函数,不知名作者采用了splinedit的refine方法,Mauricio Ferman采用了直接提取控制
点的方法,精度应该就差了,而columbia和cab用的仍然是vlax-curve的方法,应该说,这种精度可以人为选择,可以达到很高水平。据说还有一种转成DXF 12格式再导入的方法,应该也是一种没有程序时候的变通方法吧。 |