0029069: Samples - handle UNICODE filenames within C++/CLI CSharp sample
[occt.git] / samples / CSharp / OCCTProxy_D3D / OCCTProxyD3D.cpp
CommitLineData
a2560cc9 1#include <d3d9.h>
62e1beed 2#include <windows.h>
15534713 3
4// include required OCCT headers
5#include <Standard_Version.hxx>
6#include <Message_ProgressIndicator.hxx>
7//for OCC graphic
15534713 8#include <WNT_Window.hxx>
62e1beed 9#include <WNT_WClass.hxx>
15534713 10#include <Graphic3d_CView.hxx>
11#include <Graphic3d_Camera.hxx>
12#include <Graphic3d_TextureParams.hxx>
62e1beed 13#include <D3DHost_GraphicDriver.hxx>
c357e426 14#include <D3DHost_View.hxx>
15534713 15//for object display
16#include <V3d_Viewer.hxx>
17#include <V3d_View.hxx>
15534713 18#include <AIS_InteractiveContext.hxx>
19#include <AIS_Shape.hxx>
20//topology
21#include <TopoDS_Shape.hxx>
22#include <TopoDS_Compound.hxx>
23//brep tools
24#include <BRep_Builder.hxx>
25#include <BRepTools.hxx>
15534713 26// iges I/E
27#include <IGESControl_Reader.hxx>
28#include <IGESControl_Controller.hxx>
29#include <IGESControl_Writer.hxx>
30#include <IFSelect_ReturnStatus.hxx>
31#include <Interface_Static.hxx>
32//step I/E
33#include <STEPControl_Reader.hxx>
34#include <STEPControl_Writer.hxx>
35//for stl export
36#include <StlAPI_Writer.hxx>
37//for vrml export
38#include <VrmlAPI_Writer.hxx>
39//wrapper of pure C++ classes to ref classes
40#include <NCollection_Haft.h>
41
95f68826 42#include <vcclr.h>
43
15534713 44// list of required OCCT libraries
45#pragma comment(lib, "TKernel.lib")
46#pragma comment(lib, "TKMath.lib")
47#pragma comment(lib, "TKBRep.lib")
15534713 48#pragma comment(lib, "TKXSBase.lib")
49#pragma comment(lib, "TKService.lib")
50#pragma comment(lib, "TKV3d.lib")
51#pragma comment(lib, "TKOpenGl.lib")
62e1beed 52#pragma comment(lib, "TKD3dHost.lib")
15534713 53#pragma comment(lib, "TKIGES.lib")
54#pragma comment(lib, "TKSTEP.lib")
55#pragma comment(lib, "TKStl.lib")
56#pragma comment(lib, "TKVrml.lib")
57
62e1beed 58#pragma comment(lib, "D3D9.lib")
59
95f68826 60//! Auxiliary tool for converting C# string into UTF-8 string.
61static TCollection_AsciiString toAsciiString (String^ theString)
62{
63 if (theString == nullptr)
64 {
65 return TCollection_AsciiString();
66 }
67
68 pin_ptr<const wchar_t> aPinChars = PtrToStringChars (theString);
69 const wchar_t* aWCharPtr = aPinChars;
70 if (aWCharPtr == NULL
71 || *aWCharPtr == L'\0')
72 {
73 return TCollection_AsciiString();
74 }
75 return TCollection_AsciiString (aWCharPtr);
76}
77
15534713 78/// <summary>
79/// Proxy class encapsulating calls to OCCT C++ classes within
80/// C++/CLI class visible from .Net (CSharp)
81/// </summary>
82public ref class OCCTProxyD3D
83{
84public:
85
62e1beed 86 OCCTProxyD3D() {}
15534713 87
88 // ============================================
89 // Viewer functionality
90 // ============================================
91
92 /// <summary>
93 ///Initialize a viewer
94 /// </summary>
95 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
62e1beed 96 bool InitViewer()
15534713 97 {
62e1beed 98 myGraphicDriver() = new D3DHost_GraphicDriver();
99 myGraphicDriver()->ChangeOptions().buffersNoSwap = true;
15534713 100 //myGraphicDriver()->ChangeOptions().contextDebug = true;
101
6a24c6de 102 myViewer() = new V3d_Viewer (myGraphicDriver());
15534713 103 myViewer()->SetDefaultLights();
104 myViewer()->SetLightOn();
105 myView() = myViewer()->CreateView();
62e1beed 106
107 static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC);
108 Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64);
109 aWNTWindow->SetVirtual (Standard_True);
15534713 110 myView()->SetWindow(aWNTWindow);
15534713 111 myAISContext() = new AIS_InteractiveContext (myViewer());
112 myAISContext()->UpdateCurrentViewer();
113 myView()->MustBeResized();
114 return true;
115 }
116
15534713 117 /// <summary> Resizes custom FBO for Direct3D output. </summary>
62e1beed 118 System::IntPtr ResizeBridgeFBO (int theWinSizeX,
119 int theWinSizeY)
15534713 120 {
62e1beed 121 Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window());
122 aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY);
123 myView()->MustBeResized();
124 myView()->Invalidate();
c357e426 125 return System::IntPtr(Handle(D3DHost_View)::DownCast (myView()->View())->D3dColorSurface());
15534713 126 }
127
128 /// <summary>
129 /// Make dump of current view to file
130 /// </summary>
131 /// <param name="theFileName">Name of dump file</param>
95f68826 132 bool Dump (const TCollection_AsciiString& theFileName)
15534713 133 {
134 if (myView().IsNull())
135 {
136 return false;
137 }
138 myView()->Redraw();
95f68826 139 return myView()->Dump (theFileName.ToCString()) != Standard_False;
15534713 140 }
141
142 /// <summary>
143 ///Redraw view
144 /// </summary>
145 void RedrawView()
146 {
147 if (!myView().IsNull())
148 {
149 myView()->Redraw();
150 }
151 }
152
153 /// <summary>
154 ///Update view
155 /// </summary>
156 void UpdateView(void)
157 {
158 if (!myView().IsNull())
159 {
160 myView()->MustBeResized();
161 }
162 }
163
164 /// <summary>
165 ///Set computed mode in false
166 /// </summary>
167 void SetDegenerateModeOn()
168 {
169 if (!myView().IsNull())
170 {
171 myView()->SetComputedMode (Standard_False);
1eeef710 172 myView()->Redraw();
15534713 173 }
174 }
175
176 /// <summary>
177 ///Set computed mode in true
178 /// </summary>
179 void SetDegenerateModeOff()
180 {
181 if (!myView().IsNull())
182 {
183 myView()->SetComputedMode (Standard_True);
1eeef710 184 myView()->Redraw();
15534713 185 }
186 }
187
188 /// <summary>
189 ///Fit all
190 /// </summary>
191 void WindowFitAll (int theXmin, int theYmin,
192 int theXmax, int theYmax)
193 {
194 if (!myView().IsNull())
195 {
196 myView()->WindowFitAll (theXmin, theYmin, theXmax, theYmax);
197 }
198 }
199
200 /// <summary>
201 ///Current place of window
202 /// </summary>
203 /// <param name="theZoomFactor">Current zoom</param>
204 void Place (int theX, int theY, float theZoomFactor)
205 {
ee2be2a8 206 Standard_Real aZoomFactor = theZoomFactor;
15534713 207 if (!myView().IsNull())
208 {
209 myView()->Place (theX, theY, aZoomFactor);
210 }
211 }
212
213 /// <summary>
214 ///Set Zoom
215 /// </summary>
216 void Zoom (int theX1, int theY1, int theX2, int theY2)
217 {
218 if (!myView().IsNull())
219 {
220 myView()->Zoom (theX1, theY1, theX2, theY2);
221 }
222 }
223
224 /// <summary>
225 ///Set Pan
226 /// </summary>
227 void Pan (int theX, int theY)
228 {
229 if (!myView().IsNull())
230 {
231 myView()->Pan (theX, theY);
232 }
233 }
234
235 /// <summary>
236 ///Rotation
237 /// </summary>
238 void Rotation (int theX, int theY)
239 {
240 if (!myView().IsNull())
241 {
242 myView()->Rotation (theX, theY);
243 }
244 }
245
246 /// <summary>
247 ///Start rotation
248 /// </summary>
249 void StartRotation (int theX, int theY)
250 {
251 if (!myView().IsNull())
252 {
253 myView()->StartRotation (theX, theY);
254 }
255 }
256
257 /// <summary>
258 ///Select by rectangle
259 /// </summary>
260 void Select (int theX1, int theY1, int theX2, int theY2)
261 {
262 if (!myAISContext().IsNull())
263 {
0577ae8c 264 myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
15534713 265 }
266 }
267
268 /// <summary>
269 ///Select by click
270 /// </summary>
271 void Select()
272 {
273 if (!myAISContext().IsNull())
274 {
0577ae8c 275 myAISContext()->Select (Standard_True);
15534713 276 }
277 }
278
279 /// <summary>
280 ///Move view
281 /// </summary>
282 void MoveTo (int theX, int theY)
283 {
284 if (!myAISContext().IsNull() && !myView().IsNull())
285 {
0577ae8c 286 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
15534713 287 }
288 }
289
290 /// <summary>
291 ///Select by rectangle with pressed "Shift" key
292 /// </summary>
293 void ShiftSelect (int theX1, int theY1, int theX2, int theY2)
294 {
295 if (!myAISContext().IsNull() && !myView().IsNull())
296 {
0577ae8c 297 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
15534713 298 }
299 }
300
301 /// <summary>
302 ///Select by "Shift" key
303 /// </summary>
304 void ShiftSelect()
305 {
306 if (!myAISContext().IsNull())
307 {
0577ae8c 308 myAISContext()->ShiftSelect (Standard_True);
15534713 309 }
310 }
311
312 /// <summary>
313 ///Set background color
314 /// </summary>
315 void BackgroundColor (int& theRed, int& theGreen, int& theBlue)
316 {
317 if (!myView().IsNull())
318 {
319 Quantity_Color aColor = myView()->BackgroundColor();
320 theRed = (int )aColor.Red() * 255;
321 theGreen = (int )aColor.Green() * 255;
322 theBlue = (int )aColor.Blue() * 255;
323 }
324 }
325
326 /// <summary>
327 ///Get background color Red
328 /// </summary>
329 int GetBGColR()
330 {
331 int anRgb[3];
332 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
333 return anRgb[0];
334 }
335
336 /// <summary>
337 ///Get background color Green
338 /// </summary>
339 int GetBGColG()
340 {
341 int anRgb[3];
342 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
343 return anRgb[1];
344 }
345
346 /// <summary>
347 ///Get background color Blue
348 /// </summary>
349 int GetBGColB()
350 {
351 int anRgb[3];
352 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
353 return anRgb[2];
354 }
355
356 /// <summary>
357 ///Update current viewer
358 /// </summary>
359 void UpdateCurrentViewer()
360 {
361 if (!myAISContext().IsNull())
362 {
363 myAISContext()->UpdateCurrentViewer();
364 }
365 }
366
367 /// <summary>
368 ///Front side
369 /// </summary>
370 void FrontView()
371 {
372 if (!myView().IsNull())
373 {
32757c6e 374 myView()->SetProj (V3d_Yneg);
15534713 375 }
376 }
377
378 /// <summary>
379 ///Top side
380 /// </summary>
381 void TopView()
382 {
383 if (!myView().IsNull())
384 {
385 myView()->SetProj (V3d_Zpos);
386 }
387 }
388
389 /// <summary>
390 ///Left side
391 /// </summary>
392 void LeftView()
393 {
394 if (!myView().IsNull())
395 {
32757c6e 396 myView()->SetProj (V3d_Xneg);
15534713 397 }
398 }
399
400 /// <summary>
401 ///Back side
402 /// </summary>
403 void BackView()
404 {
405 if (!myView().IsNull())
406 {
32757c6e 407 myView()->SetProj (V3d_Ypos);
15534713 408 }
409 }
410
411 /// <summary>
412 ///Right side
413 /// </summary>
414 void RightView()
415 {
416 if (!myView().IsNull())
417 {
32757c6e 418 myView()->SetProj (V3d_Xpos);
15534713 419 }
420 }
421
422 /// <summary>
423 ///Bottom side
424 /// </summary>
425 void BottomView()
426 {
427 if (!myView().IsNull())
428 {
429 myView()->SetProj (V3d_Zneg);
430 }
431 }
432
433 /// <summary>
434 ///Axo side
435 /// </summary>
436 void AxoView()
437 {
438 if (!myView().IsNull())
439 {
440 myView()->SetProj (V3d_XposYnegZpos);
441 }
442 }
443
444 /// <summary>
445 ///Scale
446 /// </summary>
447 float Scale()
448 {
449 return myView().IsNull()
450 ? -1.0f
451 : float(myView()->Scale());
452 }
453
454 /// <summary>
455 ///Zoom in all view
456 /// </summary>
457 void ZoomAllView()
458 {
459 if (!myView().IsNull())
460 {
461 myView()->FitAll();
462 myView()->ZFitAll();
463 }
464 }
465
466 /// <summary>
467 ///Reset view
468 /// </summary>
469 void Reset()
470 {
471 if (!myView().IsNull())
472 {
473 myView()->Reset();
474 }
475 }
476
477 /// <summary>
478 ///Set display mode of objects
479 /// </summary>
480 /// <param name="theMode">Set current mode</param>
481 void SetDisplayMode (int theMode)
482 {
483 if (myAISContext().IsNull())
484 {
485 return;
486 }
487
488 AIS_DisplayMode aCurrentMode = theMode == 0
489 ? AIS_WireFrame
490 : AIS_Shaded;
0577ae8c 491 if (myAISContext()->NbSelected() == 0)
15534713 492 {
0577ae8c 493 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
15534713 494 }
495 else
496 {
0577ae8c 497 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 498 {
0577ae8c 499 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
15534713 500 }
501 }
502 myAISContext()->UpdateCurrentViewer();
503 }
504
505 /// <summary>
506 ///Set color
507 /// </summary>
508 void SetColor (int theR, int theG, int theB)
509 {
510 if (myAISContext().IsNull())
511 {
512 return;
513 }
514
515 Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB);
0577ae8c 516 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 517 {
87432b82 518 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, false);
15534713 519 }
0577ae8c 520 myAISContext()->UpdateCurrentViewer();
15534713 521 }
522
523 /// <summary>
524 ///Get object color red
525 /// </summary>
526 int GetObjColR()
527 {
528 int anRgb[3];
529 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
530 return anRgb[0];
531 }
532
533 /// <summary>
534 ///Get object color green
535 /// </summary>
536 int GetObjColG()
537 {
538 int anRgb[3];
539 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
540 return anRgb[1];
541 }
542
543 /// <summary>
544 ///Get object color blue
545 /// </summary>
546 int GetObjColB()
547 {
548 int anRgb[3];
549 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
550 return anRgb[2];
551 }
552
553 /// <summary>
554 ///Get object color R/G/B
555 /// </summary>
556 void ObjectColor (int& theRed, int& theGreen, int& theBlue)
557 {
558 if (myAISContext().IsNull())
559 {
560 return;
561 }
562
563 theRed = 255;
564 theGreen = 255;
565 theBlue = 255;
0577ae8c 566 myAISContext()->InitSelected();
567 if (!myAISContext()->MoreSelected())
15534713 568 {
569 return;
570 }
571
0577ae8c 572 Handle(AIS_InteractiveObject) aCurrent = myAISContext()->SelectedInteractive();
15534713 573 if (aCurrent->HasColor())
574 {
87432b82 575 Quantity_Color anObjCol;
576 myAISContext()->Color (aCurrent, anObjCol);
15534713 577 theRed = int(anObjCol.Red() * 255.0);
578 theGreen = int(anObjCol.Green() * 255.0);
579 theBlue = int(anObjCol.Blue() * 255.0);
580 }
581 }
582
583 /// <summary>
584 ///Set background color R/G/B
585 /// </summary>
586 void SetBackgroundColor (int theRed, int theGreen, int theBlue)
587 {
588 if (!myView().IsNull())
589 {
590 myView()->SetBackgroundColor (Quantity_TOC_RGB, theRed / 255.0, theGreen / 255.0, theBlue / 255.0);
591 }
592 }
593
594 /// <summary>
595 ///Erase objects
596 /// </summary>
597 void EraseObjects()
598 {
599 if (myAISContext().IsNull())
600 {
601 return;
602 }
7140edaf 603
0577ae8c 604 myAISContext()->EraseSelected (Standard_False);
605 myAISContext()->ClearSelected (Standard_True);
15534713 606 }
607
608 /// <summary>
609 ///Get version
610 /// </summary>
611 float GetOCCVersion()
612 {
613 return (float )OCC_VERSION;
614 }
615
616 /// <summary>
617 ///set material
618 /// </summary>
619 void SetMaterial (int theMaterial)
620 {
621 if (myAISContext().IsNull())
622 {
623 return;
624 }
0577ae8c 625 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 626 {
0577ae8c 627 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial )theMaterial, Standard_False);
15534713 628 }
629 myAISContext()->UpdateCurrentViewer();
630 }
631
632 /// <summary>
633 ///set transparency
634 /// </summary>
635 void SetTransparency (int theTrans)
636 {
637 if (myAISContext().IsNull())
638 {
639 return;
640 }
0577ae8c 641 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 642 {
a2560cc9 643 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real )theTrans) / 10.0, Standard_False);
15534713 644 }
0577ae8c 645 myAISContext()->UpdateCurrentViewer();
15534713 646 }
647
648 /// <summary>
649 ///Return true if object is selected
650 /// </summary>
651 bool IsObjectSelected()
652 {
653 if (myAISContext().IsNull())
654 {
655 return false;
656 }
0577ae8c 657 myAISContext()->InitSelected();
658 return myAISContext()->MoreSelected() != Standard_False;
15534713 659 }
660
661 /// <summary>
662 ///Return display mode
663 /// </summary>
664 int DisplayMode()
665 {
666 if (myAISContext().IsNull())
667 {
668 return -1;
669 }
670
671 bool isOneOrMoreInShading = false;
672 bool isOneOrMoreInWireframe = false;
0577ae8c 673 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 674 {
0577ae8c 675 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_Shaded))
15534713 676 {
677 isOneOrMoreInShading = true;
678 }
0577ae8c 679 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_WireFrame))
15534713 680 {
681 isOneOrMoreInWireframe = true;
682 }
683 }
684 if (isOneOrMoreInShading
685 && isOneOrMoreInWireframe)
686 {
687 return 10;
688 }
689 else if (isOneOrMoreInShading)
690 {
691 return 1;
692 }
693 else if (isOneOrMoreInWireframe)
694 {
695 return 0;
696 }
697 return -1;
698 }
699
15534713 700 /// <summary>
701 ///Set AISContext
702 /// </summary>
703 bool SetAISContext (OCCTProxyD3D^ theViewer)
704 {
705 this->myAISContext() = theViewer->GetContext();
706 if (myAISContext().IsNull())
707 {
708 return false;
709 }
710 return true;
711 }
712
713 /// <summary>
714 ///Get AISContext
715 /// </summary>
92efcf78 716 Handle(AIS_InteractiveContext) GetContext()
15534713 717 {
718 return myAISContext();
719 }
720
721public:
722 // ============================================
723 // Import / export functionality
724 // ============================================
725
726 /// <summary>
727 ///Import BRep file
728 /// </summary>
729 /// <param name="theFileName">Name of import file</param>
730 bool ImportBrep (System::String^ theFileName)
731 {
95f68826 732 return ImportBrep (toAsciiString (theFileName));
15534713 733 }
734
735 /// <summary>
736 ///Import BRep file
737 /// </summary>
738 /// <param name="theFileName">Name of import file</param>
95f68826 739 bool ImportBrep (const TCollection_AsciiString& theFileName)
15534713 740 {
741 TopoDS_Shape aShape;
742 BRep_Builder aBuilder;
95f68826 743 if (!BRepTools::Read (aShape, theFileName.ToCString(), aBuilder))
15534713 744 {
745 return false;
746 }
0577ae8c 747
15534713 748 Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
0577ae8c 749 myAISContext()->SetMaterial (aPrs, Graphic3d_NOM_GOLD, Standard_False);
15534713 750 myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
0577ae8c 751 myAISContext()->Display (aPrs, Standard_True);
15534713 752 return true;
753 }
754
15534713 755 /// <summary>
756 ///Import Step file
757 /// </summary>
758 /// <param name="theFileName">Name of import file</param>
95f68826 759 bool ImportStep (const TCollection_AsciiString& theFileName)
15534713 760 {
761 STEPControl_Reader aReader;
95f68826 762 if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
15534713 763 {
764 return false;
765 }
766
767 bool isFailsonly = false;
768 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
769
770 int aNbRoot = aReader.NbRootsForTransfer();
771 aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity);
772 for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter)
773 {
774 aReader.TransferRoot (aRootIter);
775 int aNbShap = aReader.NbShapes();
776 if (aNbShap > 0)
777 {
778 for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
779 {
0577ae8c 780 myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_False);
15534713 781 }
0577ae8c 782 myAISContext()->UpdateCurrentViewer();
15534713 783 }
784 }
785 return true;
786 }
787
788 /// <summary>
789 ///Import Iges file
790 /// </summary>
791 /// <param name="theFileName">Name of import file</param>
95f68826 792 bool ImportIges (const TCollection_AsciiString& theFileName)
15534713 793 {
794 IGESControl_Reader aReader;
95f68826 795 if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
15534713 796 {
797 return false;
798 }
799
800 aReader.TransferRoots();
801 TopoDS_Shape aShape = aReader.OneShape();
802 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
803 myAISContext()->UpdateCurrentViewer();
804 return true;
805 }
806
807 /// <summary>
808 ///Export BRep file
809 /// </summary>
810 /// <param name="theFileName">Name of export file</param>
95f68826 811 bool ExportBRep (const TCollection_AsciiString& theFileName)
15534713 812 {
0577ae8c 813 myAISContext()->InitSelected();
814 if (!myAISContext()->MoreSelected())
15534713 815 {
816 return false;
817 }
818
0577ae8c 819 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 820 return !anIS.IsNull()
95f68826 821 && BRepTools::Write (anIS->Shape(), theFileName.ToCString());
15534713 822 }
823
824 /// <summary>
825 ///Export Step file
826 /// </summary>
827 /// <param name="theFileName">Name of export file</param>
95f68826 828 bool ExportStep (const TCollection_AsciiString& theFileName)
15534713 829 {
830 STEPControl_StepModelType aType = STEPControl_AsIs;
831 STEPControl_Writer aWriter;
0577ae8c 832 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 833 {
0577ae8c 834 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 835 if (anIS.IsNull())
836 {
837 return false;
838 }
839
840 TopoDS_Shape aShape = anIS->Shape();
841 if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone)
842 {
843 return false;
844 }
845 }
95f68826 846 return aWriter.Write (theFileName.ToCString()) == IFSelect_RetDone;
15534713 847 }
848
849 /// <summary>
850 ///Export Iges file
851 /// </summary>
852 /// <param name="theFileName">Name of export file</param>
95f68826 853 bool ExportIges (const TCollection_AsciiString& theFileName)
15534713 854 {
855 IGESControl_Controller::Init();
856 IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
857 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
0577ae8c 858 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 859 {
0577ae8c 860 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 861 if (anIS.IsNull())
862 {
863 return false;
864 }
865
866 aWriter.AddShape (anIS->Shape());
867 }
868
869 aWriter.ComputeModel();
95f68826 870 return aWriter.Write (theFileName.ToCString()) != Standard_False;
15534713 871 }
872
873 /// <summary>
874 ///Export Vrml file
875 /// </summary>
876 /// <param name="theFileName">Name of export file</param>
95f68826 877 bool ExportVrml (const TCollection_AsciiString& theFileName)
15534713 878 {
879 TopoDS_Compound aRes;
880 BRep_Builder aBuilder;
881 aBuilder.MakeCompound (aRes);
0577ae8c 882 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 883 {
0577ae8c 884 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 885 if (anIS.IsNull())
886 {
887 return false;
888 }
889 aBuilder.Add (aRes, anIS->Shape());
890 }
891
892 VrmlAPI_Writer aWriter;
95f68826 893 aWriter.Write (aRes, theFileName.ToCString());
15534713 894 return true;
895 }
896
897 /// <summary>
898 ///Export Stl file
899 /// </summary>
900 /// <param name="theFileName">Name of export file</param>
95f68826 901 bool ExportStl (const TCollection_AsciiString& theFileName)
15534713 902 {
903 TopoDS_Compound aComp;
904 BRep_Builder aBuilder;
905 aBuilder.MakeCompound (aComp);
0577ae8c 906 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 907 {
0577ae8c 908 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 909 if (anIS.IsNull())
910 {
911 return false;
912 }
913 aBuilder.Add (aComp, anIS->Shape());
914 }
915
916 StlAPI_Writer aWriter;
95f68826 917 aWriter.Write (aComp, theFileName.ToCString());
15534713 918 return true;
919 }
920
921 /// <summary>
922 ///Define which Import/Export function must be called
923 /// </summary>
924 /// <param name="theFileName">Name of Import/Export file</param>
925 /// <param name="theFormat">Determines format of Import/Export file</param>
926 /// <param name="theIsImport">Determines is Import or not</param>
927 bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
928 {
929 bool isResult = false;
95f68826 930 const TCollection_AsciiString aFilename = toAsciiString (theFileName);
15534713 931 if (theIsImport)
932 {
933 switch (theFormat)
934 {
935 case 0: isResult = ImportBrep (aFilename); break;
41f03605 936 case 1: isResult = ImportStep (aFilename); break;
937 case 2: isResult = ImportIges (aFilename); break;
15534713 938 }
939 }
940 else
941 {
942 switch (theFormat)
943 {
944 case 0: isResult = ExportBRep (aFilename); break;
41f03605 945 case 1: isResult = ExportStep (aFilename); break;
946 case 2: isResult = ExportIges (aFilename); break;
947 case 3: isResult = ExportVrml (aFilename); break;
948 case 4: isResult = ExportStl (aFilename); break;
949 case 5: isResult = Dump (aFilename); break;
15534713 950 }
951 }
15534713 952 return isResult;
953 }
954
955 /// <summary>
956 ///Initialize OCCTProxyD3D
957 /// </summary>
958 void InitOCCTProxy()
959 {
960 myGraphicDriver().Nullify();
961 myViewer().Nullify();
962 myView().Nullify();
963 myAISContext().Nullify();
964 }
965
966private:
967
92efcf78 968 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
969 NCollection_Haft<Handle(V3d_View)> myView;
970 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
971 NCollection_Haft<Handle(D3DHost_GraphicDriver)> myGraphicDriver;
15534713 972
973};