4 // include required OCCT headers
5 #include <Standard_Version.hxx>
6 #include <Message_ProgressIndicator.hxx>
7 #include <Message_ProgressScope.hxx>
9 #include <WNT_Window.hxx>
10 #include <WNT_WClass.hxx>
11 #include <Graphic3d_CView.hxx>
12 #include <Graphic3d_Camera.hxx>
13 #include <Graphic3d_TextureParams.hxx>
14 #include <D3DHost_GraphicDriver.hxx>
15 #include <D3DHost_View.hxx>
17 #include <V3d_Viewer.hxx>
18 #include <V3d_View.hxx>
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_Shape.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <TopoDS_Compound.hxx>
25 #include <BRep_Builder.hxx>
26 #include <BRepTools.hxx>
28 #include <IGESControl_Reader.hxx>
29 #include <IGESControl_Controller.hxx>
30 #include <IGESControl_Writer.hxx>
31 #include <IFSelect_ReturnStatus.hxx>
32 #include <Interface_Static.hxx>
34 #include <STEPControl_Reader.hxx>
35 #include <STEPControl_Writer.hxx>
37 #include <StlAPI_Writer.hxx>
39 #include <VrmlAPI_Writer.hxx>
40 //wrapper of pure C++ classes to ref classes
41 #include <NCollection_Haft.h>
45 // list of required OCCT libraries
46 #pragma comment(lib, "TKernel.lib")
47 #pragma comment(lib, "TKMath.lib")
48 #pragma comment(lib, "TKBRep.lib")
49 #pragma comment(lib, "TKXSBase.lib")
50 #pragma comment(lib, "TKService.lib")
51 #pragma comment(lib, "TKV3d.lib")
52 #pragma comment(lib, "TKOpenGl.lib")
53 #pragma comment(lib, "TKD3dHost.lib")
54 #pragma comment(lib, "TKIGES.lib")
55 #pragma comment(lib, "TKSTEP.lib")
56 #pragma comment(lib, "TKStl.lib")
57 #pragma comment(lib, "TKVrml.lib")
58 #pragma comment(lib, "TKLCAF.lib")
60 #pragma comment(lib, "D3D9.lib")
62 //! Auxiliary tool for converting C# string into UTF-8 string.
63 static TCollection_AsciiString toAsciiString (String^ theString)
65 if (theString == nullptr)
67 return TCollection_AsciiString();
70 pin_ptr<const wchar_t> aPinChars = PtrToStringChars (theString);
71 const wchar_t* aWCharPtr = aPinChars;
73 || *aWCharPtr == L'\0')
75 return TCollection_AsciiString();
77 return TCollection_AsciiString (aWCharPtr);
81 /// Proxy class encapsulating calls to OCCT C++ classes within
82 /// C++/CLI class visible from .Net (CSharp)
84 public ref class OCCTProxyD3D
90 // ============================================
91 // Viewer functionality
92 // ============================================
95 ///Initialize a viewer
97 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
100 myGraphicDriver() = new D3DHost_GraphicDriver();
101 myGraphicDriver()->ChangeOptions().buffersNoSwap = true;
102 //myGraphicDriver()->ChangeOptions().contextDebug = true;
104 myViewer() = new V3d_Viewer (myGraphicDriver());
105 myViewer()->SetDefaultLights();
106 myViewer()->SetLightOn();
107 myView() = myViewer()->CreateView();
109 static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC);
110 Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64);
111 aWNTWindow->SetVirtual (Standard_True);
112 myView()->SetWindow(aWNTWindow);
113 myAISContext() = new AIS_InteractiveContext (myViewer());
114 myAISContext()->UpdateCurrentViewer();
115 myView()->MustBeResized();
119 /// <summary> Resizes custom FBO for Direct3D output. </summary>
120 System::IntPtr ResizeBridgeFBO (int theWinSizeX,
123 Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window());
124 aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY);
125 myView()->MustBeResized();
126 myView()->Invalidate();
127 return System::IntPtr(Handle(D3DHost_View)::DownCast (myView()->View())->D3dColorSurface());
131 /// Make dump of current view to file
133 /// <param name="theFileName">Name of dump file</param>
134 bool Dump (const TCollection_AsciiString& theFileName)
136 if (myView().IsNull())
141 return myView()->Dump (theFileName.ToCString()) != Standard_False;
149 if (!myView().IsNull())
158 void UpdateView(void)
160 if (!myView().IsNull())
162 myView()->MustBeResized();
167 ///Set computed mode in false
169 void SetDegenerateModeOn()
171 if (!myView().IsNull())
173 myView()->SetComputedMode (Standard_False);
179 ///Set computed mode in true
181 void SetDegenerateModeOff()
183 if (!myView().IsNull())
185 myView()->SetComputedMode (Standard_True);
193 void WindowFitAll (int theXmin, int theYmin,
194 int theXmax, int theYmax)
196 if (!myView().IsNull())
198 myView()->WindowFitAll (theXmin, theYmin, theXmax, theYmax);
203 ///Current place of window
205 /// <param name="theZoomFactor">Current zoom</param>
206 void Place (int theX, int theY, float theZoomFactor)
208 Standard_Real aZoomFactor = theZoomFactor;
209 if (!myView().IsNull())
211 myView()->Place (theX, theY, aZoomFactor);
218 void Zoom (int theX1, int theY1, int theX2, int theY2)
220 if (!myView().IsNull())
222 myView()->Zoom (theX1, theY1, theX2, theY2);
229 void Pan (int theX, int theY)
231 if (!myView().IsNull())
233 myView()->Pan (theX, theY);
240 void Rotation (int theX, int theY)
242 if (!myView().IsNull())
244 myView()->Rotation (theX, theY);
251 void StartRotation (int theX, int theY)
253 if (!myView().IsNull())
255 myView()->StartRotation (theX, theY);
260 ///Select by rectangle
262 void Select (int theX1, int theY1, int theX2, int theY2)
264 if (!myAISContext().IsNull())
266 myAISContext()->SelectRectangle (Graphic3d_Vec2i (theX1, theY1),
267 Graphic3d_Vec2i (theX2, theY2),
269 myAISContext()->UpdateCurrentViewer();
278 if (!myAISContext().IsNull())
280 myAISContext()->SelectDetected();
281 myAISContext()->UpdateCurrentViewer();
288 void MoveTo (int theX, int theY)
290 if (!myAISContext().IsNull() && !myView().IsNull())
292 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
297 ///Select by rectangle with pressed "Shift" key
299 void ShiftSelect (int theX1, int theY1, int theX2, int theY2)
301 if (!myAISContext().IsNull() && !myView().IsNull())
303 myAISContext()->SelectRectangle (Graphic3d_Vec2i (theX1, theY1),
304 Graphic3d_Vec2i (theX2, theY2),
306 AIS_SelectionScheme_XOR);
307 myAISContext()->UpdateCurrentViewer();
312 ///Select by "Shift" key
316 if (!myAISContext().IsNull())
318 myAISContext()->SelectDetected (AIS_SelectionScheme_XOR);
319 myAISContext()->UpdateCurrentViewer();
324 ///Set background color
326 void BackgroundColor (int& theRed, int& theGreen, int& theBlue)
328 if (!myView().IsNull())
330 Quantity_Color aColor = myView()->BackgroundColor();
331 theRed = (int )aColor.Red() * 255;
332 theGreen = (int )aColor.Green() * 255;
333 theBlue = (int )aColor.Blue() * 255;
338 ///Get background color Red
343 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
348 ///Get background color Green
353 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
358 ///Get background color Blue
363 BackgroundColor (anRgb[0], anRgb[1], anRgb[2]);
368 ///Update current viewer
370 void UpdateCurrentViewer()
372 if (!myAISContext().IsNull())
374 myAISContext()->UpdateCurrentViewer();
383 if (!myView().IsNull())
385 myView()->SetProj (V3d_Yneg);
394 if (!myView().IsNull())
396 myView()->SetProj (V3d_Zpos);
405 if (!myView().IsNull())
407 myView()->SetProj (V3d_Xneg);
416 if (!myView().IsNull())
418 myView()->SetProj (V3d_Ypos);
427 if (!myView().IsNull())
429 myView()->SetProj (V3d_Xpos);
438 if (!myView().IsNull())
440 myView()->SetProj (V3d_Zneg);
449 if (!myView().IsNull())
451 myView()->SetProj (V3d_XposYnegZpos);
460 return myView().IsNull()
462 : float(myView()->Scale());
470 if (!myView().IsNull())
482 if (!myView().IsNull())
489 ///Set display mode of objects
491 /// <param name="theMode">Set current mode</param>
492 void SetDisplayMode (int theMode)
494 if (myAISContext().IsNull())
499 AIS_DisplayMode aCurrentMode = theMode == 0
502 if (myAISContext()->NbSelected() == 0)
504 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
508 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
510 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
513 myAISContext()->UpdateCurrentViewer();
519 void SetColor (int theR, int theG, int theB)
521 if (myAISContext().IsNull())
526 Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB);
527 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
529 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, false);
531 myAISContext()->UpdateCurrentViewer();
535 ///Get object color red
540 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
545 ///Get object color green
550 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
555 ///Get object color blue
560 ObjectColor (anRgb[0], anRgb[1], anRgb[2]);
565 ///Get object color R/G/B
567 void ObjectColor (int& theRed, int& theGreen, int& theBlue)
569 if (myAISContext().IsNull())
577 myAISContext()->InitSelected();
578 if (!myAISContext()->MoreSelected())
583 Handle(AIS_InteractiveObject) aCurrent = myAISContext()->SelectedInteractive();
584 if (aCurrent->HasColor())
586 Quantity_Color anObjCol;
587 myAISContext()->Color (aCurrent, anObjCol);
588 theRed = int(anObjCol.Red() * 255.0);
589 theGreen = int(anObjCol.Green() * 255.0);
590 theBlue = int(anObjCol.Blue() * 255.0);
595 ///Set background color R/G/B
597 void SetBackgroundColor (int theRed, int theGreen, int theBlue)
599 if (!myView().IsNull())
601 myView()->SetBackgroundColor (Quantity_TOC_RGB, theRed / 255.0, theGreen / 255.0, theBlue / 255.0);
610 if (myAISContext().IsNull())
615 myAISContext()->EraseSelected (Standard_False);
616 myAISContext()->ClearSelected (Standard_True);
622 float GetOCCVersion()
624 return (float )OCC_VERSION;
630 void SetMaterial (int theMaterial)
632 if (myAISContext().IsNull())
636 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
638 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial )theMaterial, Standard_False);
640 myAISContext()->UpdateCurrentViewer();
646 void SetTransparency (int theTrans)
648 if (myAISContext().IsNull())
652 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
654 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real )theTrans) / 10.0, Standard_False);
656 myAISContext()->UpdateCurrentViewer();
660 ///Return true if object is selected
662 bool IsObjectSelected()
664 if (myAISContext().IsNull())
668 myAISContext()->InitSelected();
669 return myAISContext()->MoreSelected() != Standard_False;
673 ///Return display mode
677 if (myAISContext().IsNull())
682 bool isOneOrMoreInShading = false;
683 bool isOneOrMoreInWireframe = false;
684 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
686 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_Shaded))
688 isOneOrMoreInShading = true;
690 if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_WireFrame))
692 isOneOrMoreInWireframe = true;
695 if (isOneOrMoreInShading
696 && isOneOrMoreInWireframe)
700 else if (isOneOrMoreInShading)
704 else if (isOneOrMoreInWireframe)
714 bool SetAISContext (OCCTProxyD3D^ theViewer)
716 this->myAISContext() = theViewer->GetContext();
717 if (myAISContext().IsNull())
727 Handle(AIS_InteractiveContext) GetContext()
729 return myAISContext();
733 // ============================================
734 // Import / export functionality
735 // ============================================
740 /// <param name="theFileName">Name of import file</param>
741 bool ImportBrep (System::String^ theFileName)
743 return ImportBrep (toAsciiString (theFileName));
749 /// <param name="theFileName">Name of import file</param>
750 bool ImportBrep (const TCollection_AsciiString& theFileName)
753 BRep_Builder aBuilder;
754 if (!BRepTools::Read (aShape, theFileName.ToCString(), aBuilder))
759 Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
760 myAISContext()->SetMaterial (aPrs, Graphic3d_NameOfMaterial_Gold, Standard_False);
761 myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
762 myAISContext()->Display (aPrs, Standard_True);
769 /// <param name="theFileName">Name of import file</param>
770 bool ImportStep (const TCollection_AsciiString& theFileName)
772 STEPControl_Reader aReader;
773 if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
778 bool isFailsonly = false;
779 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
781 int aNbRoot = aReader.NbRootsForTransfer();
782 aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity);
783 for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter)
785 aReader.TransferRoot (aRootIter);
786 int aNbShap = aReader.NbShapes();
789 for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
791 myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_False);
793 myAISContext()->UpdateCurrentViewer();
802 /// <param name="theFileName">Name of import file</param>
803 bool ImportIges (const TCollection_AsciiString& theFileName)
805 IGESControl_Reader aReader;
806 if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
811 aReader.TransferRoots();
812 TopoDS_Shape aShape = aReader.OneShape();
813 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
814 myAISContext()->UpdateCurrentViewer();
821 /// <param name="theFileName">Name of export file</param>
822 bool ExportBRep (const TCollection_AsciiString& theFileName)
824 myAISContext()->InitSelected();
825 if (!myAISContext()->MoreSelected())
830 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
831 return !anIS.IsNull()
832 && BRepTools::Write (anIS->Shape(), theFileName.ToCString());
838 /// <param name="theFileName">Name of export file</param>
839 bool ExportStep (const TCollection_AsciiString& theFileName)
841 STEPControl_StepModelType aType = STEPControl_AsIs;
842 STEPControl_Writer aWriter;
843 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
845 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
851 TopoDS_Shape aShape = anIS->Shape();
852 if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone)
857 return aWriter.Write (theFileName.ToCString()) == IFSelect_RetDone;
863 /// <param name="theFileName">Name of export file</param>
864 bool ExportIges (const TCollection_AsciiString& theFileName)
866 IGESControl_Controller::Init();
867 IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
868 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
869 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
871 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
877 aWriter.AddShape (anIS->Shape());
880 aWriter.ComputeModel();
881 return aWriter.Write (theFileName.ToCString()) != Standard_False;
887 /// <param name="theFileName">Name of export file</param>
888 bool ExportVrml (const TCollection_AsciiString& theFileName)
890 TopoDS_Compound aRes;
891 BRep_Builder aBuilder;
892 aBuilder.MakeCompound (aRes);
893 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
895 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
900 aBuilder.Add (aRes, anIS->Shape());
903 VrmlAPI_Writer aWriter;
904 aWriter.Write (aRes, theFileName.ToCString());
911 /// <param name="theFileName">Name of export file</param>
912 bool ExportStl (const TCollection_AsciiString& theFileName)
914 TopoDS_Compound aComp;
915 BRep_Builder aBuilder;
916 aBuilder.MakeCompound (aComp);
917 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
919 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
924 aBuilder.Add (aComp, anIS->Shape());
927 StlAPI_Writer aWriter;
928 aWriter.Write (aComp, theFileName.ToCString());
933 ///Define which Import/Export function must be called
935 /// <param name="theFileName">Name of Import/Export file</param>
936 /// <param name="theFormat">Determines format of Import/Export file</param>
937 /// <param name="theIsImport">Determines is Import or not</param>
938 bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
940 bool isResult = false;
941 const TCollection_AsciiString aFilename = toAsciiString (theFileName);
946 case 0: isResult = ImportBrep (aFilename); break;
947 case 1: isResult = ImportStep (aFilename); break;
948 case 2: isResult = ImportIges (aFilename); break;
955 case 0: isResult = ExportBRep (aFilename); break;
956 case 1: isResult = ExportStep (aFilename); break;
957 case 2: isResult = ExportIges (aFilename); break;
958 case 3: isResult = ExportVrml (aFilename); break;
959 case 4: isResult = ExportStl (aFilename); break;
960 case 5: isResult = Dump (aFilename); break;
967 ///Initialize OCCTProxyD3D
971 myGraphicDriver().Nullify();
972 myViewer().Nullify();
974 myAISContext().Nullify();
979 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
980 NCollection_Haft<Handle(V3d_View)> myView;
981 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
982 NCollection_Haft<Handle(D3DHost_GraphicDriver)> myGraphicDriver;