1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ExchangePlugin_ImportFeature.cpp
4 // Created: Aug 28, 2014
5 // Author: Sergey BELASH
7 #include <ExchangePlugin_ImportFeature.h>
16 #include <Config_Common.h>
17 #include <Config_PropManager.h>
19 #include <GeomAlgoAPI_BREPImport.h>
20 #include <GeomAlgoAPI_IGESImport.h>
21 #include <GeomAlgoAPI_STEPImport.h>
22 #include <GeomAlgoAPI_XAOImport.h>
23 #include <GeomAlgoAPI_Tools.h>
25 #include <GeomAPI_Shape.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_BodyBuilder.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_Document.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultGroup.h>
36 #include <XAO_Xao.hxx>
37 #include <XAO_Group.hxx>
39 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
43 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
45 // TODO Auto-generated destructor stub
49 * Returns the unique kind of a feature
51 const std::string& ExchangePlugin_ImportFeature::getKind()
53 return ExchangePlugin_ImportFeature::ID();
57 * Request for initialization of data model of the feature: adding all attributes
59 void ExchangePlugin_ImportFeature::initAttributes()
61 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
65 * Computes or recomputes the results
67 void ExchangePlugin_ImportFeature::execute()
69 AttributeStringPtr aFilePathAttr =
70 this->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
71 std::string aFilePath = aFilePathAttr->value();
72 if (aFilePath.empty())
75 importFile(aFilePath);
78 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
80 // retrieve the file and plugin library names
82 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
87 std::shared_ptr<GeomAPI_Shape> aGeomShape;
88 std::shared_ptr<XAO::Xao> aXao;
89 if (anExtension == "BREP" || anExtension == "BRP") {
90 aGeomShape = BREPImport(theFileName, anExtension, anError);
91 } else if (anExtension == "STEP" || anExtension == "STP") {
92 aGeomShape = STEPImport(theFileName, anExtension, anError);
93 } else if (anExtension == "IGES" || anExtension == "IGS") {
94 aGeomShape = IGESImport(theFileName, anExtension, anError);
95 } else if (anExtension == "XAO") {
96 std::shared_ptr<XAO::Xao> aTmpXao(new XAO::Xao);
97 aGeomShape = XAOImport(theFileName, anExtension, anError, aTmpXao.get());
98 if (!aGeomShape->isNull())
102 // Check if shape is valid
103 if (aGeomShape->isNull()) {
104 const static std::string aShapeError =
105 "An error occurred while importing " + theFileName + ": " + anError;
106 setError(aShapeError);
110 // Pass the results into the model
111 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
112 data()->setName(anObjectName);
113 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
115 //LoadNamingDS of the imported shape
116 loadNamingDS(aGeomShape, aResultBody);
118 setResult(aResultBody);
121 XAO::Geometry* aXaoGeometry = aXao->getGeometry();
123 // Creates group results
124 for (int aGroupIndex = 0; aGroupIndex < aXao->countGroups(); ++aGroupIndex) {
125 XAO::Group* aXaoGroup = aXao->getGroup(aGroupIndex);
127 std::shared_ptr<ModelAPI_Feature> aGroupFeature = document()->addFeature("Group", false);
129 if (!aXaoGroup->getName().empty())
130 aGroupFeature->data()->setName(aXaoGroup->getName());
131 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
132 aSelectionList->setSelectionType(XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()));
134 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
135 aSelectionList->append(aResultBody, GeomShapePtr());
136 int anElementID = aXaoGroup->get(anElementIndex);
137 std::string aReferenceString =
138 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
139 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
140 aSelectionList->value(anElementIndex)->setId(aReferenceID);
143 document()->setCurrentFeature(aGroupFeature, true);
151 //============================================================================
152 void ExchangePlugin_ImportFeature::loadNamingDS(
153 std::shared_ptr<GeomAPI_Shape> theGeomShape,
154 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
157 theResultBody->store(theGeomShape);
160 std::string aNameMS = "Shape";
161 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);