Salome HOME
f28f5ea9d37b380eefbf871ba495e8ba800c7b53
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ImportDriver.cxx
1 //  Copyright (C) 2007-2010  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 "utilities.h"
30
31 #include <TopoDS_Shape.hxx>
32
33 #include <Standard_Failure.hxx>
34 #include <StdFail_NotDone.hxx>
35
36 #ifdef WNT
37 #include <windows.h>
38 #else
39 #include <dlfcn.h>
40 #endif
41
42 #ifdef WNT
43 #define LibHandle HMODULE
44 #define LoadLib( name ) LoadLibrary( name )
45 #define GetProc GetProcAddress
46 #define UnLoadLib( handle ) FreeLibrary( handle );
47 #else
48 #define LibHandle void*
49 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
50 #define GetProc dlsym
51 #define UnLoadLib( handle ) dlclose( handle );
52 #endif
53
54 typedef TopoDS_Shape (*funcPoint)(const TCollection_AsciiString&,
55                                   const TCollection_AsciiString&,
56                                   TCollection_AsciiString&,
57                                   const TDF_Label&);
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     TCollection_AsciiString aMsg = aFormatName.SubString(1,4);
105     aMsg += " plugin was not installed";
106     Standard_Failure::Raise(aMsg.ToCString());
107   }
108
109   // perform the import
110   TCollection_AsciiString anError;
111   TopoDS_Shape aShape = fp(aFileName, aFormatName, anError, aFunction->GetNamingEntry());
112
113   // unload plugin library
114   // commented by enk:
115   // the bug was occured: using ACIS Import/Export plugin
116   //UnLoadLib( anImportLib ); //This is workaround of BUG OCC13051
117
118   if ( aShape.IsNull() ) {
119     StdFail_NotDone::Raise(anError.ToCString());
120     return 0;
121   }
122
123   // set the function result
124   aFunction->SetValue(aShape);
125
126   log.SetTouched(Label());
127
128   return 1;
129 }
130
131
132 //=======================================================================
133 //function :  GEOMImpl_ImportDriver_Type_
134 //purpose  :
135 //=======================================================================
136 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ImportDriver_Type_()
137 {
138
139   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
140   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
141   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
142   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
143   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
144   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
145
146
147   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
148   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ImportDriver",
149                                                          sizeof(GEOMImpl_ImportDriver),
150                                                          1,
151                                                          (Standard_Address)_Ancestors,
152                                                          (Standard_Address)NULL);
153
154   return _aType;
155 }
156
157 //=======================================================================
158 //function : DownCast
159 //purpose  :
160 //=======================================================================
161 const Handle(GEOMImpl_ImportDriver) Handle(GEOMImpl_ImportDriver)::DownCast
162                                (const Handle(Standard_Transient)& AnObject)
163 {
164   Handle(GEOMImpl_ImportDriver) _anOtherObject;
165
166   if (!AnObject.IsNull()) {
167      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ImportDriver))) {
168        _anOtherObject = Handle(GEOMImpl_ImportDriver)((Handle(GEOMImpl_ImportDriver)&)AnObject);
169      }
170   }
171
172   return _anOtherObject;
173 }