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