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