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