]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
Copyright update 2021
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportFeature.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ExchangePlugin_ImportFeature.h>
21
22 #include <algorithm>
23 #include <string>
24
25 #include <Config_Common.h>
26 #include <Config_PropManager.h>
27
28 #include <GeomAlgoAPI_BREPImport.h>
29 #include <GeomAlgoAPI_IGESImport.h>
30 #include <GeomAlgoAPI_STEPImport.h>
31 #include <GeomAlgoAPI_Tools.h>
32 #include <GeomAlgoAPI_XAOImport.h>
33
34 #include <GeomAPI_Shape.h>
35 #include <GeomAPI_Face.h>
36 #include <GeomAPI_ShapeExplorer.h>
37 #include <GeomAPI_ShapeIterator.h>
38
39 #include <Locale_Convert.h>
40
41 #include <ModelAPI_AttributeRefList.h>
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_AttributeIntArray.h>
46 #include <ModelAPI_AttributeTables.h>
47 #include <ModelAPI_AttributeBoolean.h>
48 #include <ModelAPI_AttributeInteger.h>
49 #include <ModelAPI_BodyBuilder.h>
50 #include <ModelAPI_Data.h>
51 #include <ModelAPI_Document.h>
52 #include <ModelAPI_Events.h>
53 #include <ModelAPI_Object.h>
54 #include <ModelAPI_ResultBody.h>
55 #include <ModelAPI_ResultGroup.h>
56 #include <ModelAPI_Session.h>
57 #include <ModelAPI_Validator.h>
58 #include <ModelAPI_Tools.h>
59
60 #include <XAO_Xao.hxx>
61 #include <XAO_Group.hxx>
62 #include <XAO_Field.hxx>
63 #include <XAO_Step.hxx>
64
65 #include <ExchangePlugin_Tools.h>
66
67
68
69 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
70 {
71 }
72
73 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
74 {
75   // TODO Auto-generated destructor stub
76 }
77
78 /*
79  * Request for initialization of data model of the feature: adding all attributes
80  */
81 void ExchangePlugin_ImportFeature::initAttributes()
82 {
83   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(),
84                        ModelAPI_AttributeString::typeId());
85   AttributePtr aFeaturesAttribute =
86   data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(),
87                          ModelAPI_AttributeRefList::typeId());
88   data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
89   data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
90   data()->addAttribute(STEP_MATERIALS_ID(), ModelAPI_AttributeBoolean::typeId());
91   data()->addAttribute(STEP_COLORS_ID(), ModelAPI_AttributeBoolean::typeId());
92   data()->addAttribute(STEP_SCALE_INTER_UNITS_ID(), ModelAPI_AttributeBoolean::typeId());
93
94   aFeaturesAttribute->setIsArgument(false);
95
96   ModelAPI_Session::get()->validators()->registerNotObligatory(
97       getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
98   ModelAPI_Session::get()->validators()->registerNotObligatory(
99       getKind(), ExchangePlugin_ImportFeature::STEP_COLORS_ID());
100   ModelAPI_Session::get()->validators()->registerNotObligatory(
101       getKind(), ExchangePlugin_ImportFeature::STEP_MATERIALS_ID());
102   ModelAPI_Session::get()->validators()->registerNotObligatory(
103       getKind(), ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID());
104   ModelAPI_Session::get()->validators()->registerNotObligatory(
105       getKind(), ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
106   ModelAPI_Session::get()->validators()->registerNotObligatory(
107       getKind(), ExchangePlugin_ImportFeature::FILE_PATH_ID());
108 }
109
110 /*
111  * Computes or recomputes the results
112  */
113 void ExchangePlugin_ImportFeature::execute()
114 {
115   AttributeStringPtr aImportTypeAttr = string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
116   std::string aFormat = aImportTypeAttr->value();
117   AttributeStringPtr aFilePathAttr;
118   if (aFormat == "STEP" || aFormat == "STP")
119   {
120     aFilePathAttr = string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
121   } else {
122     aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
123   }
124   std::string aFilePath = aFilePathAttr->value();
125   if (aFilePath.empty()) {
126     setError("File path is empty.");
127     return;
128   }
129
130   importFile(aFilePath);
131 }
132
133 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
134                         std::shared_ptr<GeomAPI_Shape> theGeomShape)
135 {
136   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
137   //LoadNamingDS of the imported shape
138   loadNamingDS(theGeomShape, aResultBody);
139   return aResultBody;
140 }
141
142 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
143 {
144   // "*.brep" -> "BREP"
145   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
146
147   if (anExtension == "XAO") {
148     importXAO(theFileName);
149     return;
150   }
151
152   // Perform the import
153   std::string anError;
154   std::shared_ptr<GeomAPI_Shape> aGeomShape;
155
156   std::map<std::wstring, std::list<std::wstring>> theMaterialShape;
157
158   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
159   data()->setName(Locale::Convert::toWString(anObjectName));
160
161   ResultBodyPtr aResult = document()->createBody(data());
162
163   bool anColorGroupSelected = boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID())->value();
164   bool anMaterialsGroupSelected =
165                         boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID())->value();
166
167   if (anExtension == "BREP" || anExtension == "BRP") {
168     aGeomShape = BREPImport(theFileName, anExtension, anError);
169   } else if (anExtension == "STEP" || anExtension == "STP") {
170     bool anScalInterUnits =
171             boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())->value();
172
173     // Process groups/fields
174     std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
175     std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
176
177     // Remove previous groups/fields stored in RefList
178     std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
179     std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
180     for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
181       std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
182       if (aFeature)
183         document()->removeFeature(aFeature);
184     }
185
186     aGeomShape = STEPImportAttributs(theFileName, aResult, anScalInterUnits,
187                                      anMaterialsGroupSelected, anColorGroupSelected,
188                                      theMaterialShape, anError);
189   } else if (anExtension == "IGES" || anExtension == "IGS") {
190     aGeomShape = IGESImport(theFileName, anExtension, anError);
191   } else {
192     anError = "Unsupported format: " + anExtension;
193   }
194
195   // Check if shape is valid
196   if (!anError.empty()) {
197     setError("An error occurred while importing " + theFileName + ": " + anError);
198     return;
199   }
200
201   // Pass the results into the model
202
203   loadNamingDS(aGeomShape, aResult);
204
205   // create color group
206   if (anColorGroupSelected)
207   {
208     setColorGroups(aResult);
209   }
210
211   // create Materiel group
212   if (anMaterialsGroupSelected){
213     setMaterielGroup(aResult,theMaterialShape);
214   }
215
216   setResult(aResult);
217   aResult->clearShapeNameAndColor();
218
219 }
220
221 void ExchangePlugin_ImportFeature::setColorGroups(
222                                     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
223 {
224   std::vector<int> aColor;
225   int anIndice = 1;
226   std::list<std::vector<int>> aColorsRead;
227
228   ModelAPI_Tools::getColor(theResultBody, aColor);
229   if (!aColor.empty() ){
230     std::wstringstream aColorName;
231     aColorName <<L"Color_"<< anIndice;
232     setColorGroup(theResultBody, aColor, aColorName.str());
233     anIndice++;
234     aColorsRead.push_back(aColor);
235   }
236
237   std::list<ResultPtr> allRes;
238   ModelAPI_Tools::allSubs(theResultBody, allRes);
239   for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
240     ModelAPI_Tools::getColor(*aRes, aColor);
241     if (!aColor.empty() ){
242       auto it = std::find(aColorsRead.begin(), aColorsRead.end(), aColor);
243       if ( it == aColorsRead.end() ){
244         std::wstringstream aColorName;
245         aColorName<<L"Color_"<< anIndice;
246         setColorGroup(theResultBody, aColor, aColorName.str());
247         anIndice++;
248         aColorsRead.push_back(aColor);
249       }
250     }
251   }
252 }
253
254 void ExchangePlugin_ImportFeature::setColorGroup(
255                                         std::shared_ptr<ModelAPI_ResultBody> theResultBody,
256                                         std::vector<int> &theColor,
257                                         const std::wstring& theName )
258 {
259   std::vector<int> aColor;
260   std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
261
262    // group name
263   aGroupFeature->data()->setName(theName);
264
265   // fill selection
266   AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
267
268   ModelAPI_Tools::getColor(theResultBody, aColor);
269   if (!aColor.empty()){
270     if (aColor == theColor) {
271       GeomShapePtr aShape = theResultBody->shape();
272       aSelectionList->setSelectionType(aShape->shapeTypeStr());
273       aSelectionList->append(theResultBody,aShape);
274     }
275   }
276   // add element with the same color
277   std::list<ResultPtr> allRes;
278   ModelAPI_Tools::allSubs(theResultBody, allRes);
279   for(std::list<ResultPtr>::iterator aRes = allRes.begin();
280       aRes != allRes.end(); ++aRes) {
281     ModelAPI_Tools::getColor(*aRes, aColor);
282     GeomShapePtr aShape = (*aRes)->shape();
283
284     if (!aColor.empty()){
285       if (aRes->get() &&  aColor == theColor) {
286         if (aShape->isCompound() || aShape->isCompSolid()) {
287           GeomAPI_ShapeIterator anIt(aShape);
288           for (; anIt.more(); anIt.next()) {
289             aSelectionList->setSelectionType(anIt.current()->shapeTypeStr());
290             aSelectionList->append(theResultBody,anIt.current());
291           }
292         } else {
293           aSelectionList->setSelectionType(aShape->shapeTypeStr());
294           aSelectionList->append(theResultBody,aShape);
295         }
296       }
297     }
298   }
299
300   // Create the group in the document to be able to set its color
301   ResultPtr aGroup = document()->createGroup(aGroupFeature->data());
302   aGroupFeature->setResult(aGroup);
303
304   ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor);
305
306   if (aSelectionList->size() == 0) {
307     document()->removeFeature(aGroupFeature);
308   }
309 }
310
311 void ExchangePlugin_ImportFeature::setMaterielGroup(
312                                 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
313                                 std::map< std::wstring,std::list<std::wstring>>& theMaterialShape)
314 {
315   std::map< std::wstring, std::list<std::wstring>>::iterator anIt;
316   for (anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) {
317
318     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
319     // group name
320     aGroupFeature->data()->setName((*anIt).first);
321
322     // fill selection
323     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
324
325     std::list<ResultPtr> allRes;
326     ModelAPI_Tools::allSubs(theResultBody, allRes);
327     for (std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
328
329       GeomShapePtr aShape = (*aRes)->shape();
330       for (std::list<std::wstring>::iterator aResMat = anIt->second.begin();
331                                  aResMat != anIt->second.end(); ++aResMat) {
332         if (aRes->get() && ((*aRes)->data()->name() == (*aResMat)))
333         {
334           if (aShape->isCompound() || aShape->isCompSolid()) {
335             GeomAPI_ShapeIterator anIt(aShape);
336             for (; anIt.more(); anIt.next()) {
337               aSelectionList->setSelectionType(anIt.current()->shapeTypeStr());
338               aSelectionList->append(theResultBody,anIt.current());
339             }
340           } else {
341             aSelectionList->setSelectionType(aShape->shapeTypeStr());
342             aSelectionList->append(theResultBody,aShape);
343           }
344           break;
345         }
346       }
347     }
348     if (aSelectionList->size() == 0){
349       document()->removeFeature(aGroupFeature);
350     }
351   }
352 }
353
354 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
355 {
356   try {
357   std::string anError;
358
359   XAO::Xao aXao;
360   std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
361
362   if (!anError.empty()) {
363     setError("An error occurred while importing " + theFileName + ": " + anError);
364     return;
365   }
366
367   XAO::Geometry* aXaoGeometry = aXao.getGeometry();
368
369   // use the geometry name or the file name for the feature
370   std::string aBodyName = aXaoGeometry->getName();
371   if (aBodyName.empty())
372     aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
373   data()->setName(Locale::Convert::toWString(aBodyName));
374
375   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
376   setResult(aResultBody);
377
378   // Process groups/fields
379   std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
380       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
381
382   // Remove previous groups/fields stored in RefList
383   std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
384   std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
385   for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
386     std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
387     if (aFeature)
388       document()->removeFeature(aFeature);
389   }
390
391   // Create new groups
392   for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
393     XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
394
395     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
396
397     // group name
398     if (!aXaoGroup->getName().empty())
399       aGroupFeature->data()->setName(Locale::Convert::toWString(aXaoGroup->getName()));
400
401     // fill selection
402     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
403
404     // conversion of dimension
405     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
406     std::string aSelectionType =
407       ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
408
409     aSelectionList->setSelectionType(aSelectionType);
410     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
411       aSelectionList->append(aResultBody, GeomShapePtr());
412       // complex conversion of element index to reference id
413       int anElementID = aXaoGroup->get(anElementIndex);
414       std::string aReferenceString =
415         aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
416       int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
417
418       aSelectionList->value(anElementIndex)->setId(aReferenceID);
419     }
420   }
421   // Create new fields
422   for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
423     XAO::Field* aXaoField = aXao.getField(aFieldIndex);
424
425     std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
426
427     // group name
428     if (!aXaoField->getName().empty())
429       aFieldFeature->data()->setName(Locale::Convert::toWString(aXaoField->getName()));
430
431     // fill selection
432     AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
433
434     // conversion of dimension
435     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
436     std::string aSelectionType =
437       ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
438     aSelectionList->setSelectionType(aSelectionType);
439     // limitation: now in XAO fields are related to everything, so, iterate all sub-shapes to fill
440     int aCountSelected = aXaoField->countElements();
441     std::list<ResultPtr>::const_iterator aResIter = results().begin();
442     for(; aResIter != results().end() && aCountSelected; aResIter++) {
443       ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aResIter);
444       if (!aBody.get())
445         continue;
446       // check that only results that were created before this field are used
447       FeaturePtr aResultFeature = document()->feature(aBody);
448       if (!aResultFeature.get())
449         continue;
450       GeomShapePtr aShape = aBody->shape();
451       if (!aShape.get() || aShape->isNull())
452         continue;
453       GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::shapeTypeByStr(aSelectionType));
454       for(; anExp.more(); anExp.next()) {
455         aSelectionList->append(aBody, anExp.current());
456         aCountSelected--;
457         if (aCountSelected == 0)
458           break;
459       }
460     }
461
462     // conversion of type
463     XAO::Type aFieldType = aXaoField->getType();
464     std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
465     ModelAPI_AttributeTables::ValueType aType =
466       ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
467     // set components names
468     AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
469     aComponents->setSize(aXaoField->countComponents());
470     for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
471       aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
472     }
473
474     AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
475     aStamps->setSize(aXaoField->countSteps());
476     std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
477     aTables->setSize(
478       aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
479     aTables->setType(aType);
480     // iterate steps
481     XAO::stepIterator aStepIter = aXaoField->begin();
482     for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
483       aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
484       for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
485         for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
486           ModelAPI_AttributeTables::Value aVal;
487           std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
488           switch(aType) {
489           case ModelAPI_AttributeTables::BOOLEAN:
490             aVal.myBool = aValStr == "true";
491             break;
492           case ModelAPI_AttributeTables::INTEGER:
493             aVal.myInt = atoi(aValStr.c_str());
494             break;
495           case ModelAPI_AttributeTables::DOUBLE:
496             aVal.myDouble = atof(aValStr.c_str());
497             break;
498           case ModelAPI_AttributeTables::STRING:
499             aVal.myStr = aValStr;
500             break;
501           }
502           aTables->setValue(aVal, aRow, aCol, aStepIndex);
503         }
504       }
505     }
506     // remove everything with zero-values: zeroes are treated as defaults
507     std::set<int> aRowsToRemove;
508     for(int aRow = 1; aRow < aTables->rows(); aRow++) {
509       bool isZero = true;
510       for(int aCol = 0; aCol < aTables->columns() && isZero; aCol++) {
511         for(int aStepIndex = 0; aStepIndex != aTables->tables() && isZero; aStepIndex++) {
512           if (aTables->valueStr(aRow, aCol, aStepIndex) != aTables->valueStr(0, aCol, aStepIndex))
513             isZero = false;
514         }
515       }
516       if (isZero)
517         aRowsToRemove.insert(aRow - 1); // -1 to make prepared for remove from SelectionList
518     }
519     if (!aRowsToRemove.empty()) { // move usefull rows on bottom to the up of the tables
520       // number of rows passed during going through: the current rows will
521       // be moved up for this value
522       int aRemovedPassed = 0;
523       for(int aRow = 1; aRow < aTables->rows(); aRow++) {
524         if (aRowsToRemove.find(aRow - 1) != aRowsToRemove.end()) {
525           aRemovedPassed++;
526         } else if (aRemovedPassed != 0) { // copy the line up
527           for(int aCol = 0; aCol < aTables->columns(); aCol++) {
528             for(int aTable = 0; aTable != aTables->tables(); aTable++) {
529               aTables->setValue(
530                 aTables->value(aRow, aCol, aTable), aRow - aRemovedPassed, aCol, aTable);
531             }
532           }
533         }
534       }
535       aTables->setSize(aTables->rows() - aRemovedPassed, aTables->columns(), aTables->tables());
536       aSelectionList->remove(aRowsToRemove); // remove also selected elements
537     }
538   }
539   // Top avoid problems in Object Browser update: issue #1647.
540   ModelAPI_EventCreator::get()->sendReordered(
541     std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
542
543 // LCOV_EXCL_START
544   } catch (XAO::XAO_Exception& e) {
545     std::string anError = e.what();
546     setError("An error occurred while importing " + theFileName + ": " + anError);
547     return;
548   }
549 // LCOV_EXCL_STOP
550 }
551
552 //============================================================================
553 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
554     std::string theID)
555 {
556   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
557   if (aNew)
558     data()->reflist(FEATURES_ID())->append(aNew);
559   // set as current also after it becomes sub to set correctly enabled for other subs
560   //document()->setCurrentFeature(aNew, false);
561   return aNew;
562 }
563
564 // LCOV_EXCL_START
565 void ExchangePlugin_ImportFeature::removeFeature(
566     std::shared_ptr<ModelAPI_Feature> theFeature)
567 {
568   if (!data()->isValid())
569     return;
570   AttributeRefListPtr aList = reflist(FEATURES_ID());
571   aList->remove(theFeature);
572 }
573 // LCOV_EXCL_STOP
574
575 int ExchangePlugin_ImportFeature::numberOfSubs(bool /*forTree*/) const
576 {
577   return data()->reflist(FEATURES_ID())->size(true);
578 }
579
580 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
581     const int theIndex, bool /*forTree*/)
582 {
583   ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
584   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
585   return aRes;
586 }
587
588 // LCOV_EXCL_START
589 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
590 {
591   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
592       ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
593   std::list<ObjectPtr> aFeatures = aRefList->list();
594   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
595   int aResultIndex = 1; // number of the counted (created) features, started from 1
596   int aFeatureIndex = -1; // number of the not-empty features in the list
597   for (; anIt != aFeatures.end(); anIt++) {
598     if (anIt->get())
599       aFeatureIndex++;
600     if (aFeatureIndex == theIndex)
601       break;
602     aResultIndex++;
603   }
604   return aResultIndex;
605 }
606 // LCOV_EXCL_STOP
607
608 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
609 {
610   // check is this feature of result
611   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
612   if (aFeature)
613     return data()->reflist(FEATURES_ID())->isInList(aFeature);
614   return false;
615 }
616
617 //============================================================================
618 void ExchangePlugin_ImportFeature::loadNamingDS(
619     std::shared_ptr<GeomAPI_Shape> theGeomShape,
620     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
621 {
622   //load result
623   theResultBody->store(theGeomShape);
624   std::string aNameMS = "Shape";
625   theResultBody->loadFirstLevel(theGeomShape, aNameMS);
626 }