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:

Nethook Nesting : C++/CLI


Recommended Posts

Hi,

I am working on my C++/Cli NEThook project, here is what I have so far :

using Mastercam::Support::Configuration;
using Mastercam::Database::ChainManager;
using Mastercam::Database::CurveGeometry;
using Mastercam::IO::LevelsManager;
using Mastercam::Database::Chain;
using Mastercam::App::NetHook3App;
using Mastercam::Tools::EndMillFlatTool;
using APIToolsNative::APITools;
using System::Double;
using Mastercam::Support::Configuration;
using Mastercam::Database::ChainManager;
using Mastercam::Database::CurveGeometry;
using Mastercam::IO::LevelsManager;
using Mastercam::Database::Chain;
using Mastercam::App::NetHook3App;
using Mastercam::Tools::EndMillFlatTool;
using NETHookNesting::NestingConstants;
using Mastercam::App::Types::MCamReturn;
namespace NETHookNesting
{

	/// <summary>
	/// The nesting constants.
	/// </summary>
	ref class NestingConstants
	{
		//#region Public Constants

			/// <summary>
			/// The nest list file.
			/// </summary>
	public: static initonly System::String ^ NestListExampleFile = "NestListExample.ini";

			/// <summary>
			/// The drawings source folder.
			/// </summary>
	public: static initonly System::String ^  DrawingsSourceFolder = "mcx\\Drawings\\";

			/// <summary>
			/// The metric nesting defaults file.
			/// </summary>
	public: static initonly System::String ^  MetricNestingDefaultsFile = "Nesting\\NESTING_MM.DEFX";

			/// <summary>
			/// The english nesting defaults file.
			/// </summary>
	public: static initonly System::String ^  EnglishNestingDefaultsFile = "Nesting\\NESTING.DEFX";

			/// <summary>
			/// The ignore grain direction.
			/// </summary>
	public: static initonly System::String ^  IgnoreGrainDirection = "N";

			/// <summary>
			/// The horizontal grain direction.
			/// </summary>
	public: static initonly System::String ^  HorizontalGrainDirection = "X";

			/// <summary>
			/// The vertical grain direction.
			/// </summary>
	public: static initonly System::String ^  VerticalGrainDirection = "Y";

			/// <summary>
			/// The Mastercam file pattern.
			/// </summary>
	public: static initonly System::String ^  MastercamFilePattern = "*.mcam*";

	};


	/// <summary>
	/// The nesting manager.
	/// </summary>
	static ref class NestRunner
	{
		//#region Public Methods

			/// <summary>
			/// Writes a nest list to disk.
			/// </summary>
			/// 
			/// <param name="nestingFile">
			/// The nesting payload.
			/// </param>
			/// 
			/// <param name="nestlistFile">
			/// The name of the nestlist file.
			/// </param>
			/// 
			/// <returns>
			/// True if the nestlist was successfully created, false otherwise.
			/// </returns>
	public: static initonly  Mastercam::App::Types::MCamReturn WriteNestlist(Mastercam::Nesting::NestingFile ^ nestingFile, System::String ^ nestlistFile)
	{
		// Write the file to disk
		return !nestingFile->Commit(nestlistFile) ? Mastercam::App::Types::MCamReturn::ErrorOccurred : Mastercam::App::Types::MCamReturn::NoErrors;
	}

			/// <summary>
			/// Executes a nestist file.
			/// </summary>
			/// 
			/// <param name="nestlist">
			/// The name of the nestlist file.
			/// </param>
			/// 
			/// <param name="hide">
			/// Choose whether to display or hide the nesting dialog during nesting.
			/// </param>
			/// 
			/// <returns>
			/// True if successful, false otherwise.
			/// </returns>
	public: static initonly Mastercam::App::Types::MCamReturn ExecuteNestList(System::String ^ nestlist, bool hide)
	{
		// Execute the nestlist file
		return !Mastercam::Operations::OperationsManager::DoNesting(nestlist, hide) ? Mastercam::App::Types::MCamReturn::ErrorOccurred : Mastercam::App::Types::MCamReturn::NoErrors;
	}
	};
	using NETHookNesting::NestingConstants;
	ref class NestingFunctions
	{
	public : static array<System::String ^>^ BuildList()
		{
			// Path to our drawings per readme file, e.g. C:\Users\mg\Documents\my mcam2017\mcx\Drawings
			auto pathToDrawings = System::IO::Path::Combine(Mastercam::IO::SettingsManager::UserDirectory, NestingConstants::DrawingsSourceFolder);
			auto list = System::IO::Directory::GetFiles(pathToDrawings, NestingConstants::MastercamFilePattern);

			// Example files are X8 files, adjust the file pattern for a different file type
			return  list;
		}
	};
}

