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