3 // include required OCCT headers
4 #include <Standard_Version.hxx>
5 #include <Message_ProgressIndicator.hxx>
7 #include <WNT_Window.hxx>
8 #include <WNT_WClass.hxx>
9 #include <Graphic3d_CView.hxx>
10 #include <Graphic3d_Camera.hxx>
11 #include <Graphic3d_TextureParams.hxx>
12 #include <D3DHost_GraphicDriver.hxx>
14 #include <V3d_Viewer.hxx>
15 #include <V3d_View.hxx>
16 #include <Visual3d_View.hxx>
17 #include <AIS_InteractiveContext.hxx>
18 #include <AIS_Shape.hxx>
20 #include <TopoDS_Shape.hxx>
21 #include <TopoDS_Compound.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRepTools.hxx>
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>
32 #include <STEPControl_Reader.hxx>
33 #include <STEPControl_Writer.hxx>
35 #include <StlAPI_Writer.hxx>
37 #include <VrmlAPI_Writer.hxx>
38 //wrapper of pure C++ classes to ref classes
39 #include <NCollection_Haft.h>
41 // list of required OCCT libraries
42 #pragma comment(lib, "TKernel.lib")
43 #pragma comment(lib, "TKMath.lib")
44 #pragma comment(lib, "TKBRep.lib")
45 #pragma comment(lib, "TKXSBase.lib")
46 #pragma comment(lib, "TKService.lib")
47 #pragma comment(lib, "TKV3d.lib")
48 #pragma comment(lib, "TKOpenGl.lib")
49 #pragma comment(lib, "TKD3dHost.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")
55 #pragma comment(lib, "D3D9.lib")
58 /// Proxy class encapsulating calls to OCCT C++ classes within
59 /// C++/CLI class visible from .Net (CSharp)
61 public ref class OCCTProxyD3D
67 // ============================================
68 // Viewer functionality
69 // ============================================
72 ///Initialize a viewer
74 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
77 myGraphicDriver() = new D3DHost_GraphicDriver();
78 myGraphicDriver()->ChangeOptions().buffersNoSwap = true;
79 //myGraphicDriver()->ChangeOptions().contextDebug = true;
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);
87 myViewer()->SetDefaultLights();
88 myViewer()->SetLightOn();
89 myView() = myViewer()->CreateView();
91 static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC);
92 Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64);
93 aWNTWindow->SetVirtual (Standard_True);
94 myView()->SetWindow(aWNTWindow);
95 myAISContext() = new AIS_InteractiveContext (myViewer());
96 myAISContext()->UpdateCurrentViewer();
97 myView()->MustBeResized();
101 /// <summary> Resizes custom FBO for Direct3D output. </summary>
102 System::IntPtr ResizeBridgeFBO (int theWinSizeX,
105 Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window());
106 aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY);
107 myView()->MustBeResized();
108 myView()->Invalidate();
109 return System::IntPtr(myGraphicDriver()->D3dColorSurface (myView()->View()));
113 /// Make dump of current view to file
115 /// <param name="theFileName">Name of dump file</param>
116 bool Dump (const char* theFileName)
118 if (myView().IsNull())
123 return myView()->Dump (theFileName) != Standard_False;
131 if (!myView().IsNull())
140 void UpdateView(void)
142 if (!myView().IsNull())
144 myView()->MustBeResized();
149 ///Set computed mode in false
151 void SetDegenerateModeOn()
153 if (!myView().IsNull())
155 myView()->SetComputedMode (Standard_False);
160 ///Set computed mode in true
162 void SetDegenerateModeOff()
164 if (!myView().IsNull())
166 myView()->SetComputedMode (Standard_True);
173 void WindowFitAll (int theXmin, int theYmin,
174 int theXmax, int theYmax)
176 if (!myView().IsNull())
178 myView()->WindowFitAll (theXmin, theYmin, theXmax, theYmax);
183 ///Current place of window
185 /// <param name="theZoomFactor">Current zoom</param>
186 void Place (int theX, int theY, float theZoomFactor)
188 Quantity_Factor aZoomFactor = theZoomFactor;
189 if (!myView().IsNull())
191 myView()->Place (theX, theY, aZoomFactor);
198 void Zoom (int theX1, int theY1, int theX2, int theY2)
200 if (!myView().IsNull())
202 myView()->Zoom (theX1, theY1, theX2, theY2);
209 void Pan (int theX, int theY)
211 if (!myView().IsNull())
213 myView()->Pan (theX, theY);
220 void Rotation (int theX, int theY)
222 if (!myView().IsNull())
224 myView()->Rotation (theX, theY);
231 void StartRotation (int theX, int theY)
233 if (!myView().IsNull())
235 myView()->StartRotation (theX, theY);
240 ///Select by rectangle
242 void Select (int theX1, int theY1, int theX2, int theY2)
244 if (!myAISContext().IsNull())
246 myAISContext()->Select (theX1, theY1, theX2, theY2, myView());
255 if (!myAISContext().IsNull())
257 myAISContext()->Select();
264 void MoveTo (int theX, int theY)
266 if (!myAISContext().IsNull() && !myView().IsNull())
268 myAISContext()->MoveTo (theX, theY, myView());
273 ///Select by rectangle with pressed "Shift" key
275 void ShiftSelect (int theX1, int theY1, int theX2, int theY2)
277 if (!myAISContext().IsNull() && !myView().IsNull())
279 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView());
284 ///Select by "Shift" key
288 if (!myAISContext().IsNull())
290 myAISContext()->ShiftSelect();
295 ///Set background color
297 void BackgroundColor (int& theRed, int& theGreen, int& theBlue)
299 if (!myView().IsNull())
301 Quantity_Color aColor = myView()->BackgroundColor();
302 theRed = (int )aColor.Red() * 255;
303 theGreen = (int )aColor.Green() * 255;
304 theBlue = (int )aColor.Blue() * 255;
309 ///Get background color Red
314 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
319 ///Get background color Green
324 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
329 ///Get background color Blue
334 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
339 ///Update current viewer
341 void UpdateCurrentViewer()
343 if (!myAISContext().IsNull())
345 myAISContext()->UpdateCurrentViewer();
354 if (!myView().IsNull())
356 myView()->SetProj (V3d_Xpos);
365 if (!myView().IsNull())
367 myView()->SetProj (V3d_Zpos);
376 if (!myView().IsNull())
378 myView()->SetProj (V3d_Ypos);
387 if (!myView().IsNull())
389 myView()->SetProj (V3d_Xneg);
398 if (!myView().IsNull())
400 myView()->SetProj (V3d_Yneg);
409 if (!myView().IsNull())
411 myView()->SetProj (V3d_Zneg);
420 if (!myView().IsNull())
422 myView()->SetProj (V3d_XposYnegZpos);
431 return myView().IsNull()
433 : float(myView()->Scale());
441 if (!myView().IsNull())
453 if (!myView().IsNull())
460 ///Set display mode of objects
462 /// <param name="theMode">Set current mode</param>
463 void SetDisplayMode (int theMode)
465 if (myAISContext().IsNull())
470 AIS_DisplayMode aCurrentMode = theMode == 0
473 if (myAISContext()->NbCurrents() == 0
474 || myAISContext()->NbSelected() == 0)
476 myAISContext()->SetDisplayMode (aCurrentMode);
480 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
482 myAISContext()->SetDisplayMode (myAISContext()->Current(), theMode, Standard_False);
485 myAISContext()->UpdateCurrentViewer();
491 void SetColor (int theR, int theG, int theB)
493 if (myAISContext().IsNull())
498 Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB);
499 for (; myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
501 myAISContext()->SetColor (myAISContext()->Current(), aCol.Name());
506 ///Get object color red
511 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
516 ///Get object color green
521 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
526 ///Get object color blue
531 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
536 ///Get object color R/G/B
538 void ObjectColor (int& theRed, int& theGreen, int& theBlue)
540 if (myAISContext().IsNull())
548 myAISContext()->InitCurrent();
549 if (!myAISContext()->MoreCurrent())
554 Handle(AIS_InteractiveObject) aCurrent = myAISContext()->Current();
555 if (aCurrent->HasColor())
557 Quantity_Color anObjCol = myAISContext()->Color (myAISContext()->Current());
558 theRed = int(anObjCol.Red() * 255.0);
559 theGreen = int(anObjCol.Green() * 255.0);
560 theBlue = int(anObjCol.Blue() * 255.0);
565 ///Set background color R/G/B
567 void SetBackgroundColor (int theRed, int theGreen, int theBlue)
569 if (!myView().IsNull())
571 myView()->SetBackgroundColor (Quantity_TOC_RGB, theRed / 255.0, theGreen / 255.0, theBlue / 255.0);
580 if (myAISContext().IsNull())
584 for(myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
586 myAISContext()->Erase (myAISContext()->Current(), Standard_True);
588 myAISContext()->ClearCurrents();
594 float GetOCCVersion()
596 return (float )OCC_VERSION;
602 void SetMaterial (int theMaterial)
604 if (myAISContext().IsNull())
608 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
610 myAISContext()->SetMaterial (myAISContext()->Current(), (Graphic3d_NameOfMaterial )theMaterial);
612 myAISContext()->UpdateCurrentViewer();
618 void SetTransparency (int theTrans)
620 if (myAISContext().IsNull())
624 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextSelected())
626 myAISContext()->SetTransparency (myAISContext()->Current(), ((Standard_Real )theTrans) / 10.0);
631 ///Return true if object is selected
633 bool IsObjectSelected()
635 if (myAISContext().IsNull())
639 myAISContext()->InitCurrent();
640 return myAISContext()->MoreCurrent() != Standard_False;
644 ///Return display mode
648 if (myAISContext().IsNull())
653 bool isOneOrMoreInShading = false;
654 bool isOneOrMoreInWireframe = false;
655 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
657 if (myAISContext()->IsDisplayed (myAISContext()->Current(), AIS_Shaded))
659 isOneOrMoreInShading = true;
661 if (myAISContext()->IsDisplayed (myAISContext()->Current(), AIS_WireFrame))
663 isOneOrMoreInWireframe = true;
666 if (isOneOrMoreInShading
667 && isOneOrMoreInWireframe)
671 else if (isOneOrMoreInShading)
675 else if (isOneOrMoreInWireframe)
685 bool SetAISContext (OCCTProxyD3D^ theViewer)
687 this->myAISContext() = theViewer->GetContext();
688 if (myAISContext().IsNull())
698 Handle_AIS_InteractiveContext GetContext()
700 return myAISContext();
704 // ============================================
705 // Import / export functionality
706 // ============================================
711 /// <param name="theFileName">Name of import file</param>
712 bool ImportBrep (System::String^ theFileName)
714 bool isResult = false;
715 int aLength = theFileName->Length;
716 char* aFilename = new char[aLength + 1];
717 for(int i = 0; i < aLength; ++i)
719 aFilename[i] = (char )theFileName->ToCharArray()[i];
721 aFilename[aLength] = '\0';
722 isResult = ImportBrep (aFilename);
730 /// <param name="theFileName">Name of import file</param>
731 bool ImportBrep (char* theFileName)
734 BRep_Builder aBuilder;
735 if (!BRepTools::Read (aShape, theFileName, aBuilder))
739 if (myAISContext()->HasOpenedContext())
741 myAISContext()->CloseLocalContext();
743 Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
744 myAISContext()->SetMaterial (aPrs, Graphic3d_NOM_GOLD);
745 myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
746 myAISContext()->Display (aPrs);
753 /// <param name="theFileName">Name of import file</param>
754 bool ImportStep (char* theFileName)
756 STEPControl_Reader aReader;
757 if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
762 bool isFailsonly = false;
763 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
765 int aNbRoot = aReader.NbRootsForTransfer();
766 aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity);
767 for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter)
769 aReader.TransferRoot (aRootIter);
770 int aNbShap = aReader.NbShapes();
773 for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
775 myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_True);
785 /// <param name="theFileName">Name of import file</param>
786 bool ImportIges (char* theFileName)
788 IGESControl_Reader aReader;
789 if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
794 aReader.TransferRoots();
795 TopoDS_Shape aShape = aReader.OneShape();
796 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
797 myAISContext()->UpdateCurrentViewer();
804 /// <param name="theFileName">Name of export file</param>
805 bool ExportBRep (char* theFileName)
807 myAISContext()->InitCurrent();
808 if (!myAISContext()->MoreCurrent())
813 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
814 return !anIS.IsNull()
815 && BRepTools::Write (anIS->Shape(), theFileName);
821 /// <param name="theFileName">Name of export file</param>
822 bool ExportStep (char* theFileName)
824 STEPControl_StepModelType aType = STEPControl_AsIs;
825 STEPControl_Writer aWriter;
826 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
828 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
834 TopoDS_Shape aShape = anIS->Shape();
835 if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone)
840 return aWriter.Write (theFileName) == IFSelect_RetDone;
846 /// <param name="theFileName">Name of export file</param>
847 bool ExportIges (char* theFileName)
849 IGESControl_Controller::Init();
850 IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
851 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
852 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
854 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
860 aWriter.AddShape (anIS->Shape());
863 aWriter.ComputeModel();
864 return aWriter.Write (theFileName) != Standard_False;
870 /// <param name="theFileName">Name of export file</param>
871 bool ExportVrml (char* theFileName)
873 TopoDS_Compound aRes;
874 BRep_Builder aBuilder;
875 aBuilder.MakeCompound (aRes);
876 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
878 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
883 aBuilder.Add (aRes, anIS->Shape());
886 VrmlAPI_Writer aWriter;
887 aWriter.Write (aRes, theFileName);
894 /// <param name="theFileName">Name of export file</param>
895 bool ExportStl (char* theFileName)
897 TopoDS_Compound aComp;
898 BRep_Builder aBuilder;
899 aBuilder.MakeCompound (aComp);
900 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
902 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
907 aBuilder.Add (aComp, anIS->Shape());
910 StlAPI_Writer aWriter;
911 aWriter.Write (aComp, theFileName);
916 ///Define which Import/Export function must be called
918 /// <param name="theFileName">Name of Import/Export file</param>
919 /// <param name="theFormat">Determines format of Import/Export file</param>
920 /// <param name="theIsImport">Determines is Import or not</param>
921 bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
923 bool isResult = false;
924 int aLength = theFileName->Length;
925 char* aFilename = new char[aLength + 1];
926 for (int aCharIter = 0; aCharIter < aLength; ++aCharIter)
928 aFilename[aCharIter] = (char)theFileName->ToCharArray()[aCharIter];
930 aFilename[aLength] = '\0';
936 case 0: isResult = ImportBrep (aFilename); break;
937 case 1: isResult = ImportStep (aFilename); break;
938 case 2: isResult = ImportIges (aFilename); break;
945 case 0: isResult = ExportBRep (aFilename); break;
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;
958 ///Initialize OCCTProxyD3D
962 myGraphicDriver().Nullify();
963 myViewer().Nullify();
965 myAISContext().Nullify();
970 NCollection_Haft<Handle_V3d_Viewer> myViewer;
971 NCollection_Haft<Handle_V3d_View> myView;
972 NCollection_Haft<Handle_AIS_InteractiveContext> myAISContext;
973 NCollection_Haft<Handle_D3DHost_GraphicDriver> myGraphicDriver;