1 // include required OCCT headers
2 #include <Standard_Version.hxx>
3 #include <Message_ProgressIndicator.hxx>
4 #include <Message_ProgressScope.hxx>
6 #include <Aspect_DisplayConnection.hxx>
7 #include <WNT_Window.hxx>
8 #include <OpenGl_GraphicDriver.hxx>
10 #include <V3d_Viewer.hxx>
11 #include <V3d_View.hxx>
12 #include <AIS_InteractiveContext.hxx>
13 #include <AIS_Shape.hxx>
15 #include <TopoDS_Shape.hxx>
16 #include <TopoDS_Compound.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRepTools.hxx>
21 #include <IGESControl_Reader.hxx>
22 #include <IGESControl_Controller.hxx>
23 #include <IGESControl_Writer.hxx>
24 #include <IFSelect_ReturnStatus.hxx>
25 #include <Interface_Static.hxx>
27 #include <STEPControl_Reader.hxx>
28 #include <STEPControl_Writer.hxx>
30 #include <StlAPI_Writer.hxx>
32 #include <VrmlAPI_Writer.hxx>
33 //wrapper of pure C++ classes to ref classes
34 #include <NCollection_Haft.h>
38 // list of required OCCT libraries
39 #pragma comment(lib, "TKernel.lib")
40 #pragma comment(lib, "TKMath.lib")
41 #pragma comment(lib, "TKBRep.lib")
42 #pragma comment(lib, "TKXSBase.lib")
43 #pragma comment(lib, "TKService.lib")
44 #pragma comment(lib, "TKV3d.lib")
45 #pragma comment(lib, "TKOpenGl.lib")
46 #pragma comment(lib, "TKDEIGES.lib")
47 #pragma comment(lib, "TKDESTEP.lib")
48 #pragma comment(lib, "TKDESTL.lib")
49 #pragma comment(lib, "TKDEVRML.lib")
50 #pragma comment(lib, "TKLCAF.lib")
52 //! Auxiliary tool for converting C# string into UTF-8 string.
53 static TCollection_AsciiString toAsciiString (String^ theString)
55 if (theString == nullptr)
57 return TCollection_AsciiString();
60 pin_ptr<const wchar_t> aPinChars = PtrToStringChars (theString);
61 const wchar_t* aWCharPtr = aPinChars;
63 || *aWCharPtr == L'\0')
65 return TCollection_AsciiString();
67 return TCollection_AsciiString (aWCharPtr);
71 /// Proxy class encapsulating calls to OCCT C++ classes within
72 /// C++/CLI class visible from .Net (CSharp)
74 public ref class OCCTProxy
77 // ============================================
78 // Viewer functionality
79 // ============================================
82 ///Initialize a viewer
84 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
85 bool InitViewer(System::IntPtr theWnd)
89 Handle(Aspect_DisplayConnection) aDisplayConnection;
90 myGraphicDriver() = new OpenGl_GraphicDriver (aDisplayConnection);
92 catch (Standard_Failure)
97 myViewer() = new V3d_Viewer (myGraphicDriver());
98 myViewer()->SetDefaultLights();
99 myViewer()->SetLightOn();
100 myView() = myViewer()->CreateView();
101 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
102 myView()->SetWindow(aWNTWindow);
103 if (!aWNTWindow->IsMapped())
107 myAISContext() = new AIS_InteractiveContext( myViewer() );
108 myAISContext()->UpdateCurrentViewer();
110 myView()->MustBeResized();
115 /// Make dump of current view to file
117 /// <param name="theFileName">Name of dump file</param>
118 bool Dump(const TCollection_AsciiString& theFileName)
120 if (myView().IsNull())
125 return myView()->Dump(theFileName.ToCString()) != Standard_False;
131 void RedrawView(void)
133 if (!myView().IsNull())
142 void UpdateView(void)
144 if (!myView().IsNull())
146 myView()->MustBeResized();
151 ///Set computed mode in false
153 void SetDegenerateModeOn(void)
155 if (!myView().IsNull())
157 myView()->SetComputedMode (Standard_False);
163 ///Set computed mode in true
165 void SetDegenerateModeOff(void)
167 if (!myView().IsNull())
169 myView()->SetComputedMode (Standard_True);
177 void WindowFitAll(int theXmin, int theYmin, int theXmax, int theYmax)
179 if (!myView().IsNull())
181 myView()->WindowFitAll(theXmin, theYmin, theXmax, theYmax);
186 ///Current place of window
188 /// <param name="theZoomFactor">Current zoom</param>
189 void Place(int theX, int theY, float theZoomFactor)
191 Standard_Real aZoomFactor = theZoomFactor;
192 if (!myView().IsNull())
194 myView()->Place(theX, theY, aZoomFactor);
201 void Zoom(int theX1, int theY1, int theX2, int theY2)
203 if (!myView().IsNull())
205 myView()->Zoom(theX1, theY1, theX2, theY2);
212 void Pan(int theX, int theY)
214 if (!myView().IsNull())
216 myView()->Pan(theX, theY);
223 void Rotation(int theX, int theY)
225 if (!myView().IsNull())
227 myView()->Rotation(theX, theY);
234 void StartRotation(int theX, int theY)
236 if (!myView().IsNull())
238 myView()->StartRotation(theX, theY);
243 ///Select by rectangle
245 void Select(int theX1, int theY1, int theX2, int theY2)
247 if (!myAISContext().IsNull())
249 myAISContext()->SelectRectangle (Graphic3d_Vec2i (theX1, theY1),
250 Graphic3d_Vec2i (theX2, theY2),
252 myAISContext()->UpdateCurrentViewer();
261 if (!myAISContext().IsNull())
263 myAISContext()->SelectDetected();
264 myAISContext()->UpdateCurrentViewer();
271 void MoveTo(int theX, int theY)
273 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
275 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
280 ///Select by rectangle with pressed "Shift" key
282 void ShiftSelect(int theX1, int theY1, int theX2, int theY2)
284 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
286 myAISContext()->SelectRectangle (Graphic3d_Vec2i (theX1, theY1),
287 Graphic3d_Vec2i (theX2, theY2),
289 AIS_SelectionScheme_XOR);
290 myAISContext()->UpdateCurrentViewer();
295 ///Select by "Shift" key
297 void ShiftSelect(void)
299 if (!myAISContext().IsNull())
301 myAISContext()->SelectDetected (AIS_SelectionScheme_XOR);
302 myAISContext()->UpdateCurrentViewer();
307 ///Set background color
309 void BackgroundColor(int& theRed, int& theGreen, int& theBlue)
314 if (!myView().IsNull())
316 myView()->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
318 theRed = (int)R1*255;
319 theGreen = (int)G1*255;
320 theBlue = (int)B1*255;
324 ///Get background color Red
328 int aRed, aGreen, aBlue;
329 BackgroundColor(aRed, aGreen, aBlue);
334 ///Get background color Green
338 int aRed, aGreen, aBlue;
339 BackgroundColor(aRed, aGreen, aBlue);
344 ///Get background color Blue
348 int aRed, aGreen, aBlue;
349 BackgroundColor(aRed, aGreen, aBlue);
354 ///Update current viewer
356 void UpdateCurrentViewer(void)
358 if (!myAISContext().IsNull())
360 myAISContext()->UpdateCurrentViewer();
369 if (!myView().IsNull())
371 myView()->SetProj(V3d_Yneg);
380 if (!myView().IsNull())
382 myView()->SetProj(V3d_Zpos);
391 if (!myView().IsNull())
393 myView()->SetProj(V3d_Xneg);
402 if (!myView().IsNull())
404 myView()->SetProj(V3d_Ypos);
413 if (!myView().IsNull())
415 myView()->SetProj(V3d_Xpos);
422 void BottomView(void)
424 if (!myView().IsNull())
426 myView()->SetProj(V3d_Zneg);
435 if (!myView().IsNull())
437 myView()->SetProj(V3d_XposYnegZpos);
446 if (myView().IsNull())
452 return (float)myView()->Scale();
459 void ZoomAllView(void)
461 if (!myView().IsNull())
473 if (!myView().IsNull())
480 ///Set display mode of objects
482 /// <param name="theMode">Set current mode</param>
483 void SetDisplayMode(int theMode)
485 if (myAISContext().IsNull())
489 AIS_DisplayMode aCurrentMode;
492 aCurrentMode=AIS_WireFrame;
496 aCurrentMode=AIS_Shaded;
499 if(myAISContext()->NbSelected()==0)
501 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
505 for(myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
507 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
510 myAISContext()->UpdateCurrentViewer();
516 void SetColor(int theR, int theG, int theB)
518 if (myAISContext().IsNull())
522 Quantity_Color aCol = Quantity_Color(theR/255.,theG/255.,theB/255.,Quantity_TOC_RGB);
523 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
525 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, Standard_False);
527 myAISContext()->UpdateCurrentViewer();
531 ///Get object color red
535 int aRed, aGreen, aBlue;
536 ObjectColor(aRed, aGreen, aBlue);
541 ///Get object color green
545 int aRed, aGreen, aBlue;
546 ObjectColor(aRed, aGreen, aBlue);
551 ///Get object color R/G/B
553 void ObjectColor(int& theRed, int& theGreen, int& theBlue)
555 if (myAISContext().IsNull())
562 Handle(AIS_InteractiveObject) aCurrent ;
563 myAISContext()->InitSelected();
564 if (!myAISContext()->MoreSelected())
568 aCurrent = myAISContext()->SelectedInteractive();
569 if ( aCurrent->HasColor () )
571 Quantity_Color anObjCol;
572 myAISContext()->Color (aCurrent, anObjCol);
573 Standard_Real r1, r2, r3;
574 anObjCol.Values(r1, r2, r3, Quantity_TOC_RGB);
576 theGreen=(int)r2*255;
582 ///Get object color blue
586 int aRed, aGreen, aBlue;
587 ObjectColor(aRed, aGreen, aBlue);
592 ///Set background color R/G/B
594 void SetBackgroundColor(int theRed, int theGreen, int theBlue)
596 if (!myView().IsNull())
598 myView()->SetBackgroundColor(Quantity_TOC_RGB, theRed/255.,theGreen/255.,theBlue/255.);
605 void EraseObjects(void)
607 if (myAISContext().IsNull())
612 myAISContext()->EraseSelected (Standard_False);
613 myAISContext()->ClearSelected (Standard_True);
619 float GetOCCVersion(void)
621 return (float)OCC_VERSION;
627 void SetMaterial(int theMaterial)
629 if (myAISContext().IsNull())
633 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
635 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial)theMaterial, Standard_False);
637 myAISContext()->UpdateCurrentViewer();
643 void SetTransparency(int theTrans)
645 if (myAISContext().IsNull())
649 for( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
651 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real)theTrans) / 10.0, Standard_False);
653 myAISContext()->UpdateCurrentViewer();
657 ///Return true if object is selected
659 bool IsObjectSelected(void)
661 if (myAISContext().IsNull())
665 myAISContext()->InitSelected();
666 return myAISContext()->MoreSelected() != Standard_False;
670 ///Return display mode
672 int DisplayMode(void)
674 if (myAISContext().IsNull())
679 bool OneOrMoreInShading = false;
680 bool OneOrMoreInWireframe = false;
681 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
683 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 1 ) )
685 OneOrMoreInShading = true;
687 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 0 ) )
689 OneOrMoreInWireframe = true;
692 if (OneOrMoreInShading && OneOrMoreInWireframe)
696 else if(OneOrMoreInShading)
700 else if (OneOrMoreInWireframe)
711 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
712 void CreateNewView(System::IntPtr theWnd)
714 if (myAISContext().IsNull())
718 myView() = myAISContext()->CurrentViewer()->CreateView();
719 if (myGraphicDriver().IsNull())
721 myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
723 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
724 myView()->SetWindow(aWNTWindow);
725 Standard_Integer w=100, h=100;
726 aWNTWindow->Size(w,h);
727 if (!aWNTWindow->IsMapped())
736 bool SetAISContext(OCCTProxy^ theViewer)
738 this->myAISContext() = theViewer->GetContext();
739 if (myAISContext().IsNull())
749 Handle(AIS_InteractiveContext) GetContext(void)
751 return myAISContext();
755 // ============================================
756 // Import / export functionality
757 // ============================================
762 /// <param name="theFileName">Name of import file</param>
763 bool ImportBrep(System::String^ theFileName)
765 return ImportBrep (toAsciiString (theFileName));
771 /// <param name="theFileName">Name of import file</param>
772 bool ImportBrep (const TCollection_AsciiString& theFileName)
775 BRep_Builder aBuilder;
776 Standard_Boolean isResult = BRepTools::Read(aShape,theFileName.ToCString(),aBuilder);
782 myAISContext()->Display (new AIS_Shape (aShape), Standard_True);
789 /// <param name="theFileName">Name of import file</param>
790 bool ImportStep(const TCollection_AsciiString& theFileName)
792 STEPControl_Reader aReader;
793 IFSelect_ReturnStatus aStatus = aReader.ReadFile(theFileName.ToCString());
794 if ( aStatus == IFSelect_RetDone )
796 bool isFailsonly = false;
797 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
799 int aNbRoot = aReader.NbRootsForTransfer();
800 aReader.PrintCheckTransfer( isFailsonly, IFSelect_ItemsByEntity );
801 for ( Standard_Integer n = 1; n <= aNbRoot; n++ )
803 Standard_Boolean ok = aReader.TransferRoot( n );
804 int aNbShap = aReader.NbShapes();
807 for ( int i = 1; i <= aNbShap; i++ )
809 TopoDS_Shape aShape = aReader.Shape( i );
810 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
812 myAISContext()->UpdateCurrentViewer();
827 /// <param name="theFileName">Name of import file</param>
828 bool ImportIges(const TCollection_AsciiString& theFileName)
830 IGESControl_Reader aReader;
831 int aStatus = aReader.ReadFile( theFileName.ToCString() );
833 if ( aStatus == IFSelect_RetDone )
835 aReader.TransferRoots();
836 TopoDS_Shape aShape = aReader.OneShape();
837 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
844 myAISContext()->UpdateCurrentViewer();
851 /// <param name="theFileName">Name of export file</param>
852 bool ExportBRep(const TCollection_AsciiString& theFileName)
854 myAISContext()->InitSelected();
855 if (!myAISContext()->MoreSelected())
860 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
861 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast(anIO);
862 return BRepTools::Write (anIS->Shape(), theFileName.ToCString()) != Standard_False;
868 /// <param name="theFileName">Name of export file</param>
869 bool ExportStep(const TCollection_AsciiString& theFileName)
871 STEPControl_StepModelType aType = STEPControl_AsIs;
872 IFSelect_ReturnStatus aStatus;
873 STEPControl_Writer aWriter;
874 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
876 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
877 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
878 TopoDS_Shape aShape = anIS->Shape();
879 aStatus = aWriter.Transfer( aShape , aType );
880 if ( aStatus != IFSelect_RetDone )
886 aStatus = aWriter.Write(theFileName.ToCString());
887 if ( aStatus != IFSelect_RetDone )
898 /// <param name="theFileName">Name of export file</param>
899 bool ExportIges(const TCollection_AsciiString& theFileName)
901 IGESControl_Controller::Init();
902 IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
903 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
905 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
907 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
908 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
909 TopoDS_Shape aShape = anIS->Shape();
910 aWriter.AddShape ( aShape );
913 aWriter.ComputeModel();
914 return aWriter.Write(theFileName.ToCString()) != Standard_False;
920 /// <param name="theFileName">Name of export file</param>
921 bool ExportVrml(const TCollection_AsciiString& theFileName)
923 TopoDS_Compound aRes;
924 BRep_Builder aBuilder;
925 aBuilder.MakeCompound( aRes );
927 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
929 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
930 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
931 TopoDS_Shape aShape = anIS->Shape();
932 if ( aShape.IsNull() )
937 aBuilder.Add( aRes, aShape );
940 VrmlAPI_Writer aWriter;
941 aWriter.Write(aRes, theFileName.ToCString());
949 /// <param name="theFileName">Name of export file</param>
950 bool ExportStl(const TCollection_AsciiString& theFileName)
952 TopoDS_Compound aComp;
953 BRep_Builder aBuilder;
954 aBuilder.MakeCompound( aComp );
956 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
958 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
959 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
960 TopoDS_Shape aShape = anIS->Shape();
961 if ( aShape.IsNull() )
965 aBuilder.Add( aComp, aShape );
968 StlAPI_Writer aWriter;
969 aWriter.Write(aComp, theFileName.ToCString());
974 ///Define which Import/Export function must be called
976 /// <param name="theFileName">Name of Import/Export file</param>
977 /// <param name="theFormat">Determines format of Import/Export file</param>
978 /// <param name="theIsImport">Determines is Import or not</param>
979 bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
983 const TCollection_AsciiString aFilename = toAsciiString (theFileName);
989 isResult = ImportBrep(aFilename);
992 isResult = ImportStep(aFilename);
995 isResult = ImportIges(aFilename);
1006 isResult = ExportBRep(aFilename);
1009 isResult = ExportStep(aFilename);
1012 isResult = ExportIges(aFilename);
1015 isResult = ExportVrml(aFilename);
1018 isResult = ExportStl(aFilename);
1021 isResult = Dump(aFilename);
1031 ///Initialize OCCTProxy
1033 void InitOCCTProxy(void)
1035 myGraphicDriver()=NULL;
1038 myAISContext()=NULL;
1043 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
1044 NCollection_Haft<Handle(V3d_View)> myView;
1045 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
1046 NCollection_Haft<Handle(OpenGl_GraphicDriver)> myGraphicDriver;