//
//  offset.cpp - tool offset class implementation
//
//  $Id: offset.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* GeometryOffsetTableSection[] = {
 	"AGEOMETRYOFFSETTABLE",
	"BGEOMETRYOFFSETTABLE",
	"CGEOMETRYOFFSETTABLE",
	"DGEOMETRYOFFSETTABLE",
	"EGEOMETRYOFFSETTABLE",
	"FGEOMETRYOFFSETTABLE",
	"GGEOMETRYOFFSETTABLE",
	"HGEOMETRYOFFSETTABLE",
	"IGEOMETRYOFFSETTABLE",
	"JGEOMETRYOFFSETTABLE",
	"KGEOMETRYOFFSETTABLE",
	"LGEOMETRYOFFSETTABLE",
	"MGEOMETRYOFFSETTABLE",
	"NGEOMETRYOFFSETTABLE",
	"OGEOMETRYOFFSETTABLE",
	"PGEOMETRYOFFSETTABLE",
	"QGEOMETRYOFFSETTABLE",
	"RGEOMETRYOFFSETTABLE",
	"SGEOMETRYOFFSETTABLE",
	"TGEOMETRYOFFSETTABLE",
	"UGEOMETRYOFFSETTABLE",
	"VGEOMETRYOFFSETTABLE",
	"WGEOMETRYOFFSETTABLE",
	"XGEOMETRYOFFSETTABLE",
	"YGEOMETRYOFFSETTABLE",
	"ZGEOMETRYOFFSETTABLE"
};
									
static char* WearOffsetTableSection[] = {
 	"AWEAROFFSETTABLE",
	"BWEAROFFSETTABLE",
	"CWEAROFFSETTABLE",
	"DWEAROFFSETTABLE",
	"EWEAROFFSETTABLE",
	"FWEAROFFSETTABLE",
	"GWEAROFFSETTABLE",
	"HWEAROFFSETTABLE",
	"IWEAROFFSETTABLE",
	"JWEAROFFSETTABLE",
	"KWEAROFFSETTABLE",
	"LWEAROFFSETTABLE",
	"MWEAROFFSETTABLE",
	"NWEAROFFSETTABLE",
	"OWEAROFFSETTABLE",
	"PWEAROFFSETTABLE",
	"QWEAROFFSETTABLE",
	"RWEAROFFSETTABLE",
	"SWEAROFFSETTABLE",
	"TWEAROFFSETTABLE",
	"UWEAROFFSETTABLE",
	"VWEAROFFSETTABLE",
	"WWEAROFFSETTABLE",
	"XWEAROFFSETTABLE",
	"YWEAROFFSETTABLE",
	"ZWEAROFFSETTABLE"
};

static char* WearFactorTableSection[] = {
 	"AWEARFACTORTABLE",
	"BWEARFACTORTABLE",
	"CWEARFACTORTABLE",
	"DWEARFACTORTABLE",
	"EWEARFACTORTABLE",
	"FWEARFACTORTABLE",
	"GWEARFACTORTABLE",
	"HWEARFACTORTABLE",
	"IWEARFACTORTABLE",
	"JWEARFACTORTABLE",
	"KWEARFACTORTABLE",
	"LWEARFACTORTABLE",
	"MWEARFACTORTABLE",
	"NWEARFACTORTABLE",
	"OWEARFACTORTABLE",
	"PWEARFACTORTABLE",
	"QWEARFACTORTABLE",
	"RWEARFACTORTABLE",
	"SWEARFACTORTABLE",
	"TWEARFACTORTABLE",
	"UWEARFACTORTABLE",
	"VWEARFACTORTABLE",
	"WWEARFACTORTABLE",
	"XWEARFACTORTABLE",
	"YWEARFACTORTABLE",
	"ZWEARFACTORTABLE"
};

static char* OffsetTableEntry[] = {
	"00","01","02","03","04","05","06","07","08","09",
	"10","11","12","13","14","15","16","17","18","19",
	"20","21","22","23","24","25","26","27","28","29",
	"30","31","32","33","34","35","36","37","38","39",
	"40","41","42","43","44","45","46","47","48","49",
	"50","51","52","53","54","55","56","57","58","59",
	"60","61","62","63","64","65","66","67","68","69",
	"70","71","72","73","74","75","76","77","78","79",
	"80","81","82","83","84","85","86","87","88","89",
	"90","91","92","93","94","95","96","97","98","99"
};

