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