1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ExchangePlugin_ImportFeature.cpp
4 // Created: Aug 28, 2014
5 // Authors: Sergey BELASH, Sergey POKHODENKO
7 #include <ExchangePlugin_ImportFeature.h>
12 #include <Config_Common.h>
13 #include <Config_PropManager.h>
15 #include <GeomAlgoAPI_BREPImport.h>
16 #include <GeomAlgoAPI_IGESImport.h>
17 #include <GeomAlgoAPI_STEPImport.h>
18 #include <GeomAlgoAPI_Tools.h>
19 #include <GeomAlgoAPI_XAOImport.h>
21 #include <GeomAPI_Shape.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_BodyBuilder.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Object.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_ResultGroup.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
36 #include <XAO_Xao.hxx>
37 #include <XAO_Group.hxx>
39 #include <ExchangePlugin_Tools.h>
41 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
45 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
47 // TODO Auto-generated destructor stub
51 * Request for initialization of data model of the feature: adding all attributes
53 void ExchangePlugin_ImportFeature::initAttributes()
55 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
56 data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
58 ModelAPI_Session::get()->validators()->registerNotObligatory(
59 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
63 * Computes or recomputes the results
65 void ExchangePlugin_ImportFeature::execute()
67 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
68 std::string aFilePath = aFilePathAttr->value();
69 if (aFilePath.empty()) {
70 setError("File path is empty.");
74 importFile(aFilePath);
77 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
78 std::shared_ptr<GeomAPI_Shape> aGeomShape)
80 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
81 //LoadNamingDS of the imported shape
82 loadNamingDS(aGeomShape, aResultBody);
86 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
89 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
91 if (anExtension == "XAO") {
92 importXAO(theFileName);
98 std::shared_ptr<GeomAPI_Shape> aGeomShape;
99 if (anExtension == "BREP" || anExtension == "BRP") {
100 aGeomShape = BREPImport(theFileName, anExtension, anError);
101 } else if (anExtension == "STEP" || anExtension == "STP") {
102 aGeomShape = STEPImport(theFileName, anExtension, anError);
103 } else if (anExtension == "IGES" || anExtension == "IGS") {
104 aGeomShape = IGESImport(theFileName, anExtension, anError);
106 anError = "Unsupported format: " + anExtension;
109 // Check if shape is valid
110 if (!anError.empty()) {
111 setError("An error occurred while importing " + theFileName + ": " + anError);
115 // Pass the results into the model
116 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
117 data()->setName(anObjectName);
119 setResult(createResultBody(aGeomShape));
122 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
128 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
130 if (!anError.empty()) {
131 setError("An error occurred while importing " + theFileName + ": " + anError);
135 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
137 // use the geometry name or the file name for the feature
138 std::string aBodyName = aXaoGeometry->getName();
139 if (aBodyName.empty())
140 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
141 data()->setName(aBodyName);
143 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
144 setResult(aResultBody);
147 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
148 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
150 // Remove previous groups stored in RefList
151 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
152 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
153 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
154 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
156 document()->removeFeature(aFeature);
160 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
161 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
163 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
166 if (!aXaoGroup->getName().empty())
167 aGroupFeature->data()->setName(aXaoGroup->getName());
170 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
172 // conversion of dimension
173 XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
174 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
175 std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
177 aSelectionList->setSelectionType(aSelectionType);
178 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
179 aSelectionList->append(aResultBody, GeomShapePtr());
180 // complex conversion of element index to reference id
181 int anElementID = aXaoGroup->get(anElementIndex);
182 std::string aReferenceString =
183 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
184 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
186 aSelectionList->value(anElementIndex)->setId(aReferenceID);
189 // aRefListOfGroups->append(aGroupFeature);
191 // // hide the group in the history
192 // document()->setCurrentFeature(aGroupFeature, false);
193 // // groups features is internal part of the import
194 // aGroupFeature->setInHistory(aGroupFeature, false);
197 } catch (XAO::XAO_Exception& e) {
198 std::string anError = e.what();
199 setError("An error occurred while importing " + theFileName + ": " + anError);
204 //============================================================================
205 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
208 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
210 data()->reflist(FEATURES_ID())->append(aNew);
211 // set as current also after it becomes sub to set correctly enabled for other subs
212 //document()->setCurrentFeature(aNew, false);
216 void ExchangePlugin_ImportFeature::removeFeature(
217 std::shared_ptr<ModelAPI_Feature> theFeature)
219 if (!data()->isValid())
221 AttributeRefListPtr aList = reflist(FEATURES_ID());
222 // if the object is last, remove it from the list (needed to skip empty transaction on edit of sketch feature)
223 if (aList->object(aList->size(true) - 1, true) == theFeature) {
224 aList->remove(theFeature);
226 // to keep the persistent sub-elements indexing, do not remove elements from list,
227 // but substitute by nulls
228 aList->substitute(theFeature, ObjectPtr());
232 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
234 return data()->reflist(FEATURES_ID())->size(false);
237 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
238 const int theIndex, bool forTree)
240 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
241 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
245 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
247 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
248 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
249 std::list<ObjectPtr> aFeatures = aRefList->list();
250 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
251 int aResultIndex = 1; // number of the counted (created) features, started from 1
252 int aFeatureIndex = -1; // number of the not-empty features in the list
253 for (; anIt != aFeatures.end(); anIt++) {
256 if (aFeatureIndex == theIndex)
263 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
265 // check is this feature of result
266 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
268 return data()->reflist(FEATURES_ID())->isInList(aFeature);
272 //============================================================================
273 void ExchangePlugin_ImportFeature::loadNamingDS(
274 std::shared_ptr<GeomAPI_Shape> theGeomShape,
275 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
278 theResultBody->store(theGeomShape);
281 std::string aNameMS = "Shape";
282 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);