0027880: Samples - fix handling of Unicode paths within MFC import/export sample
[occt.git] / samples / mfc / standard / 03_Viewer2d / src / Viewer2dDoc.cpp
1 // Viewer2dDoc.cpp : implementation of the CViewer2dDoc class
2 //
3
4 #include "stdafx.h"
5 #include "Viewer2dDoc.h"
6 #include "OCC_App.h"
7 #include "Primitive\Sample2D_Markers.h"
8 #include "Primitive\Sample2D_Text.h"
9 #include "Primitive\Sample2D_Face.h"
10 #include "Primitive\Sample2D_Image.h"
11
12 /////////////////////////////////////////////////////////////////////////////
13 // CViewer2dDoc
14
15 IMPLEMENT_DYNCREATE(CViewer2dDoc, CDocument)
16
17 BEGIN_MESSAGE_MAP(CViewer2dDoc, CDocument)
18         //{{AFX_MSG_MAP(CViewer2dDoc)
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)
23         ON_COMMAND(ID_BUTTON_Test_Face, OnBUTTONTestFace)
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
30 END_MESSAGE_MAP()
31
32 /////////////////////////////////////////////////////////////////////////////
33 // CViewer2dDoc construction/destruction
34
35 CViewer2dDoc::CViewer2dDoc()
36 {
37 }
38
39 CViewer2dDoc::~CViewer2dDoc()
40 {
41 }
42
43 #ifdef _DEBUG
44 void CViewer2dDoc::AssertValid() const
45 {
46         CDocument::AssertValid();
47 }
48
49 void CViewer2dDoc::Dump(CDumpContext& dc) const
50 {
51         CDocument::Dump(dc);
52 }
53 #endif //_DEBUG
54
55 /////////////////////////////////////////////////////////////////////////////
56 // CViewer2dDoc commands
57
58 void CViewer2dDoc::OnBUTTONErase() 
59 {
60   if(myAISContext->HasOpenedContext())
61     myAISContext->CloseAllContexts(); 
62   myAISContext->EraseAll();
63 }
64
65 void CViewer2dDoc::OnBUTTONTestText() 
66 {
67  int aColor= 1;
68  Standard_Real j;
69  for (j=15;j<=20;j++)
70   {
71     TCollection_AsciiString Text("font 0 scale ");Text+=j/20.0;
72     Handle (Sample2D_Text) aText  = 
73         new Sample2D_Text(Text,
74                        gp_Pnt(0.0,15.0*(j-15.0),0.0),    //  thePosition
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);
85   }
86
87   for (j=10;j<=15;j++)
88   {
89     TCollection_AsciiString Text("font 1 scale ");Text+=j/10.0;
90     Handle (Sample2D_Text) aText  = 
91         new Sample2D_Text(Text,
92                        gp_Pnt(80.,15.0*(j-10.0),0.0),     //  thePosition
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);
103   }
104   aColor = 1;
105   for (j=5;j<=10;j++)
106   {
107     TCollection_AsciiString Text("font 2 scale ");Text+=j/10.0; 
108     Handle (Sample2D_Text) aText  = 
109         new Sample2D_Text(Text,
110                        gp_Pnt(140.0,15.0*(j-5.0),0.0),   //  thePosition
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);
120   }
121 for (j=10;j<=15;j++)
122   {
123     TCollection_AsciiString Text("font 3 scale ");Text+=j/10.0;
124         Handle (Sample2D_Text) aText  = 
125         new Sample2D_Text(Text,
126                        gp_Pnt(200.0,15.0*(j-10.0),0.0), //  thePosition
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);
136   }
137
138   FitAll2DViews(Standard_True); // Update Viewer
139 }
140
141
142 void CViewer2dDoc::OnBUTTONTestMarkers() 
143 {
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  = 
167       new Sample2D_Markers(10+20,5*i,Aspect_TOM_RING1,(Quantity_NameOfColor)(aColor++));
168     myAISContext->Display(aMarker, Standard_False);
169   }
170   for (int i=1;i<=2;i++)
171   {
172
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   }
183
184   FitAll2DViews(Standard_False); // Update Viewer
185 }
186 void CViewer2dDoc::OnBUTTONTestLine() 
187 {
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
200       Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer();
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);
212
213    }
214    this->FitAll2DViews(Standard_True);
215 }
216
217
218 #include <AIS_Trihedron.hxx>
219 #include <Geom_Axis2Placement.hxx>
220
221 void CViewer2dDoc::OnBUTTONTestFace()
222 {
223   //erase all
224   if(myAISContext->HasOpenedContext())
225     myAISContext->CloseAllContexts();
226   myAISContext->EraseAll();
227
228   CFileDialog dlg(TRUE,
229     NULL,
230     NULL,
231     OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
232     L"BRep Files (*.brep)|*.brep; ||",
233     NULL );
234
235   CString initdir(((OCC_App*) AfxGetApp())->GetInitDataDir());
236   initdir += "\\Data";
237
238   dlg.m_ofn.lpstrInitialDir = initdir;
239
240   if (dlg.DoModal() == IDOK) 
241   {
242     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
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     }
251
252     TopoDS_Shape aFaceShape;
253     BRep_Builder aBuilder;
254     BRepTools::Read (aFaceShape, aStream, aBuilder);
255     if(aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE) 
256     {
257       AfxMessageBox (L"The shape must be not a null Face");
258       return;
259     }
260
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);
267
268     FitAll2DViews(Standard_False);
269
270   }
271 }
272
273
274
275 void 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);
286   
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->Activate(aRect2,2);
297
298   FitAll2DViews(Standard_True); // Update Viewer
299 }
300
301 void CViewer2dDoc::OnBUTTONTestCurve()
302 {
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);
316 }
317
318 void CViewer2dDoc::OnBUTTONTestImage()
319 {
320   CFileDialog anOpenImageDlg (TRUE,
321                               NULL,
322                               NULL,
323                               OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
324                               SupportedImageFormats() + L"| 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)
332   {
333     SetCursor(AfxGetApp()->LoadStandardCursor (IDC_WAIT));
334     CString aFilePath = anOpenImageDlg.GetPathName();
335     TCollection_AsciiString aFileName ((const wchar_t* )aFilePath);
336
337     //erase viewer
338     if(myAISContext->HasOpenedContext())
339       myAISContext->CloseAllContexts();
340     myAISContext->EraseAll();
341
342     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
343     anImage->SetCoord (40,50) ;
344     anImage->SetScale (1.0);
345     myAISContext->Display (anImage, Standard_False);
346     myAISContext->SetDisplayMode (anImage,3,Standard_False);
347     FitAll2DViews (Standard_True);
348   }
349 }
350
351 void CViewer2dDoc::OnBUTTONMultipleImage()
352 {
353   CFileDialog anOpenImageDlg (TRUE,
354                               NULL,
355                               NULL,
356                               OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
357                               SupportedImageFormats() + L"| all files (*.*)|*.*;||",
358                               NULL);
359
360   CString anInitDir (((OCC_App*) AfxGetApp())->GetInitDataDir());
361   anInitDir += "\\Data";
362
363   anOpenImageDlg.m_ofn.lpstrInitialDir = anInitDir;
364
365   if (anOpenImageDlg.DoModal() == IDOK) 
366   {
367     SetCursor(AfxGetApp()->LoadStandardCursor (IDC_WAIT));
368     CString aFilePath = anOpenImageDlg.GetPathName();
369     TCollection_AsciiString aFileName ((const wchar_t* )aFilePath);
370
371     //erase viewer
372     if(myAISContext->HasOpenedContext())
373       myAISContext->CloseAllContexts();
374     myAISContext->EraseAll();
375
376     //create images
377     {  // 1
378     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
379     anImage->SetCoord (40, 50);
380     anImage->SetScale (0.5);
381     myAISContext->Display (anImage, Standard_False);
382     myAISContext->SetDisplayMode (anImage, 3, Standard_False);
383     }
384     {  // 2
385     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
386     anImage->SetCoord (100, 50);
387     anImage->SetScale (0.9);
388     myAISContext->Display (anImage, Standard_False);
389     myAISContext->SetDisplayMode (anImage, 3, Standard_False);
390     }
391     {  // 3
392     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
393     anImage->SetCoord (40, 40);
394     anImage->SetScale (0.3);
395     myAISContext->Display (anImage, Standard_False);
396     myAISContext->SetDisplayMode (anImage, 3, Standard_False);
397     }
398     {  // 4
399     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
400     anImage->SetCoord (50, 40);
401     myAISContext->Display (anImage, Standard_False);
402     myAISContext->SetDisplayMode (anImage, 3, Standard_False);
403     }
404     {  // 5
405     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
406     anImage->SetCoord (80, 45);
407     anImage->SetScale (2);
408     myAISContext->Display (anImage, Standard_False);
409     myAISContext->SetDisplayMode (anImage, 3, Standard_False);
410     }
411     {  // 6
412     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
413     anImage->SetCoord (20, -20);
414     myAISContext->Display (anImage, Standard_False);
415     myAISContext->SetDisplayMode (anImage, 3, Standard_False);
416     }
417     {  // 7
418     Handle(Sample2D_Image) anImage = new Sample2D_Image (aFileName);
419     anImage->SetCoord (0, 0);
420     anImage->SetScale (0.5);
421     myAISContext->Display (anImage, Standard_False);
422     myAISContext->SetDisplayMode (anImage, 3, Standard_False);
423     }
424     FitAll2DViews (Standard_True); // Update Viewer
425   }
426 }