Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportFeature.cpp
1 /*
2  * ExchangePlugin_ImportFeature.cpp
3  *
4  *  Created on: Aug 28, 2014
5  *      Author: sbh
6  */
7
8 #include <ExchangePlugin_ImportFeature.h>
9
10 #include <GeomAPI_Shape.h>
11 #include <Config_Common.h>
12 #include <Events_Error.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Document.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_ResultBody.h>
18
19 #include <TCollection_AsciiString.hxx>
20 #include <TDF_Label.hxx>
21 #include <TopoDS_Shape.hxx>
22 #include <OSD_Path.hxx>
23
24 #include <algorithm>
25 #include <string>
26 #ifdef _DEBUG
27 #include <iostream>
28 #include <ostream>
29 #endif
30
31 #ifdef WIN32
32 # define _separator_ '\\'
33 #else
34 # define _separator_ '/'
35 #endif
36
37 typedef TopoDS_Shape (*importFunctionPointer)(const TCollection_AsciiString&,
38                                               const TCollection_AsciiString&,
39                                               TCollection_AsciiString&,
40                                               const TDF_Label&);
41
42 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
43 {
44 }
45
46 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
47 {
48   // TODO Auto-generated destructor stub
49 }
50
51 /*
52  * Returns the unique kind of a feature
53  */
54 const std::string& ExchangePlugin_ImportFeature::getKind()
55 {
56   return ExchangePlugin_ImportFeature::ID();
57 }
58
59 /*
60  * Request for initialization of data model of the feature: adding all attributes
61  */
62 void ExchangePlugin_ImportFeature::initAttributes()
63 {
64   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::type());
65 }
66
67 /*
68  * Computes or recomputes the results
69  */
70 void ExchangePlugin_ImportFeature::execute()
71 {
72   AttributeStringPtr aFilePathAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeString>(
73       data()->attribute(ExchangePlugin_ImportFeature::FILE_PATH_ID()));
74   std::string aFilePath = aFilePathAttr->value();
75   if(aFilePath.empty())
76     return;
77   importFile(aFilePath);
78 }
79
80 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
81 {
82   // retrieve the file and plugin library names
83   TCollection_AsciiString aFileName (theFileName.c_str());
84   OSD_Path aPath(aFileName);
85   TCollection_AsciiString aFormatName = aPath.Extension();
86   // ".brep" -> "BREP". TCollection_AsciiString are numbered from 1
87   aFormatName = aFormatName.SubString(2, aFormatName.Length());
88   aFormatName.UpperCase();
89
90   // Load plugin library and get the "Import" method
91   LibHandle anImportLib = loadImportPlugin(std::string(aFormatName.ToCString()));
92   if(!anImportLib)
93     return false;
94   importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
95    // Perform the import
96    TCollection_AsciiString anError;
97    TDF_Label anUnknownLabel = TDF_Label();
98    TopoDS_Shape aShape = fp(aFileName,
99                             aFormatName,
100                             anError,
101                             anUnknownLabel);
102    // Check if shape is valid
103    if ( aShape.IsNull() ) {
104      std::string aShapeError = "An error occurred while importing " + theFileName + ": ";
105      aShapeError = aShapeError + std::string(anError.ToCString());
106      Events_Error::send(aShapeError, this);
107  #ifdef _DEBUG
108      std::cerr << aShapeError << std::endl;
109  #endif
110      return false;
111    }
112    // Pass the results into the model
113    std::string anObjectName = aPath.Name().ToCString();
114    data()->setName(anObjectName);
115    boost::shared_ptr<ModelAPI_ResultBody> aResult = document()->createBody(data());
116    boost::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
117    aGeomShape->setImpl(new TopoDS_Shape(aShape));
118    aResult->store(aGeomShape);
119    setResult(aResult);
120
121    return true;
122 }
123
124 LibHandle ExchangePlugin_ImportFeature::loadImportPlugin(const std::string& theFormatName)
125 {
126   std::string aLibName = library(theFormatName + ID());
127   LibHandle anImportLib = LoadLib(aLibName.c_str());
128   std::string anImportError = "Failed to load " + aLibName + ": ";
129   if(!anImportLib) {
130 #ifdef WIN32
131     LPVOID lpMsgBuf;
132     ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
133                     FORMAT_MESSAGE_FROM_SYSTEM |
134                     FORMAT_MESSAGE_IGNORE_INSERTS,
135                     0, ::GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, 0);
136     anImportError = anImportError + std::string((char*) lpMsgBuf);
137     ::LocalFree(lpMsgBuf);
138 #else
139     anImportError = anImportError + std::string(dlerror());
140 #endif
141     Events_Error::send(anImportError, this);
142 #ifdef _DEBUG
143     std::cerr << anImportError << std::endl;
144 #endif
145     return false;
146   }
147   // Test loaded plugin for existence of valid "Import" function:
148   importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
149   if (!fp) {
150     std::string aFunctionError = "No valid \"Import\" function was found in the " + aLibName;
151     Events_Error::send(aFunctionError, this);
152 #ifdef _DEBUG
153     std::cerr << aFunctionError << std::endl;
154 #endif
155     UnLoadLib(anImportLib)
156     return NULL;
157   }
158   return anImportLib;
159 }