//
//  work_coords.h - work coordinates implementation
//
//  $Id: work_coords.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 axis, these strings must exist as
// static data for Application Variable Classes. 
static char *WorkCoorSection[] = {
	"WORKCOOR1", "WORKCOOR2", "WORKCOOR3",
	"WORKCOOR4", "WORKCOOR5", "WORKCOOR6"
};

WorkCoord& WorkCoord::operator=(const WorkCoord& rhs)
{
	if (this != &rhs)
	{
		m_work_coor_id = rhs.m_work_coor_id;
		
		X = rhs.X;
		Y = rhs.Y;
		Z = rhs.Z;
		U = rhs.U;
		V = rhs.V;
		W = rhs.W;
	}
	return *this;
}

void WorkCoord::Init(int new_id)
{
	m_work_coor_id = new_id;					// Set ID for this Work Coordinate
	
	// Convert to array index
	char* section = WorkCoorSection[GetIndex()];
	
	// Initialize member Application Variables so they can save and restore themselves	
	X.Init(UNDEFINED_DIM_UNIT, 0.0, section, X_WORKOFFSET_ENTRY);
	Y.Init(UNDEFINED_DIM_UNIT, 0.0, section, Y_WORKOFFSET_ENTRY);
	Z.Init(UNDEFINED_DIM_UNIT, 0.0, section, Z_WORKOFFSET_ENTRY);
	U.Init(UNDEFINED_DIM_UNIT, 0.0, section, U_WORKOFFSET_ENTRY);
	V.Init(UNDEFINED_DIM_UNIT, 0.0, section, V_WORKOFFSET_ENTRY);
	W.Init(UNDEFINED_DIM_UNIT, 0.0, section, W_WORKOFFSET_ENTRY);
}


void WorkCoord::Initialize(void)
{
	X.Set(DEFAULT_DIM_UNIT, 0.0);
	Y.Set(DEFAULT_DIM_UNIT, 0.0);
	Z.Set(DEFAULT_DIM_UNIT, 0.0);
	U.Set(DEFAULT_DIM_UNIT, 0.0);
	V.Set(DEFAULT_DIM_UNIT, 0.0);
	W.Set(DEFAULT_DIM_UNIT, 0.0);
}

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

	// Save data
	X.Save(ini_file, precision);
	Y.Save(ini_file, precision);
	Z.Save(ini_file, precision);
	U.Save(ini_file, precision);
	V.Save(ini_file, precision);
	W.Save(ini_file, precision);
}

void WorkCoord::RestoreData(const char* ini_file)
{
	X.Restore(ini_file);
	Y.Restore(ini_file);
	Z.Restore(ini_file);
	U.Restore(ini_file);
	V.Restore(ini_file);
	W.Restore(ini_file);
}

void WorkCoord::Configure(std::fstream& cfg_file)
{
	// Generate configuration string for motion controller
	cfg_file << "WORKCOOR=" << m_work_coor_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 << "U=" << U.Get(DimUnit) << std::endl;
		cfg_file << "V=" << V.Get(DimUnit) << std::endl;
		cfg_file << "W=" << W.Get(DimUnit) << std::endl;
	}
}
