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:

How to get the tool custom stored profil


Recommended Posts

Hi,

I am trying to get the stored profile of a tool from the mastercam NET-Hook or  C-Hook.
 

In Net-hook, I think the profile is in the Cnc.Tool.TlProfile but i  don't know how to access it from the Mastercam namespace.
In C-Hook, I think I can access to the profile by the GetStoredProfile() function in TlProfilePtr but I actually don't know how to get an TlProfilePtr from a tool number.

Is there any samples or documentation to help me ? I checked the 3rd party developers data but I probably miss something.

Thanks !



 

Stored profil.PNG

Link to comment
Share on other sites

In your NET-Hook project you need to add a Reference to the ToolNetAPI.DLL, just like you do for the NETHook3_0.DLL (The NET-Hook API).
Now you have access to the functionality under the Cnc.Tool.Interop namespace.

using Cnc.Tool.Interop;

/// <summary> Gets the tool profile. </summary>
///
/// <param name="toolAssembly"> The tool assembly. </param>
/// <param name="profile">      [out] The profile. </param>
///
/// <returns> True if it succeeds, false if it fails. </returns>
private bool GetToolProfile(TlAssembly toolAssembly, ref TlProfile profile)
{
  if (toolAssembly.HasMainTool())
  {
    TlProfileResult result = toolAssembly.GetMillTool().GetProfile();
    if (result.resultCode == TlProfileResultCode.NoError)
      {
      profile = result.profile; // Profile data back to caller
      return true;
      }
    MessageBox.Show(result.errorMessage,
                    toolAssembly.GetMillTool().Name,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);
    }
    return false;
}


 

  • Like 1
Link to comment
Share on other sites

So, I made some quick test and got something really strage.

In some case, I get the TlProfile, and in other case I got a C# exception during the "GetProfile()" function.

Here is my code.

