rectangle.cpp

来自「故障诊断工作涉及的领域相当广泛」· C++ 代码 · 共 294 行

CPP
294
字号
// Rectangle.cpp: implementation of the CRectangle class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Cad.h"

#include "Entity.h"
#include "Rectangle.h"
#include "CadDoc.h"
#include "CadView.h"

#include "FillDlg.h"

#include <math.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

_declspec(dllexport) CRectangle::CRectangle()
{
	m_dLeft=m_dTop=0;
	m_dRight=m_dBottom=0;

	m_nType=ID_DRAW_RECTANGLE;
	m_nColor=RGB(0,0,0);
	m_nLineType=PS_SOLID;
}

_declspec(dllexport) CRectangle::CRectangle(double dLeft,double dTop,double dRight,
			double dBottom,UINT nFillPattern)
{
	m_dLeft=dLeft;
	m_dTop=dTop;
	m_dRight=dRight;
	m_dBottom=dBottom;
	m_nFillPattern=nFillPattern;

	m_nType=g_nCurTask;
	m_nColor=g_nCurColor;
	m_nLineType=g_nCurLineType;
}

_declspec(dllexport) CRectangle::~CRectangle()
{

}

_declspec(dllexport) BOOL CRectangle::Create(CDC* pDC,BOOL bSure)
{
	BOOL	ret=FALSE;

	if(bSure==TRUE)
	{
		CStatusBar* pStatus=(CStatusBar*)
			AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR);
		ASSERT(pStatus);

		switch(g_nStep)
		{
		case 0:
			m_dLeft=g_dCurX;
			m_dTop=g_dCurY;
			g_nStep++;
			pStatus->SetPaneText(0,"Enter the second point");
			break;
		case 1:
			m_dRight=g_dCurX;
			m_dBottom=g_dCurY;
			if(g_nCurTask==ID_DRAW_FILLED_RECTANGLE)
			{
				CFillDlg	dlg;
				dlg.DoModal();
				m_nFillPattern=dlg.m_nCurPattern;
			}

			g_nStep=0;
			pStatus->SetPaneText(0,"Enter the first point");
			ret=TRUE;
			break;
		default:
			break;
		}
	}
	else
	{
		if(g_nStep!=1) return FALSE;
		//设置R2_NOT的绘图方式
		int nDrawMode=pDC->SetROP2(R2_NOT);

		CPoint	orgPoint,curPoint,prePoint;
		g_pCurView->WorldToScreen(orgPoint,g_dOrgX,g_dOrgY);
		g_pCurView->WorldToScreen(curPoint,g_dCurX,g_dCurY);
		g_pCurView->WorldToScreen(prePoint,g_dPreX,g_dPreY);
		//覆盖先前的线
		pDC->MoveTo(orgPoint);
		pDC->LineTo(orgPoint.x,prePoint.y);
		pDC->LineTo(prePoint);
		pDC->LineTo(prePoint.x,orgPoint.y);
		pDC->LineTo(orgPoint);
		//绘制新的线
		pDC->MoveTo(orgPoint);
		pDC->LineTo(orgPoint.x,curPoint.y);
		pDC->LineTo(curPoint);
		pDC->LineTo(curPoint.x,orgPoint.y);
		pDC->LineTo(orgPoint);
		//设置先前的绘图方式
		pDC->SetROP2(nDrawMode); 
	}
	return ret;
}

_declspec(dllexport) void CRectangle::Draw(CDC* pDC,UINT nColor,UINT nLineType)
{
	CPen*	pNewPen=new CPen; 
	if(nColor==0&&nLineType==0)
		pNewPen->CreatePen(m_nLineType,1,m_nColor);
	else
		pNewPen->CreatePen(nLineType,1,nColor);
	CPen*	pOldPen = pDC->SelectObject( pNewPen );
	
	CPoint	pt1,pt2;
	g_pCurView->WorldToScreen(pt1,m_dLeft,m_dTop);
	g_pCurView->WorldToScreen(pt2,m_dRight,m_dBottom);
	if(m_nType==ID_DRAW_RECTANGLE)
	{
		pDC->MoveTo(pt1);
		pDC->LineTo(pt1.x,pt2.y);
		pDC->LineTo(pt2);
		pDC->LineTo(pt2.x,pt1.y);
		pDC->LineTo(pt1);
	}
	else
	{
		CBrush* pNewBrush=new CBrush;
		if(nColor==0&&nLineType==0)
			pNewBrush->CreateHatchBrush(m_nFillPattern,m_nColor);
		else
			pNewBrush->CreateHatchBrush(m_nFillPattern,nColor);
		CBrush* pOldBrush=pDC->SelectObject(pNewBrush);
		pDC->Rectangle(pt1.x,pt1.y,pt2.x,pt2.y);
		pDC->SelectObject(pOldBrush);
		delete	pNewBrush;	
	}

	pDC->SelectObject( pOldPen );
}

