DeerSausage

How to import operations with Net-HOOK?

Recommended Posts

Trying to create a Net-HOOK that imports operations (preferably all toolpaths from a single file) - but so far no luck. 

Example:

OperationsManager.ImportAllOperations(@"C:\\users\me\file\file.mcam",1);

                                                                                  Filepath ^                                 ^ID

Says 'no overload for method ImportAllOperations, takes 2 arguments'. I apologize, I'm new to Net-Hook development.

Share this post


Link to post
Share on other sites

Hi,

We have several examples with documentation on our website, just sign in and go to the 3rd Party Developers area for NET-Hooks.

 

  • Like 2

Share this post


Link to post
Share on other sites
49 minutes ago, Mick George said:

Hi,

We have several examples with documentation on our website, just sign in and go to the 3rd Party Developers area for NET-Hooks.

 

Thanks Mick, lots of great examples on the website I've downloaded. Still having some trouble with this one though.

Share this post


Link to post
Share on other sites

What version are you using?  I think your issue is how you are defining your path.  I haven't used ImportAllOperations but I think you'll need to define the path using the import options vs. just listing the path/name.  Check the help file, it'll list all the different options for things like the file name to import, import toolpath geometry, import T/C plane, etc...  I don't know which options are required other than the file pathe/name, I believe most/all of the others are just True/False settings which I would think would default to False if not defined. 

 

HTH

  • Thanks 1
  • Like 1

Share this post


Link to post
Share on other sites

