0028316: Coding Rules - Elimilate confusing aliases of Standard_Real type in V3d_View
[occt.git] / samples / CSharp / OCCTProxy_D3D / OCCTProxyD3D.cpp
CommitLineData
62e1beed 1#include <windows.h>
15534713 2
3// include required OCCT headers
4#include <Standard_Version.hxx>
5#include <Message_ProgressIndicator.hxx>
6//for OCC graphic
15534713 7#include <WNT_Window.hxx>
62e1beed 8#include <WNT_WClass.hxx>
15534713 9#include <Graphic3d_CView.hxx>
10#include <Graphic3d_Camera.hxx>
11#include <Graphic3d_TextureParams.hxx>
62e1beed 12#include <D3DHost_GraphicDriver.hxx>
c357e426 13#include <D3DHost_View.hxx>
15534713 14//for object display
15#include <V3d_Viewer.hxx>
16#include <V3d_View.hxx>
15534713 17#include <AIS_InteractiveContext.hxx>
18#include <AIS_Shape.hxx>
19//topology
20#include <TopoDS_Shape.hxx>
21#include <TopoDS_Compound.hxx>
22//brep tools
23#include <BRep_Builder.hxx>
24#include <BRepTools.hxx>
15534713 25// iges I/E
26#include <IGESControl_Reader.hxx>
27#include <IGESControl_Controller.hxx>
28#include <IGESControl_Writer.hxx>
29#include <IFSelect_ReturnStatus.hxx>
30#include <Interface_Static.hxx>
31//step I/E
32#include <STEPControl_Reader.hxx>
33#include <STEPControl_Writer.hxx>
34//for stl export
35#include <StlAPI_Writer.hxx>
36//for vrml export
37#include <VrmlAPI_Writer.hxx>
38//wrapper of pure C++ classes to ref classes
39#include <NCollection_Haft.h>
40
41// list of required OCCT libraries
42#pragma comment(lib, "TKernel.lib")
43#pragma comment(lib, "TKMath.lib")
44#pragma comment(lib, "TKBRep.lib")
15534713 45#pragma comment(lib, "TKXSBase.lib")
46#pragma comment(lib, "TKService.lib")
47#pragma comment(lib, "TKV3d.lib")
48#pragma comment(lib, "TKOpenGl.lib")
62e1beed 49#pragma comment(lib, "TKD3dHost.lib")
15534713 50#pragma comment(lib, "TKIGES.lib")
51#pragma comment(lib, "TKSTEP.lib")
52#pragma comment(lib, "TKStl.lib")
53#pragma comment(lib, "TKVrml.lib")
54
62e1beed 55#pragma comment(lib, "D3D9.lib")
56
15534713 57/// <summary>
58/// Proxy class encapsulating calls to OCCT C++ classes within
59/// C++/CLI class visible from .Net (CSharp)
60/// </summary>
61public ref class OCCTProxyD3D
62{
63public:
64
62e1beed 65 OCCTProxyD3D() {}
15534713 66
67 // ============================================
68 // Viewer functionality
69 // ============================================
70
71 /// <summary>
72 ///Initialize a viewer
73 /// </summary>
74 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
62e1beed 75 bool InitViewer()
15534713 76 {
62e1beed 77 myGraphicDriver() = new D3DHost_GraphicDriver();
78 myGraphicDriver()->ChangeOptions().buffersNoSwap = true;
15534713 79 //myGraphicDriver()->ChangeOptions().contextDebug = true;
80
6a24c6de 81 myViewer() = new V3d_Viewer (myGraphicDriver());
15534713 82 myViewer()->SetDefaultLights();
83 myViewer()->SetLightOn();
84 myView() = myViewer()->CreateView();
62e1beed 85
86 static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC);
87 Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64);
88 aWNTWindow->SetVirtual (Standard_True);
15534713 89 myView()->SetWindow(aWNTWindow);
15534713 90 myAISContext() = new AIS_InteractiveContext (myViewer());
91 myAISContext()->UpdateCurrentViewer();
92 myView()->MustBeResized();
93 return true;
94 }
95
15534713 96 /// <summary> Resizes custom FBO for Direct3D output. </summary>
62e1beed 97 System::IntPtr ResizeBridgeFBO (int theWinSizeX,
98 int theWinSizeY)
15534713 99 {
62e1beed 100 Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window());
101 aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY);
102 myView()->MustBeResized();
103 myView()->Invalidate();
c357e426 104 return System::IntPtr(Handle(D3DHost_View)::DownCast (myView()->View())->D3dColorSurface());
15534713 105 }
106
107 /// <summary>
108 /// Make dump of current view to file
109 /// </summary>
110 /// <param name="theFileName">Name of dump file</param>
111 bool Dump (const char* theFileName)
112 {
113 if (myView().IsNull())
114 {
115 return false;
116 }
117 myView()->Redraw();
118 return myView()->Dump (theFileName) != Standard_False;
119 }
120
121 /// <summary>
122 ///Redraw view
123 /// </summary>
124 void RedrawView()
125 {
126 if (!myView().IsNull())
127 {
128 myView()->Redraw();
129 }
130 }
131
132 /// <summary>
133 ///Update view
134 /// </summary>
135 void UpdateView(void)
136 {
137 if (!myView().IsNull())
138 {
139 myView()->MustBeResized();
140 }
141 }
142
143 /// <summary>
144 ///Set computed mode in false
145 /// </summary>
146 void SetDegenerateModeOn()
147 {
148 if (!myView().IsNull())
149 {
150 myView()->SetComputedMode (Standard_False);
151 }
152 }
153
154 /// <summary>
155 ///Set computed mode in true
156 /// </summary>
157 void SetDegenerateModeOff()
158 {
159 if (!myView().IsNull())
160 {
161 myView()->SetComputedMode (Standard_True);
162 }
163 }
164
165 /// <summary>
166 ///Fit all
167 /// </summary>
168 void WindowFitAll (int theXmin, int theYmin,
169 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 {
ee2be2a8 183 Standard_Real aZoomFactor = theZoomFactor;
15534713 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 {
0577ae8c 241 myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
15534713 242 }
243 }
244
245 /// <summary>
246 ///Select by click
247 /// </summary>
248 void Select()
249 {
250 if (!myAISContext().IsNull())
251 {
0577ae8c 252 myAISContext()->Select (Standard_True);
15534713 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 {
0577ae8c 263 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
15534713 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 {
0577ae8c 274 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
15534713 275 }
276 }
277
278 /// <summary>
279 ///Select by "Shift" key
280 /// </summary>
281 void ShiftSelect()
282 {
283 if (!myAISContext().IsNull())
284 {
0577ae8c 285 myAISContext()->ShiftSelect (Standard_True);
15534713 286 }
287 }
288
289 /// <summary>
290 ///Set background color
291 /// </summary>
292 void BackgroundColor (int& theRed, int& theGreen, int& theBlue)
293 {
294 if (!myView().IsNull())
295 {
296 Quantity_Color aColor = myView()->BackgroundColor();
297 theRed = (int )aColor.Red() * 255;
298 theGreen = (int )aColor.Green() * 255;
299 theBlue = (int )aColor.Blue() * 255;
300 }
301 }
302
303 /// <summary>
304 ///Get background color Red
305 /// </summary>
306 int GetBGColR()
307 {
308 int anRgb[3];
309 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
310 return anRgb[0];
311 }
312
313 /// <summary>
314 ///Get background color Green
315 /// </summary>
316 int GetBGColG()
317 {
318 int anRgb[3];
319 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
320 return anRgb[1];
321 }
322
323 /// <summary>
324 ///Get background color Blue
325 /// </summary>
326 int GetBGColB()
327 {
328 int anRgb[3];
329 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
330 return anRgb[2];
331 }
332
333 /// <summary>
334 ///Update current viewer
335 /// </summary>
336 void UpdateCurrentViewer()
337 {
338 if (!myAISContext().IsNull())
339 {
340 myAISContext()->UpdateCurrentViewer();
341 }
342 }
343
344 /// <summary>
345 ///Front side
346 /// </summary>
347 void FrontView()
348 {
349 if (!myView().IsNull())
350 {
32757c6e 351 myView()->SetProj (V3d_Yneg);
15534713 352 }
353 }
354
355 /// <summary>
356 ///Top side
357 /// </summary>
358 void TopView()
359 {
360 if (!myView().IsNull())
361 {
362 myView()->SetProj (V3d_Zpos);
363 }
364 }
365
366 /// <summary>
367 ///Left side
368 /// </summary>
369 void LeftView()
370 {
371 if (!myView().IsNull())
372 {
32757c6e 373 myView()->SetProj (V3d_Xneg);
15534713 374 }
375 }
376
377 /// <summary>
378 ///Back side
379 /// </summary>
380 void BackView()
381 {
382 if (!myView().IsNull())
383 {
32757c6e 384 myView()->SetProj (V3d_Ypos);
15534713 385 }
386 }
387
388 /// <summary>
389 ///Right side
390 /// </summary>
391 void RightView()
392 {
393 if (!myView().IsNull())
394 {
32757c6e 395 myView()->SetProj (V3d_Xpos);
15534713 396 }
397 }
398
399 /// <summary>
400 ///Bottom side
401 /// </summary>
402 void BottomView()
403 {
404 if (!myView().IsNull())
405 {
406 myView()->SetProj (V3d_Zneg);
407 }
408 }
409
410 /// <summary>
411 ///Axo side
412 /// </summary>
413 void AxoView()
414 {
415 if (!myView().IsNull())
416 {
417 myView()->SetProj (V3d_XposYnegZpos);
418 }
419 }
420
421 /// <summary>
422 ///Scale
423 /// </summary>
424 float Scale()
425 {
426 return myView().IsNull()
427 ? -1.0f
428 : float(myView()->Scale());
429 }
430
431 /// <summary>
432 ///Zoom in all view
433 /// </summary>
434 void ZoomAllView()
435 {
436 if (!myView().IsNull())
437 {
438 myView()->FitAll();
439 myView()->ZFitAll();
440 }
441 }
442
443 /// <summary>
444 ///Reset view
445 /// </summary>
446 void Reset()
447 {
448 if (!myView().IsNull())
449 {
450 myView()->Reset();
451 }
452 }
453
454 /// <summary>
455 ///Set display mode of objects
456 /// </summary>
457 /// <param name="theMode">Set current mode</param>
458 void SetDisplayMode (int theMode)
459 {
460 if (myAISContext().IsNull())
461 {
462 return;
463 }
464
465 AIS_DisplayMode aCurrentMode = theMode == 0
466 ? AIS_WireFrame
467 : AIS_Shaded;
0577ae8c 468 if (myAISContext()->NbSelected() == 0)
15534713 469 {
0577ae8c 470 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
15534713 471 }
472 else
473 {
0577ae8c 474 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 475 {
0577ae8c 476 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
15534713 477 }
478 }
479 myAISContext()->UpdateCurrentViewer();
480 }
481
482 /// <summary>
483 ///Set color
484 /// </summary>
485 void SetColor (int theR, int theG, int theB)
486 {
487 if (myAISContext().IsNull())
488 {
489 return;
490 }
491
492 Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB);
0577ae8c 493 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 494 {
87432b82 495 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, false);
15534713 496 }
0577ae8c 497 myAISContext()->UpdateCurrentViewer();
15534713 498 }
499
500 /// <summary>
501 ///Get object color red
502 /// </summary>
503 int GetObjColR()
504 {
505 int anRgb[3];
506 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
507 return anRgb[0];
508 }
509
510 /// <summary>
511 ///Get object color green
512 /// </summary>
513 int GetObjColG()
514 {
515 int anRgb[3];
516 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
517 return anRgb[1];
518 }
519
520 /// <summary>
521 ///Get object color blue
522 /// </summary>
523 int GetObjColB()
524 {
525 int anRgb[3];
526 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
527 return anRgb[2];
528 }
529
530 /// <summary>
531 ///Get object color R/G/B
532 /// </summary>
533 void ObjectColor (int& theRed, int& theGreen, int& theBlue)
534 {
535 if (myAISContext().IsNull())
536 {
537 return;
538 }
539
540 theRed = 255;
541 theGreen = 255;
542 theBlue = 255;
0577ae8c 543 myAISContext()->InitSelected();
544 if (!myAISContext()->MoreSelected())
15534713 545 {
546 return;
547 }
548
0577ae8c 549 Handle(AIS_InteractiveObject) aCurrent = myAISContext()->SelectedInteractive();
15534713 550 if (aCurrent->HasColor())
551 {
87432b82 552 Quantity_Color anObjCol;
553 myAISContext()->Color (aCurrent, anObjCol);
15534713 554 theRed = int(anObjCol.Red() * 255.0);
555 theGreen = int(anObjCol.Green() * 255.0);
556 theBlue = int(anObjCol.Blue() * 255.0);
557 }
558 }
559
560 /// <summary>
561 ///Set background color R/G/B
562 /// </summary>
563 void SetBackgroundColor (int theRed, int theGreen, int theBlue)
564 {
565 if (!myView().IsNull())
566 {
567 myView()->SetBackgroundColor (Quantity_TOC_RGB, theRed / 255.0, theGreen / 255.0, theBlue / 255.0);
568 }
569 }
570
571 /// <summary>
572 ///Erase objects
573 /// </summary>
574 void EraseObjects()
575 {
576 if (myAISContext().IsNull())
577 {
578 return;
579 }
7140edaf 580
0577ae8c 581 myAISContext()->EraseSelected (Standard_False);
582 myAISContext()->ClearSelected (Standard_True);
15534713 583 }
584
585 /// <summary>
586 ///Get version
587 /// </summary>
588 float GetOCCVersion()
589 {
590 return (float )OCC_VERSION;
591 }
592
593 /// <summary>
594 ///set material
595 /// </summary>
596 void SetMaterial (int theMaterial)
597 {
598 if (myAISContext().IsNull())
599 {
600 return;
601 }
0577ae8c 602 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 603 {
0577ae8c 604 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial )theMaterial, Standard_False);
15534713 605 }
606 myAISContext()->UpdateCurrentViewer();
607 }
608
609 /// <summary>
610 ///set transparency
611 /// </summary>
612 void SetTransparency (int theTrans)
613 {
614 if (myAISContext().IsNull())
615 {
616 return;
617 }
0577ae8c 618 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 619 {
0577ae8c 620 myAISContext()->SetTransparency (myAISContext()->Current(), ((Standard_Real )theTrans) / 10.0, Standard_False);
15534713 621 }
0577ae8c 622 myAISContext()->UpdateCurrentViewer();
15534713 623 }
624
625 /// <summary>
626 ///Return true if object is selected
627 /// </summary>
628 bool IsObjectSelected()
629 {
630 if (myAISContext().IsNull())
631 {
632 return false;
633 }
0577ae8c 634 myAISContext()->InitSelected();
635 return myAISContext()->MoreSelected() != Standard_False;
15534713 636 }
637
638 /// <summary>
639 ///Return display mode
640 /// </summary>
641 int DisplayMode()
642 {
643 if (myAISContext().IsNull())
644 {
645 return -1;
646 }
647
648 bool isOneOrMoreInShading = false;
649 bool isOneOrMoreInWireframe = false;
0577ae8c 650 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 651 {
0577ae8c 652 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_Shaded))
15534713 653 {
654 isOneOrMoreInShading = true;
655 }
0577ae8c 656 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_WireFrame))
15534713 657 {
658 isOneOrMoreInWireframe = true;
659 }
660 }
661 if (isOneOrMoreInShading
662 && isOneOrMoreInWireframe)
663 {
664 return 10;
665 }
666 else if (isOneOrMoreInShading)
667 {
668 return 1;
669 }
670 else if (isOneOrMoreInWireframe)
671 {
672 return 0;
673 }
674 return -1;
675 }
676
677 /// <summary>
15534713 678 ///Set AISContext
679 /// </summary>
680 bool SetAISContext (OCCTProxyD3D^ theViewer)
681 {
682 this->myAISContext() = theViewer->GetContext();
683 if (myAISContext().IsNull())
684 {
685 return false;
686 }
687 return true;
688 }
689
690 /// <summary>
691 ///Get AISContext
692 /// </summary>
92efcf78 693 Handle(AIS_InteractiveContext) GetContext()
15534713 694 {
695 return myAISContext();
696 }
697
698public:
699 // ============================================
700 // Import / export functionality
701 // ============================================
702
703 /// <summary>
704 ///Import BRep file
705 /// </summary>
706 /// <param name="theFileName">Name of import file</param>
707 bool ImportBrep (System::String^ theFileName)
708 {
709 bool isResult = false;
710 int aLength = theFileName->Length;
711 char* aFilename = new char[aLength + 1];
712 for(int i = 0; i < aLength; ++i)
713 {
714 aFilename[i] = (char )theFileName->ToCharArray()[i];
715 }
716 aFilename[aLength] = '\0';
717 isResult = ImportBrep (aFilename);
718 delete[] aFilename;
719 return isResult;
720 }
721
722 /// <summary>
723 ///Import BRep file
724 /// </summary>
725 /// <param name="theFileName">Name of import file</param>
726 bool ImportBrep (char* theFileName)
727 {
728 TopoDS_Shape aShape;
729 BRep_Builder aBuilder;
730 if (!BRepTools::Read (aShape, theFileName, aBuilder))
731 {
732 return false;
733 }
0577ae8c 734
15534713 735 Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
0577ae8c 736 myAISContext()->SetMaterial (aPrs, Graphic3d_NOM_GOLD, Standard_False);
15534713 737 myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
0577ae8c 738 myAISContext()->Display (aPrs, Standard_True);
15534713 739 return true;
740 }
741
742 /// <summary>
15534713 743 ///Import Step file
744 /// </summary>
745 /// <param name="theFileName">Name of import file</param>
746 bool ImportStep (char* theFileName)
747 {
748 STEPControl_Reader aReader;
749 if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
750 {
751 return false;
752 }
753
754 bool isFailsonly = false;
755 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
756
757 int aNbRoot = aReader.NbRootsForTransfer();
758 aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity);
759 for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter)
760 {
761 aReader.TransferRoot (aRootIter);
762 int aNbShap = aReader.NbShapes();
763 if (aNbShap > 0)
764 {
765 for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
766 {
0577ae8c 767 myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_False);
15534713 768 }
0577ae8c 769 myAISContext()->UpdateCurrentViewer();
15534713 770 }
771 }
772 return true;
773 }
774
775 /// <summary>
776 ///Import Iges file
777 /// </summary>
778 /// <param name="theFileName">Name of import file</param>
779 bool ImportIges (char* theFileName)
780 {
781 IGESControl_Reader aReader;
782 if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
783 {
784 return false;
785 }
786
787 aReader.TransferRoots();
788 TopoDS_Shape aShape = aReader.OneShape();
789 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
790 myAISContext()->UpdateCurrentViewer();
791 return true;
792 }
793
794 /// <summary>
795 ///Export BRep file
796 /// </summary>
797 /// <param name="theFileName">Name of export file</param>
798 bool ExportBRep (char* theFileName)
799 {
0577ae8c 800 myAISContext()->InitSelected();
801 if (!myAISContext()->MoreSelected())
15534713 802 {
803 return false;
804 }
805
0577ae8c 806 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 807 return !anIS.IsNull()
808 && BRepTools::Write (anIS->Shape(), theFileName);
809 }
810
811 /// <summary>
812 ///Export Step file
813 /// </summary>
814 /// <param name="theFileName">Name of export file</param>
815 bool ExportStep (char* theFileName)
816 {
817 STEPControl_StepModelType aType = STEPControl_AsIs;
818 STEPControl_Writer aWriter;
0577ae8c 819 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 820 {
0577ae8c 821 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 822 if (anIS.IsNull())
823 {
824 return false;
825 }
826
827 TopoDS_Shape aShape = anIS->Shape();
828 if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone)
829 {
830 return false;
831 }
832 }
833 return aWriter.Write (theFileName) == IFSelect_RetDone;
834 }
835
836 /// <summary>
837 ///Export Iges file
838 /// </summary>
839 /// <param name="theFileName">Name of export file</param>
840 bool ExportIges (char* theFileName)
841 {
842 IGESControl_Controller::Init();
843 IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
844 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
0577ae8c 845 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 846 {
0577ae8c 847 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 848 if (anIS.IsNull())
849 {
850 return false;
851 }
852
853 aWriter.AddShape (anIS->Shape());
854 }
855
856 aWriter.ComputeModel();
857 return aWriter.Write (theFileName) != Standard_False;
858 }
859
860 /// <summary>
861 ///Export Vrml file
862 /// </summary>
863 /// <param name="theFileName">Name of export file</param>
864 bool ExportVrml (char* theFileName)
865 {
866 TopoDS_Compound aRes;
867 BRep_Builder aBuilder;
868 aBuilder.MakeCompound (aRes);
0577ae8c 869 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 870 {
0577ae8c 871 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 872 if (anIS.IsNull())
873 {
874 return false;
875 }
876 aBuilder.Add (aRes, anIS->Shape());
877 }
878
879 VrmlAPI_Writer aWriter;
880 aWriter.Write (aRes, theFileName);
881 return true;
882 }
883
884 /// <summary>
885 ///Export Stl file
886 /// </summary>
887 /// <param name="theFileName">Name of export file</param>
888 bool ExportStl (char* theFileName)
889 {
890 TopoDS_Compound aComp;
891 BRep_Builder aBuilder;
892 aBuilder.MakeCompound (aComp);
0577ae8c 893 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
15534713 894 {
0577ae8c 895 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
15534713 896 if (anIS.IsNull())
897 {
898 return false;
899 }
900 aBuilder.Add (aComp, anIS->Shape());
901 }
902
903 StlAPI_Writer aWriter;
904 aWriter.Write (aComp, theFileName);
905 return true;
906 }
907
908 /// <summary>
909 ///Define which Import/Export function must be called
910 /// </summary>
911 /// <param name="theFileName">Name of Import/Export file</param>
912 /// <param name="theFormat">Determines format of Import/Export file</param>
913 /// <param name="theIsImport">Determines is Import or not</param>
914 bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
915 {
916 bool isResult = false;
917 int aLength = theFileName->Length;
918 char* aFilename = new char[aLength + 1];
919 for (int aCharIter = 0; aCharIter < aLength; ++aCharIter)
920 {
921 aFilename[aCharIter] = (char)theFileName->ToCharArray()[aCharIter];
922 }
923 aFilename[aLength] = '\0';
924
925 if (theIsImport)
926 {
927 switch (theFormat)
928 {
929 case 0: isResult = ImportBrep (aFilename); break;
41f03605 930 case 1: isResult = ImportStep (aFilename); break;
931 case 2: isResult = ImportIges (aFilename); break;
15534713 932 }
933 }
934 else
935 {
936 switch (theFormat)
937 {
938 case 0: isResult = ExportBRep (aFilename); break;
41f03605 939 case 1: isResult = ExportStep (aFilename); break;
940 case 2: isResult = ExportIges (aFilename); break;
941 case 3: isResult = ExportVrml (aFilename); break;
942 case 4: isResult = ExportStl (aFilename); break;
943 case 5: isResult = Dump (aFilename); break;
15534713 944 }
945 }
946 delete[] aFilename;
947 return isResult;
948 }
949
950 /// <summary>
951 ///Initialize OCCTProxyD3D
952 /// </summary>
953 void InitOCCTProxy()
954 {
955 myGraphicDriver().Nullify();
956 myViewer().Nullify();
957 myView().Nullify();
958 myAISContext().Nullify();
959 }
960
961private:
962
92efcf78 963 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
964 NCollection_Haft<Handle(V3d_View)> myView;
965 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
966 NCollection_Haft<Handle(D3DHost_GraphicDriver)> myGraphicDriver;
15534713 967
968};