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>
19 #include <MgtBRep.hxx>
20 #include <PTColStd_PersistentTransientMap.hxx>
22 #include <FSD_File.hxx>
23 #include <ShapeSchema.hxx>
24 #include <Storage_Data.hxx>
25 #include <Storage_HSeqOfRoot.hxx>
26 #include <Storage_Root.hxx>
28 #include <IGESControl_Reader.hxx>
29 #include <IGESControl_Controller.hxx>
30 #include <IGESControl_Writer.hxx>
31 #include <IFSelect_ReturnStatus.hxx>
32 #include <Interface_Static.hxx>
34 #include <STEPControl_Reader.hxx>
35 #include <STEPControl_Writer.hxx>
37 #include <StlAPI_Writer.hxx>
39 #include <VrmlAPI_Writer.hxx>
40 //wrapper of pure C++ classes to ref classes
41 #include <NCollection_Haft.h>
43 // list of required OCCT libraries
44 #pragma comment(lib, "TKernel.lib")
45 #pragma comment(lib, "TKMath.lib")
46 #pragma comment(lib, "TKBRep.lib")
47 #pragma comment(lib, "PTKernel.lib")
48 #pragma comment(lib, "TKPShape.lib")
49 #pragma comment(lib, "TKShapeSchema.lib")
50 #pragma comment(lib, "TKXSBase.lib")
51 #pragma comment(lib, "TKService.lib")
52 #pragma comment(lib, "TKV3d.lib")
53 #pragma comment(lib, "TKOpenGl.lib")
54 #pragma comment(lib, "TKIGES.lib")
55 #pragma comment(lib, "TKSTEP.lib")
56 #pragma comment(lib, "TKStl.lib")
57 #pragma comment(lib, "TKVrml.lib")
60 /// Proxy class encapsulating calls to OCCT C++ classes within
61 /// C++/CLI class visible from .Net (CSharp)
63 public ref class OCCTProxy
66 // ============================================
67 // Viewer functionality
68 // ============================================
71 ///Initialize a viewer
73 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
74 bool InitViewer(System::IntPtr theWnd)
78 Handle(Aspect_DisplayConnection) aDisplayConnection;
79 myGraphicDriver() = new OpenGl_GraphicDriver (aDisplayConnection);
81 catch (Standard_Failure)
86 TCollection_ExtendedString a3DName("Visu3D");
87 myViewer() = new V3d_Viewer (myGraphicDriver(), a3DName.ToExtString(),"", 1000.0,
88 V3d_XposYnegZpos, Quantity_NOC_GRAY30,
89 V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT,
90 Standard_True, Standard_False);
92 myViewer()->SetDefaultLights();
93 myViewer()->SetLightOn();
94 myView() = myViewer()->CreateView();
95 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
96 myView()->SetWindow(aWNTWindow);
97 if (!aWNTWindow->IsMapped())
101 myAISContext() = new AIS_InteractiveContext( myViewer() );
102 myAISContext()->UpdateCurrentViewer();
104 myView()->MustBeResized();
109 /// Make dump of current view to file
111 /// <param name="theFileName">Name of dump file</param>
112 bool Dump(char *theFileName)
114 if (myView().IsNull())
119 return myView()->Dump(theFileName) != Standard_False;
125 void RedrawView(void)
127 if (!myView().IsNull())
136 void UpdateView(void)
138 if (!myView().IsNull())
140 myView()->MustBeResized();
145 ///Set computed mode in false
147 void SetDegenerateModeOn(void)
149 if (!myView().IsNull())
151 myView()->SetComputedMode (Standard_False);
156 ///Set computed mode in true
158 void SetDegenerateModeOff(void)
160 if (!myView().IsNull())
162 myView()->SetComputedMode (Standard_True);
169 void WindowFitAll(int theXmin, int theYmin, int theXmax, int theYmax)
171 if (!myView().IsNull())
173 myView()->WindowFitAll(theXmin, theYmin, theXmax, theYmax);
178 ///Current place of window
180 /// <param name="theZoomFactor">Current zoom</param>
181 void Place(int theX, int theY, float theZoomFactor)
183 Quantity_Factor aZoomFactor = theZoomFactor;
184 if (!myView().IsNull())
186 myView()->Place(theX, theY, aZoomFactor);
193 void Zoom(int theX1, int theY1, int theX2, int theY2)
195 if (!myView().IsNull())
197 myView()->Zoom(theX1, theY1, theX2, theY2);
204 void Pan(int theX, int theY)
206 if (!myView().IsNull())
208 myView()->Pan(theX, theY);
215 void Rotation(int theX, int theY)
217 if (!myView().IsNull())
219 myView()->Rotation(theX, theY);
226 void StartRotation(int theX, int theY)
228 if (!myView().IsNull())
230 myView()->StartRotation(theX, theY);
235 ///Select by rectangle
237 void Select(int theX1, int theY1, int theX2, int theY2)
239 if (!myAISContext().IsNull())
241 myAISContext()->Select(theX1, theY1, theX2, theY2, myView());
250 if (!myAISContext().IsNull())
252 myAISContext()->Select();
259 void MoveTo(int theX, int theY)
261 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
263 myAISContext()->MoveTo(theX, theY, myView());
268 ///Select by rectangle with pressed "Shift" key
270 void ShiftSelect(int theX1, int theY1, int theX2, int theY2)
272 if ((!myAISContext().IsNull()) && (!myView().IsNull()))
274 myAISContext()->ShiftSelect(theX1, theY1, theX2, theY2, myView());
279 ///Select by "Shift" key
281 void ShiftSelect(void)
283 if (!myAISContext().IsNull())
285 myAISContext()->ShiftSelect();
290 ///Set background color
292 void BackgroundColor(int& theRed, int& theGreen, int& theBlue)
297 if (!myView().IsNull())
299 myView()->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
301 theRed = (int)R1*255;
302 theGreen = (int)G1*255;
303 theBlue = (int)B1*255;
307 ///Get background color Red
311 int aRed, aGreen, aBlue;
312 BackgroundColor(aRed, aGreen, aBlue);
317 ///Get background color Green
321 int aRed, aGreen, aBlue;
322 BackgroundColor(aRed, aGreen, aBlue);
327 ///Get background color Blue
331 int aRed, aGreen, aBlue;
332 BackgroundColor(aRed, aGreen, aBlue);
337 ///Update current viewer
339 void UpdateCurrentViewer(void)
341 if (!myAISContext().IsNull())
343 myAISContext()->UpdateCurrentViewer();
352 if (!myView().IsNull())
354 myView()->SetProj(V3d_Xpos);
363 if (!myView().IsNull())
365 myView()->SetProj(V3d_Zpos);
374 if (!myView().IsNull())
376 myView()->SetProj(V3d_Ypos);
385 if (!myView().IsNull())
387 myView()->SetProj(V3d_Xneg);
396 if (!myView().IsNull())
398 myView()->SetProj(V3d_Yneg);
405 void BottomView(void)
407 if (!myView().IsNull())
409 myView()->SetProj(V3d_Zneg);
418 if (!myView().IsNull())
420 myView()->SetProj(V3d_XposYnegZpos);
429 if (myView().IsNull())
435 return (float)myView()->Scale();
442 void ZoomAllView(void)
444 if (!myView().IsNull())
456 if (!myView().IsNull())
463 ///Set display mode of objects
465 /// <param name="theMode">Set current mode</param>
466 void SetDisplayMode(int theMode)
468 if (myAISContext().IsNull())
472 AIS_DisplayMode aCurrentMode;
475 aCurrentMode=AIS_WireFrame;
479 aCurrentMode=AIS_Shaded;
482 if(myAISContext()->NbCurrents()==0 || myAISContext()->NbSelected()==0)
484 myAISContext()->SetDisplayMode(aCurrentMode);
488 for(myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
490 myAISContext()->SetDisplayMode(myAISContext()->Current(), theMode, Standard_False);
493 myAISContext()->UpdateCurrentViewer();
499 void SetColor(int theR, int theG, int theB)
501 if (myAISContext().IsNull())
505 Quantity_Color aCol = Quantity_Color(theR/255.,theG/255.,theB/255.,Quantity_TOC_RGB);
506 for (;myAISContext()->MoreCurrent ();myAISContext()->NextCurrent ())
508 myAISContext()->SetColor (myAISContext()->Current(),aCol.Name());
513 ///Get object color red
517 int aRed, aGreen, aBlue;
518 ObjectColor(aRed, aGreen, aBlue);
523 ///Get object color green
527 int aRed, aGreen, aBlue;
528 ObjectColor(aRed, aGreen, aBlue);
533 ///Get object color R/G/B
535 void ObjectColor(int& theRed, int& theGreen, int& theBlue)
537 if (myAISContext().IsNull())
544 Handle_AIS_InteractiveObject aCurrent ;
545 Quantity_Color anObjCol;
546 myAISContext()->InitCurrent();
547 if (!myAISContext()->MoreCurrent())
551 aCurrent = myAISContext()->Current();
552 if ( aCurrent->HasColor () )
554 anObjCol = myAISContext()->Color(myAISContext()->Current());
555 Quantity_Parameter r1, r2, r3;
556 anObjCol.Values(r1, r2, r3, Quantity_TOC_RGB);
558 theGreen=(int)r2*255;
564 ///Get object color blue
568 int aRed, aGreen, aBlue;
569 ObjectColor(aRed, aGreen, aBlue);
574 ///Set background color R/G/B
576 void SetBackgroundColor(int theRed, int theGreen, int theBlue)
578 if (!myView().IsNull())
580 myView()->SetBackgroundColor(Quantity_TOC_RGB, theRed/255.,theGreen/255.,theBlue/255.);
587 void EraseObjects(void)
589 if (myAISContext().IsNull())
593 for(myAISContext()->InitCurrent();myAISContext()->MoreCurrent();myAISContext()->NextCurrent())
595 myAISContext()->Erase(myAISContext()->Current(),Standard_True);
597 myAISContext()->ClearCurrents();
603 float GetOCCVersion(void)
605 return (float)OCC_VERSION;
611 void SetMaterial(int theMaterial)
613 if (myAISContext().IsNull())
617 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent (); myAISContext()->NextCurrent () )
619 myAISContext()->SetMaterial( myAISContext()->Current(), (Graphic3d_NameOfMaterial)theMaterial );
621 myAISContext()->UpdateCurrentViewer();
627 void SetTransparency(int theTrans)
629 if (myAISContext().IsNull())
633 for( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextSelected() )
635 myAISContext()->SetTransparency( myAISContext()->Current(), ((Standard_Real)theTrans) / 10.0 );
640 ///Return true if object is selected
642 bool IsObjectSelected(void)
644 if (myAISContext().IsNull())
648 myAISContext()->InitCurrent();
649 return myAISContext()->MoreCurrent() != Standard_False;
653 ///Return display mode
655 int DisplayMode(void)
657 if (myAISContext().IsNull())
662 bool OneOrMoreInShading = false;
663 bool OneOrMoreInWireframe = false;
664 for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
666 if ( myAISContext()->IsDisplayed( myAISContext()->Current(), 1 ) )
668 OneOrMoreInShading = true;
670 if ( myAISContext()->IsDisplayed( myAISContext()->Current(), 0 ) )
672 OneOrMoreInWireframe = true;
675 if (OneOrMoreInShading && OneOrMoreInWireframe)
679 else if(OneOrMoreInShading)
683 else if (OneOrMoreInWireframe)
694 /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
695 void CreateNewView(System::IntPtr theWnd)
697 if (myAISContext().IsNull())
701 myView() = myAISContext()->CurrentViewer()->CreateView();
702 if (myGraphicDriver().IsNull())
704 myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
706 Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
707 myView()->SetWindow(aWNTWindow);
708 Standard_Integer w=100, h=100;
709 aWNTWindow->Size(w,h);
710 if (!aWNTWindow->IsMapped())
719 bool SetAISContext(OCCTProxy^ theViewer)
721 this->myAISContext() = theViewer->GetContext();
722 if (myAISContext().IsNull())
732 Handle_AIS_InteractiveContext GetContext(void)
734 return myAISContext();
738 // ============================================
739 // Import / export functionality
740 // ============================================
745 /// <param name="theFileName">Name of import file</param>
746 bool ImportBrep(System::String^ theFileName)
748 bool isResult = false;
749 int aLength = theFileName->Length;
750 char* aFilename = new char[aLength+1];
751 for(int i = 0; i<aLength; i++)
753 aFilename[i] = (char)theFileName->ToCharArray()[i];
755 aFilename[aLength] = '\0';
756 isResult = ImportBrep(aFilename);
763 /// <param name="theFileName">Name of import file</param>
764 bool ImportBrep(char* theFileName)
766 Standard_CString aFileName = (Standard_CString) theFileName;
768 BRep_Builder aBuilder;
769 Standard_Boolean isResult = BRepTools::Read(aShape,aFileName,aBuilder);
774 if(myAISContext()->HasOpenedContext())
776 myAISContext()->CloseLocalContext();
778 myAISContext()->Display(new AIS_Shape(aShape));
785 /// <param name="theFileName">Name of import file</param>
786 bool ImportCsfdb(char* theFileName)
788 Standard_CString aFileName = (Standard_CString) theFileName;
789 if ( FSD_File::IsGoodFileType(aFileName) != Storage_VSOk )
794 FSD_File aFileDriver;
795 TCollection_AsciiString aName( aFileName );
796 if ( aFileDriver.Open( aName, Storage_VSRead ) != Storage_VSOk )
801 Handle(ShapeSchema) aSchema = new ShapeSchema();
802 Handle(Storage_Data) data = aSchema->Read( aFileDriver );
803 if ( data->ErrorStatus() != Storage_VSOk )
809 Handle(Storage_HSeqOfRoot) aRoots = data->Roots();
810 for ( int i = 1; i <= aRoots->Length() ; i++ )
812 Handle(Storage_Root) aStorRoot = aRoots->Value( i );
813 Handle(Standard_Persistent) aStandPersistent = aStorRoot->Object();
814 Handle(PTopoDS_HShape) aPShape = Handle(PTopoDS_HShape)::DownCast(aStandPersistent);
815 if ( !aPShape.IsNull() )
817 PTColStd_PersistentTransientMap aMap;
818 TopoDS_Shape aTShape;
819 MgtBRep::Translate( aPShape, aMap, aTShape, MgtBRep_WithTriangle );
820 myAISContext()->Display(new AIS_Shape(aTShape));
830 /// <param name="theFileName">Name of import file</param>
831 bool ImportStep(char* theFileName)
833 Standard_CString aFileName = (Standard_CString) theFileName;
834 STEPControl_Reader aReader;
835 IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileName);
836 if ( aStatus == IFSelect_RetDone )
838 bool isFailsonly = false;
839 aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity );
841 int aNbRoot = aReader.NbRootsForTransfer();
842 aReader.PrintCheckTransfer( isFailsonly, IFSelect_ItemsByEntity );
843 for ( Standard_Integer n = 1; n <= aNbRoot; n++ )
845 Standard_Boolean ok = aReader.TransferRoot( n );
846 int aNbShap = aReader.NbShapes();
849 for ( int i = 1; i <= aNbShap; i++ )
851 TopoDS_Shape aShape = aReader.Shape( i );
852 myAISContext()->Display(new AIS_Shape(aShape));
868 /// <param name="theFileName">Name of import file</param>
869 bool ImportIges(char* theFileName)
871 Standard_CString aFileName = (Standard_CString) theFileName;
872 IGESControl_Reader aReader;
873 int aStatus = aReader.ReadFile( aFileName );
875 if ( aStatus == IFSelect_RetDone )
877 aReader.TransferRoots();
878 TopoDS_Shape aShape = aReader.OneShape();
879 myAISContext()->Display(new AIS_Shape(aShape));
886 myAISContext()->UpdateCurrentViewer();
893 /// <param name="theFileName">Name of export file</param>
894 bool ExportBRep(char* theFileName)
896 myAISContext()->InitCurrent();
897 if (!myAISContext()->MoreCurrent())
902 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
903 Handle_AIS_Shape anIS = Handle_AIS_Shape::DownCast(anIO);
904 return BRepTools::Write (anIS->Shape(), (Standard_CString)theFileName) != Standard_False;
910 /// <param name="theFileName">Name of export file</param>
911 bool ExportStep(char* theFileName)
913 STEPControl_StepModelType aType = STEPControl_AsIs;
914 IFSelect_ReturnStatus aStatus;
915 STEPControl_Writer aWriter;
916 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
918 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
919 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
920 TopoDS_Shape aShape = anIS->Shape();
921 aStatus = aWriter.Transfer( aShape , aType );
922 if ( aStatus != IFSelect_RetDone )
928 aStatus = aWriter.Write( (Standard_CString)theFileName );
929 if ( aStatus != IFSelect_RetDone )
940 /// <param name="theFileName">Name of export file</param>
941 bool ExportIges(char* theFileName)
943 IGESControl_Controller::Init();
944 IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
945 Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
947 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
949 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
950 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
951 TopoDS_Shape aShape = anIS->Shape();
952 aWriter.AddShape ( aShape );
955 aWriter.ComputeModel();
956 return aWriter.Write( (Standard_CString)theFileName) != Standard_False;
962 /// <param name="theFileName">Name of export file</param>
963 bool ExportVrml(char* theFileName)
965 TopoDS_Compound aRes;
966 BRep_Builder aBuilder;
967 aBuilder.MakeCompound( aRes );
969 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
971 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
972 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
973 TopoDS_Shape aShape = anIS->Shape();
974 if ( aShape.IsNull() )
979 aBuilder.Add( aRes, aShape );
982 VrmlAPI_Writer aWriter;
983 aWriter.Write( aRes, (Standard_CString)theFileName );
991 /// <param name="theFileName">Name of export file</param>
992 bool ExportStl(char* theFileName)
994 TopoDS_Compound aComp;
995 BRep_Builder aBuilder;
996 aBuilder.MakeCompound( aComp );
998 for ( myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent() )
1000 Handle_AIS_InteractiveObject anIO = myAISContext()->Current();
1001 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
1002 TopoDS_Shape aShape = anIS->Shape();
1003 if ( aShape.IsNull() )
1007 aBuilder.Add( aComp, aShape );
1010 StlAPI_Writer aWriter;
1011 aWriter.Write( aComp, (Standard_CString)theFileName );
1016 ///Define which Import/Export function must be called
1018 /// <param name="theFileName">Name of Import/Export file</param>
1019 /// <param name="theFormat">Determines format of Import/Export file</param>
1020 /// <param name="theIsImport">Determines is Import or not</param>
1021 bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
1024 int aLength = theFileName->Length;
1025 char* aFilename = new char[aLength+1];
1026 for(int i = 0; i<aLength; i++)
1028 aFilename[i] = (char)theFileName->ToCharArray()[i];
1030 aFilename[aLength] = '\0';
1037 isResult = ImportBrep(aFilename);
1040 isResult = ImportCsfdb(aFilename);
1043 isResult = ImportStep(aFilename);
1046 isResult = ImportIges(aFilename);
1057 isResult = ExportBRep(aFilename);
1060 isResult = ExportStep(aFilename);
1063 isResult = ExportIges(aFilename);
1066 isResult = ExportVrml(aFilename);
1069 isResult = ExportStl(aFilename);
1072 isResult = Dump(aFilename);
1082 ///Initialize OCCTProxy
1084 void InitOCCTProxy(void)
1086 myGraphicDriver()=NULL;
1089 myAISContext()=NULL;
1094 NCollection_Haft<Handle_V3d_Viewer> myViewer;
1095 NCollection_Haft<Handle_V3d_View> myView;
1096 NCollection_Haft<Handle_AIS_InteractiveContext> myAISContext;
1097 NCollection_Haft<Handle_OpenGl_GraphicDriver> myGraphicDriver;