Salome HOME
Join modifications from V3_2_0_maintainance (V3_2_6pre4 - T32x_16Aug2007_16h00m)
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ImportDriver.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/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_ImportDriver.hxx>
24 #include <GEOMImpl_IImportExport.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include "utilities.h"
29
30 #include <TopoDS_Shape.hxx>
31
32 #include <Standard_ConstructionError.hxx>
33 #include <StdFail_NotDone.hxx>
34
35 #include <NCollection_DataMap.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
59 //=======================================================================
60 //function : GetID
61 //purpose  :
62 //=======================================================================
63 const Standard_GUID& GEOMImpl_ImportDriver::GetID()
64 {
65   static Standard_GUID aImportDriver("FF1BBB60-5D14-4df2-980B-3A668264EA16");
66   return aImportDriver;
67 }
68
69
70 //=======================================================================
71 //function : GEOMImpl_ImportDriver
72 //purpose  :
73 //=======================================================================
74 GEOMImpl_ImportDriver::GEOMImpl_ImportDriver()
75 {
76 }
77
78 //=======================================================================
79 //function : Execute
80 //purpose  :
81 //=======================================================================
82 Standard_Integer GEOMImpl_ImportDriver::Execute(TFunction_Logbook& log) const
83 {
84   if (Label().IsNull()) return 0;
85   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
86
87   GEOMImpl_IImportExport aCI (aFunction);
88   //Standard_Integer aType = aFunction->GetType();
89
90   // retrieve the file and plugin library names
91   TCollection_AsciiString aFileName   = aCI.GetFileName();
92   TCollection_AsciiString aFormatName = aCI.GetFormatName();
93   TCollection_AsciiString aLibName    = aCI.GetPluginName();
94   if (aFileName.IsEmpty() || aFormatName.IsEmpty() || aLibName.IsEmpty())
95     return 0;
96
97   // load plugin library  
98   LibHandle anImportLib = LoadLib( aLibName.ToCString() ); //This is workaround of BUG OCC13051
99   funcPoint fp = 0;
100   if ( anImportLib )
101     fp = (funcPoint)GetProc( anImportLib, "Import" );
102
103   if ( !fp )
104     return 0;
105
106   // perform the import
107   TCollection_AsciiString anError;
108   TopoDS_Shape aShape = fp( aFileName, aFormatName, anError );
109
110   // unload plugin library
111   // commented by enk:
112   // the bug was occured: using ACIS Import/Export plugin
113   //UnLoadLib( anImportLib ); //This is workaround of BUG OCC13051
114
115   if ( aShape.IsNull() ) {
116     StdFail_NotDone::Raise(anError.ToCString());
117     return 0;
118   }
119
120   // set the function result
121   aFunction->SetValue(aShape);
122
123   log.SetTouched(Label());
124
125   return 1;
126 }
127
128
129 //=======================================================================
130 //function :  GEOMImpl_ImportDriver_Type_
131 //purpose  :
132 //=======================================================================
133 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ImportDriver_Type_()
134 {
135
136   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
137   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
138   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
139   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
140   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
141   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
142
143
144   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
145   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ImportDriver",
146                                                          sizeof(GEOMImpl_ImportDriver),
147                                                          1,
148                                                          (Standard_Address)_Ancestors,
149                                                          (Standard_Address)NULL);
150
151   return _aType;
152 }
153
154 //=======================================================================
155 //function : DownCast
156 //purpose  :
157 //=======================================================================
158 const Handle(GEOMImpl_ImportDriver) Handle(GEOMImpl_ImportDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
159 {
160   Handle(GEOMImpl_ImportDriver) _anOtherObject;
161
162   if (!AnObject.IsNull()) {
163      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ImportDriver))) {
164        _anOtherObject = Handle(GEOMImpl_ImportDriver)((Handle(GEOMImpl_ImportDriver)&)AnObject);
165      }
166   }
167
168   return _anOtherObject ;
169 }