本帖最后由 highflybird 于 2011-6-26 16:07 编辑
顺便说一下:
我开始的矩阵变换还是有用的:
如果一个坐标系统是定义成(UCSorg UCSXDir UCSYDir) -->CAD中UCS定义的方法
那么可按照下面的函数得到转换矩阵和逆转换矩阵。
- ;;;UCS变换矩阵
- (defun UCSMatrix (Org xAxis yAxis / d1 d2 xNrm yNrm zNrm RotMat InvMat
- WcsOrg UCS->WCS WCS->UCS matrix)
- (setq d1 (distance '(0 0 0) xAxis))
- (setq d2 (distance '(0 0 0) yAxis))
- (if (or (zerop d1) (zerop d2))
- '((1. 0. 0. 0.) (0. 1. 0. 0.) (0. 0. 1. 0.) (0. 0. 0. 1.))
- (setq xNrm (mapcar '/ xAxis (list d1 d1 d1)) ;X Axis
- yNrm (mapcar '/ yAxis (list d2 d2 d2)) ;Y Axis
- zNrm (G:CrossProductor xNrm yNrm) ;Z Axis
- RotMat (list xNrm yNrm zNrm) ;Rotation matrix
- InvMat (trp RotMat) ;Inverse Rotation matrix
- WcsOrg (mxv RotMat (mapcar '- Org)) ;The coordinate of WCS origin relate to UCS
- UCS->WCS (append (mapcar 'append RotMat (mapcar 'list WcsOrg)) '((0. 0. 0. 1.)))
- WCS->UCS (append (mapcar 'append InvMat (mapcar 'list Org)) '((0. 0. 0. 1.)))
- matrix (list UCS->WCS WCS->UCS) ;return two matrices
- )
- )
- )
例如下面的测试:
- (defun c:test()
- (if (setq Ent (car (entsel)))
- (progn
- (setq Org (getvar "ucsorg"))
- (setq XDr (getvar "ucsxdir"))
- (setq ydr (getvar "ucsydir"))
- (setq Obj (vlax-ename->vla-object Ent))
- (setq Mat (UCSMatrix Org xdr ydr))
- (vla-TransformBy Obj (vlax-tmatrix (car Mat))) ;UCS->WCS
- (command ".select" ent pause)
- (vla-TransformBy Obj (vlax-tmatrix (cadr Mat))) ;WCS->UCS
- )
- )
- )
|