public static MathTransform GetTranslationFromVector(this IMathUtility m, IMathVector v)
        {
            var vData = v.ArrayData.CastArray <double>();
            var xForm = new[] { 1, 0, 0, 0, 1, 0, 0, 0, 1, vData[0], vData[1], vData[2], 1, 0, 0, 0 };

            return((MathTransform)m.CreateTransform(xForm));
        }
Esempio n. 2
0
        private void ExportToStl(string filePath, float[] tessTriangs, float[] tessNorms, double[] transformMatrix)
        {
            IMathUtility   mathUtils = swApp.IGetMathUtility();
            IMathTransform transform = (mathUtils.CreateTransform(transformMatrix) as IMathTransform).IInverse();

            using (FileStream fileStream = File.Create(filePath))
            {
                using (BinaryWriter writer = new BinaryWriter(fileStream))
                {
                    byte[] header = new byte[80];

                    writer.Write(header);

                    uint triangsCount = (uint)tessTriangs.Length / 9;
                    writer.Write(triangsCount);

                    for (uint i = 0; i < triangsCount; i++)
                    {
                        float normalX = tessNorms[i * 9];
                        float normalY = tessNorms[i * 9 + 1];
                        float normalZ = tessNorms[i * 9 + 2];

                        IMathVector mathVec = mathUtils.CreateVector(
                            new double[] { normalX, normalY, normalZ }) as IMathVector;

                        mathVec = mathVec.MultiplyTransform(transform) as IMathVector;

                        double[] vec = mathVec.ArrayData as double[];

                        writer.Write((float)vec[0]);
                        writer.Write((float)vec[1]);
                        writer.Write((float)vec[2]);

                        for (uint j = 0; j < 3; j++)
                        {
                            float vertX = tessTriangs[i * 9 + j * 3];
                            float vertY = tessTriangs[i * 9 + j * 3 + 1];
                            float vertZ = tessTriangs[i * 9 + j * 3 + 2];

                            IMathPoint mathPt = mathUtils.CreatePoint(
                                new double[] { vertX, vertY, vertZ }) as IMathPoint;

                            mathPt = mathPt.MultiplyTransform(transform) as IMathPoint;

                            double[] pt = mathPt.ArrayData as double[];

                            writer.Write((float)pt[0]);
                            writer.Write((float)pt[1]);
                            writer.Write((float)pt[2]);
                        }

                        ushort atts = 0;
                        writer.Write(atts);
                    }
                }
            }
        }
        /// <summary>
        ///  Transformation matrix data:
        ///
        ///    |a b c.n |
        ///    |d e f.o |
        ///    |g h i.p |
        ///    |j k l.m |
        ///
        /// The SOLIDWORKS transformation matrix is stored as a homogeneous matrix of 16 elements, ordered as shown.The first 9 elements(a to i) are elements of a 3x3 rotational sub-matrix, the next 3 elements(j, k, l) define a translation vector, and the next 1 element(m) is a scaling factor.The last 3 elements(n, o, p) are unused in this context.
        ///
        /// The 3x3 rotational sub-matrix represents 3 axis sets:
        ///
        /// row 1 for x-axis components of rotation
        /// row 2 for y-axis components of rotation
        /// row 3 for z-axis components of rotation
        /// The 3 axes are constrained to be orthogonal and unified so that they
        /// produce a pure rotational transformation.Reflections can also be added
        /// to these axes by setting the components to negative.The rotation sub-matrix
        /// coupled with the lower-left translation vector and the lower-right corner scaling
        /// factor creates an affine transformation, which is a transformation that preserves lines
        /// and parallelism; i.e., maps parallel lines to parallel lines.
        ///
        /// If the 3 axis sets of the 3x3 rotational sub-matrix are not orthogonal or unified,
        /// then they are automatically corrected according to the following rules:
        ///
        ///
        /// If any axis is 0, or any two axes are parallel, or all axes are coplanar, then an identity matrix replaces the rotational sub-matrix.
        ///
        /// All axes are corrected to be of unit length.
        ///
        /// The axes are built to be orthogonal to each other in the prioritized order of Z, X, Y (X is orthogonal to Z, Y is orthogonal to Z and X).
        /// </summary>
        /// <param name=""></param>
        /// <param name="matrix"></param>
        /// <returns></returns>
        public static MathTransform ToSwMatrix(this IMathUtility math, Matrix4x4 matrix)
        {
            var    _ = matrix;
            double a = _.M11, b = _.M12, c = _.M13, n = _.M14;
            double d = _.M21, e = _.M22, f = _.M23, o = _.M24;
            double g = _.M31, h = _.M32, i = _.M33, p = _.M34;
            double j = _.M41, k = _.M42, l = _.M43, m = _.M44;

            double[] data = new[] { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p };

            return((MathTransform)math.CreateTransform(data));
        }
