2 * ExchangePlugin_ImportFeature.cpp
4 * Created on: Aug 28, 2014
8 #include <ExchangePlugin_ImportFeature.h>
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>
30 # define _separator_ '\\'
32 # define _separator_ '/'
35 typedef TopoDS_Shape (*importFunctionPointer)(const TCollection_AsciiString&,
36 const TCollection_AsciiString&,
37 TCollection_AsciiString&,
40 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
44 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
46 // TODO Auto-generated destructor stub
50 * Returns the unique kind of a feature
52 const std::string& ExchangePlugin_ImportFeature::getKind()
54 return ExchangePlugin_ImportFeature::ID();
58 * Request for initialization of data model of the feature: adding all attributes
60 void ExchangePlugin_ImportFeature::initAttributes()
62 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::type());
66 * Computes or recomputes the results
68 void ExchangePlugin_ImportFeature::execute()
70 AttributeStringPtr aFilePathAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
71 data()->attribute(ExchangePlugin_ImportFeature::FILE_PATH_ID()));
72 std::string aFilePath = aFilePathAttr->value();
75 importFile(aFilePath);
78 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
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();
88 // Load plugin library and get the "Import" method
89 LibHandle anImportLib = loadImportPlugin(std::string(aFormatName.ToCString()));
92 importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
94 TCollection_AsciiString anError;
95 TDF_Label anUnknownLabel = TDF_Label();
96 TopoDS_Shape aShape = fp(aFileName,
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);
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));
115 //LoadNamingDS of the imported shape
116 loadNamingDS(aGeomShape, aResultBody);
118 setResult(aResultBody);
123 //============================================================================
124 void ExchangePlugin_ImportFeature::loadNamingDS(
125 std::shared_ptr<GeomAPI_Shape> theGeomShape,
126 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
129 theResultBody->store(theGeomShape);
131 theResultBody->loadFirstLevel(theGeomShape, aTag);
132 theResultBody->loadDisconnectedEdges(theGeomShape, aTag);
133 theResultBody->loadDisconnectedVertexes(theGeomShape, aTag);
136 LibHandle ExchangePlugin_ImportFeature::loadImportPlugin(const std::string& theFormatName)
138 std::string aLibName = library(theFormatName + ID());
139 LibHandle anImportLib = LoadLib(aLibName.c_str());
140 std::string anImportError = "Failed to load " + aLibName + ": ";
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);
151 anImportError = anImportError + std::string(dlerror());
153 setError(anImportError);
156 // Test loaded plugin for existence of valid "Import" function:
157 importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
159 const static std::string aFunctionError =
160 "No valid \"Import\" function was found in the " + aLibName;
161 setError(aFunctionError);
162 UnLoadLib(anImportLib)