Salome HOME
Mantis issue 0021453: EDF GEOM: (regression). A fix by PKV.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ImportDriver.cxx
1 // Copyright (C) 2007-2011  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 "utilities.h"
31
32 #include <TopoDS_Shape.hxx>
33
34 #include <Standard_Failure.hxx>
35 #include <StdFail_NotDone.hxx>
36
37 #ifdef WNT
38 #include <windows.h>
39 #else
40 #include <dlfcn.h>
41 #endif
42
43 #ifdef WNT
44 #define LibHandle HMODULE
45 #define LoadLib( name ) LoadLibrary( name )
46 #define GetProc GetProcAddress
47 #define UnLoadLib( handle ) FreeLibrary( handle );
48 #else
49 #define LibHandle void*
50 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
51 #define GetProc dlsym
52 #define UnLoadLib( handle ) dlclose( handle );
53 #endif
54
55 typedef TopoDS_Shape (*funcPoint)(const TCollection_AsciiString&,
56                                   const TCollection_AsciiString&,
57                                   TCollection_AsciiString&,
58                                   const TDF_Label&);
59
60 //=======================================================================
61 //function : GetID
62 //purpose  :
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_ImportDriver::GetID()
65 {
66   static Standard_GUID aImportDriver("FF1BBB60-5D14-4df2-980B-3A668264EA16");
67   return aImportDriver;
68 }
69
70
71 //=======================================================================
72 //function : GEOMImpl_ImportDriver
73 //purpose  :
74 //=======================================================================
75 GEOMImpl_ImportDriver::GEOMImpl_ImportDriver()
76 {
77 }
78
79 //=======================================================================
80 //function : Execute
81 //purpose  :
82 //=======================================================================
83 Standard_Integer GEOMImpl_ImportDriver::Execute(TFunction_Logbook& log) const
84 {
85   if (Label().IsNull()) return 0;
86   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
87
88   GEOMImpl_IImportExport aCI (aFunction);
89   //Standard_Integer aType = aFunction->GetType();
90
91   // retrieve the file and plugin library names
92   TCollection_AsciiString aFileName   = aCI.GetFileName();
93   TCollection_AsciiString aFormatName = aCI.GetFormatName();
94   TCollection_AsciiString aLibName    = aCI.GetPluginName();
95   if (aFileName.IsEmpty() || aFormatName.IsEmpty() || aLibName.IsEmpty())
96     return 0;
97
98   // load plugin library
99   LibHandle anImportLib = LoadLib( aLibName.ToCString() ); //This is workaround of BUG OCC13051
100   funcPoint fp = 0;
101   if ( anImportLib )
102     fp = (funcPoint)GetProc( anImportLib, "Import" );
103
104   if ( !fp ) {
105     TCollection_AsciiString aMsg = aFormatName.SubString(1,4);
106     aMsg += " plugin was not installed";
107     Standard_Failure::Raise(aMsg.ToCString());
108   }
109
110   // perform the import
111   TCollection_AsciiString anError;
112   TopoDS_Shape aShape = fp(aFileName, aFormatName, anError, aFunction->GetNamingEntry());
113
114   // unload plugin library
115   // commented by enk:
116   // the bug was occured: using ACIS Import/Export plugin
117   //UnLoadLib( anImportLib ); //This is workaround of BUG OCC13051
118
119   if ( aShape.IsNull() ) {
120     StdFail_NotDone::Raise(anError.ToCString());
121     return 0;
122   }
123
124   // set the function result
125   aFunction->SetValue(aShape);
126
127   log.SetTouched(Label());
128
129   return 1;
130 }
131
132
133 //=======================================================================
134 //function :  GEOMImpl_ImportDriver_Type_
135 //purpose  :
136 //=======================================================================
137 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ImportDriver_Type_()
138 {
139
140   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
141   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
142   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
143   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
144   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
145   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
146
147
148   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
149   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ImportDriver",
150                                                          sizeof(GEOMImpl_ImportDriver),
151                                                          1,
152                                                          (Standard_Address)_Ancestors,
153                                                          (Standard_Address)NULL);
154
155   return _aType;
156 }
157
158 //=======================================================================
159 //function : DownCast
160 //purpose  :
161 //=======================================================================
162 const Handle(GEOMImpl_ImportDriver) Handle(GEOMImpl_ImportDriver)::DownCast
163                                (const Handle(Standard_Transient)& AnObject)
164 {
165   Handle(GEOMImpl_ImportDriver) _anOtherObject;
166
167   if (!AnObject.IsNull()) {
168      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ImportDriver))) {
169        _anOtherObject = Handle(GEOMImpl_ImportDriver)((Handle(GEOMImpl_ImportDriver)&)AnObject);
170      }
171   }
172
173   return _anOtherObject;
174 }