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>
35 // list of required OCCT libraries
36 #pragma comment(lib, "TKernel.lib")
37 #pragma comment(lib, "TKMath.lib")
38 #pragma comment(lib, "TKBRep.lib")
39 #pragma comment(lib, "TKXSBase.lib")
40 #pragma comment(lib, "TKService.lib")
41 #pragma comment(lib, "TKV3d.lib")
42 #pragma comment(lib, "TKOpenGl.lib")
43 #pragma comment(lib, "TKIGES.lib")
44 #pragma comment(lib, "TKSTEP.lib")
45 #pragma comment(lib, "TKStl.lib")
46 #pragma comment(lib, "TKVrml.lib")
49 /// Proxy class encapsulating calls to OCCT C++ classes within
50 /// C++/CLI class visible from .Net (CSharp)
52 public ref class OCCTProxy
55 // ============================================
56 // Viewer functionality
57 // ============================================
60 ///Initialize a viewer
62 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
63 bool InitViewer(System::IntPtr theWnd)
67 Handle(Aspect_DisplayConnection) aDisplayConnection;
68 myGraphicDriver() = new OpenGl_GraphicDriver (aDisplayConnection);
70 catch (Standard_Failure)
75 myViewer() = new V3d_Viewer (myGraphicDriver());
76 myViewer()->SetDefaultLights();
77 myViewer()->SetLightOn();
78 myView() = myViewer()->CreateView();
79 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
80 myView()->SetWindow(aWNTWindow);
81 if (!aWNTWindow->IsMapped())
85 myAISContext() = new AIS_InteractiveContext( myViewer() );
86 myAISContext()->UpdateCurrentViewer();
88 myView()->MustBeResized();
93 /// Make dump of current view to file
95 /// <param name="theFileName">Name of dump file</param>
96 bool Dump(char *theFileName)
98 if (myView().IsNull())
103 return myView()->Dump(theFileName) != Standard_False;
109 void RedrawView(void)
111 if (!myView().IsNull())
120 void UpdateView(void)
122 if (!myView().IsNull())
124 myView()->MustBeResized();
129 ///Set computed mode in false
131 void SetDegenerateModeOn(void)
133 if (!myView().IsNull())
135 myView()->SetComputedMode (Standard_False);
140 ///Set computed mode in true
142 void SetDegenerateModeOff(void)
144 if (!myView().IsNull())
146 myView()->SetComputedMode (Standard_True);
153 void WindowFitAll(int theXmin, int theYmin, int theXmax, int theYmax)
155 if (!myView().IsNull())
157 myView()->WindowFitAll(theXmin, theYmin, theXmax, theYmax);
162 ///Current place of window
164 /// <param name="theZoomFactor">Current zoom</param>
165 void Place(int theX, int theY, float theZoomFactor)
167 Quantity_Factor aZoomFactor = theZoomFactor;
168 if (!myView().IsNull())
170 myView()->Place(theX, theY, aZoomFactor);
177 void Zoom(int theX1, int theY1, int theX2, int theY2)
179 if (!myView().IsNull())
181 myView()->Zoom(theX1, theY1, theX2, theY2);
188 void Pan(int theX, int theY)
190 if (!myView().IsNull())
192 myView()->Pan(theX, theY);
199 void Rotation(int theX, int theY)
201 if (!myView().IsNull())
203 myView()->Rotation(theX, theY);
210 void StartRotation(int theX, int theY)
212 if (!myView().IsNull())
214 myView()->StartRotation(theX, theY);
219 ///Select by rectangle
221 void Select(int theX1, int theY1, int theX2, int theY2)
223 if (!myAISContext().IsNull())
225 myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
234 if (!myAISContext().IsNull())
236 myAISContext()->Select (Standard_True);
243 void MoveTo(int theX, int theY)
245 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
247 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
252 ///Select by rectangle with pressed "Shift" key
254 void ShiftSelect(int theX1, int theY1, int theX2, int theY2)
256 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
258 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
263 ///Select by "Shift" key
265 void ShiftSelect(void)
267 if (!myAISContext().IsNull())
269 myAISContext()->ShiftSelect (Standard_True);
274 ///Set background color
276 void BackgroundColor(int& theRed, int& theGreen, int& theBlue)
281 if (!myView().IsNull())
283 myView()->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
285 theRed = (int)R1*255;
286 theGreen = (int)G1*255;
287 theBlue = (int)B1*255;
291 ///Get background color Red
295 int aRed, aGreen, aBlue;
296 BackgroundColor(aRed, aGreen, aBlue);
301 ///Get background color Green
305 int aRed, aGreen, aBlue;
306 BackgroundColor(aRed, aGreen, aBlue);
311 ///Get background color Blue
315 int aRed, aGreen, aBlue;
316 BackgroundColor(aRed, aGreen, aBlue);
321 ///Update current viewer
323 void UpdateCurrentViewer(void)
325 if (!myAISContext().IsNull())
327 myAISContext()->UpdateCurrentViewer();
336 if (!myView().IsNull())
338 myView()->SetProj(V3d_Yneg);
347 if (!myView().IsNull())
349 myView()->SetProj(V3d_Zpos);
358 if (!myView().IsNull())
360 myView()->SetProj(V3d_Xneg);
369 if (!myView().IsNull())
371 myView()->SetProj(V3d_Ypos);
380 if (!myView().IsNull())
382 myView()->SetProj(V3d_Xpos);
389 void BottomView(void)
391 if (!myView().IsNull())
393 myView()->SetProj(V3d_Zneg);
402 if (!myView().IsNull())
404 myView()->SetProj(V3d_XposYnegZpos);
413 if (myView().IsNull())
419 return (float)myView()->Scale();
426 void ZoomAllView(void)
428 if (!myView().IsNull())
440 if (!myView().IsNull())
447 ///Set display mode of objects
449 /// <param name="theMode">Set current mode</param>
450 void SetDisplayMode(int theMode)
452 if (myAISContext().IsNull())
456 AIS_DisplayMode aCurrentMode;
459 aCurrentMode=AIS_WireFrame;
463 aCurrentMode=AIS_Shaded;
466 if(myAISContext()->NbSelected()==0)
468 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
472 for(myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
474 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
477 myAISContext()->UpdateCurrentViewer();
483 void SetColor(int theR, int theG, int theB)
485 if (myAISContext().IsNull())
489 Quantity_Color aCol = Quantity_Color(theR/255.,theG/255.,theB/255.,Quantity_TOC_RGB);
490 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
492 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol.Name(), Standard_False);
494 myAISContext()->UpdateCurrentViewer();
498 ///Get object color red
502 int aRed, aGreen, aBlue;
503 ObjectColor(aRed, aGreen, aBlue);
508 ///Get object color green
512 int aRed, aGreen, aBlue;
513 ObjectColor(aRed, aGreen, aBlue);
518 ///Get object color R/G/B
520 void ObjectColor(int& theRed, int& theGreen, int& theBlue)
522 if (myAISContext().IsNull())
529 Handle(AIS_InteractiveObject) aCurrent ;
530 Quantity_Color anObjCol;
531 myAISContext()->InitSelected();
532 if (!myAISContext()->MoreSelected())
536 aCurrent = myAISContext()->SelectedInteractive();
537 if ( aCurrent->HasColor () )
539 anObjCol = myAISContext()->Color(aCurrent);
540 Quantity_Parameter r1, r2, r3;
541 anObjCol.Values(r1, r2, r3, Quantity_TOC_RGB);
543 theGreen=(int)r2*255;
549 ///Get object color blue
553 int aRed, aGreen, aBlue;
554 ObjectColor(aRed, aGreen, aBlue);
559 ///Set background color R/G/B
561 void SetBackgroundColor(int theRed, int theGreen, int theBlue)
563 if (!myView().IsNull())
565 myView()->SetBackgroundColor(Quantity_TOC_RGB, theRed/255.,theGreen/255.,theBlue/255.);
572 void EraseObjects(void)
574 if (myAISContext().IsNull())
579 myAISContext()->EraseSelected (Standard_False);
580 myAISContext()->ClearSelected (Standard_True);
586 float GetOCCVersion(void)
588 return (float)OCC_VERSION;
594 void SetMaterial(int theMaterial)
596 if (myAISContext().IsNull())
600 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
602 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial)theMaterial, Standard_False);
604 myAISContext()->UpdateCurrentViewer();
610 void SetTransparency(int theTrans)
612 if (myAISContext().IsNull())
616 for( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
618 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real)theTrans) / 10.0, Standard_False);
620 myAISContext()->UpdateCurrentViewer();
624 ///Return true if object is selected
626 bool IsObjectSelected(void)
628 if (myAISContext().IsNull())
632 myAISContext()->InitSelected();
633 return myAISContext()->MoreSelected() != Standard_False;
637 ///Return display mode
639 int DisplayMode(void)
641 if (myAISContext().IsNull())
646 bool OneOrMoreInShading = false;
647 bool OneOrMoreInWireframe = false;
648 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
650 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 1 ) )
652 OneOrMoreInShading = true;
654 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 0 ) )
656 OneOrMoreInWireframe = true;
659 if (OneOrMoreInShading && OneOrMoreInWireframe)
663 else if(OneOrMoreInShading)
667 else if (OneOrMoreInWireframe)
678 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
679 void CreateNewView(System::IntPtr theWnd)
681 if (myAISContext().IsNull())
685 myView() = myAISContext()->CurrentViewer()->CreateView();
686 if (myGraphicDriver().IsNull())
688 myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
690 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
691 myView()->SetWindow(aWNTWindow);
692 Standard_Integer w=100, h=100;
693 aWNTWindow->Size(w,h);
694 if (!aWNTWindow->IsMapped())
703 bool SetAISContext(OCCTProxy^ theViewer)
705 this->myAISContext() = theViewer->GetContext();
706 if (myAISContext().IsNull())
716 Handle(AIS_InteractiveContext) GetContext(void)
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 bool isResult = false;
733 int aLength = theFileName->Length;
734 char* aFilename = new char[aLength+1];
735 for(int i = 0; i<aLength; i++)
737 aFilename[i] = (char)theFileName->ToCharArray()[i];
739 aFilename[aLength] = '\0';
740 isResult = ImportBrep(aFilename);
747 /// <param name="theFileName">Name of import file</param>
748 bool ImportBrep(char* theFileName)
750 Standard_CString aFileName = (Standard_CString) theFileName;
752 BRep_Builder aBuilder;
753 Standard_Boolean isResult = BRepTools::Read(aShape,aFileName,aBuilder);
759 myAISContext()->Display (new AIS_Shape (aShape), Standard_True);
766 /// <param name="theFileName">Name of import file</param>
767 bool ImportStep(char* theFileName)
769 Standard_CString aFileName = (Standard_CString) theFileName;
770 STEPControl_Reader aReader;
771 IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileName);
772 if ( aStatus == IFSelect_RetDone )
774 bool isFailsonly = false;
775 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
777 int aNbRoot = aReader.NbRootsForTransfer();
778 aReader.PrintCheckTransfer( isFailsonly, IFSelect_ItemsByEntity );
779 for ( Standard_Integer n = 1; n <= aNbRoot; n++ )
781 Standard_Boolean ok = aReader.TransferRoot( n );
782 int aNbShap = aReader.NbShapes();
785 for ( int i = 1; i <= aNbShap; i++ )
787 TopoDS_Shape aShape = aReader.Shape( i );
788 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
790 myAISContext()->UpdateCurrentViewer();
805 /// <param name="theFileName">Name of import file</param>
806 bool ImportIges(char* theFileName)
808 Standard_CString aFileName = (Standard_CString) theFileName;
809 IGESControl_Reader aReader;
810 int aStatus = aReader.ReadFile( aFileName );
812 if ( aStatus == IFSelect_RetDone )
814 aReader.TransferRoots();
815 TopoDS_Shape aShape = aReader.OneShape();
816 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
823 myAISContext()->UpdateCurrentViewer();
830 /// <param name="theFileName">Name of export file</param>
831 bool ExportBRep(char* theFileName)
833 myAISContext()->InitSelected();
834 if (!myAISContext()->MoreSelected())
839 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
840 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast(anIO);
841 return BRepTools::Write (anIS->Shape(), (Standard_CString)theFileName) != Standard_False;
847 /// <param name="theFileName">Name of export file</param>
848 bool ExportStep(char* theFileName)
850 STEPControl_StepModelType aType = STEPControl_AsIs;
851 IFSelect_ReturnStatus aStatus;
852 STEPControl_Writer aWriter;
853 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
855 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
856 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
857 TopoDS_Shape aShape = anIS->Shape();
858 aStatus = aWriter.Transfer( aShape , aType );
859 if ( aStatus != IFSelect_RetDone )
865 aStatus = aWriter.Write( (Standard_CString)theFileName );
866 if ( aStatus != IFSelect_RetDone )
877 /// <param name="theFileName">Name of export file</param>
878 bool ExportIges(char* theFileName)
880 IGESControl_Controller::Init();
881 IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
882 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
884 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
886 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
887 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
888 TopoDS_Shape aShape = anIS->Shape();
889 aWriter.AddShape ( aShape );
892 aWriter.ComputeModel();
893 return aWriter.Write( (Standard_CString)theFileName) != Standard_False;
899 /// <param name="theFileName">Name of export file</param>
900 bool ExportVrml(char* theFileName)
902 TopoDS_Compound aRes;
903 BRep_Builder aBuilder;
904 aBuilder.MakeCompound( aRes );
906 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
908 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
909 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
910 TopoDS_Shape aShape = anIS->Shape();
911 if ( aShape.IsNull() )
916 aBuilder.Add( aRes, aShape );
919 VrmlAPI_Writer aWriter;
920 aWriter.Write( aRes, (Standard_CString)theFileName );
928 /// <param name="theFileName">Name of export file</param>
929 bool ExportStl(char* theFileName)
931 TopoDS_Compound aComp;
932 BRep_Builder aBuilder;
933 aBuilder.MakeCompound( aComp );
935 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
937 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
938 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
939 TopoDS_Shape aShape = anIS->Shape();
940 if ( aShape.IsNull() )
944 aBuilder.Add( aComp, aShape );
947 StlAPI_Writer aWriter;
948 aWriter.Write( aComp, (Standard_CString)theFileName );
953 ///Define which Import/Export function must be called
955 /// <param name="theFileName">Name of Import/Export file</param>
956 /// <param name="theFormat">Determines format of Import/Export file</param>
957 /// <param name="theIsImport">Determines is Import or not</param>
958 bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
961 int aLength = theFileName->Length;
962 char* aFilename = new char[aLength+1];
963 for(int i = 0; i<aLength; i++)
965 aFilename[i] = (char)theFileName->ToCharArray()[i];
967 aFilename[aLength] = '\0';
974 isResult = ImportBrep(aFilename);
977 isResult = ImportStep(aFilename);
980 isResult = ImportIges(aFilename);
991 isResult = ExportBRep(aFilename);
994 isResult = ExportStep(aFilename);
997 isResult = ExportIges(aFilename);
1000 isResult = ExportVrml(aFilename);
1003 isResult = ExportStl(aFilename);
1006 isResult = Dump(aFilename);
1016 ///Initialize OCCTProxy
1018 void InitOCCTProxy(void)
1020 myGraphicDriver()=NULL;
1023 myAISContext()=NULL;
1028 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
1029 NCollection_Haft<Handle(V3d_View)> myView;
1030 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
1031 NCollection_Haft<Handle(OpenGl_GraphicDriver)> myGraphicDriver;