private void _GetProfile(Mastercam.Database.Tool Tool)
        {            
            Cnc.Tool.Interop.ToolLibrarySystem mTlib = new ToolLibrarySystem();
            if ( mTlib.OpenLibrary(Mastercam.IO.FileManager.CurrentFileName, false) )
            {
                List<TlAssembly> listAssembly = new List<TlAssembly>();
                mTlib.GetAllTlAssemblies(listAssembly);

                foreach (TlAssembly assembly in listAssembly)
                {
                    if (assembly.HasMainTool())
                    {
                        TlToolMill tlToolMill = new TlToolMill();
                        tlToolMill = assembly.GetMillTool();
                        if (tlToolMill != null && tlToolMill.ToolNumber == Tool.Number)
                        {
                            try
                            {
                                TlProfileResult result = tlToolMill.GetProfile();
                                TlProfile profile;
                                if (result.resultCode == TlProfileResultCode.NoError)
                                {
                                    profile = result.profile; // Profile data back to caller          
                                    int sgmentCount = profile.GetSegmentCount();

                                }
                            }
                            catch (Exception oException)
                            {
                                __oLog.ForceLog(oException.Message);
                            }
                        }
                    }
                }
            }

 

The exception message is : 
"Tentative de lecture ou d'écriture de mémoire protégée. Cela indique souvent qu'une autre mémoire est endommagée." who is probably "Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

Any idea for how to fix this error ?

Thank again.

Link to comment
Share on other sites

"I would also check profile for null before calling GetSegmentCount just to be safe, if it is null that could be an indication of a problem."

Done, but the exception is in the "tlToolMill.GetProfile();" function. 

"Try restarting Mastercam and see if you can reproduce this issue."
I can reproduce the issue every time.

I try my code with some other mastercam files. I got 3 kinds of result.
 

  1. The listAssembly count = 0 in some case.  Even if got some assemblies in my toolManager.
  2. The listAssembly != 0, then I trying to access to the tool profile -> 2 cases :
    1. Everything is going fine, my profile.GetSegmentCount() > 0
    2. Or I got an exception during GetProfile(); function.

 

I think I am not doing this in the rigth way. Any documentation about the ToolNetApi.dll ? I didn't find any manual or samples in the mastercam website.

Regards,

Link to comment
Share on other sites

Hello,

I am still not able to get the stored profil of a tool.
Join to my post a sample, a simple mcam file with only one tool (a pic of this tool is in my first post).
This tool as a stored profile ( HasStoredProfile() return an TlProfile) but without any segment ( profile.GetSegmentCount() == 0).
So, I am really confused, and now I don't know what to do.

Here is the code I made, 

 

Quote

private XML_Profil _GetProfile(Mastercam.Database.Tool Tool)
        {            
            Cnc.Tool.Interop.ToolLibrarySystem mTlib = new ToolLibrarySystem();
            if ( mTlib.OpenLibrary(Mastercam.IO.FileManager.CurrentFileName, true) )
            {
                List<TlAssembly> listAssembly = new List<TlAssembly>();
                mTlib.GetAllTlAssemblies(listAssembly);

                foreach (TlAssembly assembly in listAssembly)
                {
                    if (assembly.HasMainTool())
                    {
                        TlToolMill tlToolMill = new TlToolMill();
                        tlToolMill = assembly.GetMillTool();
                        if (tlToolMill != null && tlToolMill.ToolNumber == Tool.Number)
                        {                            
                            try
                            {
                                if (tlToolMill.HasStoredProfile())
                                {
                                    TlProfile profile = tlToolMill.StoredProfile;
                                    int sgmentCount = profile.GetSegmentCount();
                                    MessageBox.Show(sgmentCount.ToString());
                                }
                                else
                                {
                                    TlProfileResult result = tlToolMill.GetProfile();
                                    TlProfile profile;

                                    if (result.resultCode == TlProfileResultCode.NoError)
                                    {
                                        profile = result.profile; // Profile data back to caller          
                                        int sgmentCount = profile.GetSegmentCount();
                                        MessageBox.Show(sgmentCount.ToString());
                                    }
                                }
                            }
                            catch (Exception oException)
                            {
                                MessageBox.Show(oException.Message);
                                __oLog.ForceLog(oException.Message);
                            }
                        }
                    }
                }
            }

            return null;
        }

 

By the way, I also get an expection in the "tlToolMill.GetProfile();" call.

Can anyone help me to solve this problem ?

Thank you.

PSA_sample.mcam

Link to comment
Share on other sites

It appears that you want to search for a tool in the local (loaded) part file, correct?

But you are attempting to open the local (mcam) file "as a tool library" => Don't do that.

You use OpenLibrary for .tooldb files.

If a pass in Tool Number of "4" to this GetProfile method (with your sample file loaded),

It returns me profile data with 7 segments.

 

/// <summary> Gets the stored profile of a tool. </summary>
///
/// <param name="toolNumber"> The tool number to match in the local part file tool list. </param>
///
/// <returns> The profile is here is one, else null. </returns>
private TlProfile GetProfile(int toolNumber)
{
    var ts = TlToolFunctions.GetToolSystem();
	    // Get the Tool Assemblies in the local part file.
    var assemblies = ts.GetAssemblies();
    foreach (var assembly in assemblies)
    {
        if (assembly.HasMainTool())
        {
            var tlToolMill = assembly.GetMillTool();
            if (tlToolMill != null && tlToolMill.ToolNumber == toolNumber)
            {
                try
                {
                    TlProfile profile;
                    if (tlToolMill.HasStoredProfile())
                    {
                        profile = tlToolMill.StoredProfile;
	                        // Or getting the profile this way.
                        //var result = tlToolMill.GetProfile();
                        //if (result.resultCode == TlProfileResultCode.NoError)
                        //{                                    
                        //    profile = result.profile;
                        //}
	                        DisplaySegmentData(profile, toolNumber, "Profile Segments");
                        return profile;
                    }
	                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
    }
    return null;
}
/// <summary> Display the segment data in a profile. </summary>
///
/// <param name="profile">    The profile. </param>
/// <param name="toolNumber"> The tool number. </param>
/// <param name="title">      The title for the messagebox. </param>
private void DisplaySegmentData(TlProfile profile, int toolNumber, string title)
{
    if (profile != null)
    {
        var segments = profile.GetProfileSegmentsTopDown();
        var sb = new StringBuilder("Tool Number = " + toolNumber);
        for (var i = 0; i < segments.Count; ++i)
        {
            var h = segments[i].GetHeight();
            var l = segments[i].GetLength();
            sb.AppendFormat("\nSeg#:{0} Height={1} Length={2}", i + 1, h, l);
        }
        MessageBox.Show(sb.ToString(), title);
    }
}

Edited by Roger Martin from CNC Software
  • Like 1
Link to comment
Share on other sites
32 minutes ago, sapin said:

For example :
sample.png.bcbe48ea09f364b92e79a5a538fcd1da.png

Code was shared with you to help you solve your coding problem. This is a community of helping each other get better. You asked the community for help and you solved your problem. I foresee someone else running into the same problem down the road and if you shared your code to help solve your problem then that person will be able to take what you figured out and used it. I have maybe a 1000 different things I have shared on this forum over the years to help others. I seeing if you are a sharer of what you learn or just a taker of what you learn and keep it all to yourself. Thanks for asking the question I always get tidbits reading post like this.

Link to comment
Share on other sites

Ok, so.

First of all, my english is not really good, sorry for that. I probably don't understand your request.

Second, when I say "close this topic", it's not "delete this topic". It's something as "mark as resolved" to explain this point is resolved.

Third, my question was "How to get the tool custom stored profiled ?" and the code sample given by Roger Martin is just good. I just copy and past it and I can access to the stored profil.
So, is someone need help, just take a look to Roger Martin answer. So the community can have all benefit to Roger Martin precious help, and I don't keep anything too me as I wasn't able to solve this problem alone.

Regards,
 

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

Ok, so.

First of all, my english is not really good, sorry for that. I probably don't understand your request.

Second, when I say "close this topic", it's not "delete this topic". It's something as "mark as resolved" to explain this point is resolved.

Third, my question was "How to get the tool custom stored profiled ?" and the code sample given by Roger Martin is just good. I just copy and past it and I can access to the stored profil.
So, is someone need help, just take a look to Roger Martin answer. So the community can have all benefit to Roger Martin precious help, and I don't keep anything too me as I wasn't able to solve this problem alone.

Regards,
 

No worries and thanks for doing your best. I am American and not good with English either so good there. Have a good day.

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...