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