Salome HOME
21f8c4639bf2854510e3efa5dceab858fc59e22e
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ExportDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_ExportDriver.hxx"
4 #include "GEOMImpl_IImportExport.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include <TopoDS_Shape.hxx>
9 #include <TCollection_AsciiString.hxx>
10
11 #include <Standard_ConstructionError.hxx>
12
13 #ifdef WNT
14 #include <windows.h>
15 #else
16 #include <dlfcn.h>
17 #endif
18
19 #ifdef WNT
20 #define LibHandle HMODULE
21 #define LoadLib( name ) LoadLibrary( name )
22 #define GetProc GetProcAddress
23 #define UnLoadLib( handle ) FreeLibrary( handle );
24 #else
25 #define LibHandle void*
26 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
27 #define GetProc dlsym
28 #define UnLoadLib( handle ) dlclose( handle );
29 #endif
30
31 typedef int (*funcPoint)(const TopoDS_Shape&, const TCollection_AsciiString&);
32
33 //=======================================================================
34 //function : GetID
35 //purpose  :
36 //======================================================================= 
37 const Standard_GUID& GEOMImpl_ExportDriver::GetID()
38 {
39   static Standard_GUID aExportDriver("FF1BBB58-5D14-4df2-980B-3A668264EA16");
40   return aExportDriver; 
41 }
42
43
44 //=======================================================================
45 //function : GEOMImpl_ExportDriver
46 //purpose  : 
47 //=======================================================================
48 GEOMImpl_ExportDriver::GEOMImpl_ExportDriver() 
49 {
50 }
51
52 //=======================================================================
53 //function : Execute
54 //purpose  :
55 //======================================================================= 
56 Standard_Integer GEOMImpl_ExportDriver::Execute(TFunction_Logbook& log) const
57 {
58   if (Label().IsNull()) return 0;    
59   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
60
61   GEOMImpl_IImportExport aCI (aFunction);
62
63   // retrieve the being exported shape
64   TopoDS_Shape aShape;
65   Handle(GEOM_Function) aRefFunction = aCI.GetOriginal();
66   if (aRefFunction.IsNull()) return 0;
67   aShape = aRefFunction->GetValue();
68   if (aShape.IsNull()) return 0;
69   // !!! set the result of function to be used by next operations
70   aFunction->SetValue(aShape);
71
72   // retrieve the file and format names
73   TCollection_AsciiString aFileName = aCI.GetFileName();
74   TCollection_AsciiString aLibName  = aCI.GetPluginName();
75   if (aFileName.IsEmpty() || aLibName.IsEmpty())
76     return 0;
77
78   // load plugin library
79   LibHandle anExportLib = LoadLib( aLibName.ToCString() );
80   funcPoint fp = 0;
81   if ( anExportLib )
82     fp = (funcPoint)GetProc( anExportLib, "Export" );
83
84   if ( !fp )
85     return 0;
86
87   // perform the export
88   int res = fp( aShape, aFileName );
89
90   // unload plugin library
91   UnLoadLib( anExportLib );
92   if ( res )
93     log.SetTouched(Label()); 
94
95   return res;
96 }
97
98
99 //=======================================================================
100 //function :  GEOMImpl_ExportDriver_Type_
101 //purpose  :
102 //======================================================================= 
103 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ExportDriver_Type_()
104 {
105
106   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
107   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
108   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
109   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
110   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
111   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
112  
113
114   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
115   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ExportDriver",
116                                                          sizeof(GEOMImpl_ExportDriver),
117                                                          1,
118                                                          (Standard_Address)_Ancestors,
119                                                          (Standard_Address)NULL);
120
121   return _aType;
122 }
123
124 //=======================================================================
125 //function : DownCast
126 //purpose  :
127 //======================================================================= 
128 const Handle(GEOMImpl_ExportDriver) Handle(GEOMImpl_ExportDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
129 {
130   Handle(GEOMImpl_ExportDriver) _anOtherObject;
131
132   if (!AnObject.IsNull()) {
133      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ExportDriver))) {
134        _anOtherObject = Handle(GEOMImpl_ExportDriver)((Handle(GEOMImpl_ExportDriver)&)AnObject);
135      }
136   }
137
138   return _anOtherObject ;
139 }