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