0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[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
04232180 276 if (!aFileBuf.open(theFilePath, std::ios::out)) {
742cc8b0 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
04232180 294 if (!aFileBuf.open(theFilePath, std::ios::out)) {
742cc8b0 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
04232180 322 if (!aFileBuf.open(theFilePath, std::ios::out)) {
742cc8b0 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
4178b353 334 const TCollection_AsciiString aFilePath (theFilePath);
335 switch (aWriter.Write (aFilePath.ToCString()))
742cc8b0 336 {
337 case IFSelect_RetError:
338 Output_TextBlock->Text += L"Error: Incorrect Data\n";
339 break;
340 case IFSelect_RetFail:
341 Output_TextBlock->Text += L"Error: Writing has failed\n";
342 break;
343 case IFSelect_RetVoid:
344 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
345 break;
346 default:
347 return Standard_True;
348 }
349 return Standard_False;
350}
351
352//=======================================================================
353//function : SaveSTL
354//purpose : Export shape to .stl
355//=======================================================================
356Standard_Boolean MainPage::SaveSTL(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
357{
358 StlAPI_Writer myStlWriter;
4178b353 359 const TCollection_AsciiString aFilePath (theFilePath);
360 return myStlWriter.Write (theShape, aFilePath.ToCString());
742cc8b0 361}
362
363//=======================================================================
364//function : SaveVRML
365//purpose : Export shape to .vrml
366//=======================================================================
742cc8b0 367Standard_Boolean MainPage::SaveVRML(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
368{
369 VrmlAPI_Writer aWriter;
4178b353 370 const TCollection_AsciiString aFilePath (theFilePath);
371 aWriter.Write (theShape, aFilePath.ToCString());
742cc8b0 372 return Standard_True;
373}
742cc8b0 374
375//=======================================================================
376//function : ReadBREP
377//purpose : Import shape from .brep
378//=======================================================================
379Standard_Boolean MainPage::ReadBREP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
380{
381 theShape.Nullify();
382
383 BRep_Builder aBuilder;
4178b353 384 const TCollection_AsciiString aFilePath (theFilePath);
385 if (!BRepTools::Read(theShape, aFilePath.ToCString(), aBuilder))
742cc8b0 386 return Standard_False;
387
388 return !theShape.IsNull() && BRepAlgo::IsValid(theShape);
389}
390
391//=======================================================================
392//function : ReadIGES
393//purpose : Import shape from .iges
394//=======================================================================
395Standard_Boolean MainPage::ReadIGES(const wchar_t* theFilePath, TopoDS_Shape& theShape)
396{
397 theShape.Nullify();
398
399 IGESControl_Reader Reader;
400
4178b353 401 const TCollection_AsciiString aFilePath (theFilePath);
402 if (Reader.ReadFile (aFilePath.ToCString()) != IFSelect_RetDone)
742cc8b0 403 return Standard_False;
404
405 Reader.TransferRoots();
406 theShape = Reader.OneShape();
407
408 return !theShape.IsNull();
409}
410
411//=======================================================================
412//function : ReadSTEP
413//purpose : Import shape from .step
414//=======================================================================
415Standard_Boolean MainPage::ReadSTEP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
416{
417 theShape.Nullify();
418
419 STEPControl_Reader aReader;
4178b353 420 const TCollection_AsciiString aFilePath (theFilePath);
421 switch (aReader.ReadFile (aFilePath.ToCString()))
742cc8b0 422 {
423 case IFSelect_RetError:
424 Output_TextBlock->Text += L"Error: Not a valid Step file\n";
425 break;
426 case IFSelect_RetFail:
427 Output_TextBlock->Text += L"Error: Reading has failed\n";
428 break;
429 case IFSelect_RetVoid:
430 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
431 break;
432 default:
433 return Standard_True;
434 }
435 return Standard_False;
1ae83f57 436}