Sign in to follow this  
LMVUICHARD

VB.NET (VS2019) MC2020 Misunderstanding MCView

Recommended Posts

Hello,
I’m beginner in Hook and vb.net, but I try to build some dedicated applications. I have write some, with success, but now I meet an issue, difficult I think, to solve by myself.
I need to move some geometrys between views, and I think I have some misunderstanding about how Mastercam Mcview run.
To explain more simply what I mean, I have write a very small test where I draw one Arc in Top view and try to move it on different arc plane.
That works normally for standard Mcam view, but not in any no-standard view, why?
It Seem, with a no-standard view, that Mcam move geométries in the current display view plane.
how is it possible to place them in the selected arc plane?

Thank's in advance for your help 

 


Code:


' --------------------------------------------------------------------------------------------------------------------
' <copyright file="NethookMain.vb" company="CNC Software, Inc.">
'   Copyright (c) 2018 CNC Software, Inc.
' </copyright>
' <summary>
'  If this project is helpful please take a short survey at ->
' http://ux.mastercam.com/Surveys/APISDKSupport
' </summary>
' --------------------------------------------------------------------------------------------------------------------

Imports Mastercam.App
Imports Mastercam.App.Types
Imports Mastercam.IO
Imports Mastercam.Support
Imports Mastercam.Curves
Imports Mastercam.Database.Types
Imports Mastercam.Math

Public Class NethookMain
    Inherits NetHook3App


#Region "Public Methods"

    ''' <summary>
    ''' This method is designed to be the main entry point for your NetHook app. This is where your app should do
    ''' all of the things it's supposed to do and then return an MCamReturn value representing the outcome of your
    '''  NetHook application. 
    ''' </summary>
    ''' <param name="param">Parameter is not used</param>
    ''' <returns>A <c>MCamReturn</c> return type.</returns>
    ''' <remarks></remarks>
    Public Overrides Function Run(param As Integer) As MCamReturn
        Dim ArcSup As ArcGeometry
        Dim a As ArcGeometry
        Dim ConvertToWorldCoordinates As Point3D
        Do
            If SearchManager.GetSelectedGeometry().Any() Then
                SelectionManager.UnselectAllGeometry()
            End If
            a = SelectionManager.AskForGeometry("Select Arc", New GeometryMask(False, False, True, False, False, False, False, False))
            If IsNothing(a) = True Then
                Return MCamReturn.NoErrors
                Exit Function
            End If
            ArcSup = New ArcGeometry(SearchManager.GetViews(1)(0), New Point3D(0, 0, 0), 15.0, 0.0, 360.0)
            ArcSup.Commit()
            ConvertToWorldCoordinates = ViewManager.ConvertToWorldCoordinates(a.Data.CenterPoint, a.ViewNumber)
            ArcSup.Translate(New Point3D(0, 0, 0), ConvertToWorldCoordinates, SearchManager.GetViews(1)(0), a.View)
            GraphicsManager.ClearColors(New GroupSelectionMask(True))
            GraphicsManager.Repaint(True)
            SelectionManager.UnselectAllGeometry()
            a = Nothing
        Loop


        Return MCamReturn.NoErrors

    End Function

#End Region

End Class
 

MoveArcs.thumb.JPG.c5ef7d0dd1b5e163c7bb4a6daaed3570.JPG

Share this post


Link to post
Share on other sites

Something to try.

Note that it's looking for the Plane (view) with the name of "MyTargetPlane" to use as the to Translate To View.

Private Function TranslateArc() As Boolean
    Dim entTypeMask = New GeometryMask(False) With {
        .Arcs = True
    }
    
    ' Ask the user to select a single Arc geometry item.
    Dim geom = SelectionManager.AskForGeometry("Select Arc", entTypeMask)

    If geom Is Nothing Then
        Return False
    End If

    ' Cast the Geometry to it's specific type.
    ' We know it's an Arc, so this direct-cast is safe.
    Dim arc = CType(geom, ArcGeometry)
    
    ' Get ALL MCView (Planes) data in the file.
    Dim allViews = ViewManager.GetAllViews(False)
    
    ' Search for the destination view "by name".
    Dim targetView = New MCView()
    Dim found = False

    For Each view In allViews
        If String.Compare(view.ViewName, "MyTargetPlane", StringComparison.OrdinalIgnoreCase) = 0 Then
            targetView = view
            found = True
            Exit For
        End If
    Next

    If Not found Then
        Return False
    End If

    ' Create a new Arc and the same center point as the selected arc in Top.
    Dim topView = SearchManager.GetSystemView(SystemPlaneType.Top)
    Dim arcSup = New ArcGeometry(topView.ViewNumber, New Point3D(0, 0, 0), 15.0, 0.0, 360.0)
    arcSup.Commit()
    
    ' The center point of the selected arc in world coordinates.
    Dim ptWorldCoordinates = ViewManager.ConvertToWorldCoordinates(arc.Data.CenterPoint, arc.ViewNumber)
    
    ' Translate this new arc to the "MyTargetPlane" view.
    Return arcSup.Translate(New Point3D(0, 0, 0), ptWorldCoordinates, topView, targetView)
