Salome HOME
Merge from V6_main (04/10/2012)
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ImportDriver.cxx
1 // Copyright (C) 2007-2012  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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_ImportDriver.hxx>
25 #include <GEOMImpl_IImportExport.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <TopoDS_Shape.hxx>
30
31 #include <TCollection_HAsciiString.hxx>
32
33 #include "utilities.h"
34
35 #include <Standard_Failure.hxx>
36 #include <StdFail_NotDone.hxx>
37
38 #ifdef WNT
39 #include <windows.h>
40 #else
41 #include <dlfcn.h>
42 #endif
43
44 #ifdef WNT
45 #define LibHandle HMODULE
46 #define LoadLib( name ) LoadLibrary( name )
47 #define GetProc GetProcAddress
48 #define UnLoadLib( handle ) FreeLibrary( handle );
49 #else
50 #define LibHandle void*
51 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
52 #define GetProc dlsym
53 #define UnLoadLib( handle ) dlclose( handle );
54 #endif
55
56 typedef TopoDS_Shape (*funcPoint)(const TCollection_AsciiString&,
57                                   const TCollection_AsciiString&,
58                                   TCollection_AsciiString&,
59                                   const TDF_Label&);
60
61 typedef Handle(TCollection_HAsciiString) (*pGetValue)(const TCollection_AsciiString&,
62                                                       const TCollection_AsciiString&,
63                                                       TCollection_AsciiString&);
64
65 //=======================================================================
66 //function : GetID
67 //purpose  :
68 //=======================================================================
69 const Standard_GUID& GEOMImpl_ImportDriver::GetID()
70 {
71   static Standard_GUID aImportDriver("FF1BBB60-5D14-4df2-980B-3A668264EA16");
72   return aImportDriver;
73 }
74
75 //=======================================================================
76 //function : GEOMImpl_ImportDriver
77 //purpose  :
78 //=======================================================================
79 GEOMImpl_ImportDriver::GEOMImpl_ImportDriver()
80 {
81 }
82
83 //=======================================================================
84 //function : Execute
85 //purpose  :
86 //=======================================================================
87 Standard_Integer GEOMImpl_ImportDriver::Execute(TFunction_Logbook& log) const
88 {
89   if (Label().IsNull()) return 0;
90   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
91
92   GEOMImpl_IImportExport aCI (aFunction);
93   //Standard_Integer aType = aFunction->GetType();
94
95   // retrieve the file and plugin library names
96   TCollection_AsciiString aFileName   = aCI.GetFileName();
97   TCollection_AsciiString aFormatName = aCI.GetFormatName();
98   TCollection_AsciiString aLibName    = aCI.GetPluginName();
99   if (aFileName.IsEmpty() || aFormatName.IsEmpty() || aLibName.IsEmpty())
100     return 0;
101
102   // load plugin library
103   LibHandle anImportLib = LoadLib( aLibName.ToCString() ); //This is workaround of BUG OCC13051
104
105   // Get Import method
106   funcPoint fp = 0;
107   if ( anImportLib )
108     fp = (funcPoint)GetProc( anImportLib, "Import" );
109
110   if ( !fp ) {
111     TCollection_AsciiString aMsg = aFormatName.SubString(1,4);
112     aMsg += " plugin was not installed";
113     Standard_Failure::Raise(aMsg.ToCString());
114   }
115
116   // perform the import
117   TCollection_AsciiString anError;
118   TopoDS_Shape aShape = fp(aFileName, aFormatName, anError, aFunction->GetNamingEntry());
119
120   // unload plugin library
121   // commented by enk:
122   // the bug was occured: using ACIS Import/Export plugin
123   //UnLoadLib( anImportLib ); //This is workaround of BUG OCC13051
124
125   if ( aShape.IsNull() ) {
126     StdFail_NotDone::Raise(anError.ToCString());
127     return 0;
128   }
129
130   // set the function result
131   aFunction->SetValue(aShape);
132
133   log.SetTouched(Label());
134
135   return 1;
136 }
137
138 //=======================================================================
139 //function : ReadValue
140 //purpose  :
141 //=======================================================================
142 TCollection_AsciiString GEOMImpl_ImportDriver::ReadValue(const TCollection_AsciiString& theFileName,
143                                                          const TCollection_AsciiString& theLibName,
144                                                          const TCollection_AsciiString& theParameterName,
145                                                          TCollection_AsciiString& theError)
146 {
147   TCollection_AsciiString aValue;
148
149   if (theFileName.IsEmpty() || theLibName.IsEmpty() || theParameterName.IsEmpty())
150     return aValue;
151
152   // load plugin library
153   LibHandle anImportLib = LoadLib(theLibName.ToCString()); //This is workaround of BUG OCC13051
154   if (!anImportLib) {
155     theError = theLibName + " library was not installed";
156     return aValue;
157   }
158
159   // Get GetValue method
160   pGetValue pGV = (pGetValue)GetProc(anImportLib, "GetValue");
161
162   if (!pGV) {
163     theError = theLibName + " library doesn't support GetValue method";
164     return aValue;
165   }
166
167   Handle(TCollection_HAsciiString) aHValue = pGV(theFileName, theParameterName, theError);
168
169   if (aHValue.IsNull()) {
170     if (theError.IsEmpty())
171       theError = theFileName + " doesn't contain requested parameter";
172     return aValue;
173   }
174
175   aValue = aHValue->String();
176
177   // unload plugin library
178   // commented by enk:
179   // the bug was occured: using ACIS Import/Export plugin
180   //UnLoadLib( anImportLib ); //This is workaround of BUG OCC13051
181
182   return aValue;
183 }
184
185
186 //=======================================================================
187 //function :  GEOMImpl_ImportDriver_Type_
188 //purpose  :
189 //=======================================================================
190 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ImportDriver_Type_()
191 {
192
193   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
194   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
195   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
196   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
197   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
198   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
199
200
201   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
202   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ImportDriver",
203                                                          sizeof(GEOMImpl_ImportDriver),
204                                                          1,
205                                                          (Standard_Address)_Ancestors,
206                                                          (Standard_Address)NULL);
207
208   return _aType;
209 }
210
211 //=======================================================================
212 //function : DownCast
213 //purpose  :
214 //=======================================================================
215 const Handle(GEOMImpl_ImportDriver) Handle(GEOMImpl_ImportDriver)::DownCast
216                                (const Handle(Standard_Transient)& AnObject)
217 {
218   Handle(GEOMImpl_ImportDriver) _anOtherObject;
219
220   if (!AnObject.IsNull()) {
221      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ImportDriver))) {
222        _anOtherObject = Handle(GEOMImpl_ImportDriver)((Handle(GEOMImpl_ImportDriver)&)AnObject);
223      }
224   }
225
226   return _anOtherObject;
227 }