在三維旋轉(zhuǎn)的時(shí)候 我們很難找到旋轉(zhuǎn)軸 也很難確定 我們要旋轉(zhuǎn)的空間 角度。我通過(guò)旋轉(zhuǎn)對(duì)象的軸線和 旋轉(zhuǎn)方向上的一條直線,找到旋轉(zhuǎn)平面,然后計(jì)算這個(gè)平面的法線方程。再計(jì)算兩條直線的夾角,在平面內(nèi)旋轉(zhuǎn)這個(gè)角度就到了。三維旋轉(zhuǎn)軸就是平面的法線,旋轉(zhuǎn)的角度就是兩條直線的夾角。
Sub Rotate3D_1()
On Error Resume Next
Dim Obj As AcadEntity
Dim PickPnt
Dim A As Double, B As Double, C As Double, D As Double
Dim P1 As Variant, P2 As Variant, P3 As Variant
Dim T(0 To 2) As Double
Dim L1 As Double, L2 As Double, L3 As Double
Dim J As Double
ThisDrawing.Utility.GetEntity Obj, PickPnt, "選擇旋轉(zhuǎn)對(duì)象:"
ThisDrawing.Utility.InitializeUserInput 1, ""
P1 = ThisDrawing.Utility.GetPoint(, "旋轉(zhuǎn)對(duì)象軸線上一點(diǎn):")
ThisDrawing.Utility.InitializeUserInput 1, ""
P2 = ThisDrawing.Utility.GetPoint(, "旋轉(zhuǎn)對(duì)象軸線與旋轉(zhuǎn)方向直線的交點(diǎn):")
ThisDrawing.Utility.InitializeUserInput 1, ""
P3 = ThisDrawing.Utility.GetPoint(, "旋轉(zhuǎn)方向直線上的另一點(diǎn):")
KJPMFC P1, P2, P3, A, B, C, D
MsgBox A
'過(guò)平面法線的一點(diǎn)
T(0) = A * 10 + P2(0)
T(1) = B * 10 + P2(1)
T(2) = C * 10 + P2(2)
'計(jì)算空間兩條直線的夾角
L1 = P2PDistance(P1, P2)
L2 = P2PDistance(P2, P3)
L3 = P2PDistance(P3, P1)
'利用余弦定理 a^2=b^2+c^2-2*b*c*cos(A)
J = Arccos((L1 * L1 + L2 * L2 - L3 * L3) / 2 / L1 / L2)
Obj.Rotate3d P2, T, -J
End Sub