Salome HOME
11abbb3904ae5cd475c3100ca87601e61dfb1297
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ExchangePlugin_ImportFeature.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
10 #include <ExchangePlugin_ImportFeature.h>
11
12 #include <GeomAPI_Shape.h>
13 #include <Config_Common.h>
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_ResultBody.h>
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 = std::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      const static std::string aShapeError = 
105        "An error occurred while importing " + theFileName + ": " + anError.ToCString();
106      setError(aShapeError);
107      return false;
108    }
109   //
110    // Pass the results into the model
111    std::string anObjectName = aPath.Name().ToCString();
112    data()->setName(anObjectName);
113    std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
114    std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
115    aGeomShape->setImpl(new TopoDS_Shape(aShape));
116
117    //LoadNamingDS of the imported shape
118    loadNamingDS(aGeomShape, aResultBody);
119
120    setResult(aResultBody);
121
122    return true;
123 }
124
125 //============================================================================
126 void ExchangePlugin_ImportFeature::loadNamingDS(
127                                     std::shared_ptr<GeomAPI_Shape> theGeomShape, 
128                                              std::shared_ptr<ModelAPI_ResultBody> theResultBody)
129 {  
130   //load result
131   theResultBody->store(theGeomShape);
132   int aTag(1);
133   theResultBody->loadFirstLevel(theGeomShape, aTag);
134   theResultBody->loadDisconnectedEdges(theGeomShape, aTag);
135   theResultBody->loadDisconnectedVertexes(theGeomShape, aTag); 
136 }
137
138 LibHandle ExchangePlugin_ImportFeature::loadImportPlugin(const std::string& theFormatName)
139 {
140   std::string aLibName = library(theFormatName + ID());
141   LibHandle anImportLib = LoadLib(aLibName.c_str());
142   std::string anImportError = "Failed to load " + aLibName + ": ";
143   if(!anImportLib) {
144 #ifdef WIN32
145     LPVOID lpMsgBuf;
146     ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
147                     FORMAT_MESSAGE_FROM_SYSTEM |
148                     FORMAT_MESSAGE_IGNORE_INSERTS,
149                     0, ::GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, 0);
150     anImportError = anImportError + std::string((char*) lpMsgBuf);
151     ::LocalFree(lpMsgBuf);
152 #else
153     anImportError = anImportError + std::string(dlerror());
154 #endif
155     setError(anImportError);
156     return false;
157   }
158   // Test loaded plugin for existence of valid "Import" function:
159   importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
160   if (!fp) {
161     const static std::string aFunctionError = 
162       "No valid \"Import\" function was found in the " + aLibName;
163     setError(aFunctionError);
164     UnLoadLib(anImportLib)
165     return NULL;
166   }
167   return anImportLib;
168 }