-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinCadView.cpp
302 lines (241 loc) · 7.4 KB
/
MinCadView.cpp
1
2
3
4
5
6
7
8
9
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// MinCadView.cpp: CMinCadView 类的实现
//
#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MinCad.h"
#endif
#include "MinCadDoc.h"
#include "MinCadView.h"
// include my class
#include"CLine.h"
#include"CPoint3D.h"
#include "CQuad.h"
#include "CCube.h"
#include "CCamera.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMinCadView
IMPLEMENT_DYNCREATE(CMinCadView, CView)
BEGIN_MESSAGE_MAP(CMinCadView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CMinCadView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_CUBE, drawIDCCube)// 画cube的响应函数
ON_COMMAND(ID_Sphere, &CMinCadView::OnSphere)
ON_COMMAND(ID_SphereHide, &CMinCadView::OnSpherehide)
END_MESSAGE_MAP()
// CMinCadView 构造/析构
CMinCadView::CMinCadView() noexcept
{
// TODO: 在此处添加构造代码
rButtonDown = false;
startPoint.x = 0;
startPoint.y = 0;
endPoint.x = 0;
endPoint.y = 0;
firstRbuttonDown = true;
testAxisCal = true;
drawCube = false;
drawSphere = false;
drawSpherHide = false;
}
CMinCadView::~CMinCadView()
{
}
BOOL CMinCadView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CView::PreCreateWindow(cs);
}
// CMinCadView 绘图
void CMinCadView::OnDraw(CDC* pDC)
{
CMinCadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
DoubleBuffer(pDC);
}
// CMinCadView 打印
void CMinCadView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CMinCadView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CMinCadView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void CMinCadView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
void CMinCadView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
rButtonDown = false;
firstRbuttonDown = true;
//endPoint.x = point.x;
//endPoint.y = point.y;
ClientToScreen(&point);
//
//OnContextMenu(this, point);
//Invalidate(FALSE);
}
void CMinCadView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CMinCadView 诊断
#ifdef _DEBUG
void CMinCadView::AssertValid() const
{
CView::AssertValid();
}
void CMinCadView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMinCadDoc* CMinCadView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMinCadDoc)));
return (CMinCadDoc*)m_pDocument;
}
#endif //_DEBUG
// CMinCadView 消息处理程序
//双缓冲就是,有两款内存区域,交互着使用,防止屏幕画图时闪烁
void CMinCadView::DoubleBuffer(CDC *pDC)
{
// TODO: 在此处添加实现代码.
CRect rect;//定义客户区矩形
GetClientRect(&rect);//获得客户区的大小
pDC->SetMapMode(MM_ANISOTROPIC);// 自定义坐标系
pDC->SetWindowExt(rect.Width(), rect.Height());//设置窗口范围
pDC->SetViewportExt(rect.Width(), -rect.Height());//设置视区范围
pDC->SetViewportOrg(rect.Width() / 2, rect.Height() / 2);//客户区中心为原点
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);//创建一个与显示pDC兼容的内存memDC
CBitmap NewBitmap, * pOldBitmap;//内存中承载的临时位图
NewBitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());//创建兼容位图
pOldBitmap = memDC.SelectObject(&NewBitmap);//将兼容位图选入memDC
memDC.FillSolidRect(rect, pDC->GetBkColor());//按原来背景填充客户区,否则是黑色
memDC.SetMapMode(MM_ANISOTROPIC);//memDC自定义坐标系
memDC.SetWindowExt(rect.Width(), rect.Height());
memDC.SetViewportExt(rect.Width(), -rect.Height());
memDC.SetViewportOrg(rect.Width() / 2, rect.Height() / 2);
rect.OffsetRect(-rect.Width() / 2, -rect.Height() / 2);
////更新局部旋转坐标
if ((startPoint != endPoint))
{
CPoint2D startP;
CPoint2D endP;
startP.x = startPoint.x - rect.Width() / 2;
startP.y = startPoint.y - rect.Height() / 2;
startP.y = - startP.y;
endP.x = endPoint.x - rect.Width() / 2;
endP.y = endPoint.y - rect.Height() / 2;
endP.y = -endP.y;
acube.arcBallRotate(startP, endP, rect.Width() / 2);
}
if (this->drawCube) {
CPoint3D bLeftBottom(-20, -20, -20);
CPoint3D bLeftUp(-20, -20, 20);
CPoint3D bRightBottom(-20, 20, -20);
CPoint3D bRightUP(-20, 20, 20);
CPoint3D fLeftBottom(20, -20, -20);
CPoint3D fLeftUp(20, -20, 20);
CPoint3D fRightBottom(20, 20, -20);
CPoint3D fRightUP(20, 20, 20);
CCube cube(bLeftBottom, bRightBottom, bRightUP, bLeftUp, fLeftBottom, fRightBottom, fRightUP, fLeftUp);
cube.drawCube(rect.Height(), rect.Width(), acube, &memDC);
}
else if (this->drawSpherHide) {
CSphere csPhere(40, 12, 6);
csPhere.hideDraw(rect.Height(), rect.Width(), acube, &memDC);
}
else if (this->drawSphere) {
CSphere csPhere(40, 12, 6);
csPhere.noHideDraw(rect.Height(), rect.Width(), acube, &memDC);
}
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, -rect.Width() / 2, -rect.Height() / 2, SRCCOPY);//将内存memDC中的位图拷贝到显示pDC中
memDC.SelectObject(pOldBitmap);//恢复位图
NewBitmap.DeleteObject();//删除位图
}
void CMinCadView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
rButtonDown = true;
startPoint.x = point.x;
startPoint.y = point.y;
CView::OnRButtonDown(nFlags, point);
}
void CMinCadView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (rButtonDown)
{
if (firstRbuttonDown)
{
if ((abs(point.x - startPoint.x) + abs(point.y - startPoint.y) > 10)) {
firstRbuttonDown = false;
endPoint.x = point.x;
endPoint.y = point.y;
Invalidate(FALSE);
}
}
else {
startPoint = endPoint;
if ((abs(point.x - startPoint.x) + abs(point.y - startPoint.y) > 10)) {
endPoint.x = point.x;
endPoint.y = point.y;
Invalidate(FALSE);
}
}
}
else {
endPoint = startPoint;
}
CView::OnMouseMove(nFlags, point);
}
void CMinCadView::OnSphere()
{
// TODO: 在此添加命令处理程序代码
this->drawSphere ? this->drawSphere = false : this->drawSphere =true;
drawCube = false;
drawSpherHide = false;
Invalidate(FALSE);
}
void CMinCadView::drawIDCCube()
{
this->drawCube ? this->drawCube = false : this->drawCube = true;
drawSphere = false;
drawSpherHide = false;
Invalidate(FALSE);
}
void CMinCadView::OnSpherehide()
{
// TODO: 在此添加命令处理程序代码
this->drawSpherHide ? this->drawSpherHide = false : this->drawSpherHide = true;
drawSphere = false;
drawCube = false;
Invalidate(FALSE);
}