这里提供一个函数,得到当前UCS下的变化矩阵和逆变换矩阵。
- ;;;UCS到WCS的变换矩阵及其WCS到UCS的变换矩阵
- ;;;the transformation matrix of UCS to WCS and WCS to UCS.
- (defun UCS2WCS (/ xdir ydir zdir UcsOrg WcsOrg matlst revlst matrix)
- (defun AppendMatrix (lst org)
- (append
- (mapcar 'append lst (mapcar 'list org))
- '((0. 0. 0. 1.))
- )
- )
- (if (zerop (getvar "WORLDUCS")) ;如果不同于WCS
- (setq xdir (getvar "UCSXDIR") ;X方向
- ydir (getvar "UCSYDIR") ;Y方向
- zdir (G:CrossProductor xdir ydir) ;Z方向等于X方向叉乘Y方向
- UcsOrg (getvar "UCSORG") ;UCS原点
- WcsOrg (trans '(0 0 0) 0 1) ;WCS原点相对于UCS的坐标
- matLst (list xdir ydir zdir) ;UCS的旋转矩阵
- RevLst (trp matLst) ;UCS的旋转矩阵的逆
- matrix (list
- (AppendMatrix matlst WcsOrg) ;UCS到WCS的变换矩阵
- (AppendMatrix RevLst UcsOrg) ;WCS到UCS的变换矩阵
- )
- )
- (list
- '((1. 0. 0. 0.) (0. 1. 0. 0.) (0. 0. 1. 0.) (0. 0. 0. 1.)) ;世界坐标系下是单位矩阵
- '((1. 0. 0. 0.) (0. 1. 0. 0.) (0. 0. 1. 0.) (0. 0. 0. 1.)) ;世界坐标系下是单位矩阵
- )
- )
- )
- ;;; 矢量的点积
- ;;; VXV Returns the dot product of 2 vectors
- (defun vxv (v1 v2)
- (apply '+ (mapcar '* v1 v2))
- )
- ;;; 矢量转置
- ;;; TRP Transpose a matrix -Doug Wilson-
- (defun trp (m)
- (apply 'mapcar (cons 'list m))
- )
- ;;; 矢量的矩阵变换
- ;;; MXV Apply a transformation matrix to a vector -Vladimir Nesterovsky-
- (defun mxv (m v)
- (mapcar (function (lambda (r) (vxv r v))) m)
- )
- ;;; 矩阵相乘
- ;;; MXM Multiply two matrices -Vladimir Nesterovsky-
- (defun mxm (m q)
- (mapcar (function (lambda (r) (mxv (trp q) r))) m)
- )
- ;;;两矢量的叉积
- ;;; CrossProductor --vec1 * vec2
- (defun G:CrossProductor (vec1 vec2 / a b c d e f)
- (setq a (car vec1))
- (setq b (cadr vec1))
- (setq c (caddr vec1))
- (setq d (car vec2))
- (setq e (cadr vec2))
- (setq f (caddr vec2))
- (list
- (- (* b f) (* c e))
- (- (* c d) (* a f))
- (- (* a e) (* b d))
- )
- )
以下作为测试 -
- (defun c:test(/ ent obj mat)
- (if (setq ent (car (entsel)))
- (progn
- (setq obj (vlax-ename->vla-object ent))
- (setq mat (ucs2wcs))
- (vla-TransformBy obj (vlax-tmatrix (car mat))) ;UCS->WCS
- (command ".select" ent pause)
- (vla-TransformBy obj (vlax-tmatrix (cadr mat))) ;WCS->UCS
- )
- )
- )
该贴已经同步到 highflybird的微博
|