OffsetTable& OffsetTable::operator=(const OffsetTable& rhs)
{
	if (this != &rhs)
		for (int index = 0; index < OFFSET_SIZE; index++)
		{
			Geometry[index] = rhs.Geometry[index];
			Wear[index] = rhs.Wear[index];
			WearFactor[index] = rhs.WearFactor[index];
		}
	return *this;
}

void OffsetTable::Init(char new_id)
{
	m_offset_id = new_id;			// Set ID for this Tool

	// Geometry
	char* section = GeometryOffsetTableSection[GetIndex()];
	for (int index = 0; index < OFFSET_SIZE; index++)
	{
		// Initialize Geometry member Application Variables so they can save and restore themselves	
		char* entry = OffsetTableEntry[index];
		Geometry[index].Init(UNDEFINED_DIM_UNIT, 0.0, section, entry);
	}
	
	// Wear
	section = WearOffsetTableSection[GetIndex()];
	for (int index = 0; index < OFFSET_SIZE; index++)
	{
		// Initialize Wear member Application Variables so they can save and restore themselves	
		char* entry = OffsetTableEntry[index];
		Wear[index].Init(UNDEFINED_DIM_UNIT, 0.0, section, entry);
	}
	
	// Wear Factor
	section = WearFactorTableSection[GetIndex()];
	for (int index = 0; index < OFFSET_SIZE; index++)
	{
		// Initialize Wear Factor member Application Variables so they can save and restore themselves	
		char* entry = OffsetTableEntry[index];
		WearFactor[index].Init(0.0, section, entry);
	}
}

void OffsetTable::Initialize(void)
{
	for (int index = 0; index < OFFSET_SIZE; index++)
	{
		Geometry[index].Set(DEFAULT_DIM_UNIT, 0.0);
		Wear[index].Set(DEFAULT_DIM_UNIT, 0.0);
		WearFactor[index] = 0.0;
	}
}

void OffsetTable::SaveData(const char* ini_file)
{
	CMachine* machine = theApp.GetMachine();
	int precision = machine->GetSetupPrecision();
	
	// Save Geometry data
	Geometry[0].Erase(ini_file);		// clear first
	for (int index = 0; index < OFFSET_SIZE; index++)
		Geometry[index].Save(ini_file, precision);
	
	// Save Wear data
	Wear[0].Erase(ini_file);			// clear first
	for (int index = 0; index < OFFSET_SIZE; index++)
		Wear[index].Save(ini_file, precision);
		
	// Save Wear data
	WearFactor[0].Erase(ini_file);		// clear first
	for (int index = 0; index < OFFSET_SIZE; index++)
		WearFactor[index].Save(ini_file, precision);
}

void OffsetTable::RestoreData(const char* ini_file)
{
	for (int index = 0; index < OFFSET_SIZE; index++)
	{
		Geometry[index].Restore(ini_file);
		Wear[index].Restore(ini_file);
		WearFactor[index].Restore(ini_file);
	}
}

void OffsetTable::Configure(std::fstream& cfg_file)
{
	DIM_UNIT DimUnit;
	
	// Geometry Offset data in both english and metric units
	cfg_file << static_cast<char>(m_offset_id) << "GEOMETRYOFFSETS" << std::endl;
	for (int units = ENGLISH; units <= METRIC; units++)
	{
		cfg_file << (const char*)UnitsName[units] << std::endl;

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

		for (int index = 0; index < OFFSET_SIZE; index++)
			cfg_file << index << "=" << Geometry[index].Get(DimUnit) << std::endl;
	}
	
	// Wear Offset data in both english and metric units
	cfg_file << static_cast<char>(m_offset_id) << "WEAROFFSETS" << std::endl;
	for (int units = ENGLISH; units <= METRIC; units++)
	{
		cfg_file << (const char*)UnitsName[units] << std::endl;

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

		for (int index = 0; index < OFFSET_SIZE; index++)
			cfg_file << index << "=" << Wear[index].Get(DimUnit) << std::endl;
	}
	
	// Wear Factor data is the same for english and metric units
	cfg_file << static_cast<char>(m_offset_id) << "WEARFACTORS" << std::endl;
	for (int index = 0; index < OFFSET_SIZE; index++)
		cfg_file << index << "=" << static_cast<double>(WearFactor[index]) << std::endl;
}