Esempio n. 4
0
        internal static IMathTransform ToMathTransform(this IMathUtility mathUtils, TransformMatrix matrix)
        {
            var data = new double[]
            {
                matrix.M11 / Math.Abs(matrix.M11), matrix.M12, matrix.M13,
                matrix.M21, matrix.M22 / Math.Abs(matrix.M22), matrix.M23,
                matrix.M31, matrix.M32, matrix.M33 / Math.Abs(matrix.M33),
                matrix.M41, matrix.M42, matrix.M43,
                Math.Abs(matrix.M11),
                matrix.M14, matrix.M24, matrix.M34
            };

            return(mathUtils.CreateTransform(data) as IMathTransform);
        }
        public static IMathTransform CreateTransFormTransformationMaxtrix(
            this IMathUtility mathUtils, TransformationMaxtrix transform)
        {
            var arrData = new double[]
            {
                transform.Rotation.M11, transform.Rotation.M12, transform.Rotation.M13,
                transform.Rotation.M21, transform.Rotation.M22, transform.Rotation.M23,
                transform.Rotation.M31, transform.Rotation.M32, transform.Rotation.M33,
                transform.Translation.X, transform.Translation.Y, transform.Translation.Z,
                transform.Scale,
                0, 0, 0
            };

            return(mathUtils.CreateTransform(arrData) as IMathTransform);
        }
Esempio n. 6
0
        private void MoveCurves(Point fromPt, Vector dir, double dist, ICurve[] curves, IMathUtility mathUtils)
        {
            var newPt = fromPt.Move(dir, dist);

            var translation = fromPt - newPt;

            var maxtrix = new double[]
            {
                1, 0, 0, 0,
                1, 0, 0, 0,
                1, translation.X, translation.Y, translation.Z,
                1, 0, 0, 0
            };

            var transform = mathUtils.CreateTransform(maxtrix) as MathTransform;

            for (int i = 0; i < curves.Length; i++)
            {
                curves[i].ApplyTransform(transform);
            }
        }
Esempio n. 7
0
        CreateTransformEx(this IMathUtility mathUtility,
                          double Xx    = 0d,    // a
                          double Xy    = 0d,    // b
                          double Xz    = 0d,    // c
                          double Yx    = 0d,    // d
                          double Yy    = 0d,    // e
                          double Yz    = 0d,    // f
                          double Zx    = 0d,    // g
                          double Zy    = 0d,    // h
                          double Zz    = 0d,    // i
                          double Tx    = 0d,    // j
                          double Ty    = 0d,    // k
                          double Tz    = 0d,    // l
                          double scale = 1d)    // m

        {
            //https://help.solidworks.com/2018/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.imathtransform.html

            if (mathUtility.CreateTransform(new double[] { Xx, Xy, Xz, Yx, Yy, Yz, Zx, Zy, Zz, Tx, Ty, Tz, scale, 0d, 0d, 0d }) is MathTransform transform)
            {
                return(transform);
            }
            return(null);
        }
Esempio n. 8
0
 /// <summary>
 /// Transforms xCAD matrix to SOLIDWORKS transform
 /// </summary>
 /// <param name="mathUtils">SOLIDWORKS math utility</param>
 /// <param name="matrix">Matrix to transform</param>
 /// <returns>SOLIDWORKS transform</returns>
 public static IMathTransform ToMathTransform(this IMathUtility mathUtils, TransformMatrix matrix)
 => mathUtils.CreateTransform(ToMathTransformData(matrix)) as IMathTransform;