1 // include required OCCT headers
2 #include <Standard_Version.hxx>
3 #include <Message_ProgressIndicator.hxx>
5 #include <Aspect_DisplayConnection.hxx>
6 #include <WNT_Window.hxx>
7 #include <OpenGl_GraphicDriver.hxx>
9 #include <V3d_Viewer.hxx>
10 #include <V3d_View.hxx>
11 #include <AIS_InteractiveContext.hxx>
12 #include <AIS_Shape.hxx>
14 #include <TopoDS_Shape.hxx>
15 #include <TopoDS_Compound.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRepTools.hxx>
20 #include <IGESControl_Reader.hxx>
21 #include <IGESControl_Controller.hxx>
22 #include <IGESControl_Writer.hxx>
23 #include <IFSelect_ReturnStatus.hxx>
24 #include <Interface_Static.hxx>
26 #include <STEPControl_Reader.hxx>
27 #include <STEPControl_Writer.hxx>
29 #include <StlAPI_Writer.hxx>
31 #include <VrmlAPI_Writer.hxx>
32 //wrapper of pure C++ classes to ref classes
33 #include <NCollection_Haft.h>
37 // list of required OCCT libraries
38 #pragma comment(lib, "TKernel.lib")
39 #pragma comment(lib, "TKMath.lib")
40 #pragma comment(lib, "TKBRep.lib")
41 #pragma comment(lib, "TKXSBase.lib")
42 #pragma comment(lib, "TKService.lib")
43 #pragma comment(lib, "TKV3d.lib")
44 #pragma comment(lib, "TKOpenGl.lib")
45 #pragma comment(lib, "TKIGES.lib")
46 #pragma comment(lib, "TKSTEP.lib")
47 #pragma comment(lib, "TKStl.lib")
48 #pragma comment(lib, "TKVrml.lib")
50 //! Auxiliary tool for converting C# string into UTF-8 string.
51 static TCollection_AsciiString toAsciiString (String^ theString)
53 if (theString == nullptr)
55 return TCollection_AsciiString();
58 pin_ptr<const wchar_t> aPinChars = PtrToStringChars (theString);
59 const wchar_t* aWCharPtr = aPinChars;
61 || *aWCharPtr == L'\0')
63 return TCollection_AsciiString();
65 return TCollection_AsciiString (aWCharPtr);
69 /// Proxy class encapsulating calls to OCCT C++ classes within
70 /// C++/CLI class visible from .Net (CSharp)
72 public ref class OCCTProxy
75 // ============================================
76 // Viewer functionality
77 // ============================================
80 ///Initialize a viewer
82 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
83 bool InitViewer(System::IntPtr theWnd)
87 Handle(Aspect_DisplayConnection) aDisplayConnection;
88 myGraphicDriver() = new OpenGl_GraphicDriver (aDisplayConnection);
90 catch (Standard_Failure)
95 myViewer() = new V3d_Viewer (myGraphicDriver());
96 myViewer()->SetDefaultLights();
97 myViewer()->SetLightOn();
98 myView() = myViewer()->CreateView();
99 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
100 myView()->SetWindow(aWNTWindow);
101 if (!aWNTWindow->IsMapped())
105 myAISContext() = new AIS_InteractiveContext( myViewer() );
106 myAISContext()->UpdateCurrentViewer();
108 myView()->MustBeResized();
113 /// Make dump of current view to file
115 /// <param name="theFileName">Name of dump file</param>
116 bool Dump(const TCollection_AsciiString& theFileName)
118 if (myView().IsNull())
123 return myView()->Dump(theFileName.ToCString()) != Standard_False;
129 void RedrawView(void)
131 if (!myView().IsNull())
140 void UpdateView(void)
142 if (!myView().IsNull())
144 myView()->MustBeResized();
149 ///Set computed mode in false
151 void SetDegenerateModeOn(void)
153 if (!myView().IsNull())
155 myView()->SetComputedMode (Standard_False);
161 ///Set computed mode in true
163 void SetDegenerateModeOff(void)
165 if (!myView().IsNull())
167 myView()->SetComputedMode (Standard_True);
175 void WindowFitAll(int theXmin, int theYmin, int theXmax, int theYmax)
177 if (!myView().IsNull())
179 myView()->WindowFitAll(theXmin, theYmin, theXmax, theYmax);
184 ///Current place of window
186 /// <param name="theZoomFactor">Current zoom</param>
187 void Place(int theX, int theY, float theZoomFactor)
189 Standard_Real aZoomFactor = theZoomFactor;
190 if (!myView().IsNull())
192 myView()->Place(theX, theY, aZoomFactor);
199 void Zoom(int theX1, int theY1, int theX2, int theY2)
201 if (!myView().IsNull())
203 myView()->Zoom(theX1, theY1, theX2, theY2);
210 void Pan(int theX, int theY)
212 if (!myView().IsNull())
214 myView()->Pan(theX, theY);
221 void Rotation(int theX, int theY)
223 if (!myView().IsNull())
225 myView()->Rotation(theX, theY);
232 void StartRotation(int theX, int theY)
234 if (!myView().IsNull())
236 myView()->StartRotation(theX, theY);
241 ///Select by rectangle
243 void Select(int theX1, int theY1, int theX2, int theY2)
245 if (!myAISContext().IsNull())
247 myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
256 if (!myAISContext().IsNull())
258 myAISContext()->Select (Standard_True);
265 void MoveTo(int theX, int theY)
267 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
269 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
274 ///Select by rectangle with pressed "Shift" key
276 void ShiftSelect(int theX1, int theY1, int theX2, int theY2)
278 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
280 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
285 ///Select by "Shift" key
287 void ShiftSelect(void)
289 if (!myAISContext().IsNull())
291 myAISContext()->ShiftSelect (Standard_True);
296 ///Set background color
298 void BackgroundColor(int& theRed, int& theGreen, int& theBlue)
303 if (!myView().IsNull())
305 myView()->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
307 theRed = (int)R1*255;
308 theGreen = (int)G1*255;
309 theBlue = (int)B1*255;
313 ///Get background color Red
317 int aRed, aGreen, aBlue;
318 BackgroundColor(aRed, aGreen, aBlue);
323 ///Get background color Green
327 int aRed, aGreen, aBlue;
328 BackgroundColor(aRed, aGreen, aBlue);
333 ///Get background color Blue
337 int aRed, aGreen, aBlue;
338 BackgroundColor(aRed, aGreen, aBlue);
343 ///Update current viewer
345 void UpdateCurrentViewer(void)
347 if (!myAISContext().IsNull())
349 myAISContext()->UpdateCurrentViewer();
358 if (!myView().IsNull())
360 myView()->SetProj(V3d_Yneg);
369 if (!myView().IsNull())
371 myView()->SetProj(V3d_Zpos);
380 if (!myView().IsNull())
382 myView()->SetProj(V3d_Xneg);
391 if (!myView().IsNull())
393 myView()->SetProj(V3d_Ypos);
402 if (!myView().IsNull())
404 myView()->SetProj(V3d_Xpos);
411 void BottomView(void)
413 if (!myView().IsNull())
415 myView()->SetProj(V3d_Zneg);
424 if (!myView().IsNull())
426 myView()->SetProj(V3d_XposYnegZpos);
435 if (myView().IsNull())
441 return (float)myView()->Scale();
448 void ZoomAllView(void)
450 if (!myView().IsNull())
462 if (!myView().IsNull())
469 ///Set display mode of objects
471 /// <param name="theMode">Set current mode</param>
472 void SetDisplayMode(int theMode)
474 if (myAISContext().IsNull())
478 AIS_DisplayMode aCurrentMode;
481 aCurrentMode=AIS_WireFrame;
485 aCurrentMode=AIS_Shaded;
488 if(myAISContext()->NbSelected()==0)
490 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
494 for(myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
496 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
499 myAISContext()->UpdateCurrentViewer();
505 void SetColor(int theR, int theG, int theB)
507 if (myAISContext().IsNull())
511 Quantity_Color aCol = Quantity_Color(theR/255.,theG/255.,theB/255.,Quantity_TOC_RGB);
512 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
514 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, Standard_False);
516 myAISContext()->UpdateCurrentViewer();
520 ///Get object color red
524 int aRed, aGreen, aBlue;
525 ObjectColor(aRed, aGreen, aBlue);
530 ///Get object color green
534 int aRed, aGreen, aBlue;
535 ObjectColor(aRed, aGreen, aBlue);
540 ///Get object color R/G/B
542 void ObjectColor(int& theRed, int& theGreen, int& theBlue)
544 if (myAISContext().IsNull())
551 Handle(AIS_InteractiveObject) aCurrent ;
552 myAISContext()->InitSelected();
553 if (!myAISContext()->MoreSelected())
557 aCurrent = myAISContext()->SelectedInteractive();
558 if ( aCurrent->HasColor () )
560 Quantity_Color anObjCol;
561 myAISContext()->Color (aCurrent, anObjCol);
562 Standard_Real r1, r2, r3;
563 anObjCol.Values(r1, r2, r3, Quantity_TOC_RGB);
565 theGreen=(int)r2*255;
571 ///Get object color blue
575 int aRed, aGreen, aBlue;
576 ObjectColor(aRed, aGreen, aBlue);
581 ///Set background color R/G/B
583 void SetBackgroundColor(int theRed, int theGreen, int theBlue)
585 if (!myView().IsNull())
587 myView()->SetBackgroundColor(Quantity_TOC_RGB, theRed/255.,theGreen/255.,theBlue/255.);
594 void EraseObjects(void)
596 if (myAISContext().IsNull())
601 myAISContext()->EraseSelected (Standard_False);
602 myAISContext()->ClearSelected (Standard_True);
608 float GetOCCVersion(void)
610 return (float)OCC_VERSION;
616 void SetMaterial(int theMaterial)
618 if (myAISContext().IsNull())
622 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
624 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial)theMaterial, Standard_False);
626 myAISContext()->UpdateCurrentViewer();
632 void SetTransparency(int theTrans)
634 if (myAISContext().IsNull())
638 for( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
640 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real)theTrans) / 10.0, Standard_False);
642 myAISContext()->UpdateCurrentViewer();
646 ///Return true if object is selected
648 bool IsObjectSelected(void)
650 if (myAISContext().IsNull())
654 myAISContext()->InitSelected();
655 return myAISContext()->MoreSelected() != Standard_False;
659 ///Return display mode
661 int DisplayMode(void)
663 if (myAISContext().IsNull())
668 bool OneOrMoreInShading = false;
669 bool OneOrMoreInWireframe = false;
670 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
672 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 1 ) )
674 OneOrMoreInShading = true;
676 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 0 ) )
678 OneOrMoreInWireframe = true;
681 if (OneOrMoreInShading && OneOrMoreInWireframe)
685 else if(OneOrMoreInShading)
689 else if (OneOrMoreInWireframe)
700 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
701 void CreateNewView(System::IntPtr theWnd)
703 if (myAISContext().IsNull())
707 myView() = myAISContext()->CurrentViewer()->CreateView();
708 if (myGraphicDriver().IsNull())
710 myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
712 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
713 myView()->SetWindow(aWNTWindow);
714 Standard_Integer w=100, h=100;
715 aWNTWindow->Size(w,h);
716 if (!aWNTWindow->IsMapped())
725 bool SetAISContext(OCCTProxy^ theViewer)
727 this->myAISContext() = theViewer->GetContext();
728 if (myAISContext().IsNull())
738 Handle(AIS_InteractiveContext) GetContext(void)
740 return myAISContext();
744 // ============================================
745 // Import / export functionality
746 // ============================================
751 /// <param name="theFileName">Name of import file</param>
752 bool ImportBrep(System::String^ theFileName)
754 return ImportBrep (toAsciiString (theFileName));
760 /// <param name="theFileName">Name of import file</param>
761 bool ImportBrep (const TCollection_AsciiString& theFileName)
764 BRep_Builder aBuilder;
765 Standard_Boolean isResult = BRepTools::Read(aShape,theFileName.ToCString(),aBuilder);
771 myAISContext()->Display (new AIS_Shape (aShape), Standard_True);
778 /// <param name="theFileName">Name of import file</param>
779 bool ImportStep(const TCollection_AsciiString& theFileName)
781 STEPControl_Reader aReader;
782 IFSelect_ReturnStatus aStatus = aReader.ReadFile(theFileName.ToCString());
783 if ( aStatus == IFSelect_RetDone )
785 bool isFailsonly = false;
786 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
788 int aNbRoot = aReader.NbRootsForTransfer();
789 aReader.PrintCheckTransfer( isFailsonly, IFSelect_ItemsByEntity );
790 for ( Standard_Integer n = 1; n <= aNbRoot; n++ )
792 Standard_Boolean ok = aReader.TransferRoot( n );
793 int aNbShap = aReader.NbShapes();
796 for ( int i = 1; i <= aNbShap; i++ )
798 TopoDS_Shape aShape = aReader.Shape( i );
799 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
801 myAISContext()->UpdateCurrentViewer();
816 /// <param name="theFileName">Name of import file</param>
817 bool ImportIges(const TCollection_AsciiString& theFileName)
819 IGESControl_Reader aReader;
820 int aStatus = aReader.ReadFile( theFileName.ToCString() );
822 if ( aStatus == IFSelect_RetDone )
824 aReader.TransferRoots();
825 TopoDS_Shape aShape = aReader.OneShape();
826 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
833 myAISContext()->UpdateCurrentViewer();
840 /// <param name="theFileName">Name of export file</param>
841 bool ExportBRep(const TCollection_AsciiString& theFileName)
843 myAISContext()->InitSelected();
844 if (!myAISContext()->MoreSelected())
849 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
850 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast(anIO);
851 return BRepTools::Write (anIS->Shape(), theFileName.ToCString()) != Standard_False;
857 /// <param name="theFileName">Name of export file</param>
858 bool ExportStep(const TCollection_AsciiString& theFileName)
860 STEPControl_StepModelType aType = STEPControl_AsIs;
861 IFSelect_ReturnStatus aStatus;
862 STEPControl_Writer aWriter;
863 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
865 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
866 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
867 TopoDS_Shape aShape = anIS->Shape();
868 aStatus = aWriter.Transfer( aShape , aType );
869 if ( aStatus != IFSelect_RetDone )
875 aStatus = aWriter.Write(theFileName.ToCString());
876 if ( aStatus != IFSelect_RetDone )
887 /// <param name="theFileName">Name of export file</param>
888 bool ExportIges(const TCollection_AsciiString& theFileName)
890 IGESControl_Controller::Init();
891 IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
892 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
894 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
896 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
897 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
898 TopoDS_Shape aShape = anIS->Shape();
899 aWriter.AddShape ( aShape );
902 aWriter.ComputeModel();
903 return aWriter.Write(theFileName.ToCString()) != Standard_False;
909 /// <param name="theFileName">Name of export file</param>
910 bool ExportVrml(const TCollection_AsciiString& theFileName)
912 TopoDS_Compound aRes;
913 BRep_Builder aBuilder;
914 aBuilder.MakeCompound( aRes );
916 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
918 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
919 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
920 TopoDS_Shape aShape = anIS->Shape();
921 if ( aShape.IsNull() )
926 aBuilder.Add( aRes, aShape );
929 VrmlAPI_Writer aWriter;
930 aWriter.Write(aRes, theFileName.ToCString());
938 /// <param name="theFileName">Name of export file</param>
939 bool ExportStl(const TCollection_AsciiString& theFileName)
941 TopoDS_Compound aComp;
942 BRep_Builder aBuilder;
943 aBuilder.MakeCompound( aComp );
945 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
947 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
948 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
949 TopoDS_Shape aShape = anIS->Shape();
950 if ( aShape.IsNull() )
954 aBuilder.Add( aComp, aShape );
957 StlAPI_Writer aWriter;
958 aWriter.Write(aComp, theFileName.ToCString());
963 ///Define which Import/Export function must be called
965 /// <param name="theFileName">Name of Import/Export file</param>
966 /// <param name="theFormat">Determines format of Import/Export file</param>
967 /// <param name="theIsImport">Determines is Import or not</param>
968 bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
972 const TCollection_AsciiString aFilename = toAsciiString (theFileName);
978 isResult = ImportBrep(aFilename);
981 isResult = ImportStep(aFilename);
984 isResult = ImportIges(aFilename);
995 isResult = ExportBRep(aFilename);
998 isResult = ExportStep(aFilename);
1001 isResult = ExportIges(aFilename);
1004 isResult = ExportVrml(aFilename);
1007 isResult = ExportStl(aFilename);
1010 isResult = Dump(aFilename);
1020 ///Initialize OCCTProxy
1022 void InitOCCTProxy(void)
1024 myGraphicDriver()=NULL;
1027 myAISContext()=NULL;
1032 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
1033 NCollection_Haft<Handle(V3d_View)> myView;
1034 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
1035 NCollection_Haft<Handle(OpenGl_GraphicDriver)> myGraphicDriver;