原文:WPF中使用TranslateTransform3D修改CAD的3D旋转中心
?????? 前面一篇文章讲述了2D旋转功能的实现,文章提到了修改3D旋转中心,这一节主要总结一下具体的修改3D旋转中心的实现方法。(这个功能的实现有一段时间了,需要通过代码好好总结一下)
????? TranslateTransform3D以相对容器的偏移移动对象,通过OffsetX、OffsetY、OffsetZ属性来指定偏移量。
?
(1)定义对象
??????? private TranslateTransform3D _translateTransform3D = new TranslateTransform3D();
?
(2)点击左键进行设置旋转中心
????????private void OnMouseLeftButtonUp(object sender, MouseEventArgs e)
??????? {
??????????? Point mouseposition = e.GetPosition(ViewPort);
??????????? PointHitTestParameters pointparams = new PointHitTestParameters(mouseposition);
??????????? Point mousePosBorderPos = e.GetPosition(ViewPort);
??????????? if (Keyboard.Modifiers == ModifierKeys.Alt)
??????????? {
??????????????? VisualTreeHelper.HitTest(ViewPort, null, HTResultCenter, pointparams);
??????????? }
??????? }
??????? 其中ViewPort和CadGrid可以参考文章:WPF中CAD control的XAML实现 。
?
(3)HitTest去设置旋转中心
??????? private HitTestResultBehavior HTResultCenter(System.Windows.Media.HitTestResult rawresult)
??????? {
??????????? RayHitTestResult rayResult = rawresult as RayHitTestResult;
??????????? if (rayResult != null)
??????????? {
??????????????? _rotCenter3D = rayResult.PointHit;
??????????????? _translateTransform3D.OffsetX = rayResult.PointHit.X;
??????????????? _translateTransform3D.OffsetY = rayResult.PointHit.Y;
??????????????? _translateTransform3D.OffsetZ = rayResult.PointHit.Z;
??????????? }
??????????? return HitTestResultBehavior.Stop;
??????? }
?
?????? 通过这两个方法就实现了3D旋转中心的更改,再次旋转3D的话你就会发现旋转中心就是你设置的那个点了。
?????? 在修改旋转中心的时候,我们还做了一个小小的功能,就是设置好点后,让旋转中心移到CadGrid的中心,这样便于观察旋转结果。
??????? private void MoveCenter(Point mouseposition)
??????? {
??????????????? //convert previous 3D rotation center to 2D.
??????????????? bool bRet = false;
??????????????? Point previousRotCenter2D = Convert3DPointTo2D(_previousRotCenter3D, out bRet);
?
??????????????? //Translate previous 2D rotation center to center of cad window.
??????????????? Point centerWnd = new Point(ViewPort.Width / 2.0, ViewPort.Height / 2.0);
??????????????? Translate(centerWnd, previousRotCenter2D);
?
???????????????? _translateTransform3D.OffsetX = _rotCenter3D.X;
??????????????? _translateTransform3D.OffsetY = _rotCenter3D.Y;
??????????????? _translateTransform3D.OffsetZ = _rotCenter3D.Z;
??????????????? _previousRotCenter3D = _rotCenter3D;
??????? }
原文地址:https://www.cnblogs.com/lonelyxmas/p/9844544.html