44a637ae4eb10dd1cb54986726812f652757eafd
[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
585     myAISContext()->EraseSelected (Standard_True);
586     myAISContext()->ClearCurrents();
587   }
588
589   /// <summary>
590   ///Get version
591   /// </summary>
592   float GetOCCVersion()
593   {
594     return (float )OCC_VERSION;
595   }
596
597   /// <summary>
598   ///set material
599   /// </summary>
600   void SetMaterial (int theMaterial)
601   {
602     if (myAISContext().IsNull())
603     {
604       return;
605     }
606     for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
607     {
608       myAISContext()->SetMaterial (myAISContext()->Current(), (Graphic3d_NameOfMaterial )theMaterial);
609     }
610     myAISContext()->UpdateCurrentViewer();
611   }
612
613   /// <summary>
614   ///set transparency
615   /// </summary>
616   void SetTransparency (int theTrans)
617   {
618     if (myAISContext().IsNull())
619     {
620       return;
621     }
622     for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextSelected())
623     {
624       myAISContext()->SetTransparency (myAISContext()->Current(), ((Standard_Real )theTrans) / 10.0);
625     }
626   }
627
628   /// <summary>
629   ///Return true if object is selected
630   /// </summary>
631   bool IsObjectSelected()
632   {
633     if (myAISContext().IsNull())
634     {
635       return false;
636     }
637     myAISContext()->InitCurrent();
638     return myAISContext()->MoreCurrent() != Standard_False;
639   }
640
641   /// <summary>
642   ///Return display mode
643   /// </summary>
644   int DisplayMode()
645   {
646     if (myAISContext().IsNull())
647     {
648       return -1;
649     }
650
651     bool isOneOrMoreInShading   = false;
652     bool isOneOrMoreInWireframe = false;
653     for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
654     {
655       if (myAISContext()->IsDisplayed (myAISContext()->Current(), AIS_Shaded))
656       {
657         isOneOrMoreInShading = true;
658       }
659       if (myAISContext()->IsDisplayed (myAISContext()->Current(), AIS_WireFrame))
660       {
661         isOneOrMoreInWireframe = true;
662       }
663     }
664     if (isOneOrMoreInShading
665      && isOneOrMoreInWireframe)
666     {
667       return 10;
668     }
669     else if (isOneOrMoreInShading)
670     {
671       return 1;
672     }
673     else if (isOneOrMoreInWireframe)
674     {
675       return 0;
676     }
677     return -1;
678   }
679
680   /// <summary>
681   ///Set AISContext
682   /// </summary>
683   bool SetAISContext (OCCTProxyD3D^ theViewer)
684   {
685      this->myAISContext() = theViewer->GetContext();
686      if (myAISContext().IsNull())
687      {
688        return false;
689      }
690     return true;
691   }
692
693   /// <summary>
694   ///Get AISContext
695   /// </summary>
696   Handle_AIS_InteractiveContext GetContext()
697   {
698     return myAISContext();
699   }
700
701 public:
702   // ============================================
703   // Import / export functionality
704   // ============================================
705
706   /// <summary>
707   ///Import BRep file
708   /// </summary>
709   /// <param name="theFileName">Name of import file</param>
710   bool ImportBrep (System::String^ theFileName)
711   {
712     bool  isResult  = false;
713     int   aLength   = theFileName->Length;
714     char* aFilename = new char[aLength + 1];
715     for(int i = 0; i < aLength; ++i)
716     {
717       aFilename[i] = (char )theFileName->ToCharArray()[i];
718     }
719     aFilename[aLength] = '\0';
720     isResult = ImportBrep (aFilename);
721     delete[] aFilename;
722     return isResult;
723   }
724
725   /// <summary>
726   ///Import BRep file
727   /// </summary>
728   /// <param name="theFileName">Name of import file</param>
729   bool ImportBrep (char* theFileName)
730   {
731     TopoDS_Shape aShape;
732     BRep_Builder aBuilder;
733     if (!BRepTools::Read (aShape, theFileName, aBuilder))
734     {
735       return false;
736     }
737     if (myAISContext()->HasOpenedContext())
738     {
739       myAISContext()->CloseLocalContext();
740     }
741     Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
742     myAISContext()->SetMaterial   (aPrs, Graphic3d_NOM_GOLD);
743     myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
744     myAISContext()->Display (aPrs);
745     return true;
746   }
747
748   /// <summary>
749   ///Import Step file
750   /// </summary>
751   /// <param name="theFileName">Name of import file</param>
752   bool ImportStep (char* theFileName)
753   {
754     STEPControl_Reader aReader;
755     if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
756     {
757       return false;
758     }
759
760     bool isFailsonly = false;
761     aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
762
763     int aNbRoot = aReader.NbRootsForTransfer();
764     aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity);
765     for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter)
766     {
767       aReader.TransferRoot (aRootIter);
768       int aNbShap = aReader.NbShapes();
769       if (aNbShap > 0)
770       {
771         for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
772         {
773           myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_True);
774         }
775       }
776     }
777     return true;
778   }
779
780   /// <summary>
781   ///Import Iges file
782   /// </summary>
783   /// <param name="theFileName">Name of import file</param>
784   bool ImportIges (char* theFileName)
785   {
786     IGESControl_Reader aReader;
787     if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
788     {
789       return false;
790     }
791
792     aReader.TransferRoots();
793     TopoDS_Shape aShape = aReader.OneShape();
794     myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
795     myAISContext()->UpdateCurrentViewer();
796     return true;
797   }
798
799   /// <summary>
800   ///Export BRep file
801   /// </summary>
802   /// <param name="theFileName">Name of export file</param>
803   bool ExportBRep (char* theFileName)
804   {
805     myAISContext()->InitCurrent();
806     if (!myAISContext()->MoreCurrent())
807     {
808       return false;
809     }
810
811     Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
812     return !anIS.IsNull()
813          && BRepTools::Write (anIS->Shape(), theFileName);
814   }
815
816   /// <summary>
817   ///Export Step file
818   /// </summary>
819   /// <param name="theFileName">Name of export file</param>
820   bool ExportStep (char* theFileName)
821   {
822     STEPControl_StepModelType aType = STEPControl_AsIs;
823     STEPControl_Writer        aWriter;
824     for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
825     {
826       Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
827       if (anIS.IsNull())
828       {
829         return false;
830       }
831
832       TopoDS_Shape aShape = anIS->Shape();
833       if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone)
834       {
835         return false;
836       }
837     }
838     return aWriter.Write (theFileName) == IFSelect_RetDone;
839   }
840
841   /// <summary>
842   ///Export Iges file
843   /// </summary>
844   /// <param name="theFileName">Name of export file</param>
845   bool ExportIges (char* theFileName)
846   {
847     IGESControl_Controller::Init();
848     IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
849                                 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
850     for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
851     {
852       Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
853       if (anIS.IsNull())
854       {
855         return false;
856       }
857
858       aWriter.AddShape (anIS->Shape());
859     }
860
861     aWriter.ComputeModel();
862     return aWriter.Write (theFileName) != Standard_False;
863   }
864
865   /// <summary>
866   ///Export Vrml file
867   /// </summary>
868   /// <param name="theFileName">Name of export file</param>
869   bool ExportVrml (char* theFileName)
870   {
871     TopoDS_Compound aRes;
872     BRep_Builder    aBuilder;
873     aBuilder.MakeCompound (aRes);
874     for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
875     {
876       Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
877       if (anIS.IsNull())
878       {
879         return false;
880       }
881       aBuilder.Add (aRes, anIS->Shape());
882     }
883
884     VrmlAPI_Writer aWriter;
885     aWriter.Write (aRes, theFileName);
886     return true;
887   }
888
889   /// <summary>
890   ///Export Stl file
891   /// </summary>
892   /// <param name="theFileName">Name of export file</param>
893   bool ExportStl (char* theFileName)
894   {
895     TopoDS_Compound aComp;
896     BRep_Builder    aBuilder;
897     aBuilder.MakeCompound (aComp);
898     for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
899     {
900       Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
901       if (anIS.IsNull())
902       {
903         return false;
904       }
905       aBuilder.Add (aComp, anIS->Shape());
906     }
907
908     StlAPI_Writer aWriter;
909     aWriter.Write (aComp, theFileName);
910     return true;
911   }
912
913   /// <summary>
914   ///Define which Import/Export function must be called
915   /// </summary>
916   /// <param name="theFileName">Name of Import/Export file</param>
917   /// <param name="theFormat">Determines format of Import/Export file</param>
918   /// <param name="theIsImport">Determines is Import or not</param>
919   bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
920   {
921     bool  isResult  = false;
922     int   aLength   = theFileName->Length;
923     char* aFilename = new char[aLength + 1];
924     for (int aCharIter = 0; aCharIter < aLength; ++aCharIter)
925     {
926       aFilename[aCharIter] = (char)theFileName->ToCharArray()[aCharIter];
927     }
928     aFilename[aLength] = '\0';
929
930     if (theIsImport)
931     {
932       switch (theFormat)
933       {
934         case 0: isResult = ImportBrep  (aFilename); break;
935         case 1: isResult = ImportStep  (aFilename); break;
936         case 2: isResult = ImportIges  (aFilename); break;
937       }
938     }
939     else 
940     {
941       switch (theFormat)
942       {
943         case 0: isResult = ExportBRep (aFilename); break;
944         case 1: isResult = ExportStep (aFilename); break;
945         case 2: isResult = ExportIges (aFilename); break;
946         case 3: isResult = ExportVrml (aFilename); break;
947         case 4: isResult = ExportStl  (aFilename); break;
948         case 5: isResult = Dump (aFilename);      break;
949       }
950     }
951     delete[] aFilename;
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
966 private:
967
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;
972
973 };