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