0025773: Visualization - remove AIS_Drawer class and transfer its Link() logic to...
[occt.git] / samples / mfc / standard / 03_Viewer2d / src / Viewer2dDoc.cpp
CommitLineData
7fd59977 1// Viewer2dDoc.cpp : implementation of the CViewer2dDoc class
2//
3
4#include "stdafx.h"
7fd59977 5#include "Viewer2dDoc.h"
5c1f974e 6#include "OCC_App.h"
7fd59977 7#include "Primitive\Sample2D_Markers.h"
7fd59977 8#include "Primitive\Sample2D_Text.h"
9#include "Primitive\Sample2D_Face.h"
7fd59977 10#include "Primitive\Sample2D_Image.h"
11
7fd59977 12/////////////////////////////////////////////////////////////////////////////
13// CViewer2dDoc
14
15IMPLEMENT_DYNCREATE(CViewer2dDoc, CDocument)
16
17BEGIN_MESSAGE_MAP(CViewer2dDoc, CDocument)
18 //{{AFX_MSG_MAP(CViewer2dDoc)
7fd59977 19 ON_COMMAND(ID_BUTTON_Test_Text, OnBUTTONTestText)
20 ON_COMMAND(ID_BUTTON_Test_Markers, OnBUTTONTestMarkers)
21 ON_COMMAND(ID_BUTTON_Test_Line, OnBUTTONTestLine)
22 ON_COMMAND(ID_BUTTON_Erase, OnBUTTONErase)
5c1f974e 23 ON_COMMAND(ID_BUTTON_Test_Face, OnBUTTONTestFace)
7fd59977 24 ON_COMMAND(ID_BUTTON_Test_Rect, OnBUTTONTestRect)
25 ON_COMMAND(ID_BUTTON_Test_Curve, OnBUTTONTestCurve)
26 ON_COMMAND(ID_BUTTON32793, OnBUTTONTestImage) // test image
27 ON_COMMAND(ID_BUTTON_Test_MultiImages, OnBUTTONMultipleImage)
28
29 //}}AFX_MSG_MAP
30END_MESSAGE_MAP()
31
32/////////////////////////////////////////////////////////////////////////////
33// CViewer2dDoc construction/destruction
34
35CViewer2dDoc::CViewer2dDoc()
36{
37}
38
39CViewer2dDoc::~CViewer2dDoc()
40{
41}
42
43#ifdef _DEBUG
44void CViewer2dDoc::AssertValid() const
45{
46 CDocument::AssertValid();
47}
48
49void CViewer2dDoc::Dump(CDumpContext& dc) const
50{
51 CDocument::Dump(dc);
52}
53#endif //_DEBUG
54
55/////////////////////////////////////////////////////////////////////////////
56// CViewer2dDoc commands
57
7fd59977 58void CViewer2dDoc::OnBUTTONErase()
59{
5c1f974e 60 if(myAISContext->HasOpenedContext())
61 myAISContext->CloseAllContexts();
62 myAISContext->EraseAll();
7fd59977 63}
64
7fd59977 65void CViewer2dDoc::OnBUTTONTestText()
66{
5c1f974e 67 int aColor= 1;
7fd59977 68 Standard_Real j;
69 for (j=15;j<=20;j++)
70 {
5c1f974e 71 TCollection_AsciiString Text("font 0 scale ");Text+=j/20.0;
7fd59977 72 Handle (Sample2D_Text) aText =
73 new Sample2D_Text(Text,
5c1f974e 74 gp_Pnt(0.0,15.0*(j-15.0),0.0), // thePosition
5c1f974e 75 10.0*M_PI, // theAngle
76 (Quantity_NameOfColor)(aColor++), // theColor
77 Font_FA_Regular, // theFontAspect
78 "Courier", // theFont
79 j, // theScale
80 Graphic3d_HTA_LEFT,
81 Graphic3d_VTA_BOTTOM,
82 Standard_False); // aIsZoomable
83
84 myAISContext->Display(aText, Standard_False);
7fd59977 85 }
86
5c1f974e 87 for (j=10;j<=15;j++)
7fd59977 88 {
5c1f974e 89 TCollection_AsciiString Text("font 1 scale ");Text+=j/10.0;
7fd59977 90 Handle (Sample2D_Text) aText =
91 new Sample2D_Text(Text,
5c1f974e 92 gp_Pnt(80.,15.0*(j-10.0),0.0), // thePosition
5c1f974e 93 0.0, // theAngle
94 (Quantity_NameOfColor)(aColor++), // theColor
95 Font_FA_BoldItalic, // theFontAspect
96 "Cambria", // theFont
97 j*2, // theScale
98 Graphic3d_HTA_LEFT,
99 Graphic3d_VTA_BOTTOM,
100 Standard_False); // aIsZoomable
101
102 myAISContext->Display(aText, Standard_False);
7fd59977 103 }
5c1f974e 104 aColor = 1;
105 for (j=5;j<=10;j++)
7fd59977 106 {
5c1f974e 107 TCollection_AsciiString Text("font 2 scale ");Text+=j/10.0;
7fd59977 108 Handle (Sample2D_Text) aText =
109 new Sample2D_Text(Text,
5c1f974e 110 gp_Pnt(140.0,15.0*(j-5.0),0.0), // thePosition
5c1f974e 111 0.0, // theAngle
112 (Quantity_NameOfColor)(aColor++), // theColor
113 Font_FA_Bold, // theFontAspect
114 "Arial", // theFont
115 j*2, // theScale
116 Graphic3d_HTA_LEFT,
117 Graphic3d_VTA_BOTTOM,
118 Standard_False); // aIsZoomable
119 myAISContext->Display(aText,Standard_False);
7fd59977 120 }
5c1f974e 121for (j=10;j<=15;j++)
7fd59977 122 {
5c1f974e 123 TCollection_AsciiString Text("font 3 scale ");Text+=j/10.0;
124 Handle (Sample2D_Text) aText =
7fd59977 125 new Sample2D_Text(Text,
5c1f974e 126 gp_Pnt(200.0,15.0*(j-10.0),0.0), // thePosition
5c1f974e 127 0.0, // theAngle
128 (Quantity_NameOfColor)(aColor++),// theColor
129 Font_FA_Italic, // theFontAspect
130 "Georgia", // theFont
131 j*2, // theScale
132 Graphic3d_HTA_LEFT,
133 Graphic3d_VTA_BOTTOM,
134 Standard_False); // aIsZoomable
135 myAISContext->Display(aText,Standard_False);
7fd59977 136 }
137
138 FitAll2DViews(Standard_True); // Update Viewer
7fd59977 139}
140
141
142void CViewer2dDoc::OnBUTTONTestMarkers()
143{
5c1f974e 144 // generic Markers
145 Standard_Integer aColor = 20;
146 for (int i=1;i<=2;i++)
147 {
148 Handle (Sample2D_Markers) aMarker =
149 new Sample2D_Markers(10+5,5*i,Aspect_TOM_POINT,Quantity_NOC_YELLOW,2.0);
150 myAISContext->Display(aMarker, Standard_False);
151 }
152 for (int i=1;i<=2;i++)
153 {
154 Handle (Sample2D_Markers) aMarker =
155 new Sample2D_Markers(10+10,5*i,Aspect_TOM_O,(Quantity_NameOfColor)(aColor++));
156 myAISContext->Display(aMarker, Standard_False);
157 }
158 for (int i=1;i<=2;i++)
159 {
160 Handle (Sample2D_Markers) aMarker =
161 new Sample2D_Markers(10+15,5*i,Aspect_TOM_O_PLUS,(Quantity_NameOfColor)(aColor++));
162 myAISContext->Display(aMarker, Standard_False);
163 }
164 for (int i=1;i<=2;i++)
165 {
166 Handle (Sample2D_Markers) aMarker =
a577aaab 167 new Sample2D_Markers(10+20,5*i,Aspect_TOM_RING1,(Quantity_NameOfColor)(aColor++));
5c1f974e 168 myAISContext->Display(aMarker, Standard_False);
169 }
170 for (int i=1;i<=2;i++)
171 {
7fd59977 172
5c1f974e 173 Handle (Sample2D_Markers) aMarker =
174 new Sample2D_Markers(10+25,5*i,Aspect_TOM_STAR,(Quantity_NameOfColor)(aColor++));
175 myAISContext->Display(aMarker, Standard_False);
176 }
177 for (int i=1;i<=2;i++)
178 {
179 Handle (Sample2D_Markers) aMarker =
180 new Sample2D_Markers(10+30,5*i,Aspect_TOM_O_X,(Quantity_NameOfColor)(aColor++));
181 myAISContext->Display(aMarker, Standard_False);
182 }
7fd59977 183
184 FitAll2DViews(Standard_False); // Update Viewer
7fd59977 185}
7fd59977 186void CViewer2dDoc::OnBUTTONTestLine()
187{
5c1f974e 188 for (int i=0;i<=13;++i)
189 for (int j=0;j<=5;++j)
190 {
191 //set of rectangles here
192 TopoDS_Edge E1 = BRepBuilderAPI_MakeEdge(gp_Pnt(10*i,10*j,0.), gp_Pnt(10*i+7,10*j,0.));
193 TopoDS_Edge E2 = BRepBuilderAPI_MakeEdge(gp_Pnt(10*i+7,10*j,0.), gp_Pnt(10*i+7,10*j+5,0.));
194 TopoDS_Edge E3 = BRepBuilderAPI_MakeEdge(gp_Pnt(10*i+7,10*j+5,0.), gp_Pnt(10*i,10*j+5,0.));
195 TopoDS_Edge E4 = BRepBuilderAPI_MakeEdge(gp_Pnt(10*i,10*j+5,0.), gp_Pnt(10*i,10*j,0.));
196 TopoDS_Wire W = BRepBuilderAPI_MakeWire(E1,E2,E3,E4);
197 TopoDS_Face F = BRepBuilderAPI_MakeFace(W);
198 Handle_AIS_Shape aRect = new AIS_Shape(F);
199 //set attributes of boundaries
6262338c 200 Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer();
5c1f974e 201 Handle(Prs3d_LineAspect) aLineAttrib =
202 new Prs3d_LineAspect(Quantity_NOC_YELLOW,
203 (Aspect_TypeOfLine)(Aspect_TOL_SOLID+j),1);
204 aDrawer->SetFaceBoundaryAspect(aLineAttrib);
205 aDrawer->SetFaceBoundaryDraw(Standard_True);
206 aRect->SetAttributes(aDrawer);
207
208 myAISContext->SetDisplayMode(aRect,1);
209 myAISContext->SetColor(aRect,(Quantity_NameOfColor)(Quantity_NOC_CADETBLUE+2*i),Standard_False);
210 myAISContext->SetMaterial(aRect,Graphic3d_NOM_PLASTIC,Standard_False);
211 myAISContext->Display(aRect, Standard_False);
7fd59977 212
5c1f974e 213 }
214 this->FitAll2DViews(Standard_True);
215}
7fd59977 216
7fd59977 217
5c1f974e 218#include <AIS_Trihedron.hxx>
219#include <Geom_Axis2Placement.hxx>
7fd59977 220
5c1f974e 221void CViewer2dDoc::OnBUTTONTestFace()
7fd59977 222{
5c1f974e 223 //erase all
224 if(myAISContext->HasOpenedContext())
225 myAISContext->CloseAllContexts();
226 myAISContext->EraseAll();
227
7fd59977 228 CFileDialog dlg(TRUE,
5c1f974e 229 NULL,
230 NULL,
231 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
576f8b11 232 L"BRep Files (*.brep)|*.brep; ||",
5c1f974e 233 NULL );
7fd59977 234
5c1f974e 235 CString initdir(((OCC_App*) AfxGetApp())->GetInitDataDir());
7fd59977 236 initdir += "\\Data";
237
238 dlg.m_ofn.lpstrInitialDir = initdir;
239
240 if (dlg.DoModal() == IDOK)
241 {
242 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
576f8b11 243
244 std::filebuf aFileBuf;
245 std::istream aStream (&aFileBuf);
246 if (!aFileBuf.open (dlg.GetPathName(), ios::in))
247 {
248 AfxMessageBox (L"The shape must be not a null Face");
249 return;
250 }
7fd59977 251
5c1f974e 252 TopoDS_Shape aFaceShape;
253 BRep_Builder aBuilder;
576f8b11 254 BRepTools::Read (aFaceShape, aStream, aBuilder);
5c1f974e 255 if(aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE)
256 {
576f8b11 257 AfxMessageBox (L"The shape must be not a null Face");
5c1f974e 258 return;
259 }
7fd59977 260
5c1f974e 261 TopoDS_Face aFace = TopoDS::Face(aFaceShape);
262 Handle(Sample2D_Face) anAISFace = new Sample2D_Face(aFaceShape);
263 myAISContext->Display(anAISFace,Standard_True);
264 //activate selection mode for edges selection
265 myAISContext->OpenLocalContext();
266 myAISContext->Activate(anAISFace,2);
7fd59977 267
5c1f974e 268 FitAll2DViews(Standard_False);
7fd59977 269
7fd59977 270 }
7fd59977 271}
272
7fd59977 273
7fd59977 274
5c1f974e 275void CViewer2dDoc::OnBUTTONTestRect()
276{
277 //First rectangle
278 TopoDS_Edge E11 = BRepBuilderAPI_MakeEdge(gp_Pnt(40.,0.,0.), gp_Pnt(82.5,25.,0.));
279 TopoDS_Edge E12 = BRepBuilderAPI_MakeEdge(gp_Pnt(82.5,25.,0.), gp_Pnt(42.5,93.,0.));
280 TopoDS_Edge E13 = BRepBuilderAPI_MakeEdge(gp_Pnt(42.5,93.,0.), gp_Pnt(0.,68.,0.));
281 TopoDS_Edge E14 = BRepBuilderAPI_MakeEdge(gp_Pnt(0.,68.,0.), gp_Pnt(40.,0.,0.));
282 TopoDS_Wire W1 = BRepBuilderAPI_MakeWire(E11,E12,E13,E14);
283 Handle_AIS_Shape aRect1 = new AIS_Shape(W1);
284 myAISContext->Display(aRect1);
285 myAISContext->SetColor(aRect1,Quantity_NOC_YELLOW);
7fd59977 286
5c1f974e 287 //Second rectangle
288 TopoDS_Edge E21 = BRepBuilderAPI_MakeEdge(gp_Pnt(110.,0.,0.), gp_Pnt(152.5,25.,0.));
289 TopoDS_Edge E22 = BRepBuilderAPI_MakeEdge(gp_Pnt(152.5,25.,0.), gp_Pnt(112.5,93.,0.));
290 TopoDS_Edge E23 = BRepBuilderAPI_MakeEdge(gp_Pnt(112.5,93.,0.), gp_Pnt(70.,68.,0.));
291 TopoDS_Edge E24 = BRepBuilderAPI_MakeEdge(gp_Pnt(70.,68.,0.), gp_Pnt(110.,0.,0.));
292 TopoDS_Wire W2 = BRepBuilderAPI_MakeWire(E21,E22,E23,E24);
293 Handle_AIS_Shape aRect2 = new AIS_Shape(W2);
294 myAISContext->Display(aRect2);
295 myAISContext->SetColor(aRect2,Quantity_NOC_YELLOW);
296 myAISContext->SetSelectionMode(aRect2,2);
7fd59977 297
5c1f974e 298 FitAll2DViews(Standard_True); // Update Viewer
7fd59977 299}
300
301void CViewer2dDoc::OnBUTTONTestCurve()
302{
5c1f974e 303 for(int i=0;i<=5;++i)
304 for(int j=0;j<=5;++j)
305 {
306 Handle(Geom_Point) aStart = new Geom_CartesianPoint(gp_Pnt(10*i,10*j,0.));
307 Handle(Geom_Point) anEnd = new Geom_CartesianPoint(gp_Pnt(10*i+5,10*j+10,0.));
308 Handle(AIS_Line) aLine = new AIS_Line(aStart,anEnd);
309 Handle(Prs3d_LineAspect) aLineAttrib =
310 new Prs3d_LineAspect((Quantity_NameOfColor)(Quantity_NOC_CADETBLUE+2*i+2*j),
311 (Aspect_TypeOfLine)((Aspect_TOL_DASH+i+j)%5),2+i+j);
312 aLine->Attributes()->SetLineAspect(aLineAttrib);
313 myAISContext->Display(aLine,Standard_False);
314 }
315 FitAll2DViews(Standard_True);
7fd59977 316}
317
318void CViewer2dDoc::OnBUTTONTestImage()
319{
12c76bee 320 CFileDialog anOpenImageDlg (TRUE,
321 NULL,
322 NULL,
323 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
324 SupportedImageFormats() + "| all files (*.*)|*.*;||",
325 NULL);
326
327 CString anInitDir (((OCC_App*) AfxGetApp())->GetInitDataDir());
328 anInitDir += "\\Data";
329
330 anOpenImageDlg.m_ofn.lpstrInitialDir = anInitDir;
331 if(anOpenImageDlg.DoModal() == IDOK)
7fd59977 332 {
12c76bee 333 SetCursor(AfxGetApp()->LoadStandardCursor (IDC_WAIT));
334 CString aFilePath = anOpenImageDlg.GetPathName();
576f8b11 335 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )aFilePath);
336 TCollection_AsciiString aFileName (aFileNameW, '?');
7fd59977 337
5c1f974e 338 //erase viewer
339 if(myAISContext->HasOpenedContext())
340 myAISContext->CloseAllContexts();
341 myAISContext->EraseAll();
7fd59977 342
12c76bee 343 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
344 anImage->SetCoord (40,50) ;
345 anImage->SetScale (1.0);
346 myAISContext->Display (anImage, Standard_False);
347 myAISContext->SetDisplayMode (anImage,3,Standard_False);
348 FitAll2DViews (Standard_True);
7fd59977 349 }
350}
351
352void CViewer2dDoc::OnBUTTONMultipleImage()
353{
12c76bee 354 CFileDialog anOpenImageDlg (TRUE,
355 NULL,
356 NULL,
357 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
358 SupportedImageFormats() + "| all files (*.*)|*.*;||",
359 NULL);
7fd59977 360
12c76bee 361 CString anInitDir (((OCC_App*) AfxGetApp())->GetInitDataDir());
362 anInitDir += "\\Data";
7fd59977 363
12c76bee 364 anOpenImageDlg.m_ofn.lpstrInitialDir = anInitDir;
7fd59977 365
12c76bee 366 if (anOpenImageDlg.DoModal() == IDOK)
7fd59977 367 {
12c76bee 368 SetCursor(AfxGetApp()->LoadStandardCursor (IDC_WAIT));
369 CString aFilePath = anOpenImageDlg.GetPathName();
576f8b11 370 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )aFilePath);
371 TCollection_AsciiString aFileName (aFileNameW, '?');
12c76bee 372
5c1f974e 373 //erase viewer
374 if(myAISContext->HasOpenedContext())
375 myAISContext->CloseAllContexts();
376 myAISContext->EraseAll();
12c76bee 377
5c1f974e 378 //create images
7fd59977 379 { // 1
12c76bee 380 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
381 anImage->SetCoord (40, 50);
382 anImage->SetScale (0.5);
383 myAISContext->Display (anImage, Standard_False);
384 myAISContext->SetDisplayMode (anImage, 3, Standard_False);
7fd59977 385 }
386 { // 2
12c76bee 387 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
388 anImage->SetCoord (100, 50);
389 anImage->SetScale (0.9);
390 myAISContext->Display (anImage, Standard_False);
391 myAISContext->SetDisplayMode (anImage, 3, Standard_False);
7fd59977 392 }
393 { // 3
12c76bee 394 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
395 anImage->SetCoord (40, 40);
396 anImage->SetScale (0.3);
397 myAISContext->Display (anImage, Standard_False);
398 myAISContext->SetDisplayMode (anImage, 3, Standard_False);
7fd59977 399 }
400 { // 4
12c76bee 401 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
402 anImage->SetCoord (50, 40);
403 myAISContext->Display (anImage, Standard_False);
404 myAISContext->SetDisplayMode (anImage, 3, Standard_False);
7fd59977 405 }
406 { // 5
12c76bee 407 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
408 anImage->SetCoord (80, 45);
409 anImage->SetScale (2);
410 myAISContext->Display (anImage, Standard_False);
411 myAISContext->SetDisplayMode (anImage, 3, Standard_False);
7fd59977 412 }
5c1f974e 413 { // 6
12c76bee 414 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
415 anImage->SetCoord (20, -20);
416 myAISContext->Display (anImage, Standard_False);
417 myAISContext->SetDisplayMode (anImage, 3, Standard_False);
7fd59977 418 }
5c1f974e 419 { // 7
12c76bee 420 Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
421 anImage->SetCoord (0, 0);
422 anImage->SetScale (0.5);
423 myAISContext->Display (anImage, Standard_False);
424 myAISContext->SetDisplayMode (anImage, 3, Standard_False);
7fd59977 425 }
12c76bee 426 FitAll2DViews (Standard_True); // Update Viewer
7fd59977 427 }
428}