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