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