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>
44 // list of required OCCT libraries
45 #pragma comment(lib, "TKernel.lib")
46 #pragma comment(lib, "TKMath.lib")
47 #pragma comment(lib, "TKBRep.lib")
48 #pragma comment(lib, "TKXSBase.lib")
49 #pragma comment(lib, "TKService.lib")
50 #pragma comment(lib, "TKV3d.lib")
51 #pragma comment(lib, "TKOpenGl.lib")
52 #pragma comment(lib, "TKD3dHost.lib")
53 #pragma comment(lib, "TKIGES.lib")
54 #pragma comment(lib, "TKSTEP.lib")
55 #pragma comment(lib, "TKStl.lib")
56 #pragma comment(lib, "TKVrml.lib")
58 #pragma comment(lib, "D3D9.lib")
60 //! Auxiliary tool for converting C# string into UTF-8 string.
61 static TCollection_AsciiString toAsciiString (String^ theString)
63 if (theString == nullptr)
65 return TCollection_AsciiString();
68 pin_ptr<const wchar_t> aPinChars = PtrToStringChars (theString);
69 const wchar_t* aWCharPtr = aPinChars;
71 || *aWCharPtr == L'\0')
73 return TCollection_AsciiString();
75 return TCollection_AsciiString (aWCharPtr);
79 /// Proxy class encapsulating calls to OCCT C++ classes within
80 /// C++/CLI class visible from .Net (CSharp)
82 public ref class OCCTProxyD3D
88 // ============================================
89 // Viewer functionality
90 // ============================================
93 ///Initialize a viewer
95 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
98 myGraphicDriver() = new D3DHost_GraphicDriver();
99 myGraphicDriver()->ChangeOptions().buffersNoSwap = true;
100 //myGraphicDriver()->ChangeOptions().contextDebug = true;
102 myViewer() = new V3d_Viewer (myGraphicDriver());
103 myViewer()->SetDefaultLights();
104 myViewer()->SetLightOn();
105 myView() = myViewer()->CreateView();
107 static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC);
108 Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64);
109 aWNTWindow->SetVirtual (Standard_True);
110 myView()->SetWindow(aWNTWindow);
111 myAISContext() = new AIS_InteractiveContext (myViewer());
112 myAISContext()->UpdateCurrentViewer();
113 myView()->MustBeResized();
117 /// <summary> Resizes custom FBO for Direct3D output. </summary>
118 System::IntPtr ResizeBridgeFBO (int theWinSizeX,
121 Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window());
122 aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY);
123 myView()->MustBeResized();
124 myView()->Invalidate();
125 return System::IntPtr(Handle(D3DHost_View)::DownCast (myView()->View())->D3dColorSurface());
129 /// Make dump of current view to file
131 /// <param name="theFileName">Name of dump file</param>
132 bool Dump (const TCollection_AsciiString& theFileName)
134 if (myView().IsNull())
139 return myView()->Dump (theFileName.ToCString()) != Standard_False;
147 if (!myView().IsNull())
156 void UpdateView(void)
158 if (!myView().IsNull())
160 myView()->MustBeResized();
165 ///Set computed mode in false
167 void SetDegenerateModeOn()
169 if (!myView().IsNull())
171 myView()->SetComputedMode (Standard_False);
177 ///Set computed mode in true
179 void SetDegenerateModeOff()
181 if (!myView().IsNull())
183 myView()->SetComputedMode (Standard_True);
191 void WindowFitAll (int theXmin, int theYmin,
192 int theXmax, int theYmax)
194 if (!myView().IsNull())
196 myView()->WindowFitAll (theXmin, theYmin, theXmax, theYmax);
201 ///Current place of window
203 /// <param name="theZoomFactor">Current zoom</param>
204 void Place (int theX, int theY, float theZoomFactor)
206 Standard_Real aZoomFactor = theZoomFactor;
207 if (!myView().IsNull())
209 myView()->Place (theX, theY, aZoomFactor);
216 void Zoom (int theX1, int theY1, int theX2, int theY2)
218 if (!myView().IsNull())
220 myView()->Zoom (theX1, theY1, theX2, theY2);
227 void Pan (int theX, int theY)
229 if (!myView().IsNull())
231 myView()->Pan (theX, theY);
238 void Rotation (int theX, int theY)
240 if (!myView().IsNull())
242 myView()->Rotation (theX, theY);
249 void StartRotation (int theX, int theY)
251 if (!myView().IsNull())
253 myView()->StartRotation (theX, theY);
258 ///Select by rectangle
260 void Select (int theX1, int theY1, int theX2, int theY2)
262 if (!myAISContext().IsNull())
264 myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
273 if (!myAISContext().IsNull())
275 myAISContext()->Select (Standard_True);
282 void MoveTo (int theX, int theY)
284 if (!myAISContext().IsNull() && !myView().IsNull())
286 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
291 ///Select by rectangle with pressed "Shift" key
293 void ShiftSelect (int theX1, int theY1, int theX2, int theY2)
295 if (!myAISContext().IsNull() && !myView().IsNull())
297 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
302 ///Select by "Shift" key
306 if (!myAISContext().IsNull())
308 myAISContext()->ShiftSelect (Standard_True);
313 ///Set background color
315 void BackgroundColor (int& theRed, int& theGreen, int& theBlue)
317 if (!myView().IsNull())
319 Quantity_Color aColor = myView()->BackgroundColor();
320 theRed = (int )aColor.Red() * 255;
321 theGreen = (int )aColor.Green() * 255;
322 theBlue = (int )aColor.Blue() * 255;
327 ///Get background color Red
332 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
337 ///Get background color Green
342 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
347 ///Get background color Blue
352 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
357 ///Update current viewer
359 void UpdateCurrentViewer()
361 if (!myAISContext().IsNull())
363 myAISContext()->UpdateCurrentViewer();
372 if (!myView().IsNull())
374 myView()->SetProj (V3d_Yneg);
383 if (!myView().IsNull())
385 myView()->SetProj (V3d_Zpos);
394 if (!myView().IsNull())
396 myView()->SetProj (V3d_Xneg);
405 if (!myView().IsNull())
407 myView()->SetProj (V3d_Ypos);
416 if (!myView().IsNull())
418 myView()->SetProj (V3d_Xpos);
427 if (!myView().IsNull())
429 myView()->SetProj (V3d_Zneg);
438 if (!myView().IsNull())
440 myView()->SetProj (V3d_XposYnegZpos);
449 return myView().IsNull()
451 : float(myView()->Scale());
459 if (!myView().IsNull())
471 if (!myView().IsNull())
478 ///Set display mode of objects
480 /// <param name="theMode">Set current mode</param>
481 void SetDisplayMode (int theMode)
483 if (myAISContext().IsNull())
488 AIS_DisplayMode aCurrentMode = theMode == 0
491 if (myAISContext()->NbSelected() == 0)
493 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
497 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
499 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
502 myAISContext()->UpdateCurrentViewer();
508 void SetColor (int theR, int theG, int theB)
510 if (myAISContext().IsNull())
515 Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB);
516 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
518 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, false);
520 myAISContext()->UpdateCurrentViewer();
524 ///Get object color red
529 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
534 ///Get object color green
539 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
544 ///Get object color blue
549 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
554 ///Get object color R/G/B
556 void ObjectColor (int& theRed, int& theGreen, int& theBlue)
558 if (myAISContext().IsNull())
566 myAISContext()->InitSelected();
567 if (!myAISContext()->MoreSelected())
572 Handle(AIS_InteractiveObject) aCurrent = myAISContext()->SelectedInteractive();
573 if (aCurrent->HasColor())
575 Quantity_Color anObjCol;
576 myAISContext()->Color (aCurrent, anObjCol);
577 theRed = int(anObjCol.Red() * 255.0);
578 theGreen = int(anObjCol.Green() * 255.0);
579 theBlue = int(anObjCol.Blue() * 255.0);
584 ///Set background color R/G/B
586 void SetBackgroundColor (int theRed, int theGreen, int theBlue)
588 if (!myView().IsNull())
590 myView()->SetBackgroundColor (Quantity_TOC_RGB, theRed / 255.0, theGreen / 255.0, theBlue / 255.0);
599 if (myAISContext().IsNull())
604 myAISContext()->EraseSelected (Standard_False);
605 myAISContext()->ClearSelected (Standard_True);
611 float GetOCCVersion()
613 return (float )OCC_VERSION;
619 void SetMaterial (int theMaterial)
621 if (myAISContext().IsNull())
625 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
627 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial )theMaterial, Standard_False);
629 myAISContext()->UpdateCurrentViewer();
635 void SetTransparency (int theTrans)
637 if (myAISContext().IsNull())
641 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
643 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real )theTrans) / 10.0, Standard_False);
645 myAISContext()->UpdateCurrentViewer();
649 ///Return true if object is selected
651 bool IsObjectSelected()
653 if (myAISContext().IsNull())
657 myAISContext()->InitSelected();
658 return myAISContext()->MoreSelected() != Standard_False;
662 ///Return display mode
666 if (myAISContext().IsNull())
671 bool isOneOrMoreInShading = false;
672 bool isOneOrMoreInWireframe = false;
673 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
675 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_Shaded))
677 isOneOrMoreInShading = true;
679 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_WireFrame))
681 isOneOrMoreInWireframe = true;
684 if (isOneOrMoreInShading
685 && isOneOrMoreInWireframe)
689 else if (isOneOrMoreInShading)
693 else if (isOneOrMoreInWireframe)
703 bool SetAISContext (OCCTProxyD3D^ theViewer)
705 this->myAISContext() = theViewer->GetContext();
706 if (myAISContext().IsNull())
716 Handle(AIS_InteractiveContext) GetContext()
718 return myAISContext();
722 // ============================================
723 // Import / export functionality
724 // ============================================
729 /// <param name="theFileName">Name of import file</param>
730 bool ImportBrep (System::String^ theFileName)
732 return ImportBrep (toAsciiString (theFileName));
738 /// <param name="theFileName">Name of import file</param>
739 bool ImportBrep (const TCollection_AsciiString& theFileName)
742 BRep_Builder aBuilder;
743 if (!BRepTools::Read (aShape, theFileName.ToCString(), aBuilder))
748 Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
749 myAISContext()->SetMaterial (aPrs, Graphic3d_NOM_GOLD, Standard_False);
750 myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
751 myAISContext()->Display (aPrs, Standard_True);
758 /// <param name="theFileName">Name of import file</param>
759 bool ImportStep (const TCollection_AsciiString& theFileName)
761 STEPControl_Reader aReader;
762 if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
767 bool isFailsonly = false;
768 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
770 int aNbRoot = aReader.NbRootsForTransfer();
771 aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity);
772 for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter)
774 aReader.TransferRoot (aRootIter);
775 int aNbShap = aReader.NbShapes();
778 for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
780 myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_False);
782 myAISContext()->UpdateCurrentViewer();
791 /// <param name="theFileName">Name of import file</param>
792 bool ImportIges (const TCollection_AsciiString& theFileName)
794 IGESControl_Reader aReader;
795 if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
800 aReader.TransferRoots();
801 TopoDS_Shape aShape = aReader.OneShape();
802 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
803 myAISContext()->UpdateCurrentViewer();
810 /// <param name="theFileName">Name of export file</param>
811 bool ExportBRep (const TCollection_AsciiString& theFileName)
813 myAISContext()->InitSelected();
814 if (!myAISContext()->MoreSelected())
819 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
820 return !anIS.IsNull()
821 && BRepTools::Write (anIS->Shape(), theFileName.ToCString());
827 /// <param name="theFileName">Name of export file</param>
828 bool ExportStep (const TCollection_AsciiString& theFileName)
830 STEPControl_StepModelType aType = STEPControl_AsIs;
831 STEPControl_Writer aWriter;
832 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
834 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
840 TopoDS_Shape aShape = anIS->Shape();
841 if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone)
846 return aWriter.Write (theFileName.ToCString()) == IFSelect_RetDone;
852 /// <param name="theFileName">Name of export file</param>
853 bool ExportIges (const TCollection_AsciiString& theFileName)
855 IGESControl_Controller::Init();
856 IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
857 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
858 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
860 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
866 aWriter.AddShape (anIS->Shape());
869 aWriter.ComputeModel();
870 return aWriter.Write (theFileName.ToCString()) != Standard_False;
876 /// <param name="theFileName">Name of export file</param>
877 bool ExportVrml (const TCollection_AsciiString& theFileName)
879 TopoDS_Compound aRes;
880 BRep_Builder aBuilder;
881 aBuilder.MakeCompound (aRes);
882 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
884 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
889 aBuilder.Add (aRes, anIS->Shape());
892 VrmlAPI_Writer aWriter;
893 aWriter.Write (aRes, theFileName.ToCString());
900 /// <param name="theFileName">Name of export file</param>
901 bool ExportStl (const TCollection_AsciiString& theFileName)
903 TopoDS_Compound aComp;
904 BRep_Builder aBuilder;
905 aBuilder.MakeCompound (aComp);
906 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
908 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
913 aBuilder.Add (aComp, anIS->Shape());
916 StlAPI_Writer aWriter;
917 aWriter.Write (aComp, theFileName.ToCString());
922 ///Define which Import/Export function must be called
924 /// <param name="theFileName">Name of Import/Export file</param>
925 /// <param name="theFormat">Determines format of Import/Export file</param>
926 /// <param name="theIsImport">Determines is Import or not</param>
927 bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
929 bool isResult = false;
930 const TCollection_AsciiString aFilename = toAsciiString (theFileName);
935 case 0: isResult = ImportBrep (aFilename); break;
936 case 1: isResult = ImportStep (aFilename); break;
937 case 2: isResult = ImportIges (aFilename); break;
944 case 0: isResult = ExportBRep (aFilename); break;
945 case 1: isResult = ExportStep (aFilename); break;
946 case 2: isResult = ExportIges (aFilename); break;
947 case 3: isResult = ExportVrml (aFilename); break;
948 case 4: isResult = ExportStl (aFilename); break;
949 case 5: isResult = Dump (aFilename); break;
956 ///Initialize OCCTProxyD3D
960 myGraphicDriver().Nullify();
961 myViewer().Nullify();
963 myAISContext().Nullify();
968 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
969 NCollection_Haft<Handle(V3d_View)> myView;
970 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
971 NCollection_Haft<Handle(D3DHost_GraphicDriver)> myGraphicDriver;