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>
19 #include <TCollection_AsciiString.hxx>
20 #include <TDF_Label.hxx>
21 #include <TopoDS_Shape.hxx>
22 #include <OSD_Path.hxx>
32 # define _separator_ '\\'
34 # define _separator_ '/'
37 typedef TopoDS_Shape (*importFunctionPointer)(const TCollection_AsciiString&,
38 const TCollection_AsciiString&,
39 TCollection_AsciiString&,
42 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
46 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
48 // TODO Auto-generated destructor stub
52 * Returns the unique kind of a feature
54 const std::string& ExchangePlugin_ImportFeature::getKind()
56 return ExchangePlugin_ImportFeature::ID();
60 * Request for initialization of data model of the feature: adding all attributes
62 void ExchangePlugin_ImportFeature::initAttributes()
64 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::type());
68 * Computes or recomputes the results
70 void ExchangePlugin_ImportFeature::execute()
72 AttributeStringPtr aFilePathAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeString>(
73 data()->attribute(ExchangePlugin_ImportFeature::FILE_PATH_ID()));
74 std::string aFilePath = aFilePathAttr->value();
77 importFile(aFilePath);
80 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
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();
90 // Load plugin library and get the "Import" method
91 LibHandle anImportLib = loadImportPlugin(std::string(aFormatName.ToCString()));
94 importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
96 TCollection_AsciiString anError;
97 TDF_Label anUnknownLabel = TDF_Label();
98 TopoDS_Shape aShape = fp(aFileName,
102 // Check if shape is valid
103 if ( aShape.IsNull() ) {
104 std::string aShapeError = "An error occurred while importing " + theFileName + ": ";
105 aShapeError = aShapeError + std::string(anError.ToCString());
106 Events_Error::send(aShapeError, this);
108 std::cerr << aShapeError << std::endl;
112 // Pass the results into the model
113 std::string anObjectName = aPath.Name().ToCString();
114 data()->setName(anObjectName);
115 boost::shared_ptr<ModelAPI_ResultBody> aResult = document()->createBody(data());
116 boost::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
117 aGeomShape->setImpl(new TopoDS_Shape(aShape));
118 aResult->store(aGeomShape);
124 LibHandle ExchangePlugin_ImportFeature::loadImportPlugin(const std::string& theFormatName)
126 std::string aLibName = library(theFormatName + ID());
127 LibHandle anImportLib = LoadLib(aLibName.c_str());
128 std::string anImportError = "Failed to load " + aLibName + ": ";
132 ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
133 FORMAT_MESSAGE_FROM_SYSTEM |
134 FORMAT_MESSAGE_IGNORE_INSERTS,
135 0, ::GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, 0);
136 anImportError = anImportError + std::string((char*) lpMsgBuf);
137 ::LocalFree(lpMsgBuf);
139 anImportError = anImportError + std::string(dlerror());
141 Events_Error::send(anImportError, this);
143 std::cerr << anImportError << std::endl;
147 // Test loaded plugin for existence of valid "Import" function:
148 importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
150 std::string aFunctionError = "No valid \"Import\" function was found in the " + aLibName;
151 Events_Error::send(aFunctionError, this);
153 std::cerr << aFunctionError << std::endl;
155 UnLoadLib(anImportLib)