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:

Mastercam NetHook C++/Cli Sort Oper/Closed Chains


Recommended Posts

Hi,

 I will be sharing some new code for NEThooks using C++/Cli .

The following code chains all the selected geometry in the Mastercam file and Colors it green if the chain is closed and red if the chain is open.

I used inline recursion functions to iterate through the array, the inline keyword is there to ease pressure on the stack from repeated function calls.

the Delegate keyword created managed callbacks that you can use to pass a function as a parameter to another function.

The delegate must be initialized with gcnew.


namespace Nethook
{
	//Chain Manipulation Delegate (CallBack Definition)
	public delegate void ChainFunction(Mastercam::Database::Chain^chain);
	//Geometry Manipulation Delegate (CallBack Definition)
	public delegate void GeometryFunction(Mastercam::Database::CurveGeometry^geometry,int color);
	//the nethook class
	public ref class NethookClass : public Mastercam::App::NetHook3App
	{
    
	public :
#pragma region Callback Functions
	//Callback for recursive geomtry function
	inline static void ProcessGeometryCallback(Mastercam::Database::CurveGeometry^geometry, int color)
	{
		//Set the new color
		geometry->Color = color;
		//Commit the changes to the datatbase
		geometry->Commit();
	}
	//loop through all the curve geomtry in an array using a recursive function
    // executes callback 
    // increments array index 
	inline static void ProcessChainCallback(Mastercam::Database::Chain^chain)
	{
		//No chains foud, Bail!
		if (chain == nullptr) { return; }
		//Get only Closed Chains
		if (chain->IsClosed)
		{
			//Get a geometry array from the chain
			auto geometry = Mastercam::Database::ChainManager::GetGeometryInChain(chain);
			//Make sure we don't have a null or empty array
			if (geometry != nullptr && geometry->Length > 0)
			{
				//Initialize the Delegate Callback
				auto geometryFunc = gcnew GeometryFunction(ProcessGeometryCallback);
				//Set the Start index to search from in the array
				auto i = 0;
				recurseGeometry(i, geometry, geometryFunc, 10);
			}
		}
		//Get only Open Chains
		if (!chain->IsClosed)
		{
			//Get a geometry array from the chain
			auto geometry = Mastercam::Database::ChainManager::GetGeometryInChain(chain);
			//Make sure we don't have a null or empty array
			if (geometry != nullptr && geometry->Length > 0)
			{
				//Initialize the Delegate Callback
				auto geometryFunc = gcnew GeometryFunction(ProcessGeometryCallback);
				//Set the Start index to search from in the array
				auto i = 0;
				recurseGeometry(i, geometry, geometryFunc, 12);
			}
		}

	}
#pragma endregion



#pragma region Recursive Functions

	//loop through all the curve geomtry in an array using a recursive function
	// executes callback 
	// increments array index 
	inline static void recurseGeometry(int &i, array<Mastercam::Database::CurveGeometry^>^geometries, GeometryFunction^ geometryFunction,int color)
	{
		//Make sure we don't leak memory by calling outside the bounds of the array
		if (geometries->Length > i)
		{
			//Execute the Callback function
			geometryFunction(geometries[i], color);
			//Increment the index of the aarray
			i++;
			//Execute the recursive function to loop through the geometry from the chain
			recurseGeometry(i, geometries, geometryFunction, color);
		}
	}

	
	//loop through all the chains in an array using a recursive function
    // executes callback 
    // increments array index 
	inline static void recurseChains(int &i, array<Mastercam::Database::Chain^>^chains, ChainFunction^ chainFunction)
	{
		//Make sure we don't leak memory by calling outside the bounds of the array
		if (chains->Length > i)
		{
			//Execute the Callback function
			chainFunction(chains[i]);
			//Increment the index of the aarray
			i++;
			//Execute the recursive function to loop through the chains
			recurseChains(i, chains, chainFunction);
		}
	}
#pragma endregion

#pragma region Mastercam Entry
	//The Mastercam Entry Point
	public: virtual Mastercam::App::Types::MCamReturn Run(int param) override
	{
		try
		{
			//Get all selected chains
			auto chains = Mastercam::Database::ChainManager::ChainAllSelected();
			//No chains foud, Bail!
			if (chains == nullptr) { return Mastercam::App::Types::MCamReturn::NoErrors; }
			//Set the Start index to search from in the array
			auto i = 0;
			//Initialize the Delegate Callback
			auto chainFunc = gcnew ChainFunction(ProcessChainCallback);
			//Execute the recursive function to loop through the chains
			recurseChains(i, chains, chainFunc);
		}
		catch(System::Exception^e)
		{
			//send any unhandled errors to the event logger
			Mastercam::IO::EventManager::LogEvent(Mastercam::IO::Types::MessageSeverityType::ErrorMessage, "Find Closed Chains Add-In", e->Message);
		}
		//return 
		//terminate the NEThook
		return Mastercam::App::Types::MCamReturn::NoErrors; 
	}

	};
}
#pragma endregion

for details on creating a nethook in c++ see this tutorial ->

 

Link to comment
Share on other sites
  • 2 weeks later...

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