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