This should help you get started.

 using Mastercam.App;
    using Mastercam.App.Types;
    using Mastercam.Database;
    using Mastercam.Database.Types;
    using Mastercam.IO;
    using Mastercam.Operations;
    using Mastercam.Support;
    using System.IO;
    using static Mastercam.Operations.OperationsManager;

    public class Main : NetHook3App
    {
        #region Public Override Methods
        
        public override MCamReturn Run(int param)
        {

            {
                string operations = "C:/users/me/file.mcam";//set path to operation
                ImportOptions opt = new ImportOptions(operations, -1);//set import options
                OperationsManager.ImportAllOperations((ImportOptions)opt);//pass the import options into the function
                return MCamReturn.NoErrors;
            }
        }

 

  • Thanks 1

Share this post


Link to post
Share on other sites
57 minutes ago, Roger Peterson said:

What version are you using?  I think your issue is how you are defining your path.  I haven't used ImportAllOperations but I think you'll need to define the path using the import options vs. just listing the path/name.  Check the help file, it'll list all the different options for things like the file name to import, import toolpath geometry, import T/C plane, etc...  I don't know which options are required other than the file pathe/name, I believe most/all of the others are just True/False settings which I would think would default to False if not defined. 

 

HTH

 

41 minutes ago, Peter Evans said:

This should help you get started.

 using Mastercam.App;
    using Mastercam.App.Types;
    using Mastercam.Database;
    using Mastercam.Database.Types;
    using Mastercam.IO;
    using Mastercam.Operations;
    using Mastercam.Support;
    using System.IO;
    using static Mastercam.Operations.OperationsManager;

    public class Main : NetHook3App
    {
        #region Public Override Methods
        
        public override MCamReturn Run(int param)
        {

            {
                string operations = "C:/users/me/file.mcam";//set path to operation
                ImportOptions opt = new ImportOptions(operations, -1);//set import options
                OperationsManager.ImportAllOperations((ImportOptions)opt);//pass the import options into the function
                return MCamReturn.NoErrors;
            }
        }

 

Thanks guys, it's working great now. You were right, just needed to define the Import options.

Share this post


Link to post
Share on other sites

Okay so It imports mill toolpaths just fine but not Wire EDM operations?? Anyone have a clue as to why this is? If I try to import it with a mill group as my active machine it gives an error, so I know it's trying to import. But when I have my EDM group active it just refreshes the operations manager.

Share this post


Link to post
Share on other sites

Not all mastercam operations are supported by the NETHOOK API. It is possible you won't be able to access or manipulate wire operations via a nethook.

Share this post


Link to post
Share on other sites

As others have said, you'll need a C-Hook to import wire or Lathe operations. 

I've wanted to create an interop C-Hook for these commonly used functions (Import, calculating cycle time, etc.) but I haven't had the time.

Share this post


Link to post
Share on other sites
7 hours ago, jeff.D said:

As others have said, you'll need a C-Hook to import wire or Lathe operations. 

I've wanted to create an interop C-Hook for these commonly used functions (Import, calculating cycle time, etc.) but I haven't had the time.

I created a project recently using C++/Cli to "bridge" managed C# functions into unmanaged C++. It is not difficult to setup I'd be happy to share how I went about it ,are you looking to Import C++ functionality into C# or vice versa?

Share this post


Link to post
Share on other sites
3 hours ago, Peter Evans said:

I created a project recently using C++/Cli to "bridge" managed C# functions into unmanaged C++. It is not difficult to setup I'd be happy to share how I went about it ,are you looking to Import C++ functionality into C# or vice versa?

I want to use unmanaged code in a NET-Hook.  I'm interested in what you've done; do you have a GitHub?

Share this post


Link to post
Share on other sites

 

29 minutes ago, jeff.D said:

I want to use unmanaged code in a NET-Hook.  I'm interested in what you've done; do you have a GitHub?

What version of mastercam are you using Jeff? I'm currently working with the 2018 SDK, would an example built for that version be sufficient? 

Share this post


Link to post
Share on other sites
12 hours ago, Peter Evans said:

 

What version of mastercam are you using Jeff? I'm currently working with the 2018 SDK, would an example built for that version be sufficient? 

2020 - but any example would do.

Share this post


Link to post
Share on other sites

Okay I should have it ready sometime today,I got a test working last night just need to add it into a SDK based dll to export the function. FYI you will need to download C++/CLI from the visual studio installer in order to use this.

7 minutes ago, jeff.D said:

2020 - but any example would do.

Okay perfect.

Share this post


Link to post
Share on other sites

So I made 3 projects a C++ project that exports sdk functions in a static library (.lib) a c++/cli project that has a definition of the functions in c# and c++ and exports the functions to a dynamic link library, and a c# project that imports the functions from the c++/cli project.

Share this post


Link to post
Share on other sites

My GitHub is Crazyturtlez if you give me your username I will access you to the project, I couldn't upload the whole C++ project, it's very large .The header and cpp files are there, the rest of it is just. a c-hook wizard file set static library with references to the sdk headers .lib files etc.

Share this post


Link to post
Share on other sites

I downloaded the repo, thanks for the example.  This is likely the same approach I intended on taking.

Native code compiled to a static library, then a CLR wrapper dll  for use with .NET.

Share this post


Link to post
Share on other sites

I added two more repositories on git, the wrapper example and the nethook example. FYI the DLL generated by the c++/cli project will need to be stored in the same folder as mastercam.

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks, but you didn't have to bother; I got the jist of it from you're first project.

Share this post


Link to post
Share on other sites
51 minutes ago, jeff.D said:

Thanks, but you didn't have to bother; I got the jist of it from you're first project.

Cool, I didn't want to give an incomplete example. I'm sure you will get set up no problem.

Share this post


Link to post
Share on other sites
On 9/8/2019 at 6:35 PM, Zaffin said:

I downloaded the repo, thanks for the example.  This is likely the same approach I intended on taking.

Native code compiled to a static library, then a CLR wrapper dll  for use with .NET.

Follow up : when I initially started doing this It was working well, however certain functions completely mangled the graphics when called from C#. I then swapped out the language for F# instead of C# and the problems vanished leaving me somewhat perplexed...  in  c++ :

//compile all with C++/CLI FLAG
//cppwrapper.h
#pragma once

#include "api_tools_exp.h"

using namespace System;

namespace CppWrapper {
	public ref class ApiToolsWrapper
	{
	public:
		// constructor
		ApiToolsWrapper();

		// wrapper methods
		int BlankAllNonSolidEntsWrapper(double a, double b, int c, int d);

		// public variable
		double initVal;

	private:
		ApiTools *myCppClass; // an instance of class in C++
	};

}
//api_tools_exp.h
#include "api_tools.h"
//functions 1:
  ///mastercam stuff :D
int doentblnk(long selectionMask = ALL_ENTITIES_MASK, int level = 0, MC_BYTE color = 0)
{
	long count = 0; bool succf, suc = false; ent entity; DB_LIST_ENT_PTR foundPtr; DB_LIST_ENT_PTR eptr = db_start;
	do
	{
		get_raw_ent(false, &eptr, &foundPtr, &entity, 0, ALL_ENTITIES_MASK, &succf);

		if (succf)
		{
			if (entity.id != selectionMask)
			{
				suc = true;

				sel_bit_turn_on(&entity, BLANK_BIT);

				write_ent_sel(&entity, entity.eptr);
			}

		}
	
	} while (succf); 

	return suc ? 1 : 0;
}
//class to export:
class ApiTools
{
public:
	int BlankAllNonSolidEnts(double a, double b, int c, int d)
	{
		set_clr_sel_bits(0, SELECT_BIT, 0, ALIVE_BIT, ALL_ENTITIES_MASK, true);
		return doentblnk(SOLID_ID, 0, 0)
	}
}
//main.cpp
#pragma once
#include "cppwrapper.h"
#include "api_tools_exp.h"

// Constructor implementaion
CppWrapper::ApiToolsWrapper::ApiToolsWrapper()
{
	myCppClass = new ApiTools(); //initiate C++ class's instance
}
int CppWrapper::ApiToolsWrapper::BlankAllNonSolidEntsWrapper(double a, double b, int c, int d)
{
	return myCppClass->BlankAllNonSolidEnts(a, b, c, d);
}

F:


#light                         // optional these days
namespace fsharplib
open System
open Mastercam.App;
open Mastercam.App.Types;
open Mastercam.Database;
open Mastercam.Database.Types;
open Mastercam.GeometryUtility;
open Mastercam.IO;
open System.Numerics 
open System.Drawing
open System.Windows.Forms
open System.Collections.Generic
open FSharp.Configuration
open System.Runtime.CompilerServices
open Mastercam.Tools
    
     // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    type Main () = 
     inherit NetHook3App ()
     ///the nethook entry point
     override x.Run(param : int) = 
           ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//implementation :
          let apitools = new CppWrapper.ApiToolsWrapper()
           ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //parameterize function :
          let blankAllNonSolidEnts(param1,param2,param3,param4) = apitools.BlankAllNonSolidEntsWrapper(param1,param2,param3,param4)
  //Implement function    :
          let x1 = blankAllNonSolidEnts(0.0,0.0,0,0)
           ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
          MCamReturn.NoErrors // return value
          

 

Share this post


Link to post
Share on other sites
On 8/28/2019 at 9:42 AM, DeerSausage said:

Okay so It imports mill toolpaths just fine but not Wire EDM operations?? Anyone have a clue as to why this is? If I try to import it with a mill group as my active machine it gives an error, so I know it's trying to import. But when I have my EDM group active it just refreshes the operations manager.

You would need to import the operations via c++ for example this function imports an operation  :

int ImportOperationById(int id)

{

	// Must call this prior to accessing any Resources in the C-Hook DLL !

	ChangeResCl res(GetChookResourceHandle());

	
	//set SELECT_BIT or select by select_ents

	DB_LIST_ENT_PTR eptr;

	ent e;

	bool succf;

	operation op;

	memset(&op, 0, sizeof(operation));

	TCHAR buff[_MAX_PATH] = "C:\\Users\\Public\\Documents\\shared Mcam2018\\mill\\Ops\\test.mcam";

	LPCTSTR path = buff;

	std::vector<long> op_ids;

	op_ids.push_back(id);

	struct DUPTOOL_CHK chk = { DUPTOOL_CHK_NO };

	import_operations(buff, NULL, op_ids, 1, 0, false, false, chk, true, &succf, true);

	size_t size = op_ids.size();

	char message[2000];

	sprintf_s(message, "Operation import ok, Operation ID # : %d Imported from path %s", size, buff);

	MessageBox(NULL, succf ? message : "Operation Failed", "SharkBait", MB_OK);



}

You could then access this function from .NET using the method in the post above this one.  

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

  • 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