End Function

 

Share this post


Link to post
Share on other sites

Thanks for your very quick answer!

I have try your code, but it don’t solve my issue.
If the user click on an Arc based on a no-standard view -> this view don’t have any name, and your routine create nothing!
I have try to address the name this no-standard arc view, with:

Dim arc = CType(geom, ArcGeometry)

        If arc.View.ViewName = "" Then
            arc.View.ViewName = "tibidi"
            arc.Commit()
        End If
        Dim TargetName As String = arc.View.ViewName
        'Dim TargetName As Integer = arc.View.ViewNumber

        ' Get ALL MCView (Planes) data in the file.
        Dim allViews = ViewManager.GetAllViews(False)

        ' Search for the destination view "by name".
        Dim targetView = New MCView()
        Dim found = False

        For Each view In allViews
            If String.Compare(view.ViewName, TargetName, StringComparison.OrdinalIgnoreCase) = 0 Then
                targetView = view
                found = True
                Exit For
            End If
        Next

But, again, this solve nothing, because:

Dim allViews = ViewManager.GetAllViews(False)

Load only standard Mastercam views, from “top” to “trimetric”, but not the "exotics" views...


 

Share this post


Link to post
Share on other sites

I am not sure what you mean be "exotic" views.
Mastercam has Named Planes (views). Those are displayed in the Planes Manager pane in Mastercam.
ViewManager.GetAllViews(False) retrieves ALL of these known views.
The arc you have the user select contains ViewNumber data of the view that arc is in.
You can work with that value to get all of the Plane (MCView) data for the arc’s view if needed.
 

Share this post


Link to post
Share on other sites

Sorry, if my explainations are not clear... my english is not at it's top, but i can try to explain again,  thank you, for your effort of understanding!

If, in Mcam, in top view, you draw one arc at any position, and in front cplane you rotate it arond origin of 45°
-> this arc depend of no referenced named view in the view manager.


It's this "no name", what i have named "exotics view".


if we analyse it, it's view number is 21.
it's the same in the code, arc.viewnumber=21

It's the reason i don't have use the named view, and beause of that it's impossible to search, for these arcs, the named views.

 

In my original code, it' the reason i have try to use "a.view", the arc's Mcview clicked by the user for the plane rotation.

I have try to understand what was going on and have analyse all arc data.

i think the problem come from  the returned arc's Mcview matrix, who is changed, depending on the display view.

i have try to load it in a new MCview, and convert it each point in WCS, but it's run approximatively only if i try it in a top view.

i don't know what it be possible to do to have directly the arc's MCview, in WCS coordinates who can be simply use in the .Translate command.

its more clear for you?

I can give you some examples, but if you make only one top arc rotation from any side view, you can see my code is wrong, and your's proposal make nothing.

Thank's for your help.
 

Share this post


Link to post
Share on other sites

To try to give a clearers explainations:

1/ Mastercam part:

expl1b.thumb.JPG.b43bbf09a259b8faa7626e2a517b81d7.JPG

2/Modifications in your code:

 Dim targetView = New MCView()
        Dim found = False
        Dim TargetName As String = arc.View.ViewName

        For Each view In allViews
            If String.Compare(view.ViewName, TargetName, StringComparison.OrdinalIgnoreCase) = 0 Then

3/Result:

expl2.JPG.73c1a54338868ac10529e5cfbf1246db.JPG

4/ Second modification in your code:

       For Each view In allViews
            If String.Compare(view.ViewName, TargetName, StringComparison.OrdinalIgnoreCase) = 0 Then
                targetView = view
                found = True
                Exit For
            End If
        Next
        targetView = arc.View
        found = True

        If Not found Then
            Return False
        End If

