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);
141 ///Set computed mode in true
143 void SetDegenerateModeOff(void)
145 if (!myView().IsNull())
147 myView()->SetComputedMode (Standard_True);
155 void WindowFitAll(int theXmin, int theYmin, int theXmax, int theYmax)
157 if (!myView().IsNull())
159 myView()->WindowFitAll(theXmin, theYmin, theXmax, theYmax);
164 ///Current place of window
166 /// <param name="theZoomFactor">Current zoom</param>
167 void Place(int theX, int theY, float theZoomFactor)
169 Standard_Real aZoomFactor = theZoomFactor;
170 if (!myView().IsNull())
172 myView()->Place(theX, theY, aZoomFactor);
179 void Zoom(int theX1, int theY1, int theX2, int theY2)
181 if (!myView().IsNull())
183 myView()->Zoom(theX1, theY1, theX2, theY2);
190 void Pan(int theX, int theY)
192 if (!myView().IsNull())
194 myView()->Pan(theX, theY);
201 void Rotation(int theX, int theY)
203 if (!myView().IsNull())
205 myView()->Rotation(theX, theY);
212 void StartRotation(int theX, int theY)
214 if (!myView().IsNull())
216 myView()->StartRotation(theX, theY);
221 ///Select by rectangle
223 void Select(int theX1, int theY1, int theX2, int theY2)
225 if (!myAISContext().IsNull())
227 myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
236 if (!myAISContext().IsNull())
238 myAISContext()->Select (Standard_True);
245 void MoveTo(int theX, int theY)
247 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
249 myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
254 ///Select by rectangle with pressed "Shift" key
256 void ShiftSelect(int theX1, int theY1, int theX2, int theY2)
258 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
260 myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
265 ///Select by "Shift" key
267 void ShiftSelect(void)
269 if (!myAISContext().IsNull())
271 myAISContext()->ShiftSelect (Standard_True);
276 ///Set background color
278 void BackgroundColor(int& theRed, int& theGreen, int& theBlue)
283 if (!myView().IsNull())
285 myView()->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
287 theRed = (int)R1*255;
288 theGreen = (int)G1*255;
289 theBlue = (int)B1*255;
293 ///Get background color Red
297 int aRed, aGreen, aBlue;
298 BackgroundColor(aRed, aGreen, aBlue);
303 ///Get background color Green
307 int aRed, aGreen, aBlue;
308 BackgroundColor(aRed, aGreen, aBlue);
313 ///Get background color Blue
317 int aRed, aGreen, aBlue;
318 BackgroundColor(aRed, aGreen, aBlue);
323 ///Update current viewer
325 void UpdateCurrentViewer(void)
327 if (!myAISContext().IsNull())
329 myAISContext()->UpdateCurrentViewer();
338 if (!myView().IsNull())
340 myView()->SetProj(V3d_Yneg);
349 if (!myView().IsNull())
351 myView()->SetProj(V3d_Zpos);
360 if (!myView().IsNull())
362 myView()->SetProj(V3d_Xneg);
371 if (!myView().IsNull())
373 myView()->SetProj(V3d_Ypos);
382 if (!myView().IsNull())
384 myView()->SetProj(V3d_Xpos);
391 void BottomView(void)
393 if (!myView().IsNull())
395 myView()->SetProj(V3d_Zneg);
404 if (!myView().IsNull())
406 myView()->SetProj(V3d_XposYnegZpos);
415 if (myView().IsNull())
421 return (float)myView()->Scale();
428 void ZoomAllView(void)
430 if (!myView().IsNull())
442 if (!myView().IsNull())
449 ///Set display mode of objects
451 /// <param name="theMode">Set current mode</param>
452 void SetDisplayMode(int theMode)
454 if (myAISContext().IsNull())
458 AIS_DisplayMode aCurrentMode;
461 aCurrentMode=AIS_WireFrame;
465 aCurrentMode=AIS_Shaded;
468 if(myAISContext()->NbSelected()==0)
470 myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
474 for(myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
476 myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
479 myAISContext()->UpdateCurrentViewer();
485 void SetColor(int theR, int theG, int theB)
487 if (myAISContext().IsNull())
491 Quantity_Color aCol = Quantity_Color(theR/255.,theG/255.,theB/255.,Quantity_TOC_RGB);
492 for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
494 myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, Standard_False);
496 myAISContext()->UpdateCurrentViewer();
500 ///Get object color red
504 int aRed, aGreen, aBlue;
505 ObjectColor(aRed, aGreen, aBlue);
510 ///Get object color green
514 int aRed, aGreen, aBlue;
515 ObjectColor(aRed, aGreen, aBlue);
520 ///Get object color R/G/B
522 void ObjectColor(int& theRed, int& theGreen, int& theBlue)
524 if (myAISContext().IsNull())
531 Handle(AIS_InteractiveObject) aCurrent ;
532 myAISContext()->InitSelected();
533 if (!myAISContext()->MoreSelected())
537 aCurrent = myAISContext()->SelectedInteractive();
538 if ( aCurrent->HasColor () )
540 Quantity_Color anObjCol;
541 myAISContext()->Color (aCurrent, anObjCol);
542 Standard_Real r1, r2, r3;
543 anObjCol.Values(r1, r2, r3, Quantity_TOC_RGB);
545 theGreen=(int)r2*255;
551 ///Get object color blue
555 int aRed, aGreen, aBlue;
556 ObjectColor(aRed, aGreen, aBlue);
561 ///Set background color R/G/B
563 void SetBackgroundColor(int theRed, int theGreen, int theBlue)
565 if (!myView().IsNull())
567 myView()->SetBackgroundColor(Quantity_TOC_RGB, theRed/255.,theGreen/255.,theBlue/255.);
574 void EraseObjects(void)
576 if (myAISContext().IsNull())
581 myAISContext()->EraseSelected (Standard_False);
582 myAISContext()->ClearSelected (Standard_True);
588 float GetOCCVersion(void)
590 return (float)OCC_VERSION;
596 void SetMaterial(int theMaterial)
598 if (myAISContext().IsNull())
602 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
604 myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial)theMaterial, Standard_False);
606 myAISContext()->UpdateCurrentViewer();
612 void SetTransparency(int theTrans)
614 if (myAISContext().IsNull())
618 for( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
620 myAISContext()->SetTransparency (myAISContext()->SelectedInteractive(), ((Standard_Real)theTrans) / 10.0, Standard_False);
622 myAISContext()->UpdateCurrentViewer();
626 ///Return true if object is selected
628 bool IsObjectSelected(void)
630 if (myAISContext().IsNull())
634 myAISContext()->InitSelected();
635 return myAISContext()->MoreSelected() != Standard_False;
639 ///Return display mode
641 int DisplayMode(void)
643 if (myAISContext().IsNull())
648 bool OneOrMoreInShading = false;
649 bool OneOrMoreInWireframe = false;
650 for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
652 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 1 ) )
654 OneOrMoreInShading = true;
656 if ( myAISContext()->IsDisplayed( myAISContext()->SelectedInteractive(), 0 ) )
658 OneOrMoreInWireframe = true;
661 if (OneOrMoreInShading && OneOrMoreInWireframe)
665 else if(OneOrMoreInShading)
669 else if (OneOrMoreInWireframe)
680 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
681 void CreateNewView(System::IntPtr theWnd)
683 if (myAISContext().IsNull())
687 myView() = myAISContext()->CurrentViewer()->CreateView();
688 if (myGraphicDriver().IsNull())
690 myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
692 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
693 myView()->SetWindow(aWNTWindow);
694 Standard_Integer w=100, h=100;
695 aWNTWindow->Size(w,h);
696 if (!aWNTWindow->IsMapped())
705 bool SetAISContext(OCCTProxy^ theViewer)
707 this->myAISContext() = theViewer->GetContext();
708 if (myAISContext().IsNull())
718 Handle(AIS_InteractiveContext) GetContext(void)
720 return myAISContext();
724 // ============================================
725 // Import / export functionality
726 // ============================================
731 /// <param name="theFileName">Name of import file</param>
732 bool ImportBrep(System::String^ theFileName)
734 bool isResult = false;
735 int aLength = theFileName->Length;
736 char* aFilename = new char[aLength+1];
737 for(int i = 0; i<aLength; i++)
739 aFilename[i] = (char)theFileName->ToCharArray()[i];
741 aFilename[aLength] = '\0';
742 isResult = ImportBrep(aFilename);
749 /// <param name="theFileName">Name of import file</param>
750 bool ImportBrep(char* theFileName)
752 Standard_CString aFileName = (Standard_CString) theFileName;
754 BRep_Builder aBuilder;
755 Standard_Boolean isResult = BRepTools::Read(aShape,aFileName,aBuilder);
761 myAISContext()->Display (new AIS_Shape (aShape), Standard_True);
768 /// <param name="theFileName">Name of import file</param>
769 bool ImportStep(char* theFileName)
771 Standard_CString aFileName = (Standard_CString) theFileName;
772 STEPControl_Reader aReader;
773 IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileName);
774 if ( aStatus == IFSelect_RetDone )
776 bool isFailsonly = false;
777 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
779 int aNbRoot = aReader.NbRootsForTransfer();
780 aReader.PrintCheckTransfer( isFailsonly, IFSelect_ItemsByEntity );
781 for ( Standard_Integer n = 1; n <= aNbRoot; n++ )
783 Standard_Boolean ok = aReader.TransferRoot( n );
784 int aNbShap = aReader.NbShapes();
787 for ( int i = 1; i <= aNbShap; i++ )
789 TopoDS_Shape aShape = aReader.Shape( i );
790 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
792 myAISContext()->UpdateCurrentViewer();
807 /// <param name="theFileName">Name of import file</param>
808 bool ImportIges(char* theFileName)
810 Standard_CString aFileName = (Standard_CString) theFileName;
811 IGESControl_Reader aReader;
812 int aStatus = aReader.ReadFile( aFileName );
814 if ( aStatus == IFSelect_RetDone )
816 aReader.TransferRoots();
817 TopoDS_Shape aShape = aReader.OneShape();
818 myAISContext()->Display (new AIS_Shape (aShape), Standard_False);
825 myAISContext()->UpdateCurrentViewer();
832 /// <param name="theFileName">Name of export file</param>
833 bool ExportBRep(char* theFileName)
835 myAISContext()->InitSelected();
836 if (!myAISContext()->MoreSelected())
841 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
842 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast(anIO);
843 return BRepTools::Write (anIS->Shape(), (Standard_CString)theFileName) != Standard_False;
849 /// <param name="theFileName">Name of export file</param>
850 bool ExportStep(char* theFileName)
852 STEPControl_StepModelType aType = STEPControl_AsIs;
853 IFSelect_ReturnStatus aStatus;
854 STEPControl_Writer aWriter;
855 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
857 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
858 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
859 TopoDS_Shape aShape = anIS->Shape();
860 aStatus = aWriter.Transfer( aShape , aType );
861 if ( aStatus != IFSelect_RetDone )
867 aStatus = aWriter.Write( (Standard_CString)theFileName );
868 if ( aStatus != IFSelect_RetDone )
879 /// <param name="theFileName">Name of export file</param>
880 bool ExportIges(char* theFileName)
882 IGESControl_Controller::Init();
883 IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
884 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
886 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
888 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
889 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
890 TopoDS_Shape aShape = anIS->Shape();
891 aWriter.AddShape ( aShape );
894 aWriter.ComputeModel();
895 return aWriter.Write( (Standard_CString)theFileName) != Standard_False;
901 /// <param name="theFileName">Name of export file</param>
902 bool ExportVrml(char* theFileName)
904 TopoDS_Compound aRes;
905 BRep_Builder aBuilder;
906 aBuilder.MakeCompound( aRes );
908 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
910 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
911 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
912 TopoDS_Shape aShape = anIS->Shape();
913 if ( aShape.IsNull() )
918 aBuilder.Add( aRes, aShape );
921 VrmlAPI_Writer aWriter;
922 aWriter.Write( aRes, (Standard_CString)theFileName );
930 /// <param name="theFileName">Name of export file</param>
931 bool ExportStl(char* theFileName)
933 TopoDS_Compound aComp;
934 BRep_Builder aBuilder;
935 aBuilder.MakeCompound( aComp );
937 for ( myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected() )
939 Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
940 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
941 TopoDS_Shape aShape = anIS->Shape();
942 if ( aShape.IsNull() )
946 aBuilder.Add( aComp, aShape );
949 StlAPI_Writer aWriter;
950 aWriter.Write( aComp, (Standard_CString)theFileName );
955 ///Define which Import/Export function must be called
957 /// <param name="theFileName">Name of Import/Export file</param>
958 /// <param name="theFormat">Determines format of Import/Export file</param>
959 /// <param name="theIsImport">Determines is Import or not</param>
960 bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
963 int aLength = theFileName->Length;
964 char* aFilename = new char[aLength+1];
965 for(int i = 0; i<aLength; i++)
967 aFilename[i] = (char)theFileName->ToCharArray()[i];
969 aFilename[aLength] = '\0';
976 isResult = ImportBrep(aFilename);
979 isResult = ImportStep(aFilename);
982 isResult = ImportIges(aFilename);
993 isResult = ExportBRep(aFilename);
996 isResult = ExportStep(aFilename);
999 isResult = ExportIges(aFilename);
1002 isResult = ExportVrml(aFilename);
1005 isResult = ExportStl(aFilename);
1008 isResult = Dump(aFilename);
1018 ///Initialize OCCTProxy
1020 void InitOCCTProxy(void)
1022 myGraphicDriver()=NULL;
1025 myAISContext()=NULL;
1030 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
1031 NCollection_Haft<Handle(V3d_View)> myView;
1032 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
1033 NCollection_Haft<Handle(OpenGl_GraphicDriver)> myGraphicDriver;