0027350: Support for Universal Windows Platform
[occt.git] / samples / xaml / MainPage.xaml.cpp
... / ...
CommitLineData
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
259/*
260 // Export box to .vrml
261 StringCchCopyW(filePath, _countof(filePath), tmpPath);
262 StringCchCatW(filePath, _countof(filePath), L"/box.vrml");
263
264 if (SaveVRML(filePath, theBox))
265 Output_TextBlock->Text += L"OK: export to .vrml\n";
266*/
267}
268
269//=======================================================================
270//function : SaveBREP
271//purpose : Export shape to .brep
272//=======================================================================
273Standard_Boolean MainPage::SaveBREP(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
274{
275 std::filebuf aFileBuf;
276 std::ostream aStream(&aFileBuf);
277
278 if (!aFileBuf.open(theFilePath, ios::out)) {
279 Output_TextBlock->Text += L"Error: cannot open file for export (brep)\n";
280 return Standard_False;
281 }
282
283 BRepTools::Write(theShape, aStream);
284 return Standard_True;
285}
286
287//=======================================================================
288//function : SaveIGES
289//purpose : Export shape to .iges
290//=======================================================================
291Standard_Boolean MainPage::SaveIGES(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
292{
293 std::filebuf aFileBuf;
294 std::ostream aStream(&aFileBuf);
295
296 if (!aFileBuf.open(theFilePath, ios::out)) {
297 Output_TextBlock->Text += L"Error: cannot open file for export (iges)\n";
298 return Standard_False;
299 }
300
301 IGESControl_Controller::Init();
302 IGESControl_Writer ICW(Interface_Static::CVal("XSTEP.iges.unit"), Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
303
304 ICW.AddShape(theShape);
305 ICW.ComputeModel();
306
307 if (!ICW.Write(aStream)) {
308 Output_TextBlock->Text += L"Error: cannot export box to .iges\n";
309 return Standard_False;
310 }
311
312 return Standard_True;
313}
314
315//=======================================================================
316//function : SaveSTEP
317//purpose : Export shape to .step
318//=======================================================================
319Standard_Boolean MainPage::SaveSTEP(const wchar_t* theFilePath, const TopoDS_Shape& theShape, const STEPControl_StepModelType theValue)
320{
321 std::filebuf aFileBuf;
322 std::ostream aStream(&aFileBuf);
323
324 if (!aFileBuf.open(theFilePath, ios::out)) {
325 Output_TextBlock->Text += L"Error: cannot open file for export (step)\n";
326 return Standard_False;
327 }
328
329 STEPControl_Writer aWriter;
330
331 if (aWriter.Transfer(theShape, theValue) != IFSelect_RetDone) {
332 Output_TextBlock->Text += L"Error: cannot translate shape to STEP\n";
333 return Standard_False;
334 }
335
336 char theFilePathA[MAX_PATH];
337 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
338
339 switch (aWriter.Write(theFilePathA))
340 {
341 case IFSelect_RetError:
342 Output_TextBlock->Text += L"Error: Incorrect Data\n";
343 break;
344 case IFSelect_RetFail:
345 Output_TextBlock->Text += L"Error: Writing has failed\n";
346 break;
347 case IFSelect_RetVoid:
348 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
349 break;
350 default:
351 return Standard_True;
352 }
353 return Standard_False;
354}
355
356//=======================================================================
357//function : SaveSTL
358//purpose : Export shape to .stl
359//=======================================================================
360Standard_Boolean MainPage::SaveSTL(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
361{
362 StlAPI_Writer myStlWriter;
363
364 char theFilePathA[MAX_PATH];
365 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
366
367 return myStlWriter.Write(theShape, theFilePathA) == StlAPI_StatusOK;
368}
369
370//=======================================================================
371//function : SaveVRML
372//purpose : Export shape to .vrml
373//=======================================================================
374/*
375Standard_Boolean MainPage::SaveVRML(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
376{
377 VrmlAPI_Writer aWriter;
378
379 char theFilePathA[MAX_PATH];
380 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
381
382 aWriter.Write(theShape, theFilePathA);
383
384 return Standard_True;
385}
386*/
387
388//=======================================================================
389//function : ReadBREP
390//purpose : Import shape from .brep
391//=======================================================================
392Standard_Boolean MainPage::ReadBREP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
393{
394 theShape.Nullify();
395
396 BRep_Builder aBuilder;
397
398 char theFilePathA[MAX_PATH];
399 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
400
401 if (!BRepTools::Read(theShape, theFilePathA, aBuilder))
402 return Standard_False;
403
404 return !theShape.IsNull() && BRepAlgo::IsValid(theShape);
405}
406
407//=======================================================================
408//function : ReadIGES
409//purpose : Import shape from .iges
410//=======================================================================
411Standard_Boolean MainPage::ReadIGES(const wchar_t* theFilePath, TopoDS_Shape& theShape)
412{
413 theShape.Nullify();
414
415 IGESControl_Reader Reader;
416
417 char theFilePathA[MAX_PATH];
418 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
419
420 if (Reader.ReadFile(theFilePathA) != IFSelect_RetDone)
421 return Standard_False;
422
423 Reader.TransferRoots();
424 theShape = Reader.OneShape();
425
426 return !theShape.IsNull();
427}
428
429//=======================================================================
430//function : ReadSTEP
431//purpose : Import shape from .step
432//=======================================================================
433Standard_Boolean MainPage::ReadSTEP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
434{
435 theShape.Nullify();
436
437 STEPControl_Reader aReader;
438
439 char theFilePathA[MAX_PATH];
440 WideCharToMultiByte(CP_UTF8, 0, theFilePath, -1, theFilePathA, sizeof(theFilePathA), NULL, NULL);
441
442 switch (aReader.ReadFile(theFilePathA))
443 {
444 case IFSelect_RetError:
445 Output_TextBlock->Text += L"Error: Not a valid Step file\n";
446 break;
447 case IFSelect_RetFail:
448 Output_TextBlock->Text += L"Error: Reading has failed\n";
449 break;
450 case IFSelect_RetVoid:
451 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
452 break;
453 default:
454 return Standard_True;
455 }
456 return Standard_False;
457}