📄 line.cpp
字号:
// Line.cpp: implementation of the CLine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Cad.h"
#include "Entity.h"
#include "Line.h"
#include "CadDoc.h"
#include "CadView.h"
#include <math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
_declspec(dllexport) CLine::CLine()
{
m_dSx=m_dSy=0.0;
m_dEx=m_dEy=0.0;
m_nType=ID_DRAW_LINE;
m_nColor=RGB(0,0,0);
m_nLineType=PS_SOLID;
}
_declspec(dllexport) CLine::CLine(double dSx,double dSy,double dEx,double dEy)
{
m_dSx=dSx;
m_dSy=dSy;
m_dEx=dEx;
m_dEy=dEy;
m_nType=ID_DRAW_LINE;
m_nColor=g_nCurColor;
m_nLineType=g_nCurLineType;
}
_declspec(dllexport) CLine::~CLine()
{
}
_declspec(dllexport) BOOL CLine::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_dSx=g_dCurX;
m_dSy=g_dCurY;
g_nStep++;
pStatus->SetPaneText(0,"Enter the end point");
break;
case 1:
m_dEx=g_dCurX;
m_dEy=g_dCurY;
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(prePoint);
//绘制新的线
pDC->MoveTo(orgPoint);
pDC->LineTo(curPoint);
//设置先前的绘图方式
pDC->SetROP2(nDrawMode);
}
return ret;
}
_declspec(dllexport) void CLine::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 point;
g_pCurView->WorldToScreen(point,m_dSx,m_dSy);
pDC->MoveTo(point);
g_pCurView->WorldToScreen(point,m_dEx,m_dEy);
pDC->LineTo(point);
pDC->SelectObject( pOldPen );
}
_declspec(dllexport) BOOL CLine::OSnap()
{
double d;
double sd=g_nOSnapSize*g_pCurView->m_dScreenToWorld;
double x[3],y[3];
int cnt=0;
BOOL ret=FALSE;
switch(g_nOpqTask)
{
case ID_OSNAP_ENDPOINT:
x[cnt]=m_dSx;
y[cnt]=m_dSy;
cnt++;
x[cnt]=m_dEx;
y[cnt]=m_dEy;
cnt++;
break;
case ID_OSNAP_MIDPOINT:
x[cnt]=(m_dSx+m_dEx)/2.0;
y[cnt]=(m_dSy+m_dEy)/2.0;
cnt++;
break;
default:
if((g_nOSnapType&1)==1)
{
x[cnt]=m_dSx;
y[cnt]=m_dSy;
cnt++;
x[cnt]=m_dEx;
y[cnt]=m_dEy;
cnt++;
}
if((g_nOSnapType&2)==2)
{
x[cnt]=(m_dSx+m_dEx)/2.0;
y[cnt]=(m_dSy+m_dEy)/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 CLine::Pick()
{
BOOL ret=FALSE;
double x=g_dCurX-m_dSx;
double y=g_dCurY-m_dSy;
double a=m_dEx-m_dSx;
double b=m_dEy-m_dSy;
double c=sqrt(a*a+b*b);
double sin_theta=b/c;
double cos_theta=a/c;
double chg_x=x*cos_theta+y*sin_theta;
double chg_y=-x*sin_theta+y*cos_theta;
double sd=5.0*g_pCurView->m_dScreenToWorld;
if(fabs(chg_y)<sd&&(chg_x>=0&&chg_x<=c))
ret=TRUE;
return ret;
}
_declspec(dllexport) void operator <<(CArchive& ar,CLine& it)
{
ar<<it.m_dEx;
ar<<it.m_dEy;
ar<<it.m_dSx;
ar<<it.m_dSy;
}
_declspec(dllexport) void operator >>(CArchive& ar,CLine& it)
{ ar>>it.m_dEx;
ar>>it.m_dEy;
ar>>it.m_dSx;
ar>>it.m_dSy;
}
_declspec(dllexport) void CLine::Serialize(CArchive& ar)
{AFX_MANAGE_STATE(AfxGetStaticModuleState());
CEntity::Serialize(ar);
if(ar.IsStoring())
ar<<*this;
else
ar>>*this;
}
_declspec(dllexport) CLine& CLine::operator=(CLine& in)
{
return in;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -