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_Tools.h>
23 #include <GeomAlgoAPI_XAOImport.h>
25 #include <GeomAPI_Shape.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_BodyBuilder.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Document.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Object.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_ResultGroup.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Validator.h>
40 #include <XAO_Xao.hxx>
41 #include <XAO_Group.hxx>
43 #include <ExchangePlugin_Tools.h>
45 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
49 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
51 // TODO Auto-generated destructor stub
55 * Request for initialization of data model of the feature: adding all attributes
57 void ExchangePlugin_ImportFeature::initAttributes()
59 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
60 data()->addAttribute(ExchangePlugin_ImportFeature::GROUP_LIST_ID(), ModelAPI_AttributeRefList::typeId());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ImportFeature::GROUP_LIST_ID());
66 * Computes or recomputes the results
68 void ExchangePlugin_ImportFeature::execute()
70 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
71 std::string aFilePath = aFilePathAttr->value();
72 if (aFilePath.empty()) {
73 setError("File path is empty.");
77 importFile(aFilePath);
80 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
81 std::shared_ptr<GeomAPI_Shape> aGeomShape)
83 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
84 //LoadNamingDS of the imported shape
85 loadNamingDS(aGeomShape, aResultBody);
89 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
92 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
94 if (anExtension == "XAO") {
95 importXAO(theFileName);
101 std::shared_ptr<GeomAPI_Shape> aGeomShape;
102 if (anExtension == "BREP" || anExtension == "BRP") {
103 aGeomShape = BREPImport(theFileName, anExtension, anError);
104 } else if (anExtension == "STEP" || anExtension == "STP") {
105 aGeomShape = STEPImport(theFileName, anExtension, anError);
106 } else if (anExtension == "IGES" || anExtension == "IGS") {
107 aGeomShape = IGESImport(theFileName, anExtension, anError);
109 anError = "Unsupported format: " + anExtension;
112 // Check if shape is valid
113 if (!anError.empty()) {
114 setError("An error occurred while importing " + theFileName + ": " + anError);
118 // Pass the results into the model
119 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
120 data()->setName(anObjectName);
122 setResult(createResultBody(aGeomShape));
125 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
131 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
133 if (!anError.empty()) {
134 setError("An error occurred while importing " + theFileName + ": " + anError);
138 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
140 // use the geometry name or the file name for the feature
141 std::string aBodyName = aXaoGeometry->getName().empty()
142 ? GeomAlgoAPI_Tools::File_Tools::name(theFileName)
143 : aXaoGeometry->getName();
144 data()->setName(aBodyName);
146 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
147 setResult(aResultBody);
150 AttributeRefListPtr aRefListOfGroups = reflist(ExchangePlugin_ImportFeature::GROUP_LIST_ID());
152 // Remove previous groups stored in RefList
153 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
154 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
155 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
156 std::shared_ptr<ModelAPI_Feature> aFeature =
157 std::dynamic_pointer_cast<ModelAPI_Feature>(*anGroupIt);
159 document()->removeFeature(aFeature);
163 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
164 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
166 std::shared_ptr<ModelAPI_Feature> aGroupFeature = document()->addFeature("Group", false);
169 if (!aXaoGroup->getName().empty())
170 aGroupFeature->data()->setName(aXaoGroup->getName());
173 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
175 // conversion of dimension
176 XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
177 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
178 std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
180 aSelectionList->setSelectionType(aSelectionType);
181 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
182 aSelectionList->append(aResultBody, GeomShapePtr());
183 // complex conversion of element index to reference id
184 int anElementID = aXaoGroup->get(anElementIndex);
185 std::string aReferenceString =
186 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
187 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
189 aSelectionList->value(anElementIndex)->setId(aReferenceID);
192 aRefListOfGroups->append(aGroupFeature);
194 document()->setCurrentFeature(aGroupFeature, true);
197 } catch (XAO::XAO_Exception& e) {
198 std::string anError = e.what();
199 setError("An error occurred while importing " + theFileName + ": " + anError);
204 //============================================================================
205 void ExchangePlugin_ImportFeature::loadNamingDS(
206 std::shared_ptr<GeomAPI_Shape> theGeomShape,
207 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
210 theResultBody->store(theGeomShape);
213 std::string aNameMS = "Shape";
214 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);