Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Chook convert to view coordinates


Recommended Posts

I am working on this Chook for Mastercam 2021, I don't have a lot of experience, but what I'm trying to do is convert the X Y Z of a single-point from a given view to another view and output that information to a text file.

I am using the viewManager.IO.ConvertToWorldCoordinates  and viewManager.IO.ConvertToWorldCoordinates .

One thing I notice about this functionality is that it does don't work for non-standard (custom) Mastercam views.

It always defaults to one of the standard Views. So, it outputs the dimensions referencing the standard View and not the custom View.

I ran a few tests and came up empty.

 

 

 

Link to comment
Share on other sites

The next slide, is the debug mode and it shows the results at runtime.

You can see the non-standard view 53 (2a_FRONT_PLANE) is being used for world transformation of the point coordinates. But instead looks like it is transforming from World coordinates to the standard MasterCAM's view 2 (Front). The actual point coordinate values are circled above Line 32.

At line 44 the values are converted to view coordinates using the values from ptInView1 in refence to view 53.

The results should be X1.1,Y-2.3,Z-4.7, instead we have X1.1,y2.2Z-3.3. as sown at line 42.

image.thumb.png.fd03b295efb472dbdc5f28f6039a00fd.png

 

Link to comment
Share on other sites

I hope that the images above make any sense in my explanation.

Tried to upload them at the very beginning for clarity but I had some trouble in doing so.

Like I said before, I lack experience at this, and I trying my best.

 Just wondering if anyone has experience the same issue and can point me in the right direction.

Here is a snippet of the code I am using ;

 

 public static class PointTransformXY
    {
        //bool GetPointFromUser(bool display, string prompt, ref Mastercam.Math.Point3D pt)
        public static Mastercam.Math.Point3D GetPointFromUser(bool display, string prompt)
        {
            var pt = new Mastercam.Math.Point3D();

            Mastercam.Database.MCView cPlane = Mastercam.IO.ViewManager.CPlane;
            Array allViews =  Mastercam.IO.ViewManager.GetAllViews(true);
            var strTargetPlane = cPlane.ViewName;
            // Ask the user to select a single POINT geometry item.
            bool result = Mastercam.IO.SelectionManager.AskForPoint(prompt, Mastercam.IO.Types.PointMask.Null, ref pt);
            if (result)
            {
                string dataWorld = string.Format("X: {0}\nY: {1}\nZ: {2}", pt.x, pt.y, pt.z);
                MessageBox.Show(dataWorld, "Point (World)", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            // Convert the World coordinates of the point to a specific View# (in this case the CPlane)

            //Mastercam.Math.Point3D ptInView = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, cPlane.ViewNumber);
            //Mastercam.Math.Point3D ptInView1 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(ptInView, cPlane.ViewNumber);

            Mastercam.Math.Point3D ptInView1 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, cPlane.ViewID);
            Mastercam.Math.Point3D ptInView2 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, 2);
            Mastercam.Math.Point3D ptInView3 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, 3);
            Mastercam.Math.Point3D ptInView4 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, 4);
            Mastercam.Math.Point3D ptInView5 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, 5);
            Mastercam.Math.Point3D ptInView6 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, 6);
            Mastercam.Math.Point3D ptInView7 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, 7);


            // Mastercam.Math.Point3D ptInView = Mastercam.IO.ViewManager.ConvertToViewCoordinates(pt);

            // Convert the World coordinates of the point to the current CPlane View
            Mastercam.Math.Point3D ptInView11 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(ptInView1, cPlane.ViewID);
            Mastercam.Math.Point3D ptInView22 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(pt, 2);
            Mastercam.Math.Point3D ptInView33 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(pt, 3);
            Mastercam.Math.Point3D ptInView44 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(pt, 4);
            Mastercam.Math.Point3D ptInView55 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(pt, 5);
            Mastercam.Math.Point3D ptInView66 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(pt, 6);
            Mastercam.Math.Point3D ptInView77 = Mastercam.IO.ViewManager.ConvertToViewCoordinates(pt, 7);


            if (display)
            {
                string dataView = string.Format("View#: {0}\nName: {1}\nX: {2}\nY: {3}\nZ: {4}", 
                    cPlane.ViewNumber, cPlane.ViewName, ptInView77.x, ptInView77.y, ptInView77.z);
                MessageBox.Show(dataView, "Point (View)", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
            return pt;
        }
    }

 

Cheers!  😀

Link to comment
Share on other sites

I think you’ll need to map the point yourself if it’s not in world coordinates to begin with.

Try multiplying the point through the transpose/inverse (They are the same in a unitized, orthogonal DCM) of the source view’s DCM (matrix), then multiply that point through the destination matrix.

With this method you can convert a point in any view to any other view; the source point doesn’t need to be in world coordinates. 

Side note; you’re typing too much.  Add some using directives so you don’t need to type out the namespaces.

 

  • Like 1
Link to comment
Share on other sites

ViewManager.ConvertToViewCoordinates only appears to map the point; it doesn't seem to include the origin.

 Below is a short example showing how to take the shift into account.

using Mastercam.App;
using Mastercam.App.Types;

using Mastercam.IO;
using Mastercam.IO.Types;

using Mastercam.Math;
using Mastercam.Database;


