Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ImportDriver.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_ImportDriver.hxx>
26 #include <GEOMImpl_IImportExport.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <TopoDS_Shape.hxx>
31
32 #include <TCollection_HAsciiString.hxx>
33
34 #include "utilities.h"
35
36 #include <Standard_Failure.hxx>
37 #include <StdFail_NotDone.hxx>
38
39 #ifdef WNT
40 #include <windows.h>
41 #else
42 #include <dlfcn.h>
43 #endif
44
45 #ifdef WNT
46 #define LibHandle HMODULE
47 #define LoadLib( name ) LoadLibrary( name )
48 #define GetProc GetProcAddress
49 #define UnLoadLib( handle ) FreeLibrary( handle );
50 #else
51 #define LibHandle void*
52 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
53 #define GetProc dlsym
54 #define UnLoadLib( handle ) dlclose( handle );
55 #endif
56
57 typedef TopoDS_Shape (*funcPoint)(const TCollection_AsciiString&,
58                                   const TCollection_AsciiString&,
59                                   TCollection_AsciiString&,
60                                   const TDF_Label&);
61
62 typedef Handle(TCollection_HAsciiString) (*pGetValue)(const TCollection_AsciiString&,
63                                                       const TCollection_AsciiString&,
64                                                       TCollection_AsciiString&);
65
66 //=======================================================================
67 //function : GetID
68 //purpose  :
69 //=======================================================================
70 const Standard_GUID& GEOMImpl_ImportDriver::GetID()
71 {
72   static Standard_GUID aImportDriver("FF1BBB60-5D14-4df2-980B-3A668264EA16");
73   return aImportDriver;
74 }
75
76 //=======================================================================
77 //function : GEOMImpl_ImportDriver
78 //purpose  :
79 //=======================================================================
80 GEOMImpl_ImportDriver::GEOMImpl_ImportDriver()
81 {
82 }
83
84 //=======================================================================
85 //function : Execute
86 //purpose  :
87 //=======================================================================
88 Standard_Integer GEOMImpl_ImportDriver::Execute(TFunction_Logbook& log) const
89 {
90   if (Label().IsNull()) return 0;
91   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
92
93   GEOMImpl_IImportExport aCI (aFunction);
94   //Standard_Integer aType = aFunction->GetType();
95
96   // retrieve the file and plugin library names
97   TCollection_AsciiString aFileName   = aCI.GetFileName();
98   TCollection_AsciiString aFormatName = aCI.GetFormatName();
99   TCollection_AsciiString aLibName    = aCI.GetPluginName();
100   if (aFileName.IsEmpty() || aFormatName.IsEmpty() || aLibName.IsEmpty())
101     return 0;
102
103   // load plugin library
104   LibHandle anImportLib = LoadLib( aLibName.ToCString() ); //This is workaround of BUG OCC13051
105
106   // Get Import method
107   funcPoint fp = 0;
108   if ( anImportLib )
109     fp = (funcPoint)GetProc( anImportLib, "Import" );
110
111   if ( !fp ) {
112     TCollection_AsciiString aMsg = aFormatName.SubString(1,4);
113     aMsg += " plugin was not installed";
114     Standard_Failure::Raise(aMsg.ToCString());
115   }
116
117   // perform the import
118   TCollection_AsciiString anError;
119   TopoDS_Shape aShape = fp(aFileName, aFormatName, anError, aFunction->GetNamingEntry());
120
121   // unload plugin library
122   // commented by enk:
123   // the bug was occured: using ACIS Import/Export plugin
124   //UnLoadLib( anImportLib ); //This is workaround of BUG OCC13051
125
126   if ( aShape.IsNull() ) {
127     StdFail_NotDone::Raise(anError.ToCString());
128     return 0;
129   }
130
131   // set the function result
132   aFunction->SetValue(aShape);
133
134   log.SetTouched(Label());
135
136   return 1;
137 }
138
139 //=======================================================================
140 //function : ReadValue
141 //purpose  :
142 //=======================================================================
143 TCollection_AsciiString GEOMImpl_ImportDriver::ReadValue(const TCollection_AsciiString& theFileName,
144                                                          const TCollection_AsciiString& theLibName,
145                                                          const TCollection_AsciiString& theParameterName,
146                                                          TCollection_AsciiString& theError)
147 {
148   TCollection_AsciiString aValue;
149
150   if (theFileName.IsEmpty() || theLibName.IsEmpty() || theParameterName.IsEmpty())
151     return aValue;
152
153   // load plugin library
154   LibHandle anImportLib = LoadLib(theLibName.ToCString()); //This is workaround of BUG OCC13051
155   if (!anImportLib) {
156     theError = theLibName + " library was not installed";
157     return aValue;
158   }
159
160   // Get GetValue method
161   pGetValue pGV = (pGetValue)GetProc(anImportLib, "GetValue");
162
163   if (!pGV) {
164     theError = theLibName + " library doesn't support GetValue method";
165     return aValue;
166   }
167
168   Handle(TCollection_HAsciiString) aHValue = pGV(theFileName, theParameterName, theError);
169
170   if (aHValue.IsNull()) {
171     if (theError.IsEmpty())
172       theError = theFileName + " doesn't contain requested parameter";
173     return aValue;
174   }
175
176   aValue = aHValue->String();
177
178   // unload plugin library
179   // commented by enk:
180   // the bug was occured: using ACIS Import/Export plugin
181   //UnLoadLib( anImportLib ); //This is workaround of BUG OCC13051
182
183   return aValue;
184 }
185
186
187 //=======================================================================
188 //function :  GEOMImpl_ImportDriver_Type_
189 //purpose  :
190 //=======================================================================
191 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ImportDriver_Type_()
192 {
193
194   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
195   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
196   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
197   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
198   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
199   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
200
201
202   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
203   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ImportDriver",
204                                                          sizeof(GEOMImpl_ImportDriver),
205                                                          1,
206                                                          (Standard_Address)_Ancestors,
207                                                          (Standard_Address)NULL);
208
209   return _aType;
210 }
211
212 //=======================================================================
213 //function : DownCast
214 //purpose  :
215 //=======================================================================
216 const Handle(GEOMImpl_ImportDriver) Handle(GEOMImpl_ImportDriver)::DownCast
217                                (const Handle(Standard_Transient)& AnObject)
218 {
219   Handle(GEOMImpl_ImportDriver) _anOtherObject;
220
221   if (!AnObject.IsNull()) {
222      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ImportDriver))) {
223        _anOtherObject = Handle(GEOMImpl_ImportDriver)((Handle(GEOMImpl_ImportDriver)&)AnObject);
224      }
225   }
226
227   return _anOtherObject;
228 }