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