1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ExchangePlugin_ImportFeature.cpp
6 * Created on: Aug 28, 2014
10 #include <ExchangePlugin_ImportFeature.h>
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>
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 = std::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 const static std::string aShapeError =
105 "An error occurred while importing " + theFileName + ": " + anError.ToCString();
106 setError(aShapeError);
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));
117 //LoadNamingDS of the imported shape
118 loadNamingDS(aGeomShape, aResultBody);
120 setResult(aResultBody);
125 //============================================================================
126 void ExchangePlugin_ImportFeature::loadNamingDS(
127 std::shared_ptr<GeomAPI_Shape> theGeomShape,
128 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
131 theResultBody->store(theGeomShape);
132 std::string aPrefix = data()->name() + "/";
134 std::string aNameMS = aPrefix + "Shape";
135 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
136 std::string aNameDE = aPrefix + "DiscEdges";
137 theResultBody->loadDisconnectedEdges(theGeomShape, aNameDE, aTag);
138 std::string aNameDV = aPrefix + "DiscVertexes";
139 theResultBody->loadDisconnectedVertexes(theGeomShape, aNameDV, aTag);
142 LibHandle ExchangePlugin_ImportFeature::loadImportPlugin(const std::string& theFormatName)
144 std::string aLibName = library(theFormatName + ID());
145 LibHandle anImportLib = LoadLib(aLibName.c_str());
146 std::string anImportError = "Failed to load " + aLibName + ": ";
150 ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
151 FORMAT_MESSAGE_FROM_SYSTEM |
152 FORMAT_MESSAGE_IGNORE_INSERTS,
153 0, ::GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, 0);
154 anImportError = anImportError + std::string((char*) lpMsgBuf);
155 ::LocalFree(lpMsgBuf);
157 anImportError = anImportError + std::string(dlerror());
159 setError(anImportError);
162 // Test loaded plugin for existence of valid "Import" function:
163 importFunctionPointer fp = (importFunctionPointer) GetProc(anImportLib, "Import");
165 const static std::string aFunctionError =
166 "No valid \"Import\" function was found in the " + aLibName;
167 setError(aFunctionError);
168 UnLoadLib(anImportLib)