Salome HOME
Copyrights update
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ExportDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_ExportDriver.hxx>
24 #include <GEOMImpl_IImportExport.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <TopoDS_Shape.hxx>
29 #include <TCollection_AsciiString.hxx>
30
31 #include <Standard_ConstructionError.hxx>
32
33 #ifdef WNT
34 #include <windows.h>
35 #else
36 #include <dlfcn.h>
37 #endif
38
39 #ifdef WNT
40 #define LibHandle HMODULE
41 #define LoadLib( name ) LoadLibrary( name )
42 #define GetProc GetProcAddress
43 #define UnLoadLib( handle ) FreeLibrary( handle );
44 #else
45 #define LibHandle void*
46 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
47 #define GetProc dlsym
48 #define UnLoadLib( handle ) dlclose( handle );
49 #endif
50
51 typedef int (*funcPoint)(const TopoDS_Shape&, const TCollection_AsciiString&);
52
53 //=======================================================================
54 //function : GetID
55 //purpose  :
56 //======================================================================= 
57 const Standard_GUID& GEOMImpl_ExportDriver::GetID()
58 {
59   static Standard_GUID aExportDriver("FF1BBB58-5D14-4df2-980B-3A668264EA16");
60   return aExportDriver; 
61 }
62
63
64 //=======================================================================
65 //function : GEOMImpl_ExportDriver
66 //purpose  : 
67 //=======================================================================
68 GEOMImpl_ExportDriver::GEOMImpl_ExportDriver() 
69 {
70 }
71
72 //=======================================================================
73 //function : Execute
74 //purpose  :
75 //======================================================================= 
76 Standard_Integer GEOMImpl_ExportDriver::Execute(TFunction_Logbook& log) const
77 {
78   if (Label().IsNull()) return 0;    
79   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
80
81   GEOMImpl_IImportExport aCI (aFunction);
82
83   // retrieve the being exported shape
84   TopoDS_Shape aShape;
85   Handle(GEOM_Function) aRefFunction = aCI.GetOriginal();
86   if (aRefFunction.IsNull()) return 0;
87   aShape = aRefFunction->GetValue();
88   if (aShape.IsNull()) return 0;
89   // !!! set the result of function to be used by next operations
90   aFunction->SetValue(aShape);
91
92   // retrieve the file and format names
93   TCollection_AsciiString aFileName = aCI.GetFileName();
94   TCollection_AsciiString aLibName  = aCI.GetPluginName();
95   if (aFileName.IsEmpty() || aLibName.IsEmpty())
96     return 0;
97
98   // load plugin library
99   LibHandle anExportLib = LoadLib( aLibName.ToCString() );
100   funcPoint fp = 0;
101   if ( anExportLib )
102     fp = (funcPoint)GetProc( anExportLib, "Export" );
103
104   if ( !fp )
105     return 0;
106
107   // perform the export
108   int res = fp( aShape, aFileName );
109
110   // unload plugin library
111   UnLoadLib( anExportLib );
112   if ( res )
113     log.SetTouched(Label()); 
114
115   return res;
116 }
117
118
119 //=======================================================================
120 //function :  GEOMImpl_ExportDriver_Type_
121 //purpose  :
122 //======================================================================= 
123 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ExportDriver_Type_()
124 {
125
126   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
127   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
128   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
129   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
130   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
131   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
132  
133
134   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
135   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ExportDriver",
136                                                          sizeof(GEOMImpl_ExportDriver),
137                                                          1,
138                                                          (Standard_Address)_Ancestors,
139                                                          (Standard_Address)NULL);
140
141   return _aType;
142 }
143
144 //=======================================================================
145 //function : DownCast
146 //purpose  :
147 //======================================================================= 
148 const Handle(GEOMImpl_ExportDriver) Handle(GEOMImpl_ExportDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
149 {
150   Handle(GEOMImpl_ExportDriver) _anOtherObject;
151
152   if (!AnObject.IsNull()) {
153      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ExportDriver))) {
154        _anOtherObject = Handle(GEOMImpl_ExportDriver)((Handle(GEOMImpl_ExportDriver)&)AnObject);
155      }
156   }
157
158   return _anOtherObject ;
159 }