Update CSharp sample (issue 0024147) for current state of OCCT
[occt.git] / samples / CSharp / OCCTProxy / OCCTProxy.cpp
CommitLineData
d1a2fee8 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 <Graphic3d.hxx>
8#include <Graphic3d_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#include <MgtBRep.hxx>
21#include <PTColStd_PersistentTransientMap.hxx>
22//csfdb I/E
23#include <FSD_File.hxx>
24#include <ShapeSchema.hxx>
25#include <Storage_Data.hxx>
26#include <Storage_HSeqOfRoot.hxx>
27#include <Storage_Root.hxx>
28// iges I/E
29#include <IGESControl_Reader.hxx>
30#include <IGESControl_Controller.hxx>
31#include <IGESControl_Writer.hxx>
32#include <IFSelect_ReturnStatus.hxx>
33#include <Interface_Static.hxx>
34//step I/E
35#include <STEPControl_Reader.hxx>
36#include <STEPControl_Writer.hxx>
37//for stl export
38#include <StlAPI_Writer.hxx>
39//for vrml export
40#include <VrmlAPI_Writer.hxx>
41//wrapper of pure C++ classes to ref classes
42#include <NCollection_Haft.h>
43
44// list of required OCCT libraries
45#pragma comment(lib, "TKernel.lib")
46#pragma comment(lib, "TKMath.lib")
47#pragma comment(lib, "TKBRep.lib")
48#pragma comment(lib, "PTKernel.lib")
49#pragma comment(lib, "TKPShape.lib")
50#pragma comment(lib, "TKShapeSchema.lib")
51#pragma comment(lib, "TKXSBase.lib")
52#pragma comment(lib, "TKService.lib")
53#pragma comment(lib, "TKV3d.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>
63public ref class OCCTProxy
64{
65public:
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() = Graphic3d::InitGraphicDriver (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
d1a2fee8 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 Handle(Aspect_DisplayConnection) aDisplayConnection;
705 myGraphicDriver() = Graphic3d::InitGraphicDriver (aDisplayConnection);
706 }
707 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
708 myView()->SetWindow(aWNTWindow);
709 Standard_Integer w=100, h=100;
710 aWNTWindow->Size(w,h);
711 if (!aWNTWindow->IsMapped())
712 {
713 aWNTWindow->Map();
714 }
715 }
716
717 /// <summary>
718 ///Set AISContext
719 /// </summary>
720 bool SetAISContext(OCCTProxy^ theViewer)
721 {
722 this->myAISContext() = theViewer->GetContext();
723 if (myAISContext().IsNull())
724 {
725 return false;
726 }
727 return true;
728 }
729
730 /// <summary>
731 ///Get AISContext
732 /// </summary>
733 Handle_AIS_InteractiveContext GetContext(void)
734 {
735 return myAISContext();
736 }
737
738public:
739 // ============================================
740 // Import / export functionality
741 // ============================================
742
743 /// <summary>
744 ///Import BRep file
745 /// </summary>
746 /// <param name="theFileName">Name of import file</param>
747 bool ImportBrep(System::String^ theFileName)
748 {
749 bool isResult = false;
750 int aLength = theFileName->Length;
751 char* aFilename = new char[aLength+1];
752 for(int i = 0; i<aLength; i++)
753 {
754 aFilename[i] = (char)theFileName->ToCharArray()[i];
755 }
756 aFilename[aLength] = '\0';
757 isResult = ImportBrep(aFilename);
758 return isResult;
759 }
760
761 /// <summary>
762 ///Import BRep file
763 /// </summary>
764 /// <param name="theFileName">Name of import file</param>
765 bool ImportBrep(char* theFileName)
766 {
767 Standard_CString aFileName = (Standard_CString) theFileName;
768 TopoDS_Shape aShape;
769 BRep_Builder aBuilder;
770 Standard_Boolean isResult = BRepTools::Read(aShape,aFileName,aBuilder);
771 if (!isResult)
772 {
773 return false;
774 }
775 if(myAISContext()->HasOpenedContext())
776 {
777 myAISContext()->CloseLocalContext();
778 }
779 myAISContext()->Display(new AIS_Shape(aShape));
780 return true;
781 }
782
783 /// <summary>
784 ///Import Csfdb file
785 /// </summary>
786 /// <param name="theFileName">Name of import file</param>
787 bool ImportCsfdb(char* theFileName)
788 {
789 Standard_CString aFileName = (Standard_CString) theFileName;
790 if ( FSD_File::IsGoodFileType(aFileName) != Storage_VSOk )
791 {
792 return false;
793 }
794
795 FSD_File aFileDriver;
796 TCollection_AsciiString aName( aFileName );
797 if ( aFileDriver.Open( aName, Storage_VSRead ) != Storage_VSOk )
798 {
799 return false;
800 }
801
802 Handle(ShapeSchema) aSchema = new ShapeSchema();
803 Handle(Storage_Data) data = aSchema->Read( aFileDriver );
804 if ( data->ErrorStatus() != Storage_VSOk )
805 {
806 return false;
807 }
808 aFileDriver.Close();
809
810 Handle(Storage_HSeqOfRoot) aRoots = data->Roots();
811 for ( int i = 1; i <= aRoots->Length() ; i++ )
812 {
813 Handle(Storage_Root) aStorRoot = aRoots->Value( i );
814 Handle(Standard_Persistent) aStandPersistent = aStorRoot->Object();
815 Handle(PTopoDS_HShape) aPShape = Handle(PTopoDS_HShape)::DownCast(aStandPersistent);
816 if ( !aPShape.IsNull() )
817 {
818 PTColStd_PersistentTransientMap aMap;
819 TopoDS_Shape aTShape;
820 MgtBRep::Translate( aPShape, aMap, aTShape, MgtBRep_WithTriangle );
821 myAISContext()->Display(new AIS_Shape(aTShape));
822 }
823 }
824
825 return true;
826 }
827
828 /// <summary>
829 ///Import Step file
830 /// </summary>
831 /// <param name="theFileName">Name of import file</param>
832 bool ImportStep(char* theFileName)
833 {
834 Standard_CString aFileName = (Standard_CString) theFileName;
835 STEPControl_Reader aReader;
836 IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileName);
837 if ( aStatus == IFSelect_RetDone )
838 {
839 bool isFailsonly = false;
840 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
841
842 int aNbRoot = aReader.NbRootsForTransfer();
843 aReader.PrintCheckTransfer( isFailsonly, IFSelect_ItemsByEntity );
844 for ( Standard_Integer n = 1; n <= aNbRoot; n++ )
845 {
846 Standard_Boolean ok = aReader.TransferRoot( n );
847 int aNbShap = aReader.NbShapes();
848 if ( aNbShap > 0 )
849 {
850 for ( int i = 1; i <= aNbShap; i++ )
851 {
852 TopoDS_Shape aShape = aReader.Shape( i );
853 myAISContext()->Display(new AIS_Shape(aShape));
854 }
855 }
856 }
857 }
858 else
859 {
860 return false;
861 }
862
863 return true;
864 }
865
866 /// <summary>
867 ///Import Iges file
868 /// </summary>
869 /// <param name="theFileName">Name of import file</param>
870 bool ImportIges(char* theFileName)
871 {
872 Standard_CString aFileName = (Standard_CString) theFileName;
873 IGESControl_Reader aReader;
874 int aStatus = aReader.ReadFile( aFileName );
875
876 if ( aStatus == IFSelect_RetDone )
877 {
878 aReader.TransferRoots();
879 TopoDS_Shape aShape = aReader.OneShape();
880 myAISContext()->Display(new AIS_Shape(aShape));
881 }
882 else
883 {
884 return false;
885 }
886
887 myAISContext()->UpdateCurrentViewer();
888 return true;
889 }
890
891 /// <summary>
892 ///Export BRep file
893 /// </summary>
894 /// <param name="theFileName">Name of export file</param>
895 bool ExportBRep(char* theFileName)
896 {
897 myAISContext()->InitCurrent();
898 if (!myAISContext()->MoreCurrent())
899 {
900 return false;
901 }
902
903 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
904 Handle_AIS_Shape anIS = Handle_AIS_Shape::DownCast(anIO);
905 return BRepTools::Write (anIS->Shape(), (Standard_CString)theFileName) != Standard_False;
906 }
907
908 /// <summary>
909 ///Export Step file
910 /// </summary>
911 /// <param name="theFileName">Name of export file</param>
912 bool ExportStep(char* theFileName)
913 {
914 STEPControl_StepModelType aType = STEPControl_AsIs;
915 IFSelect_ReturnStatus aStatus;
916 STEPControl_Writer aWriter;
917 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
918 {
919 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
920 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
921 TopoDS_Shape aShape = anIS->Shape();
922 aStatus = aWriter.Transfer( aShape , aType );
923 if ( aStatus != IFSelect_RetDone )
924 {
925 return false;
926 }
927 }
928
929 aStatus = aWriter.Write( (Standard_CString)theFileName );
930 if ( aStatus != IFSelect_RetDone )
931 {
932 return false;
933 }
934
935 return true;
936 }
937
938 /// <summary>
939 ///Export Iges file
940 /// </summary>
941 /// <param name="theFileName">Name of export file</param>
942 bool ExportIges(char* theFileName)
943 {
944 IGESControl_Controller::Init();
945 IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
946 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
947
948 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
949 {
950 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
951 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
952 TopoDS_Shape aShape = anIS->Shape();
953 aWriter.AddShape ( aShape );
954 }
955
956 aWriter.ComputeModel();
957 return aWriter.Write( (Standard_CString)theFileName) != Standard_False;
958 }
959
960 /// <summary>
961 ///Export Vrml file
962 /// </summary>
963 /// <param name="theFileName">Name of export file</param>
964 bool ExportVrml(char* theFileName)
965 {
966 TopoDS_Compound aRes;
967 BRep_Builder aBuilder;
968 aBuilder.MakeCompound( aRes );
969
970 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
971 {
972 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
973 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
974 TopoDS_Shape aShape = anIS->Shape();
975 if ( aShape.IsNull() )
976 {
977 return false;
978 }
979
980 aBuilder.Add( aRes, aShape );
981 }
982
983 VrmlAPI_Writer aWriter;
984 aWriter.Write( aRes, (Standard_CString)theFileName );
985
986 return true;
987 }
988
989 /// <summary>
990 ///Export Stl file
991 /// </summary>
992 /// <param name="theFileName">Name of export file</param>
993 bool ExportStl(char* theFileName)
994 {
995 TopoDS_Compound aComp;
996 BRep_Builder aBuilder;
997 aBuilder.MakeCompound( aComp );
998
999 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
1000 {
1001 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
1002 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
1003 TopoDS_Shape aShape = anIS->Shape();
1004 if ( aShape.IsNull() )
1005 {
1006 return false;
1007 }
1008 aBuilder.Add( aComp, aShape );
1009 }
1010
1011 StlAPI_Writer aWriter;
1012 aWriter.Write( aComp, (Standard_CString)theFileName );
1013 return true;
1014 }
1015
1016 /// <summary>
1017 ///Define which Import/Export function must be called
1018 /// </summary>
1019 /// <param name="theFileName">Name of Import/Export file</param>
1020 /// <param name="theFormat">Determines format of Import/Export file</param>
1021 /// <param name="theIsImport">Determines is Import or not</param>
1022 bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
1023 {
1024 bool isResult;
1025 int aLength = theFileName->Length;
1026 char* aFilename = new char[aLength+1];
1027 for(int i = 0; i<aLength; i++)
1028 {
1029 aFilename[i] = (char)theFileName->ToCharArray()[i];
1030 }
1031 aFilename[aLength] = '\0';
1032
1033 if (theIsImport)
1034 {
1035 switch(theFormat)
1036 {
1037 case 0:
1038 isResult = ImportBrep(aFilename);
1039 break;
1040 case 1:
1041 isResult = ImportCsfdb(aFilename);
1042 break;
1043 case 2:
1044 isResult = ImportStep(aFilename);
1045 break;
1046 case 3:
1047 isResult = ImportIges(aFilename);
1048 break;
1049 default:
1050 isResult = false;
1051 }
1052 }
1053 else
1054 {
1055 switch(theFormat)
1056 {
1057 case 0:
1058 isResult = ExportBRep(aFilename);
1059 break;
1060 case 2:
1061 isResult = ExportStep(aFilename);
1062 break;
1063 case 3:
1064 isResult = ExportIges(aFilename);
1065 break;
1066 case 4:
1067 isResult = ExportVrml(aFilename);
1068 break;
1069 case 5:
1070 isResult = ExportStl(aFilename);
1071 break;
1072 case 6:
1073 isResult = Dump(aFilename);
1074 break;
1075 default:
1076 isResult = false;
1077 }
1078 }
1079 return isResult;
1080 }
1081
1082 /// <summary>
1083 ///Initialize OCCTProxy
1084 /// </summary>
1085 void InitOCCTProxy(void)
1086 {
1087 myGraphicDriver()=NULL;
1088 myViewer()=NULL;
1089 myView()=NULL;
1090 myAISContext()=NULL;
1091 }
1092
1093private:
1094 // fields
1095 NCollection_Haft<Handle_V3d_Viewer> myViewer;
1096 NCollection_Haft<Handle_V3d_View> myView;
1097 NCollection_Haft<Handle_AIS_InteractiveContext> myAISContext;
1098 NCollection_Haft<Handle_Graphic3d_GraphicDriver> myGraphicDriver;
1099};