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 <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>
31 # define _separator_ '\\'
33 # define _separator_ '/'
36 typedef TopoDS_Shape (*importFunctionPointer)(const TCollection_AsciiString&,
37 const TCollection_AsciiString&,
38 TCollection_AsciiString&,
41 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
45 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
47 // TODO Auto-generated destructor stub
51 * Returns the unique kind of a feature
53 const std::string& ExchangePlugin_ImportFeature::getKind()
55 return ExchangePlugin_ImportFeature::ID();
59 * Request for initialization of data model of the feature: adding all attributes
61 void ExchangePlugin_ImportFeature::initAttributes()
63 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::type());
67 * Computes or recomputes the results
69 void ExchangePlugin_ImportFeature::execute()
71 AttributeStringPtr aFilePathAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
72 data()->attribute(ExchangePlugin_ImportFeature::FILE_PATH_ID()));
73 std::string aFilePath = aFilePathAttr->value();
76 importFile(aFilePath);
79 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
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();
89 // Load plugin library and get the "Import" method
90 LibHandle anImportLib = loadImportPlugin(std::string(aFormatName.ToCString()));
93 importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
95 TCollection_AsciiString anError;
96 TDF_Label anUnknownLabel = TDF_Label();
97 TopoDS_Shape aShape = fp(aFileName,
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);
107 std::cerr << aShapeError << std::endl;
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));
119 //LoadNamingDS of the imported shape
120 loadNamingDS(aGeomShape, aResultBody);
122 setResult(aResultBody);
127 //============================================================================
128 void ExchangePlugin_ImportFeature::loadNamingDS(
129 std::shared_ptr<GeomAPI_Shape> theGeomShape,
130 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
133 theResultBody->store(theGeomShape);
135 theResultBody->loadFirstLevel(theGeomShape, aTag);
136 theResultBody->loadDisconnectedEdges(theGeomShape, aTag);
137 theResultBody->loadDisconnectedVertexes(theGeomShape, aTag);
140 LibHandle ExchangePlugin_ImportFeature::loadImportPlugin(const std::string& theFormatName)
142 std::string aLibName = library(theFormatName + ID());
143 LibHandle anImportLib = LoadLib(aLibName.c_str());
144 std::string anImportError = "Failed to load " + aLibName + ": ";
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);
155 anImportError = anImportError + std::string(dlerror());
157 Events_Error::send(anImportError, this);
159 std::cerr << anImportError << std::endl;
163 // Test loaded plugin for existence of valid "Import" function:
164 importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
166 std::string aFunctionError = "No valid \"Import\" function was found in the " + aLibName;
167 Events_Error::send(aFunctionError, this);
169 std::cerr << aFunctionError << std::endl;
171 UnLoadLib(anImportLib)