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