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