(Ok, after that , a lot of lines are useless, but it's for the test…)

5/ Result:

expl3.JPG.e29db3a567c2c1f1954b50957d048bdf.JPG

Obviously, there is a link with the graphic view...

 

Share this post


Link to post
Share on other sites

I think this may be what you are looking for.

If not, email us:  Your code sample file and company contact info to > SDK [at] mastercam [dot] com

As you see in the View data for the arc you have the ViewNumber of the selected arc. You can use that View Number data.

// arc = the user selected arc
var ctr = ViewManager.ConvertToWorldCoordinates(arc.Data.CenterPoint, arc.ViewNumber);
// Use this as the center point for the new arc...

NET-Hook API Docs - https://nethookdocs.mastercam.com
See in Mastercam.IO.ViewManager:
Point3D ConvertToViewCoordinates(Point3D WorldCoordinates, short ViewNumber)
Point3D ConvertToViewCoordinates(Point3D WorldCoordinates, short ViewNumber)
Point3D ConvertToWorldCoordinates(Point3D ViewCoordinates, MCView View)
Point3D ConvertToViewCoordinates(Point3D WorldCoordinates, MCView View)
 

  • Like 1

Share this post


Link to post
Share on other sites

Thank you very much for your help!
 I have seen the online documentation, i work already with.
 I find it a little bit succinct and lack set of examples of uses (I know, you maked some application examples, i have download it, but nothing about Mcview for example).
It exists, it's already good, and I understand the work it requires...

I had try to use both commands "ConvertToViewCoordinates" and "ConvertToWorldCoordinates",
to now, without any success...

Probably, due to some misunderstanding from my side about how these two commands really runs.

For the moment, I don't have any time left, but I'll look at that up quietly as soon as possible.

I note the e-mail adress and will use it if i don't find myself any solution.

Thank's again!

  • Like 1

Share this post


Link to post
Share on other sites

Are you trying to create a view from a rotated arc?

If so you'll find that if the arc's view matrix is incorrect if the arc's view isn't in the database.

I do have a few solutions; let me know if I'm on the right track and all post them.

Share this post


Link to post
Share on other sites

I had some time today so I dug into this.

I started by drawing a 1" diameter arc in the Top view.  I then copy-rotated the arc about the Y-axis giving me two arcs shown below.

uu8g4olp.png

I wrote a quick hook to query the arcs, results below.

cljyeuev.gif

 

There are a couple ways to work around this.  The best thing would be to create a CLR project and call the C-Hook SDK's get_view_matrix function as shown in the .gif above, but you cold also fake it.

You can fake it without using the C-Hook SDK, but the calculated matrix probably won't match the matrix from get_view_matrix; see the CreateViewFromArc(ArcGeometry arc) method below.

namespace ArcMatrixExampleManaged
{
    using System.Linq;

    using Mastercam.IO;
    using Mastercam.Database;
    using Mastercam.Database.Types;
    using Mastercam.Curves;
    using Mastercam.App;
    using Mastercam.App.Types;
    using Mastercam.Support;
    using Mastercam.Math;

    using ArcServiceNative;


    public class Main : NetHook3App
    {

        bool CreateViewFromArc(ArcGeometry arc)
        {
            arc.Data.StartAngleDegrees = 0;
            arc.Data.EndAngleDegrees = 90;

            var vectorOne = VectorManager.Normalize(arc.EndPoint1);
            var vectorTwo = VectorManager.Normalize(arc.EndPoint2);
            var vectorThree = VectorManager.Normalize(Point3D.Cross(vectorOne, vectorTwo));

            var arcView = new MCView()
            {
                ViewName = $"Arc View[{arc.TimeStamp}]",
                ViewOrigin = arc.Data.CenterPoint,
                ViewMatrix = new Matrix3D(vectorOne, vectorTwo, vectorThree)
            };

            return arcView.Commit();
        }

        public override MCamReturn Run(int param)
        {
            using (var arcService = new ArcService())
            {
                var arcMask = new GeometryMask(false)
                {
                    Arcs = true
                };

                var selectedArc = (ArcGeometry)SelectionManager.AskForGeometry("Select and Arc",
                                                                               arcMask);
                if (selectedArc != null)
                {
                    var arcViewMatrix = selectedArc.View.ViewMatrix;

                    var managedViewMatrix = SearchManager.GetViews(selectedArc.ViewNumber).FirstOrDefault();

                    var nativeViewMatrix = arcService.GetViewMatrix(selectedArc.ViewNumber);

                    DialogManager.OK("View Matrix from ArcGeometry object\n" +
                                     $"  {arcViewMatrix.Row1.ToString()}\n" +
                                     $"  {arcViewMatrix.Row2.ToString()}\n" +
                                     $"  {arcViewMatrix.Row3.ToString()}\n" +
                                     $"\n" +
                                     $"View Matrix from SearchManager.GetViews\n" +
                                     $"  {managedViewMatrix?.ViewMatrix.Row1.ToString()}\n" +
                                     $"  {managedViewMatrix?.ViewMatrix.Row2.ToString()}\n" +
                                     $"  {managedViewMatrix?.ViewMatrix.Row3.ToString()}\n" +
                                     $"\n" +
                                     $"View Matrix from native get_view_matrix\n" +
                                     $"  X{nativeViewMatrix.AxisX.X.ToString("0.0000")} Y{nativeViewMatrix.AxisX.Y.ToString("0.0000")} Z{nativeViewMatrix.AxisX.Z.ToString("0.0000")}\n" +
                                     $"  X{nativeViewMatrix.AxisY.X.ToString("0.0000")} Y{nativeViewMatrix.AxisY.Y.ToString("0.0000")} Z{nativeViewMatrix.AxisY.Z.ToString("0.0000")}\n" +
                                     $"  X{nativeViewMatrix.AxisZ.X.ToString("0.0000")} Y{nativeViewMatrix.AxisZ.Y.ToString("0.0000")} Z{nativeViewMatrix.AxisZ.Z.ToString("0.0000")}\n",
                                     "Arc View Report");

                    var result = CreateViewFromArc(selectedArc);
                }
            }
            return MCamReturn.NoErrors;
        }
    }
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • 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