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