4 // include required OCCT headers
5 #include <Standard_Version.hxx>
6 #include <Message_ProgressIndicator.hxx>
8 #include <WNT_Window.hxx>
9 #include <WNT_WClass.hxx>
10 #include <Graphic3d_CView.hxx>
11 #include <Graphic3d_Camera.hxx>
12 #include <Graphic3d_TextureParams.hxx>
13 #include <D3DHost_GraphicDriver.hxx>
14 #include <D3DHost_View.hxx>
16 #include <V3d_Viewer.hxx>
17 #include <V3d_View.hxx>
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_Shape.hxx>
21 #include <TopoDS_Shape.hxx>
22 #include <TopoDS_Compound.hxx>
24 #include <BRep_Builder.hxx>
25 #include <BRepTools.hxx>
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>
33 #include <STEPControl_Reader.hxx>
34 #include <STEPControl_Writer.hxx>
36 #include <StlAPI_Writer.hxx>
38 #include <VrmlAPI_Writer.hxx>
39 //wrapper of pure C++ classes to ref classes
40 #include <NCollection_Haft.h>
42 // list of required OCCT libraries
43 #pragma comment(lib, "TKernel.lib")
44 #pragma comment(lib, "TKMath.lib")
45 #pragma comment(lib, "TKBRep.lib")
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, "TKD3dHost.lib")
51 #pragma comment(lib, "TKIGES.lib")
52 #pragma comment(lib, "TKSTEP.lib")
53 #pragma comment(lib, "TKStl.lib")
54 #pragma comment(lib, "TKVrml.lib")
56 #pragma comment(lib, "D3D9.lib")
59 /// Proxy class encapsulating calls to OCCT C++ classes within
60 /// C++/CLI class visible from .Net (CSharp)
62 public ref class OCCTProxyD3D
68 // ============================================
69 // Viewer functionality
70 // ============================================
73 ///Initialize a viewer
75 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
78 myGraphicDriver() = new D3DHost_GraphicDriver();
79 myGraphicDriver()->ChangeOptions().buffersNoSwap = true;
80 //myGraphicDriver()->ChangeOptions().contextDebug = true;
82 myViewer() = new V3d_Viewer (myGraphicDriver());
83 myViewer()->SetDefaultLights();
84 myViewer()->SetLightOn();
85 myView() = myViewer()->CreateView();
87 static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC);
88 Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64);
89 aWNTWindow->SetVirtual (Standard_True);
90 myView()->SetWindow(aWNTWindow);
91 myAISContext() = new AIS_InteractiveContext (myViewer());
92 myAISContext()->UpdateCurrentViewer();
93 myView()->MustBeResized();
97 /// <summary> Resizes custom FBO for Direct3D output. </summary>
98 System::IntPtr ResizeBridgeFBO (int theWinSizeX,
101 Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window());
102 aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY);
103 myView()->MustBeResized();
104 myView()->Invalidate();
105 return System::IntPtr(Handle(D3DHost_View)::DownCast (myView()->View())->D3dColorSurface());
109 /// Make dump of current view to file
111 /// <param name="theFileName">Name of dump file</param>
112 bool Dump (const char* theFileName)
114 if (myView().IsNull())
119 return myView()->Dump (theFileName) != Standard_False;
127 if (!myView().IsNull())
136 void UpdateView(void)
138 if (!myView().IsNull())
140 myView()->MustBeResized();
145 ///Set computed mode in false
147 void SetDegenerateModeOn()
149 if (!myView().IsNull())
151 myView()->SetComputedMode (Standard_False);
157 ///Set computed mode in true
159 void SetDegenerateModeOff()
161 if (!myView().IsNull())
163 myView()->SetComputedMode (Standard_True);
171 void WindowFitAll (int theXmin, int theYmin,
172 int theXmax, int theYmax)
174 if (!myView().IsNull())
176 myView()->WindowFitAll (theXmin, theYmin, theXmax, theYmax);
181 ///Current place of window
183 /// <param name="theZoomFactor">Current zoom</param>
184 void Place (int theX, int theY, float theZoomFactor)
186 Standard_Real aZoomFactor = theZoomFactor;
187 if (!myView().IsNull())
189 myView()->Place (theX, theY, aZoomFactor);
196 void Zoom (int theX1, int theY1, int theX2, int theY2)
198 if (!myView().IsNull())
200 myView()->Zoom (theX1, theY1, theX2, theY2);
207 void Pan (int theX, int theY)
209 if (!myView().IsNull())
211 myView()->Pan (theX, theY);
218 void Rotation (int theX, int theY)
220 if (!myView().IsNull())
222 myView()->Rotation (theX, theY);
229 void StartRotation (int theX, int theY)
231 if (!myView().IsNull())
233 myView()->StartRotation (theX, theY);
238 ///Select by rectangle
240 void Select (int theX1, int theY1, int theX2, int theY2)
242 if (!myAISContext().IsNull())
244 myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
253 if (!myAISContext().IsNull())
255 myAISContext()->Select (Standard_True);
262 void MoveTo (int theX, int theY)
264 if (!myAISContext().IsNull() && !myView().IsNull())
266 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
271 ///Select by rectangle with pressed "Shift" key
273 void ShiftSelect (int theX1, int theY1, int theX2, int theY2)
275 if (!myAISContext().IsNull() && !myView().IsNull())
277 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
282 ///Select by "Shift" key
286 if (!myAISContext().IsNull())
288 myAISContext()->ShiftSelect (Standard_True);
293 ///Set background color
295 void BackgroundColor (int& theRed, int& theGreen, int& theBlue)
297 if (!myView().IsNull())
299 Quantity_Color aColor = myView()->BackgroundColor();
300 theRed = (int )aColor.Red() * 255;
301 theGreen = (int )aColor.Green() * 255;
302 theBlue = (int )aColor.Blue() * 255;
307 ///Get background color Red
312 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
317 ///Get background color Green
322 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
327 ///Get background color Blue
332 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
337 ///Update current viewer
339 void UpdateCurrentViewer()
341 if (!myAISContext().IsNull())
343 myAISContext()->UpdateCurrentViewer();
352 if (!myView().IsNull())
354 myView()->SetProj (V3d_Yneg);
363 if (!myView().IsNull())
365 myView()->SetProj (V3d_Zpos);
374 if (!myView().IsNull())
376 myView()->SetProj (V3d_Xneg);
385 if (!myView().IsNull())
387 myView()->SetProj (V3d_Ypos);
396 if (!myView().IsNull())
398 myView()->SetProj (V3d_Xpos);
407 if (!myView().IsNull())
409 myView()->SetProj (V3d_Zneg);
418 if (!myView().IsNull())
420 myView()->SetProj (V3d_XposYnegZpos);
429 return myView().IsNull()
431 : float(myView()->Scale());
439 if (!myView().IsNull())
451 if (!myView().IsNull())
458 ///Set display mode of objects
460 /// <param name="theMode">Set current mode</param>
461 void SetDisplayMode (int theMode)
463 if (myAISContext().IsNull())
468 AIS_DisplayMode aCurrentMode = theMode == 0
471 if (myAISContext()->NbSelected() == 0)
473 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
477 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
479 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
482 myAISContext()->UpdateCurrentViewer();
488 void SetColor (int theR, int theG, int theB)
490 if (myAISContext().IsNull())
495 Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB);
496 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
498 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, false);
500 myAISContext()->UpdateCurrentViewer();
504 ///Get object color red
509 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
514 ///Get object color green
519 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
524 ///Get object color blue
529 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
534 ///Get object color R/G/B
536 void ObjectColor (int& theRed, int& theGreen, int& theBlue)
538 if (myAISContext().IsNull())
546 myAISContext()->InitSelected();
547 if (!myAISContext()->MoreSelected())
552 Handle(AIS_InteractiveObject) aCurrent = myAISContext()->SelectedInteractive();
553 if (aCurrent->HasColor())
555 Quantity_Color anObjCol;
556 myAISContext()->Color (aCurrent, anObjCol);
557 theRed = int(anObjCol.Red() * 255.0);
558 theGreen = int(anObjCol.Green() * 255.0);
559 theBlue = int(anObjCol.Blue() * 255.0);
564 ///Set background color R/G/B
566 void SetBackgroundColor (int theRed, int theGreen, int theBlue)
568 if (!myView().IsNull())
570 myView()->SetBackgroundColor (Quantity_TOC_RGB, theRed / 255.0, theGreen / 255.0, theBlue / 255.0);
579 if (myAISContext().IsNull())
584 myAISContext()->EraseSelected (Standard_False);
585 myAISContext()->ClearSelected (Standard_True);
591 float GetOCCVersion()
593 return (float )OCC_VERSION;
599 void SetMaterial (int theMaterial)
601 if (myAISContext().IsNull())
605 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
607 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial )theMaterial, Standard_False);
609 myAISContext()->UpdateCurrentViewer();
615 void SetTransparency (int theTrans)
617 if (myAISContext().IsNull())
621 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
623 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real )theTrans) / 10.0, Standard_False);
625 myAISContext()->UpdateCurrentViewer();
629 ///Return true if object is selected
631 bool IsObjectSelected()
633 if (myAISContext().IsNull())
637 myAISContext()->InitSelected();
638 return myAISContext()->MoreSelected() != Standard_False;
642 ///Return display mode
646 if (myAISContext().IsNull())
651 bool isOneOrMoreInShading = false;
652 bool isOneOrMoreInWireframe = false;
653 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
655 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_Shaded))
657 isOneOrMoreInShading = true;
659 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_WireFrame))
661 isOneOrMoreInWireframe = true;
664 if (isOneOrMoreInShading
665 && isOneOrMoreInWireframe)
669 else if (isOneOrMoreInShading)
673 else if (isOneOrMoreInWireframe)
683 bool SetAISContext (OCCTProxyD3D^ theViewer)
685 this->myAISContext() = theViewer->GetContext();
686 if (myAISContext().IsNull())
696 Handle(AIS_InteractiveContext) GetContext()
698 return myAISContext();
702 // ============================================
703 // Import / export functionality
704 // ============================================
709 /// <param name="theFileName">Name of import file</param>
710 bool ImportBrep (System::String^ theFileName)
712 bool isResult = false;
713 int aLength = theFileName->Length;
714 char* aFilename = new char[aLength + 1];
715 for(int i = 0; i < aLength; ++i)
717 aFilename[i] = (char )theFileName->ToCharArray()[i];
719 aFilename[aLength] = '\0';
720 isResult = ImportBrep (aFilename);
728 /// <param name="theFileName">Name of import file</param>
729 bool ImportBrep (char* theFileName)
732 BRep_Builder aBuilder;
733 if (!BRepTools::Read (aShape, theFileName, aBuilder))
738 Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
739 myAISContext()->SetMaterial (aPrs, Graphic3d_NOM_GOLD, Standard_False);
740 myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
741 myAISContext()->Display (aPrs, Standard_True);
748 /// <param name="theFileName">Name of import file</param>
749 bool ImportStep (char* theFileName)
751 STEPControl_Reader aReader;
752 if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
757 bool isFailsonly = false;
758 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
760 int aNbRoot = aReader.NbRootsForTransfer();
761 aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity);
762 for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter)
764 aReader.TransferRoot (aRootIter);
765 int aNbShap = aReader.NbShapes();
768 for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
770 myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_False);
772 myAISContext()->UpdateCurrentViewer();
781 /// <param name="theFileName">Name of import file</param>
782 bool ImportIges (char* theFileName)
784 IGESControl_Reader aReader;
785 if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
790 aReader.TransferRoots();
791 TopoDS_Shape aShape = aReader.OneShape();
792 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
793 myAISContext()->UpdateCurrentViewer();
800 /// <param name="theFileName">Name of export file</param>
801 bool ExportBRep (char* theFileName)
803 myAISContext()->InitSelected();
804 if (!myAISContext()->MoreSelected())
809 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
810 return !anIS.IsNull()
811 && BRepTools::Write (anIS->Shape(), theFileName);
817 /// <param name="theFileName">Name of export file</param>
818 bool ExportStep (char* theFileName)
820 STEPControl_StepModelType aType = STEPControl_AsIs;
821 STEPControl_Writer aWriter;
822 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
824 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
830 TopoDS_Shape aShape = anIS->Shape();
831 if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone)
836 return aWriter.Write (theFileName) == IFSelect_RetDone;
842 /// <param name="theFileName">Name of export file</param>
843 bool ExportIges (char* theFileName)
845 IGESControl_Controller::Init();
846 IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
847 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
848 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
850 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
856 aWriter.AddShape (anIS->Shape());
859 aWriter.ComputeModel();
860 return aWriter.Write (theFileName) != Standard_False;
866 /// <param name="theFileName">Name of export file</param>
867 bool ExportVrml (char* theFileName)
869 TopoDS_Compound aRes;
870 BRep_Builder aBuilder;
871 aBuilder.MakeCompound (aRes);
872 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
874 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
879 aBuilder.Add (aRes, anIS->Shape());
882 VrmlAPI_Writer aWriter;
883 aWriter.Write (aRes, theFileName);
890 /// <param name="theFileName">Name of export file</param>
891 bool ExportStl (char* theFileName)
893 TopoDS_Compound aComp;
894 BRep_Builder aBuilder;
895 aBuilder.MakeCompound (aComp);
896 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
898 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
903 aBuilder.Add (aComp, anIS->Shape());
906 StlAPI_Writer aWriter;
907 aWriter.Write (aComp, theFileName);
912 ///Define which Import/Export function must be called
914 /// <param name="theFileName">Name of Import/Export file</param>
915 /// <param name="theFormat">Determines format of Import/Export file</param>
916 /// <param name="theIsImport">Determines is Import or not</param>
917 bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
919 bool isResult = false;
920 int aLength = theFileName->Length;
921 char* aFilename = new char[aLength + 1];
922 for (int aCharIter = 0; aCharIter < aLength; ++aCharIter)
924 aFilename[aCharIter] = (char)theFileName->ToCharArray()[aCharIter];
926 aFilename[aLength] = '\0';
932 case 0: isResult = ImportBrep (aFilename); break;
933 case 1: isResult = ImportStep (aFilename); break;
934 case 2: isResult = ImportIges (aFilename); break;
941 case 0: isResult = ExportBRep (aFilename); break;
942 case 1: isResult = ExportStep (aFilename); break;
943 case 2: isResult = ExportIges (aFilename); break;
944 case 3: isResult = ExportVrml (aFilename); break;
945 case 4: isResult = ExportStl (aFilename); break;
946 case 5: isResult = Dump (aFilename); break;
954 ///Initialize OCCTProxyD3D
958 myGraphicDriver().Nullify();
959 myViewer().Nullify();
961 myAISContext().Nullify();
966 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
967 NCollection_Haft<Handle(V3d_View)> myView;
968 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
969 NCollection_Haft<Handle(D3DHost_GraphicDriver)> myGraphicDriver;