- UID
- 118401
- 积分
- 2156
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2004-3-28
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2013-5-7 21:08:07
|
显示全部楼层
本帖最后由 Highflybird 于 2013-5-7 21:09 编辑
顺便说一下:
我开始的矩阵变换还是有用的:
如果一个坐标系统是定义成(UCSorg UCSXDir UCSYDir) -->CAD中UCS定义的方法
那么可按照下面的函数得到转换矩阵和逆转换矩阵。
[pcode=lisp,true]
;;;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
)
)
)
[/pcode]
例如下面的测试:
[pcode=lisp,true]
(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
)
)
)[/pcode]
另外附上旋转矩阵函数:
这个旋转矩阵的参数是(旋转角度,旋转轴,旋转基点)
[pcode=lisp,true]
;;;Ang ---旋转角度
;;;Nrm ---旋转轴(矢量)(WCS下的)
;;;Org ---旋转基点 (WCS下的)
(defun RotationMatrix (Ang Nrm Org / A B C dist M N PT WX WY WZ)
(setq dist (distance '(0 0 0) Nrm))
(if (> dist 1e-8)
(setq N (mapcar '/ nrm (list dist dist dist))
wx (car N)
wy (cadr N)
wz (caddr N)
A (cos Ang)
B (sin Ang)
C (- 1 A)
M (list (list (+ A (* wx wx C))
(- (* wx wy C) (* wz B))
(+ (* wy B) (* wx wz C))
)
(list (+ (* wz B) (* wx wy C))
(+ A (* wy wy C))
(- (* wy wz C) (* wx B))
)
(list (- (* wx wz C) (* wy B))
(+ (* wx B) (* wy wz C))
(+ A (* wz wz C))
)
)
pt (mapcar '- org (mxv M org))
M (append (mapcar 'append M (mapcar 'list pt))
'((0. 0. 0. 1.))
)
)
'((1. 0. 0. 0.) (0. 1. 0. 0.) (0. 0. 1. 0.) (0. 0. 0. 1.))
)
)
[/pcode]
例如下面的测试例子:
[pcode=lisp,true]
(defun c:ttt (/ Ent Obj Mat Org Vec Ang Nrm)
(setq app (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument app))
(setq ucs (vla-get-UserCoordinateSystems doc))
(setq i 0)
(repeat (vla-get-Count ucs)
(setq u (vla-item ucs i))
(vlax-dump-object u T)
(setq i (1+ i))
)
(if (and (setq Ent (car (entsel)))
(setq Org (getpoint "\nInput origin point of rotation:"))
(setq Vec (getpoint Org "\nInput the Axis vector of rotation:"))
(setq Ang (getangle "\nInput the angle of rotation:"))
)
(progn
(setq Org (trans Org 1 0))
(setq Vec (trans Vec 1 0))
(setq Nrm (mapcar '- Vec Org))
(setq Obj (vlax-ename->vla-object Ent))
(setq Mat (RotationMatrix Ang Nrm Org))
(vla-TransformBy Obj (vlax-tmatrix Mat)) ;旋转矩阵
)
)
)
[/pcode]
|
|