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