Search code examples
c#eyeshot

How to align in parallel a Brep object using one of its face to XY, XZ, or YZ plane?


I tried a few possibilities but I could not get it right. On SelectionChanged, I create a plane from a selected face. This is good, I can get the plane correctly.

                    var item = e.AddedItems[0];
                    if (item is Model.SelectedFace)
                    {
                        var faceItem = ((Model.SelectedFace)item);
                        var ent = faceItem.Item;
                          if (ent is Brep)
                          {
                            var sol = (Brep)ent;
                            if (faceItem.ShellIndex == 0)
                            {
                                var mesh = sol.Faces[faceItem.Index].ConvertToMesh();
                                var plane = new Plane(mesh.Vertices[0], mesh.Vertices[1], mesh.Vertices[2]);
                            }
                          }
                    }

ThexyTheta, xzTheta, and yzTheta between the selected face's plane and Plane.XY, Plane.XZ, & Plane.YZ are correctly calculated. For the purpose of this question, I only show xyTheta as below (I already tried using this with Transformation Matrix but it did not work well either. Thus its purpose is just for me to check if the angle between the two planes is correct.

   var x0 = plane.Equation.X; var y0 = plane.Equation.Y; var z0 = plane.Equation.Z;
   var x1 = Plane.XY.Equation.X; var y1 = Plane.XY.Equation.Y; var z1 = Plane.XY.Equation.Z;

   xyTheta = Math.Acos(Math.Abs(x0 * x1 + y0 * y1 + z0 * z1)
                  / (Math.Sqrt(x0 * x0 + y0 * y0 + z0 * z0)
                  * Math.Sqrt(x1 * x1 + y1 * y1 + z1 * z1)));

                           

My transformation transXY can only do the Translation correctly but not the Rotation. For example, after transformated, my object still has 10 degree difference between plane and Plane.XY, although it gets moved.

       transXY = new Transformation();
       transXY.Rotation(plane, Plane.XY);
       // transXZ = new Align3D(plane, Plane.XZ);

        foreach (Entity ent in theModel.Entities)
        {
            ent.TransformBy(transXY);
        }

Solution

  • plane.AxisX,Y,Z seem to work for the transformation matrix. **Your BREP has to be transformed back to the origin for this to work, hence the transforming it by the inverse of the previous to move it back to origin.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using devDept.Eyeshot;
    using devDept.Eyeshot.Entities;
    using devDept.Geometry;
    
    namespace EyeshotTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                makeSquare();
            }
            Solid3D square, square2;
            Transformation trans2 = new Transformation(1);
            int i = 0;
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                Mesh faceMesh = square2.Faces[i].ConvertToMesh();
                var plane = new Plane(faceMesh.Vertices[0], faceMesh.Vertices[1], faceMesh.Vertices[2]);
    
                Point3D origin = plane.Origin;
                Vector3D xVec = plane.AxisX;
                Vector3D yVec = plane.AxisY;
                Vector3D zVec = plane.AxisZ;
    
                trans2.Invert();
    
                square.TransformBy(trans2);
    
                trans2 = new Transformation(origin, xVec, yVec, zVec);
    
                square.TransformBy(trans2);
    
                viewportLayout1.Entities.Regen();
                viewportLayout1.Invalidate();
    
                i++;
                if(i == 4) { i = 0; }
            }
    
            private void makeSquare()
            {
                square = Solid3D.CreateBox(5, 5, 5, 0.01);
                square2 = Solid3D.CreateBox(5, 5, 5, 0.01);
    
                viewportLayout1.Entities.Add(square, Color.Green);
                viewportLayout1.Entities.Add(square2, Color.FromArgb(75, Color.Blue));
    
                Transformation trans = new Transformation(new double[,]{
                    {-0.17,0.67,0.72, 47.33 },
                    {0.28,-0.67,0.69, 10.24 },
                    {0.95,0.32,-0.07, 15.98 },
                    {0,0,0,1 } });
    
                square2.TransformBy(trans);
    
                viewportLayout1.Invalidate();           
            }
        }
    }
    
    

    Cheers!