lwpoly23dpoly - 3dpoly2lwpoly.lsp
; transptucs & transptwcs by M.R. (Marko Ribar, d.i.a.); arguments :
; pt - point to be transformed from WCS to imaginary UCS with transptucs and from imaginary UCS to WCS with transptwcs
; pt1 - origin of imaginary UCS
; pt2 - point to define X axis of imaginary UCS (vector pt1-pt2 represents X axis)
; pt3 - point to define Y axis of imaginary UCS (vector pt1-pt3 represents Y axis)
; important note : angle between X and Y axises of imaginary UCS must always be 90 degree for correct transformation calculation
;; Unit Vector - M.R.
;; Args: v - vector in R^n
(defun unit ( v )
(mapcar '(lambda ( x ) (/ x (distance '(0.0 0.0 0.0) v))) v)
)
;; Matrix x Vector - Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n
(defun mxv ( m v )
(mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)
;; Vector Cross Product - Lee Mac
;; Args: u,v - vectors in R^3
(defun v^v ( u v )
(list
(- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
(- (* (carv) (caddr u)) (* (caru) (caddr v)))
(- (* (caru) (cadrv)) (* (carv) (cadru)))
)
)
(defun transptucs ( pt p1 p2 p3 / ux uy uz )
(setq uz (unit (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))))
(setq ux (unit (mapcar '- p2 p1)))
(setq uy (unit (mapcar '- p3 p1)))
(mxv (list ux uy uz) (mapcar '- pt p1))
)
(defun transptwcs ( pt pt1 pt2 pt3 / pt1n pt2n pt3n )
(setq pt1n (transptucs '(0.0 0.0 0.0) pt1 pt2 pt3))
(setq pt2n (transptucs '(1.0 0.0 0.0) pt1 pt2 pt3))
(setq pt3n (transptucs '(0.0 1.0 0.0) pt1 pt2 pt3))
(transptucs pt pt1n pt2n pt3n)
)
(defun entmakelwpoly3dpts ( ptlst 70dxfflag / ux uy uz uptlst )
(setq uz (unit (v^v (mapcar '- (cadr ptlst) (car ptlst)) (mapcar '- (caddr ptlst) (car ptlst)))))
(if (equal uz '(0.0 0.0 1.0) 1e-8) (setq ux '(1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
(if (equal uz '(0.0 0.0 -1.0) 1e-8) (setq ux '(-1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
(if (not (or (equal uz '(0.0 0.0 1.0) 1e-8) (equal uz '(0.0 0.0 -1.0) 1e-8))) (setq ux (unit (v^v '(0.0 0.0 1.0) uz))))
(if (not uy) (setq uy (unit (v^v uz ux))))
(setq uptlst (mapcar '(lambda ( p ) (transptucs p '(0.0 0.0 0.0) ux uy)) ptlst))
(entmake
(append
(list
'(0 . "LWPOLYLINE")
'(100 . "AcDbEntity")
'(100 . "AcDbPolyline")
(cons 90 (length uptlst))
(cons 70 70dxfflag)
'(62 . 3)
(cons 38 (caddar uptlst))
)
(mapcar '(lambda ( x ) (list 10 (car x) (cadr x))) uptlst)
(list (cons 210 uz))
)
)
(princ)
)
(defun c:lwpoly23dpoly ( / lwpol lwdxf lwptl lwel ux uy uz ptlst )
(setq lwpol (car (entsel "\nPick lwpolyline to convert to 3dpolyline")))
(if (and lwpol (= (cdr (assoc 0 (setq lwdxf (entget lwpol)))) "LWPOLYLINE"))
(progn
(setq lwptl (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) lwdxf))
(setq lwptl (mapcar '(lambda ( x ) (cdr x)) lwptl))
(setq lwel (cdr (assoc 38 lwdxf)))
(setq lwptl (mapcar '(lambda ( x ) (list (car x) (cadr x) lwel)) lwptl))
(setq uz (cdr (assoc 210 lwdxf)))
(if (equal uz '(0.0 0.0 1.0) 1e-8) (setq ux '(1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
(if (equal uz '(0.0 0.0 -1.0) 1e-8) (setq ux '(-1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
(if (not (or (equal uz '(0.0 0.0 1.0) 1e-8) (equal uz '(0.0 0.0 -1.0) 1e-8))) (setq ux (unit (v^v '(0.0 0.0 1.0) uz))))
(if (not uy) (setq uy (unit (v^v uz ux))))
(setq ptlst (mapcar '(lambda ( p ) (transptwcs p '(0.0 0.0 0.0) ux uy)) lwptl))
(entmake
(list
'(0 . "POLYLINE")
'(100 . "AcDbEntity")
'(100 . "AcDb3dPolyline")
'(66 . 1)
'(62 . 1)
'(10 0.0 0.0 0.0)
(cons 70 (+ 8 (cdr (assoc 70 lwdxf))))
'(210 0.0 0.0 1.0)
)
)
(foreach pt ptlst
(entmake
(list
'(0 . "VERTEX")
'(100 . "AcDbEntity")
'(100 . "AcDbVertex")
'(100 . "AcDb3dPolylineVertex")
(cons 10 pt)
'(70 . 32)
)
)
)
(entmake
(list
'(0 . "SEQEND")
'(100 . "AcDbEntity")
)
)
)
(prompt "\nNo lwpolyline picked")
)
(princ)
)
(defun c:3dpoly2lwpoly ( / pol vert pt ptlst )
(setq pol (car (entsel "\nPick 3dpolyline to convert to lwpolyline")))
(if (and pol (= (cdr (assoc 100 (cdr (member (assoc 100 (entget pol)) (entget pol))))) "AcDb3dPolyline"))
(progn
(setq vert (entnext pol))
(while (= (cdr (assoc 0 (entget vert))) "VERTEX")
(setq pt (cdr (assoc 10 (entget vert))))
(setq ptlst (cons pt ptlst))
(setq vert (entnext vert))
)
(setq ptlst (reverse ptlst))
(entmakelwpoly3dpts ptlst (- (cdr (assoc 70 (entget pol))) 8))
)
(prompt "\nNo 3dpolyline picked")
)
(princ)
)
(prompt "\nDefined functions are c:lwpoly23dpoly and c:3dpoly2lwpoly")
(princ)
(defun 采用3dlwpolyline(l col d70 / vl v1 v2 norm z)
(setq vl (mapcar (function (lambda (a b) (mapcar (function -) a b))) (cdr l) l))
(setq v1 (car vl)
vl (cdr vl))
(while (and vl (not norm))
(setq v2 (cadr vl)
vl (cdr vl))
(if (equal (setq norm (v^v v1 v2)) '(0 0 0) 1e-6)
(setq norm nil)))
(if norm
(progn
(setq z (caddr (trans (car l) 0 norm))
l (mapcar
(function (lambda (a)
(reverse (cdr (reverse (trans a 0 norm))))))
l))
(entmakex
(append
(list
(cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 (length l))
(cons 70 d70)
(cons 38 z))
(mapcar (function (lambda (a) (cons 10 a))) l)
(list (cons 62 col)
(cons 210 norm))
)))
)
) (defun c:2dpe ()
(setq sset (ssget '((-4 . "<OR")(0 . "POINT")
(0 . "LWPOLYLINE")(-4 . "OR>"))))
(if sset
(progn
(setq itm 0 num (sslength sset))
(setq fn (getfiled "save to file Χ,Υ" "" "txt" 1))
(if (/= fn nil)
(progn
(setq fh (open fn "w"))
(while (< itm num)
(setq hnd (ssname sset itm))
(setq ent (entget hnd))
(setq obj (cdr (assoc 0 ent)))
(cond
((= obj "POINT")
(setq pnt (cdr (assoc 10 ent)))
(setq pnt (trans pnt 0 1));;**CAB
(princ (strcat (rtos (car pnt) 2 3) ","
(rtos (cadr pnt) 2 3)) fh)
(princ "\n" fh)
)
((= obj "LWPOLYLINE")
(if (= (cdr (assoc 38 ent)) nil)
(setq elv 0.0)
(setq elv (cdr (assoc 38 ent)))
)
(foreach rec ent
(if (= (car rec) 10)
(progn
(setq pnt (cdr rec))
(setq pnt (trans pnt 0 1));;**CAB
(princ (strcat (rtos (car pnt) 2 3) ","
(rtos (cadr pnt) 2 3)) fh)
(princ "\n" fh)
)
)
)
)
(t nil)
)
(setq itm (1+ itm))
)
(close fh)
)
)
)
)
(princ)
)
(princ)
页:
[1]