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:

exchange tool with mastercam


Recommended Posts

Roger,

thank you very much for your quick response and the mail.

I tried the sample #1. It compiles, but in Mastercam it crashes at the moment of calling the read_ent(..)-function.

Maybe it's a problem of my Visual Studio 2003.

I read in another thread, that Visual Studio 2005 is recommended. Hope, that my order for VS2005 is quickly fullfilled, so that i can give you a positive feedback.

Link to comment
Share on other sites
  • 1 month later...
  • 11 years later...

I know this is a really old post but it addresses what I need to do.  I have SDK 2020 and want to create a CHOOK which I can use to exchange data between a TXT file and a Mastercam database.  Can anyone give some sample code on how to accomplish?

Thanks,

Marie

Link to comment
Share on other sites
On 3/25/2020 at 4:48 PM, MarieA said:

  I have SDK 2020 and want to create a CHOOK which I can use to exchange data between a TXT file and a Mastercam database.  Can anyone give some sample code on how to accomplish?

Hi Marie,

I created a sample of the workflow needed to read information from a text file and save it into a user defined struct:

The example is a console application

Main.Cpp

#include <windows.h>
#include <atlstr.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <filesystem>
#include <conio.h>
#include <string>
#include <iostream>
#include <clocale>
#include <locale>
#include <vector>


struct EndmillTool
{

	int toolnumber;
	int spindlespeed;
	double feedrate;
	double diameter;
	double length;
	double flutelength;
};
#ifdef UNICODE //Test to see if we're using wchar_ts or not.
typedef std::wstring StringType;
#else
typedef std::string StringType;
#endif
double StringToDouble(StringType input)
{
	CString c_str = input.c_str();
	// Convert a TCHAR string to a LPCSTR
	CT2CA pszConvertedAnsiString(c_str);
	// construct a std::string using the LPCSTR input
	std::string strStd(pszConvertedAnsiString);
	return atof(strStd.c_str()); /*c_str is needed to convert string to const char*/
}
int StringToInt(StringType input)
{
	CString c_str = input.c_str();
	// Convert a TCHAR string to a LPCSTR
	CT2CA pszConvertedAnsiString(c_str);
	// construct a std::string using the LPCSTR input
	std::string strStd(pszConvertedAnsiString);
	return atoi(strStd.c_str()); /*c_str is needed to convert string to const char*/
}

StringType GetExtension(StringType filename)
{
	//store the position of last '.' in the file name
	int position = filename.find_last_of(_T("."));

	//store the characters after the '.' from the file_name string
	StringType result = filename.substr(position + 1);

	//print the result
	return result;
}
StringType GetBaseFilename(StringType filename)
{
	size_t pos = filename.rfind(_T("."));
	if (pos == StringType::npos)  //No extension.
		return filename;

	if (pos == 0)    //. is at the front. Not an extension.
		return filename;

	return filename.substr(0, pos);
}
StringType GetCurrentPath() 
{
#ifdef UNICODE //Test to see if we're using wchar_ts or not.
	wchar_t buffer[MAX_PATH];
#else
	char buffer[MAX_PATH];
#endif
	GetModuleFileName(NULL, buffer, MAX_PATH);
	StringType::size_type pos = StringType(buffer).find_last_of(_T("\\/"));
	return StringType(buffer).substr(0, pos);
}
std::vector<StringType> GetListOfFiles(StringType path)
{
	std::vector<StringType>stringvector;
	WIN32_FIND_DATA fileData;
	memset(&fileData, 0, sizeof(WIN32_FIND_DATA));
	CString string;
	string.Format(_T("%s\\*"),path.c_str());
	HANDLE handle = FindFirstFile(string, &fileData);

	if (handle != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (_tcscmp(fileData.cFileName, _T(".")) != 0 && // ignore "." and ".."
				_tcscmp(fileData.cFileName, _T("..")) != 0)
			{
				stringvector.push_back(fileData.cFileName);
			}
		} while (FindNextFile(handle, &fileData));

		FindClose(handle);
	}

	return stringvector;
}
bool PathExists(const StringType &s)
{
	struct stat buffer;

	CString c_str = s.c_str();
	// Convert a TCHAR string to a LPCSTR
	CT2CA pszConvertedAnsiString(c_str);
	// construct a std::string using the LPCSTR input
	std::string strStd(pszConvertedAnsiString);
	return (stat(strStd.c_str(), &buffer) == 0);
}
inline bool FileExists(const StringType& name) 
{
	CString c_str = name.c_str();
	// Convert a TCHAR string to a LPCSTR
	CT2CA pszConvertedAnsiString(c_str);
	// construct a std::string using the LPCSTR input
	std::string strStd(pszConvertedAnsiString);
	struct stat buffer;
	return (stat(strStd.c_str(), &buffer) == 0);
}
std::vector<StringType> GetLinesFromFile(StringType filename)
{

	auto count = 0;
	std::vector<StringType> stringvector;
	if (FileExists(filename))
	{
		std::ifstream file(filename);
		std::string str;
		while (std::getline(file, str))
		{
			CString c_str = str.c_str();
			StringType stringtype(c_str);
			stringvector.push_back(stringtype);
			count++;
			// Process str
		}
	}
	return count > 0 ? stringvector : std::vector<StringType>(0);
}



