//
//  tool.h - tool implementation
//
//  $Id: tool.cpp 53 2014-02-07 19:39:59Z brian $ 
//
//  Copyright (c) 1999-2014 by Precision Micro Control Corp. All rights reserved.
//
#include "stdafx.h"

// Initialization file sections for each tool, these strings must exist as
// static data for Application Variable Classes. 
static char* ToolSection[] =  {
	"TOOL1", "TOOL2", "TOOL3",
	"TOOL4", "TOOL5", "TOOL6",
	"TOOL7", "TOOL8", "TOOL9"
};

Tool& Tool::operator=(const Tool& rhs)
{
	if (this != &rhs)
	{
		m_tool_id = rhs.m_tool_id;
		Description = rhs.Description;	
		X = rhs.X;
		Y = rhs.Y;
		Z = rhs.Z;
		R = rhs.R;
		W = rhs.W;
		F = rhs.F;
		Q = rhs.Q;
	}
	return *this;
}

void Tool::Init(int new_id)
{
	m_tool_id = new_id;				// Set ID for this Tool
	
	// Convert to array index
	char* section = ToolSection[GetIndex()];
	
	// Initialize member Application Variables so they can save and restore themselves	
	Description.Init("", section, TOOLDESCRIPTION_ENTRY);
	X.Init(UNDEFINED_DIM_UNIT, 0.0, section, X_TOOLOFFSET_ENTRY);
	Y.Init(UNDEFINED_DIM_UNIT, 0.0, section, Y_TOOLOFFSET_ENTRY);
	Z.Init(UNDEFINED_DIM_UNIT, 0.0, section, Z_TOOLOFFSET_ENTRY);
	R.Init(UNDEFINED_DIM_UNIT, 0.0, section, TOOLRADIUS_ENTRY);
	W.Init(UNDEFINED_DIM_UNIT, 0.0, section, TOOLWEAR_ENTRY);
	F.Init(0.0, section, TOOLWEARFACTOR_ENTRY);
	Q.Init(0, section, TOOLORIENTATION_ENTRY);
}

void Tool::Initialize(void)
{
	X.Set(DEFAULT_DIM_UNIT, 0.0);
	Y.Set(DEFAULT_DIM_UNIT, 0.0);
	Z.Set(DEFAULT_DIM_UNIT, 0.0);
	R.Set(DEFAULT_DIM_UNIT, 0.0);
	W.Set(DEFAULT_DIM_UNIT, 0.0);
	F = 0.0;
	Q = 0;
	Description = "";
}

void Tool::SaveData(const char* ini_file)
{
	int precision = theApp.GetMachine()->GetSetupPrecision();
	
	// Clear section for this tool
	X.Erase(ini_file);

	// Save data
	Description.Save(ini_file);
	X.Save(ini_file, precision);
	Y.Save(ini_file, precision);
	Z.Save(ini_file, precision);
	R.Save(ini_file, precision);
	W.Save(ini_file, precision);
	F.Save(ini_file, precision);
	Q.Save(ini_file);
}

void Tool::RestoreData(const char* ini_file)
{
	Description.Restore(ini_file);
	X.Restore(ini_file);
	Y.Restore(ini_file);
	Z.Restore(ini_file);
	R.Restore(ini_file);
	W.Restore(ini_file);
	F.Restore(ini_file);
	Q.Restore(ini_file);
}

// Generate configuration text for tool and place in file
void Tool::Configure(std::fstream& cfg_file)
{
	// Generate configuration string for motion controller
	cfg_file << "TOOL=" << m_tool_id << std::endl;
	
	// Configuration data in both english and metric units
	for (int units = ENGLISH; units <= METRIC; units++)
	{
		DIM_UNIT DimUnit;
		cfg_file << (const char*)UnitsName[units] << std::endl;

		if (units == ENGLISH)
			DimUnit = theApp.GetMachine()->PartPrgmEnglishDimUnit.Get();
		else
			DimUnit = theApp.GetMachine()->PartPrgmMetricDimUnit.Get();

		cfg_file << "X=" << X.Get(DimUnit) << std::endl;
		cfg_file << "Y=" << Y.Get(DimUnit) << std::endl;
		cfg_file << "Z=" << Z.Get(DimUnit) << std::endl;
		cfg_file << "R=" << R.Get(DimUnit) << std::endl;
		cfg_file << "W=" << W.Get(DimUnit) << std::endl;
	}
	
	// The following data is the same for english and metric units
	cfg_file << "F=" << (double)F << std::endl;
	cfg_file << "Q=" << (int)Q << std::endl;
}
