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