3 // Implementation of the MainPage class.
7 #include "MainPage.xaml.h"
11 using namespace Platform;
12 using namespace Windows::Foundation;
13 using namespace Windows::Foundation::Collections;
14 using namespace Windows::UI::ViewManagement;
15 using namespace Windows::UI::Xaml;
16 using namespace Windows::UI::Xaml::Controls;
17 using namespace Windows::UI::Xaml::Controls::Primitives;
18 using namespace Windows::UI::Xaml::Data;
19 using namespace Windows::UI::Xaml::Input;
20 using namespace Windows::UI::Xaml::Media;
21 using namespace Windows::UI::Xaml::Navigation;
23 using namespace Windows::UI::Popups;
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>
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>
50 #include <TopoDS_Shape.hxx>
51 #include <VrmlAPI_Writer.hxx>
55 //=======================================================================
58 //=======================================================================
61 InitializeComponent();
62 ApplicationView::PreferredLaunchViewSize = Size(1000, 500);
63 ApplicationView::PreferredLaunchWindowingMode = ApplicationViewWindowingMode::PreferredLaunchViewSize;
66 //=======================================================================
68 //purpose : Test offset functionality
69 //=======================================================================
70 void MainPage::OnClickOffset(Platform::Object^ theSender,
71 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
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);
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);
86 Platform::String ^aMessage;
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;
93 aMessage = "Result: Two offset curves OC and OC2 were successfully created from source curve SPL1. \n";
95 Output_TextBlock->Text = aMessage;
98 //=======================================================================
99 //function : OnClickMesh
100 //purpose : Test mesh functionality
101 //=======================================================================
102 void MainPage::OnClickMesh(Platform::Object^ theSender,
103 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
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);
110 Standard_Integer result(0);
112 for (TopExp_Explorer ex(ShapeFused, TopAbs_FACE); ex.More(); ex.Next()) {
113 TopoDS_Face F = TopoDS::Face(ex.Current());
115 Handle(Poly_Triangulation) facing = BRep_Tool::Triangulation(F, L);
116 result = result + facing->NbTriangles();
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";
125 aMessage = "Result: Mesh was created\
126 --- Number of created triangles ---\n";
129 aMessage += ("\n\n");
131 Output_TextBlock->Text = aMessage;
134 //=======================================================================
135 //function : OnClickBoolean
136 //purpose : Test boolean operations
137 //=======================================================================
138 void MainPage::OnClickBoolean(Platform::Object^ theSender,
139 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
141 TCollection_AsciiString asd;
143 TopoDS_Shape theBox1 = BRepPrimAPI_MakeBox(60, 200, 70).Shape();
145 TopoDS_Shape theBox2 = BRepPrimAPI_MakeBox(P, 20, 150, 110).Shape();
147 TopoDS_Shape FusedShape = BRepAlgoAPI_Fuse(theBox1, theBox2);
149 Platform::String ^aMessage;
151 if (FusedShape.IsNull()) {
152 aMessage = "Error: could not fuse shapes theBox1 and theBox2";
154 aMessage = "Result: Shapes were successfully fused. \n";
156 Output_TextBlock->Text = aMessage;
159 //=======================================================================
160 //function : OnClickBuildTemporary
161 //purpose : Test OSD_File::BuildTemporary() method
162 //=======================================================================
163 void MainPage::OnClickBuildTemporary(Platform::Object^ theSender,
164 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
167 theTmpFile.BuildTemporary();
168 Standard_Boolean fKO = theTmpFile.Failed();
171 Output_TextBlock->Text = "Error: cannot create temporary file";
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;
186 //=======================================================================
187 //function : OnClickDataExchange
188 //purpose : Test data exchange functionality
189 //=======================================================================
190 void MainPage::OnClickDataExchange(Platform::Object^ theSender,
191 Windows::UI::Xaml::Input::PointerRoutedEventArgs^ theEvent)
193 Output_TextBlock->Text = "";
195 // Create box for export
196 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200, 60, 60);
198 // Create temporary directory
199 wchar_t tmpPath[MAX_PATH];
200 wchar_t filePath[MAX_PATH];
201 char tmpPathA[MAX_PATH];
203 if (!GetTempPathW(_countof(tmpPath), tmpPath)) {
204 StringCchCopyW(tmpPath, _countof(tmpPath), L"./");
207 WideCharToMultiByte(CP_UTF8, 0, tmpPath, -1, tmpPathA, sizeof(tmpPathA), NULL, NULL);
208 OSD_Path tmpDirPath(tmpPathA);
209 OSD_Directory tmpDir(tmpDirPath);
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";
220 TopoDS_Shape theBoxFromBrep;
221 if (ReadBREP(filePath, theBoxFromBrep))
222 Output_TextBlock->Text += L"OK: import from .brep\n";
224 Output_TextBlock->Text += L"Error: import from .brep\n";
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";
232 TopoDS_Shape theBoxFromIges;
233 if (ReadIGES(filePath, theBoxFromIges))
234 Output_TextBlock->Text += L"OK: import from .iges\n";
236 Output_TextBlock->Text += L"Error: import from .iges\n";
238 // Export box to .step
239 StringCchCopyW(filePath, _countof(filePath), tmpPath);
240 StringCchCatW(filePath, _countof(filePath), L"/box.step");
242 STEPControl_StepModelType aSelection = STEPControl_AsIs;
243 if (SaveSTEP(filePath, theBox, aSelection))
244 Output_TextBlock->Text += L"OK: export to .iges\n";
246 TopoDS_Shape theBoxFromStep;
247 if (ReadSTEP(filePath, theBoxFromStep))
248 Output_TextBlock->Text += L"OK: import from .step\n";
250 Output_TextBlock->Text += L"Error: import from .step\n";
252 // Export box to .stl
253 StringCchCopyW(filePath, _countof(filePath), tmpPath);
254 StringCchCatW(filePath, _countof(filePath), L"/box.stl");
256 if (SaveSTL(filePath, theBox))
257 Output_TextBlock->Text += L"OK: export to .stl\n";
259 // Export box to .vrml
260 StringCchCopyW(filePath, _countof(filePath), tmpPath);
261 StringCchCatW(filePath, _countof(filePath), L"/box.vrml");
263 if (SaveVRML(filePath, theBox))
264 Output_TextBlock->Text += L"OK: export to .vrml\n";
267 //=======================================================================
268 //function : SaveBREP
269 //purpose : Export shape to .brep
270 //=======================================================================
271 Standard_Boolean MainPage::SaveBREP(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
273 std::filebuf aFileBuf;
274 std::ostream aStream(&aFileBuf);
276 if (!aFileBuf.open(theFilePath, std::ios::out)) {
277 Output_TextBlock->Text += L"Error: cannot open file for export (brep)\n";
278 return Standard_False;
281 BRepTools::Write(theShape, aStream);
282 return Standard_True;
285 //=======================================================================
286 //function : SaveIGES
287 //purpose : Export shape to .iges
288 //=======================================================================
289 Standard_Boolean MainPage::SaveIGES(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
291 std::filebuf aFileBuf;
292 std::ostream aStream(&aFileBuf);
294 if (!aFileBuf.open(theFilePath, std::ios::out)) {
295 Output_TextBlock->Text += L"Error: cannot open file for export (iges)\n";
296 return Standard_False;
299 IGESControl_Controller::Init();
300 IGESControl_Writer ICW(Interface_Static::CVal("XSTEP.iges.unit"), Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
302 ICW.AddShape(theShape);
305 if (!ICW.Write(aStream)) {
306 Output_TextBlock->Text += L"Error: cannot export box to .iges\n";
307 return Standard_False;
310 return Standard_True;
313 //=======================================================================
314 //function : SaveSTEP
315 //purpose : Export shape to .step
316 //=======================================================================
317 Standard_Boolean MainPage::SaveSTEP(const wchar_t* theFilePath, const TopoDS_Shape& theShape, const STEPControl_StepModelType theValue)
319 std::filebuf aFileBuf;
320 std::ostream aStream(&aFileBuf);
322 if (!aFileBuf.open(theFilePath, std::ios::out)) {
323 Output_TextBlock->Text += L"Error: cannot open file for export (step)\n";
324 return Standard_False;
327 STEPControl_Writer aWriter;
329 if (aWriter.Transfer(theShape, theValue) != IFSelect_RetDone) {
330 Output_TextBlock->Text += L"Error: cannot translate shape to STEP\n";
331 return Standard_False;
334 const TCollection_AsciiString aFilePath (theFilePath);
335 switch (aWriter.Write (aFilePath.ToCString()))
337 case IFSelect_RetError:
338 Output_TextBlock->Text += L"Error: Incorrect Data\n";
340 case IFSelect_RetFail:
341 Output_TextBlock->Text += L"Error: Writing has failed\n";
343 case IFSelect_RetVoid:
344 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
347 return Standard_True;
349 return Standard_False;
352 //=======================================================================
354 //purpose : Export shape to .stl
355 //=======================================================================
356 Standard_Boolean MainPage::SaveSTL(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
358 StlAPI_Writer myStlWriter;
359 const TCollection_AsciiString aFilePath (theFilePath);
360 return myStlWriter.Write (theShape, aFilePath.ToCString());
363 //=======================================================================
364 //function : SaveVRML
365 //purpose : Export shape to .vrml
366 //=======================================================================
367 Standard_Boolean MainPage::SaveVRML(const wchar_t* theFilePath, const TopoDS_Shape& theShape)
369 VrmlAPI_Writer aWriter;
370 const TCollection_AsciiString aFilePath (theFilePath);
371 aWriter.Write (theShape, aFilePath.ToCString());
372 return Standard_True;
375 //=======================================================================
376 //function : ReadBREP
377 //purpose : Import shape from .brep
378 //=======================================================================
379 Standard_Boolean MainPage::ReadBREP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
383 BRep_Builder aBuilder;
384 const TCollection_AsciiString aFilePath (theFilePath);
385 if (!BRepTools::Read(theShape, aFilePath.ToCString(), aBuilder))
386 return Standard_False;
388 return !theShape.IsNull() && BRepAlgo::IsValid(theShape);
391 //=======================================================================
392 //function : ReadIGES
393 //purpose : Import shape from .iges
394 //=======================================================================
395 Standard_Boolean MainPage::ReadIGES(const wchar_t* theFilePath, TopoDS_Shape& theShape)
399 IGESControl_Reader Reader;
401 const TCollection_AsciiString aFilePath (theFilePath);
402 if (Reader.ReadFile (aFilePath.ToCString()) != IFSelect_RetDone)
403 return Standard_False;
405 Reader.TransferRoots();
406 theShape = Reader.OneShape();
408 return !theShape.IsNull();
411 //=======================================================================
412 //function : ReadSTEP
413 //purpose : Import shape from .step
414 //=======================================================================
415 Standard_Boolean MainPage::ReadSTEP(const wchar_t* theFilePath, TopoDS_Shape& theShape)
419 STEPControl_Reader aReader;
420 const TCollection_AsciiString aFilePath (theFilePath);
421 switch (aReader.ReadFile (aFilePath.ToCString()))
423 case IFSelect_RetError:
424 Output_TextBlock->Text += L"Error: Not a valid Step file\n";
426 case IFSelect_RetFail:
427 Output_TextBlock->Text += L"Error: Reading has failed\n";
429 case IFSelect_RetVoid:
430 Output_TextBlock->Text += L"Error: Nothing to transfer\n";
433 return Standard_True;
435 return Standard_False;