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