_declspec(dllexport) BOOL CRectangle::OSnap()
{
	double	d;
	double	sd=g_nOSnapSize*g_pCurView->m_dScreenToWorld;
	double	x[8],y[8];
	int		cnt=0;	
	BOOL	ret=FALSE;
	
	switch(g_nOpqTask)
	{
	case ID_OSNAP_ENDPOINT:
		x[cnt]=m_dLeft;
		y[cnt]=m_dTop;
		cnt++;
		x[cnt]=m_dRight;
		y[cnt]=m_dBottom;
		cnt++;
		x[cnt]=m_dLeft;
		y[cnt]=m_dBottom;
		cnt++;
		x[cnt]=m_dRight;
		y[cnt]=m_dTop;
		cnt++;
		break;
	case ID_OSNAP_MIDPOINT:
		x[cnt]=(m_dLeft+m_dRight)/2.0;
		y[cnt]=m_dTop;
		cnt++;
		x[cnt]=(m_dLeft+m_dRight)/2.0;
		y[cnt]=m_dBottom;
		cnt++;
		x[cnt]=m_dLeft;
		y[cnt]=(m_dTop+m_dBottom)/2.0;
		cnt++;
		x[cnt]=m_dRight;
		y[cnt]=(m_dTop+m_dBottom)/2.0;
		cnt++;
		break;
	default:
		if((g_nOSnapType&1)==1)
		{
			x[cnt]=m_dLeft;
			y[cnt]=m_dTop;
			cnt++;
			x[cnt]=m_dRight;
			y[cnt]=m_dBottom;
			cnt++;
			x[cnt]=m_dLeft;
			y[cnt]=m_dBottom;
			cnt++;
			x[cnt]=m_dRight;
			y[cnt]=m_dTop;
			cnt++;
		}
		if((g_nOSnapType&2)==2)
		{
			x[cnt]=(m_dLeft+m_dRight)/2.0;
			y[cnt]=m_dTop;
			cnt++;
			x[cnt]=(m_dLeft+m_dRight)/2.0;
			y[cnt]=m_dBottom;
			cnt++;
			x[cnt]=m_dLeft;
			y[cnt]=(m_dTop+m_dBottom)/2.0;
			cnt++;
			x[cnt]=m_dRight;
			y[cnt]=(m_dTop+m_dBottom)/2.0;
			cnt++;
		}
		break;
	}

	for(int i=0;i<cnt;i++)
	{
		d=GetTwoPntDis2(g_dCurX,g_dCurY,x[i],y[i]);
		if(d<sd*sd)
		{
			g_dCurX=x[i];
			g_dCurY=y[i];
			ret=TRUE;
			break;
		}
	}

	return ret;
}

_declspec(dllexport) BOOL CRectangle::Pick()
{
	BOOL	ret=FALSE;
	double	sd=5.0*g_pCurView->m_dScreenToWorld;

	if(fabs(g_dCurX-m_dLeft)<sd||fabs(g_dCurX-m_dRight)<sd)
	{
		if((g_dCurY>=m_dTop&&g_dCurY<=m_dBottom)
			||(g_dCurY<=m_dTop&&g_dCurY>=m_dBottom))
		{
			ret=TRUE;
		}
	}

	if(fabs(g_dCurY-m_dTop)<sd||fabs(g_dCurY-m_dBottom)<sd)
	{
		if((g_dCurX>=m_dLeft&&g_dCurX<=m_dRight)
			||(g_dCurX<=m_dLeft&&g_dCurX>=m_dRight))
		{
			ret=TRUE;
		}
	}

	return ret;			
}
_declspec(dllexport) void operator <<(CArchive& ar,CRectangle& it)
{	
	ar<<it.m_dBottom;
	ar<<it.m_dLeft;
	ar<<it.m_dRight;
	ar<<it.m_dTop;
	ar<<it.m_nFillPattern;
}
  _declspec(dllexport) void operator >>(CArchive& ar,CRectangle& it)
{	ar>>it.m_dBottom;
	ar>>it.m_dLeft;
	ar>>it.m_dRight;
	ar>>it.m_dTop;
	ar>>it.m_nFillPattern;
}
_declspec(dllexport) void CRectangle::Serialize(CArchive&  ar)
{AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CEntity::Serialize(ar);
if(ar.IsStoring())
 ar<<*this;
else
 ar>>*this;
}
_declspec(dllexport) CRectangle& CRectangle::operator=(CRectangle& in)
{
return in;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?