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