namespace pointMappingExample
{
    public class Main : NetHook3App
    {
        public override MCamReturn Run(int param)
        {
            var selectedPoint = new Point3D(0, 0, 1);

            SelectionManager.AskForPoint("Select a point", PointMask.Null, ref selectedPoint);

            PromptManager.WriteString("Select the source view");

            var sourceView = ViewManager.SelectFromPlaneList();

            PromptManager.WriteString("Select the destination view");

            var destinationView = ViewManager.SelectFromPlaneList();

            PromptManager.Clear();

            var pointService = new PointService();

            var convertedPoint = pointService.ConvertToViewCoordinates(selectedPoint,
                                                                       destinationView);

            var transformedPoint = pointService.TransformToViewCoordinates(selectedPoint,
                                                                           sourceView,
                                                                           destinationView);

            DialogManager.OK($"Selected point(world)\n" +
                             $"    X:{selectedPoint.x}\n" +
                             $"    Y:{selectedPoint.y}\n" +
                             $"    Z:{selectedPoint.z}" +
                             $"\n" +
                             $"Converted point({destinationView.ViewName})\n" +
                             $"    X:{convertedPoint.x}\n" +
                             $"    Y:{convertedPoint.y}\n" +
                             $"    Z:{convertedPoint.z}" +
                             $"\n" +
                             $"Transformed point({destinationView.ViewName})\n" +
                             $"    X:{transformedPoint.x}\n" +
                             $"    Y:{transformedPoint.y}\n" +
                             $"    Z:{transformedPoint.z}",
                             $"Results");

            return MCamReturn.NoErrors;
        }

    }
    

    public class PointService
    {
        //Short method
        public Point3D ConvertToViewCoordinates(Point3D point, MCView destinationView)
        {
            var destinationViewMappedOrigin = destinationView.ViewMatrix.MapPoint(destinationView.ViewOrigin);

            return ViewManager.ConvertToViewCoordinates(point, destinationView) - destinationViewMappedOrigin;
        }

        //Long method
        public Point3D TransformToViewCoordinates(Point3D point, MCView sourceView, MCView destinationView)
        {
            var sourceViewMappedOrigin = sourceView.ViewMatrix.MapPoint(sourceView.ViewOrigin);

            var pointInSource = sourceView.ViewMatrix.MapPoint(point) - sourceViewMappedOrigin;

            var pointInTranspose = sourceView.ViewMatrix.GetTranspose().MapPoint(pointInSource) + sourceView.ViewOrigin;

            var destinationViewMappedOrigin = destinationView.ViewMatrix.MapPoint(destinationView.ViewOrigin);

            var pointInDestination = destinationView.ViewMatrix.MapPoint(pointInTranspose) - destinationViewMappedOrigin;

            return pointInDestination;
        }
    }

    public static class Matrix3DExtensions
    {
        public static Matrix3D GetTranspose(this Matrix3D m)
        {
            var rowOne = new Point3D(m.Row1.x,
                                     m.Row2.x,
                                     m.Row3.x);

            var rowtwo = new Point3D(m.Row1.y,
                                     m.Row2.y,
                                     m.Row3.y);


            var rowthree = new Point3D(m.Row1.z,
                                       m.Row2.z,
                                       m.Row3.z);

            return new Matrix3D(rowOne, 
                                rowtwo, 
                                rowthree);
        }

        public static Point3D MapPoint(this Matrix3D m, Point3D p)
        {
            var mappedX = (p.x * m.Row1.x) + (p.y * m.Row1.y) + (p.z * m.Row1.z);
            var mappedY = (p.x * m.Row2.x) + (p.y * m.Row2.y) + (p.z * m.Row2.z);
            var mappedZ = (p.x * m.Row3.x) + (p.y * m.Row3.y) + (p.z * m.Row3.z);

            return new Point3D(mappedX, mappedY, mappedZ);
        }
    }
}

 

  • Like 1
Link to comment
Share on other sites
On 5/7/2021 at 4:04 AM, Zaffin_D said:

Side note; you’re typing too much.  Add some using directives so you don’t need to type out the namespaces.

I see now: typing the directives at the beginning saves me from over typing.  😮

go from this :   Mastercam.Math.Point3 D ptInView1 = Mastercam.IO.ViewManager.ConvertToWorldCoordinates(pt, cPlane.ViewID);

to this :  Point3 D ptInView1 = ViewManager.ConvertToWorldCoordinates(pt, cPlane.ViewID);

🤯 thanks.

Link to comment
Share on other sites
1 hour ago, Zaffin_D said:

ViewManager.ConvertToViewCoordinates only appears to map the point; it doesn't seem to include the origin.

 Below is a short example showing how to take the shift into account.

I'll be darn! you are right, your code works.:thumbsup:

Awesome!

image.png.c4e80239ce0ae07c295915cc7d8e08e1.png

 Thanks for the example code,🙏 it would have taken me a gazillion hours to figure  the Matrix part of it. :rtfm:

Link to comment
Share on other sites
16 minutes ago, Compaq007 said:

Thanks for the example code,🙏 it would have taken me a gazillion hours to figure  the Matrix part of it. :rtfm:

That was provided just to show the math; you don't actually need it.  The below method leverages the provided math stuff and achieves the same result.

        public Point3D ConvertToViewCoordinates(Point3D point, MCView destinationView)
        {
            var destinationViewMappedOrigin = ViewManager.ConvertToViewCoordinates(destinationView.ViewOrigin, destinationView);

            return ViewManager.ConvertToViewCoordinates(point, destinationView) - destinationViewMappedOrigin;
        }

 

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...