public: virtual Mastercam::App::Types::MCamReturn RunNesting(int param) override {
	/////////////////////////////////////////////////////////////////////////////////////////////
       
	const auto drawings = NETHookNesting::NestingFunctions::BuildList();

	// Sanity check
	if (!drawings->Length > 0)
	{
		System::Windows::Forms::MessageBox::Show("We need a drawing!");
		return Mastercam::App::Types::MCamReturn::ErrorOccurred;
	}
	auto nestingDefaultsFile = System::IO::Path::Combine(Mastercam::IO::SettingsManager::SharedDirectory, Mastercam::IO::SettingsManager::Metric ? NETHookNesting::NestingConstants::MetricNestingDefaultsFile : NETHookNesting::NestingConstants::EnglishNestingDefaultsFile);
	auto nestListFile = System::IO::Path::Combine(System::IO::Path::Combine(Mastercam::IO::SettingsManager::UserDirectory, "data"), NETHookNesting::NestingConstants::NestListExampleFile);
	auto nestingfile = CreateNEstingFile(nestingDefaultsFile);
	for (auto i = 0; i < drawings->Length; i++)
	{
		auto part = gcnew Mastercam::Nesting::NestingPart;
		part->PartLabel->Data = i.ToString(System::Globalization::CultureInfo::InvariantCulture);
		part->Priority->Data = 1;
		part->Quantity->Data = 1;
		part->MinQuantity->Data = 1;
		part->Mirror->Data = false;

		if (nestingfile->Params->GrainDirection->Data == NestingConstants::IgnoreGrainDirection)
		{
			part->GrainDirection->Data = NestingConstants::IgnoreGrainDirection;
			part->StepAngle->Data = 90;
		}
		if (nestingfile->Params->GrainDirection->Data == NestingConstants::HorizontalGrainDirection)
		{
			part->GrainDirection->Data = NestingConstants::HorizontalGrainDirection;
			part->StepAngle->Data = 180;
		}
		if (nestingfile->Params->GrainDirection->Data == NestingConstants::VerticalGrainDirection)
		{
			part->GrainDirection->Data = NestingConstants::VerticalGrainDirection;
			part->StepAngle->Data = 180;

		}
		// Add the part to our nestlist
		nestingfile->AddPart(part);
	}
	if (NETHookNesting::NestRunner::WriteNestlist(nestingfile, nestListFile) == MCamReturn::ErrorOccurred)
	{
		System::Windows::Forms::MessageBox::Show("We need a drawing!");
		return MCamReturn::ErrorOccurred;
	}

	// Execute the nestlist
	if (NETHookNesting::NestRunner::ExecuteNestList(nestListFile, true) == MCamReturn::ErrorOccurred)
	{
		return MCamReturn::ErrorOccurred;
	}

	// Fit the screen
	Mastercam::IO::GraphicsManager::FitScreen();
	
	return Mastercam::App::Types::MCamReturn::NoErrors;
}

 

Link to comment
Share on other sites
Mastercam::Nesting::NestingFile ^ CreateNEstingFile(System::String ^ nestingDefaultsFile)
        {
	auto nestingfile = gcnew Mastercam::Nesting::NestingFile(nestingDefaultsFile);
	//return 
	array<Mastercam::Nesting::NestingPart^>^ partArray;
	nestingfile->PartArray = partArray;
	array<Mastercam::Nesting::NestingSheet^>^ sheetArray;
	nestingfile->SheetArray = sheetArray;
	// Properties that fall under the [PARAMETERS] category of the ini file that pertain to the nest as a whole.
	nestingfile->Params->Toolpaths->Data = true; // NOTE: Set to 'false' for geometry nesting only
	nestingfile->Params->AddLabels->Data = true;
	nestingfile->Params->LabelHeightAuto->Data = true;
	nestingfile->Params->WorkOffsets->Data = 0;
	nestingfile->Params->WorkOffsetNumbering->Data = 2;
	nestingfile->Params->AutoOrigins->Data = true;
	nestingfile->Params->GrainDirection->Data = NETHookNesting::NestingConstants::IgnoreGrainDirection;
	nestingfile->Params->PartToPartDistance->Data = 0.010;
	nestingfile->Params->SortMinToolChange->Data = true;
	nestingfile->Params->SeperateOpsPerSheet->Data = true;

	return nestingfile;
	
}

 

Link to comment
Share on other sites
7 minutes ago, Roger Martin from CNC Software said:

Your array items are null -


array<Mastercam::Nesting::NestingPart^>^ partArray;    // Not gcnew initialized
nestingfile->PartArray = partArray;
array<Mastercam::Nesting::NestingSheet^>^ sheetArray;  // Not gcnew initialized
nestingfile->SheetArray = sheetArray;

 

Yeah, I had to go read the msdn docs a bit it was giving me an error when I tried to gcnew the array my syntax was off and it was giving me a warning.

Link to comment
Share on other sites

Mastercam::Nesting::NestingFile ^ CreateNEstingFile(System::String ^ nestingDefaultsFile)

{
	//array<System::String^>^ myArray = gcnew array<System::String^> {"first", "second"};
	auto nestingfile = gcnew Mastercam::Nesting::NestingFile(nestingDefaultsFile);

	//return 

	nestingfile->PartArray = gcnew array<Mastercam::Nesting::NestingPart^>{};

	nestingfile->SheetArray = gcnew array<Mastercam::Nesting::NestingSheet^>{}Array;

	// Properties that fall under the [PARAMETERS] category of the ini file that pertain to the nest as a whole.

	nestingfile->Params->Toolpaths->Data = true; // NOTE: Set to 'false' for geometry nesting only

	nestingfile->Params->AddLabels->Data = true;

	nestingfile->Params->LabelHeightAuto->Data = true;

	nestingfile->Params->WorkOffsets->Data = 0;

	nestingfile->Params->WorkOffsetNumbering->Data = 2;

	nestingfile->Params->AutoOrigins->Data = true;

	nestingfile->Params->GrainDirection->Data = NETHookNesting::NestingConstants::IgnoreGrainDirection;

	nestingfile->Params->PartToPartDistance->Data = 0.010;

	nestingfile->Params->SortMinToolChange->Data = true;

	nestingfile->Params->SeperateOpsPerSheet->Data = true;



	return nestingfile;



}

 

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