Salome HOME
19a2593c0bfadebfa01672ed5bf36206002b3e3e
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ExportDriver.cxx
1
2 #include <Standard_Stream.hxx>
3
4 #include <GEOMImpl_ExportDriver.hxx>
5 #include <GEOMImpl_IImportExport.hxx>
6 #include <GEOMImpl_Types.hxx>
7 #include <GEOM_Function.hxx>
8
9 #include <TopoDS_Shape.hxx>
10 #include <TCollection_AsciiString.hxx>
11
12 #include <Standard_ConstructionError.hxx>
13
14 #ifdef WNT
15 #include <windows.h>
16 #else
17 #include <dlfcn.h>
18 #endif
19
20 #ifdef WNT
21 #define LibHandle HMODULE
22 #define LoadLib( name ) LoadLibrary( name )
23 #define GetProc GetProcAddress
24 #define UnLoadLib( handle ) FreeLibrary( handle );
25 #else
26 #define LibHandle void*
27 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
28 #define GetProc dlsym
29 #define UnLoadLib( handle ) dlclose( handle );
30 #endif
31
32 typedef int (*funcPoint)(const TopoDS_Shape&, const TCollection_AsciiString&);
33
34 //=======================================================================
35 //function : GetID
36 //purpose  :
37 //======================================================================= 
38 const Standard_GUID& GEOMImpl_ExportDriver::GetID()
39 {
40   static Standard_GUID aExportDriver("FF1BBB58-5D14-4df2-980B-3A668264EA16");
41   return aExportDriver; 
42 }
43
44
45 //=======================================================================
46 //function : GEOMImpl_ExportDriver
47 //purpose  : 
48 //=======================================================================
49 GEOMImpl_ExportDriver::GEOMImpl_ExportDriver() 
50 {
51 }
52
53 //=======================================================================
54 //function : Execute
55 //purpose  :
56 //======================================================================= 
57 Standard_Integer GEOMImpl_ExportDriver::Execute(TFunction_Logbook& log) const
58 {
59   if (Label().IsNull()) return 0;    
60   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
61
62   GEOMImpl_IImportExport aCI (aFunction);
63
64   // retrieve the being exported shape
65   TopoDS_Shape aShape;
66   Handle(GEOM_Function) aRefFunction = aCI.GetOriginal();
67   if (aRefFunction.IsNull()) return 0;
68   aShape = aRefFunction->GetValue();
69   if (aShape.IsNull()) return 0;
70   // !!! set the result of function to be used by next operations
71   aFunction->SetValue(aShape);
72
73   // retrieve the file and format names
74   TCollection_AsciiString aFileName = aCI.GetFileName();
75   TCollection_AsciiString aLibName  = aCI.GetPluginName();
76   if (aFileName.IsEmpty() || aLibName.IsEmpty())
77     return 0;
78
79   // load plugin library
80   LibHandle anExportLib = LoadLib( aLibName.ToCString() );
81   funcPoint fp = 0;
82   if ( anExportLib )
83     fp = (funcPoint)GetProc( anExportLib, "Export" );
84
85   if ( !fp )
86     return 0;
87
88   // perform the export
89   int res = fp( aShape, aFileName );
90
91   // unload plugin library
92   UnLoadLib( anExportLib );
93   if ( res )
94     log.SetTouched(Label()); 
95
96   return res;
97 }
98
99
100 //=======================================================================
101 //function :  GEOMImpl_ExportDriver_Type_
102 //purpose  :
103 //======================================================================= 
104 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ExportDriver_Type_()
105 {
106
107   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
108   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
109   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
110   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
111   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
112   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
113  
114
115   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
116   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ExportDriver",
117                                                          sizeof(GEOMImpl_ExportDriver),
118                                                          1,
119                                                          (Standard_Address)_Ancestors,
120                                                          (Standard_Address)NULL);
121
122   return _aType;
123 }
124
125 //=======================================================================
126 //function : DownCast
127 //purpose  :
128 //======================================================================= 
129 const Handle(GEOMImpl_ExportDriver) Handle(GEOMImpl_ExportDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
130 {
131   Handle(GEOMImpl_ExportDriver) _anOtherObject;
132
133   if (!AnObject.IsNull()) {
134      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ExportDriver))) {
135        _anOtherObject = Handle(GEOMImpl_ExportDriver)((Handle(GEOMImpl_ExportDriver)&)AnObject);
136      }
137   }
138
139   return _anOtherObject ;
140 }