typedef int(*f_funci)();


std::vector<StringType> split(StringType stringToBeSplitted, StringType delimeter)
{
	std::vector<StringType> splittedString;
	int startIndex = 0;
	int  endIndex = 0;
	while ((endIndex = stringToBeSplitted.find(delimeter, startIndex)) < stringToBeSplitted.size())
	{

		StringType val = stringToBeSplitted.substr(startIndex, endIndex - startIndex);
		splittedString.push_back(val);
		startIndex = endIndex + delimeter.size();

	}
	if (startIndex < stringToBeSplitted.size())
	{
		StringType val = stringToBeSplitted.substr(startIndex);
		splittedString.push_back(val);
	}
	return splittedString;

}
int main()
{
	std::vector<EndmillTool> toolvector;
	//get out current working directory
	auto path = GetCurrentPath();
	//get the list of the files in our current working directory
	auto list = GetListOfFiles(path);
	for (auto listitem : list)
	{
	    //get the current files extension
		auto extension = GetExtension(listitem);
		//get the current filename without extension
		auto basefilename = GetBaseFilename(listitem);
		//filter only text files
		StringType targetextension = _T("txt");
		if (extension == targetextension)
		{
			if (basefilename == _T("ToolDefinition"))
			{
				//get a vector of lines from the current file
				auto lines = GetLinesFromFile(listitem);
				if (!lines.empty())
				{
					for (auto line : lines)
					{
						EndmillTool tool;
						auto count = 0;
						//get a vector of each "word" in the current line
						auto splitlines = split(line, _T(" "));
						for (auto splitline : splitlines)
						{
							//use a switch statement to assign values based on the position of the number in the line
							switch (count)
							{
							case 0:
								tool.toolnumber = StringToInt(splitline);
								break;
							case 1:
								tool.spindlespeed = StringToInt(splitline);
								break;
							case 2:
								tool.feedrate = StringToDouble(splitline);
								break;
							case 3:
								tool.diameter = StringToDouble(splitline);
								break;
							case 4:
								tool.length = StringToDouble(splitline);
								break;
							case 5:
								tool.flutelength = StringToDouble(splitline);
								break;


							}
							//CString strMessage;
							//strMessage.Format(_T("Base FileName = %s Extension = %s Line = %s SplitLine = %s"), basefilename.c_str(), extension.c_str(), line.c_str(), splitline.c_str());
							//auto pszFileName = _T("File");
							//auto msgboxID = MessageBox(NULL, strMessage, pszFileName, MB_OK);
							count++;
						}

						toolvector.push_back(tool);
					}
				}
			}
		}
	}
	for (auto tool : toolvector)
	{
		CString strMessage;
		strMessage.Format(_T("<Tool # = %d> <SpindleSpeed = %d> <Feedrate = %lf> <Diameter = %lf> <length = %lf> <flutelength = %lf>"), tool.toolnumber,tool.spindlespeed,tool.feedrate,tool.diameter,tool.length,tool.flutelength);
		auto pszFileName = _T("File");
		auto msgboxID = MessageBox(NULL, strMessage, pszFileName, MB_OK);			
	}
	return 0;
}

In the folder where the application runs it looks for a .txt file named ToolDefinition.txt

Inside is this line 3 22000 420.0 0.25 2.0 .5

The line is divided by whitespace and converted into integers and floating point numbers as required.

I added the example to my GitHub here -> https://github.com/PeterRussellEvans/LoadToolDataFromTextFile

Here is a short video :

 

 

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