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