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