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 TCollection_ExtendedString a3DName("Visu3D");
76 myViewer() = new V3d_Viewer (myGraphicDriver(), a3DName.ToExtString(),"", 1000.0,
77 V3d_XposYnegZpos, Quantity_NOC_GRAY30,
78 V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT,
79 Standard_True, Standard_False);
81 myViewer()->SetDefaultLights();
82 myViewer()->SetLightOn();
83 myView() = myViewer()->CreateView();
84 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
85 myView()->SetWindow(aWNTWindow);
86 if (!aWNTWindow->IsMapped())
90 myAISContext() = new AIS_InteractiveContext( myViewer() );
91 myAISContext()->UpdateCurrentViewer();
93 myView()->MustBeResized();
98 /// Make dump of current view to file
100 /// <param name="theFileName">Name of dump file</param>
101 bool Dump(char *theFileName)
103 if (myView().IsNull())
108 return myView()->Dump(theFileName) != Standard_False;
114 void RedrawView(void)
116 if (!myView().IsNull())
125 void UpdateView(void)
127 if (!myView().IsNull())
129 myView()->MustBeResized();
134 ///Set computed mode in false
136 void SetDegenerateModeOn(void)
138 if (!myView().IsNull())
140 myView()->SetComputedMode (Standard_False);
145 ///Set computed mode in true
147 void SetDegenerateModeOff(void)
149 if (!myView().IsNull())
151 myView()->SetComputedMode (Standard_True);
158 void WindowFitAll(int theXmin, int theYmin, int theXmax, int theYmax)
160 if (!myView().IsNull())
162 myView()->WindowFitAll(theXmin, theYmin, theXmax, theYmax);
167 ///Current place of window
169 /// <param name="theZoomFactor">Current zoom</param>
170 void Place(int theX, int theY, float theZoomFactor)
172 Quantity_Factor aZoomFactor = theZoomFactor;
173 if (!myView().IsNull())
175 myView()->Place(theX, theY, aZoomFactor);
182 void Zoom(int theX1, int theY1, int theX2, int theY2)
184 if (!myView().IsNull())
186 myView()->Zoom(theX1, theY1, theX2, theY2);
193 void Pan(int theX, int theY)
195 if (!myView().IsNull())
197 myView()->Pan(theX, theY);
204 void Rotation(int theX, int theY)
206 if (!myView().IsNull())
208 myView()->Rotation(theX, theY);
215 void StartRotation(int theX, int theY)
217 if (!myView().IsNull())
219 myView()->StartRotation(theX, theY);
224 ///Select by rectangle
226 void Select(int theX1, int theY1, int theX2, int theY2)
228 if (!myAISContext().IsNull())
230 myAISContext()->Select(theX1, theY1, theX2, theY2, myView());
239 if (!myAISContext().IsNull())
241 myAISContext()->Select();
248 void MoveTo(int theX, int theY)
250 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
252 myAISContext()->MoveTo(theX, theY, myView());
257 ///Select by rectangle with pressed "Shift" key
259 void ShiftSelect(int theX1, int theY1, int theX2, int theY2)
261 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
263 myAISContext()->ShiftSelect(theX1, theY1, theX2, theY2, myView());
268 ///Select by "Shift" key
270 void ShiftSelect(void)
272 if (!myAISContext().IsNull())
274 myAISContext()->ShiftSelect();
279 ///Set background color
281 void BackgroundColor(int& theRed, int& theGreen, int& theBlue)
286 if (!myView().IsNull())
288 myView()->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
290 theRed = (int)R1*255;
291 theGreen = (int)G1*255;
292 theBlue = (int)B1*255;
296 ///Get background color Red
300 int aRed, aGreen, aBlue;
301 BackgroundColor(aRed, aGreen, aBlue);
306 ///Get background color Green
310 int aRed, aGreen, aBlue;
311 BackgroundColor(aRed, aGreen, aBlue);
316 ///Get background color Blue
320 int aRed, aGreen, aBlue;
321 BackgroundColor(aRed, aGreen, aBlue);
326 ///Update current viewer
328 void UpdateCurrentViewer(void)
330 if (!myAISContext().IsNull())
332 myAISContext()->UpdateCurrentViewer();
341 if (!myView().IsNull())
343 myView()->SetProj(V3d_Xpos);
352 if (!myView().IsNull())
354 myView()->SetProj(V3d_Zpos);
363 if (!myView().IsNull())
365 myView()->SetProj(V3d_Ypos);
374 if (!myView().IsNull())
376 myView()->SetProj(V3d_Xneg);
385 if (!myView().IsNull())
387 myView()->SetProj(V3d_Yneg);
394 void BottomView(void)
396 if (!myView().IsNull())
398 myView()->SetProj(V3d_Zneg);
407 if (!myView().IsNull())
409 myView()->SetProj(V3d_XposYnegZpos);
418 if (myView().IsNull())
424 return (float)myView()->Scale();
431 void ZoomAllView(void)
433 if (!myView().IsNull())
445 if (!myView().IsNull())
452 ///Set display mode of objects
454 /// <param name="theMode">Set current mode</param>
455 void SetDisplayMode(int theMode)
457 if (myAISContext().IsNull())
461 AIS_DisplayMode aCurrentMode;
464 aCurrentMode=AIS_WireFrame;
468 aCurrentMode=AIS_Shaded;
471 if(myAISContext()->NbCurrents()==0 || myAISContext()->NbSelected()==0)
473 myAISContext()->SetDisplayMode(aCurrentMode);
477 for(myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
479 myAISContext()->SetDisplayMode(myAISContext()->Current(), theMode, Standard_False);
482 myAISContext()->UpdateCurrentViewer();
488 void SetColor(int theR, int theG, int theB)
490 if (myAISContext().IsNull())
494 Quantity_Color aCol = Quantity_Color(theR/255.,theG/255.,theB/255.,Quantity_TOC_RGB);
495 for (;myAISContext()->MoreCurrent ();myAISContext()->NextCurrent ())
497 myAISContext()->SetColor (myAISContext()->Current(),aCol.Name());
502 ///Get object color red
506 int aRed, aGreen, aBlue;
507 ObjectColor(aRed, aGreen, aBlue);
512 ///Get object color green
516 int aRed, aGreen, aBlue;
517 ObjectColor(aRed, aGreen, aBlue);
522 ///Get object color R/G/B
524 void ObjectColor(int& theRed, int& theGreen, int& theBlue)
526 if (myAISContext().IsNull())
533 Handle(AIS_InteractiveObject) aCurrent ;
534 Quantity_Color anObjCol;
535 myAISContext()->InitCurrent();
536 if (!myAISContext()->MoreCurrent())
540 aCurrent = myAISContext()->Current();
541 if ( aCurrent->HasColor () )
543 anObjCol = myAISContext()->Color(myAISContext()->Current());
544 Quantity_Parameter r1, r2, r3;
545 anObjCol.Values(r1, r2, r3, Quantity_TOC_RGB);
547 theGreen=(int)r2*255;
553 ///Get object color blue
557 int aRed, aGreen, aBlue;
558 ObjectColor(aRed, aGreen, aBlue);
563 ///Set background color R/G/B
565 void SetBackgroundColor(int theRed, int theGreen, int theBlue)
567 if (!myView().IsNull())
569 myView()->SetBackgroundColor(Quantity_TOC_RGB, theRed/255.,theGreen/255.,theBlue/255.);
576 void EraseObjects(void)
578 if (myAISContext().IsNull())
583 myAISContext()->EraseSelected (Standard_True);
584 myAISContext()->ClearCurrents();
590 float GetOCCVersion(void)
592 return (float)OCC_VERSION;
598 void SetMaterial(int theMaterial)
600 if (myAISContext().IsNull())
604 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent (); myAISContext()->NextCurrent () )
606 myAISContext()->SetMaterial( myAISContext()->Current(), (Graphic3d_NameOfMaterial)theMaterial );
608 myAISContext()->UpdateCurrentViewer();
614 void SetTransparency(int theTrans)
616 if (myAISContext().IsNull())
620 for( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextSelected() )
622 myAISContext()->SetTransparency( myAISContext()->Current(), ((Standard_Real)theTrans) / 10.0 );
627 ///Return true if object is selected
629 bool IsObjectSelected(void)
631 if (myAISContext().IsNull())
635 myAISContext()->InitCurrent();
636 return myAISContext()->MoreCurrent() != Standard_False;
640 ///Return display mode
642 int DisplayMode(void)
644 if (myAISContext().IsNull())
649 bool OneOrMoreInShading = false;
650 bool OneOrMoreInWireframe = false;
651 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
653 if ( myAISContext()->IsDisplayed( myAISContext()->Current(), 1 ) )
655 OneOrMoreInShading = true;
657 if ( myAISContext()->IsDisplayed( myAISContext()->Current(), 0 ) )
659 OneOrMoreInWireframe = true;
662 if (OneOrMoreInShading && OneOrMoreInWireframe)
666 else if(OneOrMoreInShading)
670 else if (OneOrMoreInWireframe)
681 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
682 void CreateNewView(System::IntPtr theWnd)
684 if (myAISContext().IsNull())
688 myView() = myAISContext()->CurrentViewer()->CreateView();
689 if (myGraphicDriver().IsNull())
691 myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
693 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
694 myView()->SetWindow(aWNTWindow);
695 Standard_Integer w=100, h=100;
696 aWNTWindow->Size(w,h);
697 if (!aWNTWindow->IsMapped())
706 bool SetAISContext(OCCTProxy^ theViewer)
708 this->myAISContext() = theViewer->GetContext();
709 if (myAISContext().IsNull())
719 Handle(AIS_InteractiveContext) GetContext(void)
721 return myAISContext();
725 // ============================================
726 // Import / export functionality
727 // ============================================
732 /// <param name="theFileName">Name of import file</param>
733 bool ImportBrep(System::String^ theFileName)
735 bool isResult = false;
736 int aLength = theFileName->Length;
737 char* aFilename = new char[aLength+1];
738 for(int i = 0; i<aLength; i++)
740 aFilename[i] = (char)theFileName->ToCharArray()[i];
742 aFilename[aLength] = '\0';
743 isResult = ImportBrep(aFilename);
750 /// <param name="theFileName">Name of import file</param>
751 bool ImportBrep(char* theFileName)
753 Standard_CString aFileName = (Standard_CString) theFileName;
755 BRep_Builder aBuilder;
756 Standard_Boolean isResult = BRepTools::Read(aShape,aFileName,aBuilder);
761 if(myAISContext()->HasOpenedContext())
763 myAISContext()->CloseLocalContext();
765 myAISContext()->Display(new AIS_Shape(aShape));
772 /// <param name="theFileName">Name of import file</param>
773 bool ImportStep(char* theFileName)
775 Standard_CString aFileName = (Standard_CString) theFileName;
776 STEPControl_Reader aReader;
777 IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileName);
778 if ( aStatus == IFSelect_RetDone )
780 bool isFailsonly = false;
781 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
783 int aNbRoot = aReader.NbRootsForTransfer();
784 aReader.PrintCheckTransfer( isFailsonly, IFSelect_ItemsByEntity );
785 for ( Standard_Integer n = 1; n <= aNbRoot; n++ )
787 Standard_Boolean ok = aReader.TransferRoot( n );
788 int aNbShap = aReader.NbShapes();
791 for ( int i = 1; i <= aNbShap; i++ )
793 TopoDS_Shape aShape = aReader.Shape( i );
794 myAISContext()->Display(new AIS_Shape(aShape));
810 /// <param name="theFileName">Name of import file</param>
811 bool ImportIges(char* theFileName)
813 Standard_CString aFileName = (Standard_CString) theFileName;
814 IGESControl_Reader aReader;
815 int aStatus = aReader.ReadFile( aFileName );
817 if ( aStatus == IFSelect_RetDone )
819 aReader.TransferRoots();
820 TopoDS_Shape aShape = aReader.OneShape();
821 myAISContext()->Display(new AIS_Shape(aShape));
828 myAISContext()->UpdateCurrentViewer();
835 /// <param name="theFileName">Name of export file</param>
836 bool ExportBRep(char* theFileName)
838 myAISContext()->InitCurrent();
839 if (!myAISContext()->MoreCurrent())
844 Handle(AIS_InteractiveObject) anIO = myAISContext()->Current();
845 Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast(anIO);
846 return BRepTools::Write (anIS->Shape(), (Standard_CString)theFileName) != Standard_False;
852 /// <param name="theFileName">Name of export file</param>
853 bool ExportStep(char* theFileName)
855 STEPControl_StepModelType aType = STEPControl_AsIs;
856 IFSelect_ReturnStatus aStatus;
857 STEPControl_Writer aWriter;
858 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
860 Handle(AIS_InteractiveObject) anIO = myAISContext()->Current();
861 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
862 TopoDS_Shape aShape = anIS->Shape();
863 aStatus = aWriter.Transfer( aShape , aType );
864 if ( aStatus != IFSelect_RetDone )
870 aStatus = aWriter.Write( (Standard_CString)theFileName );
871 if ( aStatus != IFSelect_RetDone )
882 /// <param name="theFileName">Name of export file</param>
883 bool ExportIges(char* theFileName)
885 IGESControl_Controller::Init();
886 IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
887 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
889 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
891 Handle(AIS_InteractiveObject) anIO = myAISContext()->Current();
892 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
893 TopoDS_Shape aShape = anIS->Shape();
894 aWriter.AddShape ( aShape );
897 aWriter.ComputeModel();
898 return aWriter.Write( (Standard_CString)theFileName) != Standard_False;
904 /// <param name="theFileName">Name of export file</param>
905 bool ExportVrml(char* theFileName)
907 TopoDS_Compound aRes;
908 BRep_Builder aBuilder;
909 aBuilder.MakeCompound( aRes );
911 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
913 Handle(AIS_InteractiveObject) anIO = myAISContext()->Current();
914 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
915 TopoDS_Shape aShape = anIS->Shape();
916 if ( aShape.IsNull() )
921 aBuilder.Add( aRes, aShape );
924 VrmlAPI_Writer aWriter;
925 aWriter.Write( aRes, (Standard_CString)theFileName );
933 /// <param name="theFileName">Name of export file</param>
934 bool ExportStl(char* theFileName)
936 TopoDS_Compound aComp;
937 BRep_Builder aBuilder;
938 aBuilder.MakeCompound( aComp );
940 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
942 Handle(AIS_InteractiveObject) anIO = myAISContext()->Current();
943 Handle(AIS_Shape) anIS=Handle(AIS_Shape)::DownCast(anIO);
944 TopoDS_Shape aShape = anIS->Shape();
945 if ( aShape.IsNull() )
949 aBuilder.Add( aComp, aShape );
952 StlAPI_Writer aWriter;
953 aWriter.Write( aComp, (Standard_CString)theFileName );
958 ///Define which Import/Export function must be called
960 /// <param name="theFileName">Name of Import/Export file</param>
961 /// <param name="theFormat">Determines format of Import/Export file</param>
962 /// <param name="theIsImport">Determines is Import or not</param>
963 bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
966 int aLength = theFileName->Length;
967 char* aFilename = new char[aLength+1];
968 for(int i = 0; i<aLength; i++)
970 aFilename[i] = (char)theFileName->ToCharArray()[i];
972 aFilename[aLength] = '\0';
979 isResult = ImportBrep(aFilename);
982 isResult = ImportStep(aFilename);
985 isResult = ImportIges(aFilename);
996 isResult = ExportBRep(aFilename);
999 isResult = ExportStep(aFilename);
1002 isResult = ExportIges(aFilename);
1005 isResult = ExportVrml(aFilename);
1008 isResult = ExportStl(aFilename);
1011 isResult = Dump(aFilename);
1021 ///Initialize OCCTProxy
1023 void InitOCCTProxy(void)
1025 myGraphicDriver()=NULL;
1028 myAISContext()=NULL;
1033 NCollection_Haft<Handle(V3d_Viewer)> myViewer;
1034 NCollection_Haft<Handle(V3d_View)> myView;
1035 NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
1036 NCollection_Haft<Handle(OpenGl_GraphicDriver)> myGraphicDriver;