]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
fix coding style
[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   loadNamingDS(aGeomShape, aResult);
210
211   // create color group
212   if (anColorGroupSelected)
213   {
214     setColorGroups(aResult);
215   }
216
217   // create Materiel 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         aSelectionList->setSelectionType(aShape->shapeTypeStr());
293         aSelectionList->append(theResultBody,aShape);
294       }
295     }
296   }
297
298   // Create the group in the document to be able to set its color
299   ResultPtr aGroup = document()->createGroup(aGroupFeature->data());
300   aGroupFeature->setResult(aGroup);
301
302   ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor);
303
304   if (aSelectionList->size() == 0) {
305     document()->removeFeature(aGroupFeature);
306   }
307 }
308
309 void ExchangePlugin_ImportFeature::setMaterielGroup(
310                                 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
311                                 std::map< std::wstring,std::list<std::wstring>>& theMaterialShape)
312 {
313   std::map< std::wstring, std::list<std::wstring>>::iterator anIt;
314   for (anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) {
315
316     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
317     // group name
318     aGroupFeature->data()->setName((*anIt).first);
319
320     // fill selection
321     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
322
323     std::list<ResultPtr> allRes;
324     ModelAPI_Tools::allSubs(theResultBody, allRes);
325     for (std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
326
327       GeomShapePtr aShape = (*aRes)->shape();
328       for (std::list<std::wstring>::iterator aResMat = anIt->second.begin();
329                                  aResMat != anIt->second.end(); ++aResMat) {
330         if (aRes->get() && ((*aRes)->data()->name() == (*aResMat)))
331         {
332           aSelectionList->append(theResultBody,aShape);
333           aSelectionList->setSelectionType(aShape->shapeTypeStr());
334           break;
335         }
336       }
337     }
338     if (aSelectionList->size() == 0){
339       document()->removeFeature(aGroupFeature);
340     }
341   }
342 }
343
344 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
345 {
346   try {
347   std::string anError;
348
349   XAO::Xao aXao;
350   std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
351
352   if (!anError.empty()) {
353     setError("An error occurred while importing " + theFileName + ": " + anError);
354     return;
355   }
356
357   XAO::Geometry* aXaoGeometry = aXao.getGeometry();
358
359   // use the geometry name or the file name for the feature
360   std::string aBodyName = aXaoGeometry->getName();
361   if (aBodyName.empty())
362     aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
363   data()->setName(Locale::Convert::toWString(aBodyName));
364
365   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
366   setResult(aResultBody);
367
368   // Process groups/fields
369   std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
370       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
371
372   // Remove previous groups/fields stored in RefList
373   std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
374   std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
375   for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
376     std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
377     if (aFeature)
378       document()->removeFeature(aFeature);
379   }
380
381   // Create new groups
382   for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
383     XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
384
385     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
386
387     // group name
388     if (!aXaoGroup->getName().empty())
389       aGroupFeature->data()->setName(Locale::Convert::toWString(aXaoGroup->getName()));
390
391     // fill selection
392     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
393
394     // conversion of dimension
395     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
396     std::string aSelectionType =
397       ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
398
399     aSelectionList->setSelectionType(aSelectionType);
400     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
401       aSelectionList->append(aResultBody, GeomShapePtr());
402       // complex conversion of element index to reference id
403       int anElementID = aXaoGroup->get(anElementIndex);
404       std::string aReferenceString =
405         aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
406       int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
407
408       aSelectionList->value(anElementIndex)->setId(aReferenceID);
409     }
410   }
411   // Create new fields
412   for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
413     XAO::Field* aXaoField = aXao.getField(aFieldIndex);
414
415     std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
416
417     // group name
418     if (!aXaoField->getName().empty())
419       aFieldFeature->data()->setName(Locale::Convert::toWString(aXaoField->getName()));
420
421     // fill selection
422     AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
423
424     // conversion of dimension
425     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
426     std::string aSelectionType =
427       ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
428     aSelectionList->setSelectionType(aSelectionType);
429     // limitation: now in XAO fields are related to everything, so, iterate all sub-shapes to fill
430     int aCountSelected = aXaoField->countElements();
431     std::list<ResultPtr>::const_iterator aResIter = results().begin();
432     for(; aResIter != results().end() && aCountSelected; aResIter++) {
433       ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aResIter);
434       if (!aBody.get())
435         continue;
436       // check that only results that were created before this field are used
437       FeaturePtr aResultFeature = document()->feature(aBody);
438       if (!aResultFeature.get())
439         continue;
440       GeomShapePtr aShape = aBody->shape();
441       if (!aShape.get() || aShape->isNull())
442         continue;
443       GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::shapeTypeByStr(aSelectionType));
444       for(; anExp.more(); anExp.next()) {
445         aSelectionList->append(aBody, anExp.current());
446         aCountSelected--;
447         if (aCountSelected == 0)
448           break;
449       }
450     }
451
452     // conversion of type
453     XAO::Type aFieldType = aXaoField->getType();
454     std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
455     ModelAPI_AttributeTables::ValueType aType =
456       ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
457     // set components names
458     AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
459     aComponents->setSize(aXaoField->countComponents());
460     for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
461       aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
462     }
463
464     AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
465     aStamps->setSize(aXaoField->countSteps());
466     std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
467     aTables->setSize(
468       aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
469     aTables->setType(aType);
470     // iterate steps
471     XAO::stepIterator aStepIter = aXaoField->begin();
472     for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
473       aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
474       for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
475         for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
476           ModelAPI_AttributeTables::Value aVal;
477           std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
478           switch(aType) {
479           case ModelAPI_AttributeTables::BOOLEAN:
480             aVal.myBool = aValStr == "true";
481             break;
482           case ModelAPI_AttributeTables::INTEGER:
483             aVal.myInt = atoi(aValStr.c_str());
484             break;
485           case ModelAPI_AttributeTables::DOUBLE:
486             aVal.myDouble = atof(aValStr.c_str());
487             break;
488           case ModelAPI_AttributeTables::STRING:
489             aVal.myStr = aValStr;
490             break;
491           }
492           aTables->setValue(aVal, aRow, aCol, aStepIndex);
493         }
494       }
495     }
496     // remove everything with zero-values: zeroes are treated as defaults
497     std::set<int> aRowsToRemove;
498     for(int aRow = 1; aRow < aTables->rows(); aRow++) {
499       bool isZero = true;
500       for(int aCol = 0; aCol < aTables->columns() && isZero; aCol++) {
501         for(int aStepIndex = 0; aStepIndex != aTables->tables() && isZero; aStepIndex++) {
502           if (aTables->valueStr(aRow, aCol, aStepIndex) != aTables->valueStr(0, aCol, aStepIndex))
503             isZero = false;
504         }
505       }
506       if (isZero)
507         aRowsToRemove.insert(aRow - 1); // -1 to make prepared for remove from SelectionList
508     }
509     if (!aRowsToRemove.empty()) { // move usefull rows on bottom to the up of the tables
510       // number of rows passed during going through: the current rows will
511       // be moved up for this value
512       int aRemovedPassed = 0;
513       for(int aRow = 1; aRow < aTables->rows(); aRow++) {
514         if (aRowsToRemove.find(aRow - 1) != aRowsToRemove.end()) {
515           aRemovedPassed++;
516         } else if (aRemovedPassed != 0) { // copy the line up
517           for(int aCol = 0; aCol < aTables->columns(); aCol++) {
518             for(int aTable = 0; aTable != aTables->tables(); aTable++) {
519               aTables->setValue(
520                 aTables->value(aRow, aCol, aTable), aRow - aRemovedPassed, aCol, aTable);
521             }
522           }
523         }
524       }
525       aTables->setSize(aTables->rows() - aRemovedPassed, aTables->columns(), aTables->tables());
526       aSelectionList->remove(aRowsToRemove); // remove also selected elements
527     }
528   }
529   // Top avoid problems in Object Browser update: issue #1647.
530   ModelAPI_EventCreator::get()->sendReordered(
531     std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
532
533 // LCOV_EXCL_START
534   } catch (XAO::XAO_Exception& e) {
535     std::string anError = e.what();
536     setError("An error occurred while importing " + theFileName + ": " + anError);
537     return;
538   }
539 // LCOV_EXCL_STOP
540 }
541
542 //============================================================================
543 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeatureBase::addFeature(
544     std::string theID)
545 {
546   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
547   if (aNew)
548     data()->reflist(FEATURES_ID())->append(aNew);
549   // set as current also after it becomes sub to set correctly enabled for other subs
550   //document()->setCurrentFeature(aNew, false);
551   return aNew;
552 }
553
554 // LCOV_EXCL_START
555 void ExchangePlugin_ImportFeatureBase::removeFeature(
556     std::shared_ptr<ModelAPI_Feature> theFeature)
557 {
558   if (!data()->isValid())
559     return;
560   AttributeRefListPtr aList = reflist(FEATURES_ID());
561   aList->remove(theFeature);
562 }
563 // LCOV_EXCL_STOP
564
565 int ExchangePlugin_ImportFeatureBase::numberOfSubs(bool /*forTree*/) const
566 {
567   return data()->reflist(FEATURES_ID())->size(true);
568 }
569
570 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeatureBase::subFeature(
571     const int theIndex, bool /*forTree*/)
572 {
573   ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
574   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
575   return aRes;
576 }
577
578 // LCOV_EXCL_START
579 int ExchangePlugin_ImportFeatureBase::subFeatureId(const int theIndex) const
580 {
581   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
582       ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
583   std::list<ObjectPtr> aFeatures = aRefList->list();
584   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
585   int aResultIndex = 1; // number of the counted (created) features, started from 1
586   int aFeatureIndex = -1; // number of the not-empty features in the list
587   for (; anIt != aFeatures.end(); anIt++) {
588     if (anIt->get())
589       aFeatureIndex++;
590     if (aFeatureIndex == theIndex)
591       break;
592     aResultIndex++;
593   }
594   return aResultIndex;
595 }
596 // LCOV_EXCL_STOP
597
598 bool ExchangePlugin_ImportFeatureBase::isSub(ObjectPtr theObject) const
599 {
600   // check is this feature of result
601   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
602   if (aFeature)
603     return data()->reflist(FEATURES_ID())->isInList(aFeature);
604   return false;
605 }
606
607 //============================================================================
608 void ExchangePlugin_ImportFeatureBase::loadNamingDS(
609     std::shared_ptr<GeomAPI_Shape> theGeomShape,
610     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
611 {
612   //load result
613   theResultBody->store(theGeomShape);
614
615   std::string aNameMS = "Shape";
616   theResultBody->loadFirstLevel(theGeomShape, aNameMS);
617 }
618
619 void ExchangePlugin_Import_ImageFeature::importFile(const std::string& theFileName)
620 {
621
622   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
623   std::string theTextureFileName = "";
624   // Perform the import
625   std::string anError;
626   std::shared_ptr<GeomAPI_Shape> aGeomShape;
627    if (anExtension == "PNG" || anExtension == "GIF" ||
628              anExtension == "TIFF" || anExtension == "JPE" ||
629              anExtension == "JPG" || anExtension == "JPEG" ||
630              anExtension == "BMP"|| anExtension == "PPM"
631              ) {
632      aGeomShape = ImageImport(theFileName, anError);
633      if(anError == "")
634        theTextureFileName = theFileName;
635     } else {
636     anError = "Unsupported format: " + anExtension;
637   }
638
639   // Check if shape is valid
640   if (!anError.empty()) {
641     setError("An error occurred while importing " + theFileName + ": " + anError);
642     return;
643   }
644
645   // Pass the results into the model
646   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
647   data()->setName(Locale::Convert::toWString(anObjectName));
648
649   auto resultBody = createResultBody(aGeomShape);
650   resultBody->setTextureFile(theTextureFileName);
651   setResult(resultBody);
652 }