//
//	Copyright © 2005 Nemetschek North America 
//	All Rights Reserved.
//
// Created by Vladislav Stanev

#include "StdAfx.h"

using namespace VWFC::Math;
using namespace VWFC::VWObjects;

VWLine2DObj::VWLine2DObj(double x1, double y1, double x2, double y2)
{
	fhObject		= ::GS_CreateLine( gCBP, WorldPt(x1,y1), WorldPt(x2,y2) );
	VWFC_ASSERT( fhObject );
	if ( fhObject == NULL )
		THROW_VWFC_EXCEPTION( VWCannotCreateObjectException, 0, "cannot create line2D" );
}

VWLine2DObj::VWLine2DObj(const VWPoint2D& startPt, const VWPoint2D& endPt)
{
	fhObject		= ::GS_CreateLine( gCBP, startPt, endPt );
	VWFC_ASSERT( fhObject );
	if ( fhObject == NULL )
		THROW_VWFC_EXCEPTION( VWCannotCreateObjectException, 0, "cannot create line2D" );
}

VWLine2DObj::VWLine2DObj(const VWLine2D& line2DMath)
{
	fhObject		= ::GS_CreateLine( gCBP, line2DMath.GetStart(), line2DMath.GetEnd() );
	VWFC_ASSERT( fhObject );
	if ( fhObject == NULL )
		THROW_VWFC_EXCEPTION( VWCannotCreateObjectException, 0, "cannot create line2D" );
}

VWLine2DObj::VWLine2DObj(MCObjectHandle hLine2D)
{
	VWFC_ASSERT( VWLine2DObj::IsLine2DObject( hLine2D ) );
	if ( ! VWLine2DObj::IsLine2DObject( hLine2D ) )
		THROW_VWFC_EXCEPTION( VWBadObjectTypeException, 0, "bad handle type when creating" );
	fhObject		= hLine2D;
	VWFC_ASSERT( fhObject != nil );
}

VWLine2DObj::VWLine2DObj(const VWLine2DObj& src)
{
	fhObject	= src.fhObject;
}

VWLine2DObj::~VWLine2DObj()
{
	fhObject		= nil;
}

VWLine2DObj& VWLine2DObj::operator=(const VWLine2DObj& src)
{
	fhObject	= src.fhObject;
	return *this;
}

bool VWLine2DObj::IsLine2DObject(MCObjectHandle hObj)
{
	short	type	= ::GS_GetObjectType( gCBP, hObj );
	return type == kLineNode;
}

void VWLine2DObj::SetStartPoint(const VWPoint2D& pt)
{
	VWFC_ASSERT( fhObject );
	WorldPt			startPt;
	WorldPt			endPt;
	::GS_GetEndPoints( gCBP, fhObject, startPt, endPt );
	::GS_SetEndPoints( gCBP, fhObject, pt, endPt );
}

void VWLine2DObj::SetEndPoint(const VWPoint2D& pt)
{
	VWFC_ASSERT( fhObject );
	WorldPt			startPt;
	WorldPt			endPt;
	::GS_GetEndPoints( gCBP, fhObject, startPt, endPt );
	::GS_SetEndPoints( gCBP, fhObject, startPt, pt );
}

void VWLine2DObj::SetLine(const VWPoint2D& startPt, const VWPoint2D& endPt)
{
	VWFC_ASSERT( fhObject );
	::GS_SetEndPoints( gCBP, fhObject, startPt, endPt );
}

void VWLine2DObj::SetLine(const VWLine2D& line2DMath)
{
	this->SetLine( line2DMath.GetStart(), line2DMath.GetEnd() );
}

void VWLine2DObj::SetLine(const VWLine2DObj& line2D)
{
	VWFC_ASSERT( fhObject );
	WorldPt			startPt;
	WorldPt			endPt;
	::GS_GetEndPoints( gCBP, line2D.fhObject, startPt, endPt );
	::GS_SetEndPoints( gCBP, fhObject, startPt, endPt );
}

VWPoint2D VWLine2DObj::GetStartPoint() const
{
	WorldPt			startPt;
	WorldPt			endPt;
	VWFC_ASSERT( fhObject );
	::GS_GetEndPoints( gCBP, fhObject, startPt, endPt );
	return startPt;
}

VWPoint2D VWLine2DObj::GetEndPoint() const
{
	WorldPt			startPt;
	WorldPt			endPt;
	VWFC_ASSERT( fhObject );
	::GS_GetEndPoints( gCBP, fhObject, startPt, endPt );
	return endPt;
}

void VWLine2DObj::GetLine(VWPoint2D& outStartPt, VWPoint2D& outEndPt)
{
	VWFC_ASSERT( fhObject );
	WorldPt		a, b;
	::GS_GetEndPoints( gCBP, fhObject, a, b );
	outStartPt		= a;
	outEndPt		= b;
}

void VWLine2DObj::GetLine(VWLine2D& outLine2DMath)
{
	this->GetLine( outLine2DMath.GetStart(), outLine2DMath.GetEnd() );
}
