0027827: Samples - fix compilation errors in XAML (UWP) sample.
[occt.git] / samples / xaml / MainPage.xaml.cpp
CommitLineData
742cc8b0 1//
2// MainPage.xaml.cpp
3// Implementation of the MainPage class.
4//
5
6#include "pch.h"
7#include "MainPage.xaml.h"
8
9using namespace uwp;
10
11using namespace Platform;
12using namespace Windows::Foundation;
13using namespace Windows::Foundation::Collections;
14using namespace Windows::UI::ViewManagement;
15using namespace Windows::UI::Xaml;
16using namespace Windows::UI::Xaml::Controls;
17using namespace Windows::UI::Xaml::Controls::Primitives;
18using namespace Windows::UI::Xaml::Data;
19using namespace Windows::UI::Xaml::Input;
20using namespace Windows::UI::Xaml::Media;
21using namespace Windows::UI::Xaml::Navigation;
22
23using namespace Windows::UI::Popups;
24
25#include <BRepAlgo.hxx>
26#include <BRepAlgoAPI_Fuse.hxx>
27#include <BRepMesh_IncrementalMesh.hxx>
28#include <BRepPrimAPI_MakeBox.hxx>
29#include <BRepPrimAPI_MakeSphere.hxx>
30#include <BRepTools.hxx>
31#include <Geom2dAPI_PointsToBSpline.hxx>
32#include <Geom2d_BSplineCurve.hxx>
33#include <Geom2d_OffsetCurve.hxx>
34#include <gp_Pnt.hxx>
35#include <IGESControl_Controller.hxx>
36#include <IGESControl_Reader.hxx>
37#include <IGESControl_Writer.hxx>
38#include <Interface_Static.hxx>
39#include <OSD_Directory.hxx>
40#include <OSD_File.hxx>
41#include <OSD_Path.hxx>
42#include <OSD_Protection.hxx>
43#include <STEPControl_Reader.hxx>
44#include <STEPControl_Writer.hxx>
45#include <StlAPI_Writer.hxx>
46#include <TCollection_AsciiString.hxx>
47#include <TCollection_ExtendedString.hxx>
48#include <TopExp_Explorer.hxx>
49#include <TopoDS.hxx>
50#include <TopoDS_Shape.hxx>
51#include <VrmlAPI_Writer.hxx>
52
53#include <Strsafe.h>
54
55//=======================================================================
56//function : MainPage
57//purpose :
58//=======================================================================
59MainPage::MainPage()
60{
61 InitializeComponent();
62 ApplicationView::PreferredLaunchViewSize = Size(1000, 500);
63 ApplicationView::PreferredLaunchWindowingMode = ApplicationViewWindowingMode::PreferredLaunchViewSize;
64}
65
66//=======================================================================
67//function : Throw
68//purpose : Test offset functionality
69//=======================================================================
70void MainPage::OnClickOffset(Platform::Object^ theSender,
71 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
72{
73 TColgp_Array1OfPnt2d array(1, 5); // sizing array
74 array.SetValue(1, gp_Pnt2d(-4, 0)); array.SetValue(2, gp_Pnt2d(-7, 2));
75 array.SetValue(3, gp_Pnt2d(-6, 3)); array.SetValue(4, gp_Pnt2d(-4, 3));
76 array.SetValue(5, gp_Pnt2d(-3, 5));
77 Handle(Geom2d_BSplineCurve) SPL1 = Geom2dAPI_PointsToBSpline(array);
78
79 Standard_Real dist = 1;
80 Handle(Geom2d_OffsetCurve) OC =
81 new Geom2d_OffsetCurve(SPL1, dist);
82 Standard_Real dist2 = 1.5;
83 Handle(Geom2d_OffsetCurve) OC2 =
84 new Geom2d_OffsetCurve(SPL1, dist2);
85
86 Platform::String ^aMessage;
87
88 if (OC.IsNull()) {
89 aMessage = "Error: could not create offset curve with offset distance " + dist;
90 } else if (OC2.IsNull()) {
91 aMessage = "Error: could not create offset curve with offset distance 1.5" + dist2;
92 } else {
93 aMessage = "Result: Two offset curves OC and OC2 were successfully created from source curve SPL1. \n";
94 }
95 Output_TextBlock->Text = aMessage;
96}
97
98//=======================================================================
99//function : OnClickMesh
100//purpose : Test mesh functionality
101//=======================================================================
102void MainPage::OnClickMesh(Platform::Object^ theSender,
103 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
104{
105 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200, 60, 60);
106 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80);
107 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere, theBox);
108 BRepMesh_IncrementalMesh(ShapeFused, 1);
109
110 Standard_Integer result(0);
111
112 for (TopExp_Explorer ex(ShapeFused, TopAbs_FACE); ex.More(); ex.Next()) {
113 TopoDS_Face F = TopoDS::Face(ex.Current());
114 TopLoc_Location L;
115 Handle(Poly_Triangulation) facing = BRep_Tool::Triangulation(F, L);
116 result = result + facing->NbTriangles();
117 }
118
119 Platform::String ^aMessage;
120 if (ShapeFused.IsNull()) {
121 aMessage = "Error: could not fuse source shapes";
122 } else if (result == 0) {
123 aMessage = "Error: mesh could not be created";
124 } else {
125 aMessage = "Result: Mesh was created\
126--- Number of created triangles ---\n";
127
128 aMessage += result;
129 aMessage += ("\n\n");
130 }
131 Output_TextBlock->Text = aMessage;
132}
133
134//=======================================================================
135//function : OnClickBoolean
136//purpose : Test boolean operations
137//=======================================================================
138void MainPage::OnClickBoolean(Platform::Object^ theSender,
139 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
140{
141 TCollection_AsciiString asd;
142 gp_Pnt P(-5, 5, -5);
143 TopoDS_Shape theBox1 = BRepPrimAPI_MakeBox(60, 200, 70).Shape();
144
145 TopoDS_Shape theBox2 = BRepPrimAPI_MakeBox(P, 20, 150, 110).Shape();
146
147 TopoDS_Shape FusedShape = BRepAlgoAPI_Fuse(theBox1, theBox2);
148
149 Platform::String ^aMessage;
150
151 if (FusedShape.IsNull()) {
152 aMessage = "Error: could not fuse shapes theBox1 and theBox2";
153 } else {
154 aMessage = "Result: Shapes were successfully fused. \n";
155 }
156 Output_TextBlock->Text = aMessage;
157}
158
159//=======================================================================
160//function : OnClickBuildTemporary
161//purpose : Test OSD_File::BuildTemporary() method
162//=======================================================================
163void MainPage::OnClickBuildTemporary(Platform::Object^ theSender,
164 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
165{
166 OSD_File theTmpFile;
167 theTmpFile.BuildTemporary();
168 Standard_Boolean fKO = theTmpFile.Failed();
169 if (fKO)
170 {
171 Output_TextBlock->Text = "Error: cannot create temporary file";
172 } else {
173 OSD_Path theTmpFilepath;
174 theTmpFile.Path(theTmpFilepath);
175 TCollection_AsciiString theTmpFilepathTrek;
176 theTmpFilepath.SystemName(theTmpFilepathTrek);
177 wchar_t wchar_str[MAX_PATH];
178 StringCchCopy(wchar_str, _countof(wchar_str), L"Result: ");
179 TCollection_ExtendedString aWName(theTmpFilepathTrek);
180 StringCchCat(wchar_str, _countof(wchar_str), (const wchar_t*)aWName.ToExtString());
181 Platform::String^ p_string = ref new Platform::String(wchar_str);
182 Output_TextBlock->Text = p_string;
183 }
184}
185
186//=======================================================================
187//function : OnClickDataExchange
188//purpose : Test data exchange functionality
189//=======================================================================
190void MainPage::OnClickDataExchange(Platform::Object^ theSender,
191 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
192{
193 Output_TextBlock->Text = "";
194
195 // Create box for export
196 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200, 60, 60);
197
198 // Create temporary directory
199 wchar_t tmpPath[MAX_PATH];
200 wchar_t filePath[MAX_PATH];
201 char tmpPathA[MAX_PATH];
202
203 if (!GetTempPathW(_countof(tmpPath), tmpPath)) {
204 StringCchCopyW(tmpPath, _countof(tmpPath), L"./");
205 }
206
207 WideCharToMultiByte(CP_UTF8, 0, tmpPath, -1, tmpPathA, sizeof(tmpPathA), NULL, NULL);
208 OSD_Path tmpDirPath(tmpPathA);
209 OSD_Directory tmpDir(tmpDirPath);
210
211 OSD_Protection srt;
212 tmpDir.Build(srt);
213
214 // Export box to .brep
215 StringCchCopyW(filePath, _countof(filePath), tmpPath);
216 StringCchCatW(filePath, _countof(filePath), L"/box.brep");
217 if (SaveBREP(filePath, theBox))
218 Output_TextBlock->Text += L"OK: export to .brep\n";
219 // Import from .brep
220 TopoDS_Shape theBoxFromBrep;
221 if (ReadBREP(filePath, theBoxFromBrep))
222 Output_TextBlock->Text += L"OK: import from .brep\n";
223 else
224 Output_TextBlock->Text += L"Error: import from .brep\n";
225
226 // Export box to .iges
227 StringCchCopyW(filePath, _countof(filePath), tmpPath);
228 StringCchCatW(filePath, _countof(filePath), L"/box.iges");
229 if (SaveIGES(filePath, theBox))
230 Output_TextBlock->Text += L"OK: export to .iges\n";
231 // Import from .iges
232 TopoDS_Shape theBoxFromIges;
233 if (ReadIGES(filePath, theBoxFromIges))
234 Output_TextBlock->Text += L"OK: import from .iges\n";
235 else
236 Output_TextBlock->Text += L"Error: import from .iges\n";
237
238 // Export box to .step
239 StringCchCopyW(filePath, _countof(filePath), tmpPath);
240 StringCchCatW(filePath, _countof(filePath), L"/box.step");
241
242 STEPControl_StepModelType aSelection = STEPControl_AsIs;
243 if (SaveSTEP(filePath, theBox, aSelection))
244 Output_TextBlock->Text += L"OK: export to .iges\n";
245 // Import from .iges
246 TopoDS_Shape theBoxFromStep;
247 if (ReadSTEP(filePath, theBoxFromStep))
248 Output_TextBlock->Text += L"OK: import from .step\n";
249 else
250 Output_TextBlock->Text += L"Error: import from .step\n";
251
252 // Export box to .stl
253 StringCchCopyW(filePath, _countof(filePath), tmpPath);
254 StringCchCatW(filePath, _countof(filePath), L"/box.stl");
255
256 if (SaveSTL(filePath, theBox))
257 Output_TextBlock->Text += L"OK: export to .stl\n";
258
742cc8b0 259 // Export box to .vrml
260 StringCchCopyW(filePath, _countof(filePath), tmpPath);
261 StringCchCatW(filePath, _countof(filePath), L"/box.vrml");
262
263 if (SaveVRML(filePath, theBox))
264 Output_TextBlock->Text += L"OK: export to .vrml\n";
742cc8b0 265}
266
267//=======================================================================
268//function : SaveBREP
269//purpose : Export shape to .brep
270//=======================================================================
271Standard_Boolean MainPage::SaveBREP(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
272{
273 std::filebuf aFileBuf;
274 std::ostream aStream(&aFileBuf);
275
276 if (!aFileBuf.open(theFilePath, ios::out)) {
277 Output_TextBlock->Text += L"Error: cannot open file for export (brep)\n";
278 return Standard_False;
279 }
280
281 BRepTools::Write(theShape, aStream);
282 return Standard_True;
283}
284
285//=======================================================================
286//function : SaveIGES
287//purpose : Export shape to .iges
288//=======================================================================
289Standard_Boolean MainPage::SaveIGES(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
290{
291 std::filebuf aFileBuf;
292 std::ostream aStream(&aFileBuf);
293
294 if (!aFileBuf.open(theFilePath, ios::out)) {
295 Output_TextBlock->Text += L"Error: cannot open file for export (iges)\n";
296 return Standard_False;
297 }
298
299 IGESControl_Controller::Init();
300 IGESControl_Writer ICW(Interface_Static::CVal("XSTEP.iges.unit"), Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
301
302 ICW.AddShape(theShape);
303 ICW.ComputeModel();
304
305 if (!ICW.Write(aStream)) {
306 Output_TextBlock->Text += L"Error: cannot export box to .iges\n";
307 return Standard_False;
308 }
309
310 return Standard_True;
311}
312
313//=======================================================================
314//function : SaveSTEP
315//purpose : Export shape to .step
316//=======================================================================
317Standard_Boolean MainPage::SaveSTEP(const wchar_t* theFilePath, const TopoDS_Shape& theShape, const STEPControl_StepModelType theValue)
318{
319 std::filebuf aFileBuf;
320 std::ostream aStream(&aFileBuf);
321
322 if (!aFileBuf.open(theFilePath, ios::out)) {
323 Output_TextBlock->Text += L"Error: cannot open file for export (step)\n";
324 return Standard_False;
325 }
326
327 STEPControl_Writer aWriter;
328
329 if (aWriter.Transfer(theShape, theValue) != IFSelect_RetDone) {
330 Output_TextBlock->Text += L"Error: cannot translate shape to STEP\n";
331 return Standard_False;
332 }
333
334 char theFilePathA[MAX_PATH];
335 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
336
337 switch (aWriter.Write(theFilePathA))
338 {
339 case IFSelect_RetError:
340 Output_TextBlock->Text += L"Error: Incorrect Data\n";
341 break;
342 case IFSelect_RetFail:
343 Output_TextBlock->Text += L"Error: Writing has failed\n";
344 break;
345 case IFSelect_RetVoid:
346 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
347 break;
348 default:
349 return Standard_True;
350 }
351 return Standard_False;
352}
353
354//=======================================================================
355//function : SaveSTL
356//purpose : Export shape to .stl
357//=======================================================================
358Standard_Boolean MainPage::SaveSTL(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
359{
360 StlAPI_Writer myStlWriter;
361
362 char theFilePathA[MAX_PATH];
363 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
364
365 return myStlWriter.Write(theShape, theFilePathA) == StlAPI_StatusOK;
366}
367
368//=======================================================================
369//function : SaveVRML
370//purpose : Export shape to .vrml
371//=======================================================================
742cc8b0 372Standard_Boolean MainPage::SaveVRML(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
373{
374 VrmlAPI_Writer aWriter;
375
376 char theFilePathA[MAX_PATH];
377 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
378
379 aWriter.Write(theShape, theFilePathA);
380
381 return Standard_True;
382}
742cc8b0 383
384//=======================================================================
385//function : ReadBREP
386//purpose : Import shape from .brep
387//=======================================================================
388Standard_Boolean MainPage::ReadBREP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
389{
390 theShape.Nullify();
391
392 BRep_Builder aBuilder;
393
394 char theFilePathA[MAX_PATH];
395 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
396
397 if (!BRepTools::Read(theShape, theFilePathA, aBuilder))
398 return Standard_False;
399
400 return !theShape.IsNull() && BRepAlgo::IsValid(theShape);
401}
402
403//=======================================================================
404//function : ReadIGES
405//purpose : Import shape from .iges
406//=======================================================================
407Standard_Boolean MainPage::ReadIGES(const wchar_t* theFilePath, TopoDS_Shape& theShape)
408{
409 theShape.Nullify();
410
411 IGESControl_Reader Reader;
412
413 char theFilePathA[MAX_PATH];
414 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
415
416 if (Reader.ReadFile(theFilePathA) != IFSelect_RetDone)
417 return Standard_False;
418
419 Reader.TransferRoots();
420 theShape = Reader.OneShape();
421
422 return !theShape.IsNull();
423}
424
425//=======================================================================
426//function : ReadSTEP
427//purpose : Import shape from .step
428//=======================================================================
429Standard_Boolean MainPage::ReadSTEP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
430{
431 theShape.Nullify();
432
433 STEPControl_Reader aReader;
434
435 char theFilePathA[MAX_PATH];
436 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
437
438 switch (aReader.ReadFile(theFilePathA))
439 {
440 case IFSelect_RetError:
441 Output_TextBlock->Text += L"Error: Not a valid Step file\n";
442 break;
443 case IFSelect_RetFail:
444 Output_TextBlock->Text += L"Error: Reading has failed\n";
445 break;
446 case IFSelect_RetVoid:
447 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
448 break;
449 default:
450 return Standard_True;
451 }
452 return Standard_False;
1ae83f57 453}