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>
11 #include <GeomAlgoAPI_BREPImport.h>
12 #include <GeomAlgoAPI_STEPImport.h>
13 #include <GeomAlgoAPI_IGESImport.h>
15 #include <GeomAPI_Shape.h>
16 #include <Config_Common.h>
17 #include <Config_PropManager.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <TCollection_AsciiString.hxx>
25 #include <TDF_Label.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <OSD_Path.hxx>
36 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
40 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
42 // TODO Auto-generated destructor stub
46 * Returns the unique kind of a feature
48 const std::string& ExchangePlugin_ImportFeature::getKind()
50 return ExchangePlugin_ImportFeature::ID();
54 * Request for initialization of data model of the feature: adding all attributes
56 void ExchangePlugin_ImportFeature::initAttributes()
58 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
62 * Computes or recomputes the results
64 void ExchangePlugin_ImportFeature::execute()
66 AttributeStringPtr aFilePathAttr =
67 this->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
68 std::string aFilePath = aFilePathAttr->value();
69 if (aFilePath.empty())
72 importFile(aFilePath);
75 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
77 // retrieve the file and plugin library names
78 TCollection_AsciiString aFileName(theFileName.c_str());
79 OSD_Path aPath(aFileName);
80 TCollection_AsciiString anExtension = aPath.Extension();
81 // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1
82 anExtension = anExtension.SubString(2, anExtension.Length());
83 anExtension.UpperCase();
86 TCollection_AsciiString anError;
89 if (anExtension == "BREP" || anExtension == "BRP") {
90 aShape = BREPImport::Import(aFileName, anExtension, anError);
91 } else if (anExtension == "STEP" || anExtension == "STP") {
92 aShape = STEPImport::Import(aFileName, anExtension, anError);
93 } else if (anExtension == "IGES" || anExtension == "IGS") {
94 aShape = IGESImport::Import(aFileName, anExtension, anError);
96 // Check if shape is valid
97 if ( aShape.IsNull() ) {
98 const static std::string aShapeError =
99 "An error occurred while importing " + theFileName + ": " + anError.ToCString();
100 setError(aShapeError);
104 // Pass the results into the model
105 std::string anObjectName = aPath.Name().ToCString();
106 data()->setName(anObjectName);
107 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
108 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
109 aGeomShape->setImpl(new TopoDS_Shape(aShape));
111 //LoadNamingDS of the imported shape
112 loadNamingDS(aGeomShape, aResultBody);
114 setResult(aResultBody);
119 //============================================================================
120 void ExchangePlugin_ImportFeature::loadNamingDS(
121 std::shared_ptr<GeomAPI_Shape> theGeomShape,
122 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
125 theResultBody->store(theGeomShape);
128 std::string aNameMS = "Shape";
129 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);