Salome HOME
3924fb73376db6af1bb9c53a4f193fc5a8c5b807
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.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_ExportFeature.h>
21
22 #include <algorithm>
23 #include <iterator>
24 #include <string>
25 #ifdef _DEBUG
26 #include <iostream>
27 #include <ostream>
28 #endif
29
30 #include <Config_Common.h>
31 #include <Config_PropManager.h>
32
33 #include <GeomAlgoAPI_BREPExport.h>
34 #include <GeomAlgoAPI_CompoundBuilder.h>
35 #include <GeomAlgoAPI_IGESExport.h>
36 #include <GeomAlgoAPI_STEPExport.h>
37 #include <GeomAlgoAPI_STLExport.h>
38 #include <GeomAlgoAPI_Tools.h>
39 #include <GeomAlgoAPI_XAOExport.h>
40
41 #include <GeomAPI_IndexedMapOfShape.h>
42 #include <GeomAPI_Shape.h>
43 #include <GeomAPI_ShapeExplorer.h>
44 #include <GeomAPI_Trsf.h>
45
46 #include <Locale_Convert.h>
47
48 #include <ModelAPI_AttributeSelectionList.h>
49 #include <ModelAPI_AttributeString.h>
50 #include <ModelAPI_AttributeStringArray.h>
51 #include <ModelAPI_AttributeIntArray.h>
52 #include <ModelAPI_AttributeTables.h>
53 #include <ModelAPI_AttributeDouble.h>
54 #include <ModelAPI_AttributeBoolean.h>
55 #include <ModelAPI_Data.h>
56 #include <ModelAPI_Document.h>
57 #include <ModelAPI_Object.h>
58 #include <ModelAPI_ResultBody.h>
59 #include <ModelAPI_ResultPart.h>
60 #include <ModelAPI_ResultGroup.h>
61 #include <ModelAPI_ResultField.h>
62 #include <ModelAPI_Session.h>
63 #include <ModelAPI_Validator.h>
64 #include <ModelAPI_Tools.h>
65
66 #include <Events_InfoMessage.h>
67
68 #include <XAO_Group.hxx>
69 #include <XAO_Field.hxx>
70 #include <XAO_Xao.hxx>
71 #include <XAO_Geometry.hxx>
72
73 #include <ExchangePlugin_Tools.h>
74
75 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
76 {
77 }
78
79 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
80 {
81   // TODO Auto-generated destructor stub
82 }
83
84 /*
85  * Request for initialization of data model of the feature: adding all attributes
86  */
87 void ExchangePlugin_ExportFeature::initAttributes()
88 {
89   data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(),
90     ModelAPI_AttributeString::typeId());
91   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
92     ModelAPI_AttributeString::typeId());
93   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
94     ModelAPI_AttributeString::typeId());
95   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
96     ModelAPI_AttributeString::typeId());
97   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
98     ModelAPI_AttributeSelectionList::typeId());
99   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
100     ModelAPI_AttributeString::typeId());
101   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
102     ModelAPI_AttributeString::typeId());
103   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID(),
104     ModelAPI_AttributeSelectionList::typeId());
105   data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID(),
106     ModelAPI_AttributeString::typeId());
107   data()->addAttribute(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED(),
108     ModelAPI_AttributeSelection::typeId());
109   data()->addAttribute(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE(),
110    ModelAPI_AttributeString::typeId());
111   data()->addAttribute(ExchangePlugin_ExportFeature::STL_RELATIVE(),
112     ModelAPI_AttributeDouble::typeId());
113
114   double defelection = Config_PropManager::real("Visualization", "body_deflection");
115   real(ExchangePlugin_ExportFeature::STL_RELATIVE())->setValue(defelection);
116
117   data()->addAttribute(ExchangePlugin_ExportFeature::STL_ABSOLUTE(),
118     ModelAPI_AttributeDouble::typeId());
119   data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE(),
120    ModelAPI_AttributeString::typeId());
121
122   // export to memory buffer (implemented for XAO format only)
123   data()->addAttribute(ExchangePlugin_ExportFeature::MEMORY_BUFFER_ID(),
124                        ModelAPI_AttributeString::typeId());
125
126   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
127     ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
128   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
129     ExchangePlugin_ExportFeature::STL_FILE_PATH_ID());
130   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
131     ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
132   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
133     ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
134   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
135     ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID());
136   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
137     ExchangePlugin_ExportFeature::MEMORY_BUFFER_ID());
138
139   // to support previous version of document, move the selection list
140   // if the type of export operation is XAO
141   AttributeStringPtr aTypeAttr = string(EXPORT_TYPE_ID());
142   if (aTypeAttr->isInitialized() &&
143       (aTypeAttr->value() == "XAO" || aTypeAttr->value() == "XAOMem")) {
144     bool aWasBlocked = data()->blockSendAttributeUpdated(true, false);
145     AttributeSelectionListPtr aSelList = selectionList(SELECTION_LIST_ID());
146     AttributeSelectionListPtr aXAOSelList = selectionList(XAO_SELECTION_LIST_ID());
147     if (aSelList->size() > 0 && aXAOSelList->size() == 0)
148       aSelList->copyTo(aXAOSelList);
149     aSelList->clear();
150     data()->blockSendAttributeUpdated(aWasBlocked, false);
151   }
152 }
153
154 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
155 {
156   if (theID == XAO_FILE_PATH_ID()) {
157     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
158       string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
159   }
160   else if (theID == STL_FILE_PATH_ID()) {
161     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
162       string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID())->value());
163   }
164 }
165
166 /*
167  * Computes or recomputes the results
168  */
169 void ExchangePlugin_ExportFeature::execute()
170 {
171   AttributeStringPtr aFormatAttr =
172       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
173   std::string aFormat = aFormatAttr->value();
174
175   bool isMemoryExport = false;
176   AttributeStringPtr aTypeAttr = string(EXPORT_TYPE_ID());
177   if (aTypeAttr->isInitialized() && aTypeAttr->value() == "XAOMem")
178     isMemoryExport = true;
179
180   AttributeStringPtr aFilePathAttr =
181       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
182   std::string aFilePath = aFilePathAttr->value();
183   if (aFilePath.empty() && !isMemoryExport)
184     return;
185
186   exportFile(aFilePath, aFormat, isMemoryExport);
187 }
188
189 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
190                                               const std::string& theFormat,
191                                               const bool         isMemoryExport)
192 {
193   std::string aFormatName = theFormat;
194
195   if (aFormatName.empty()) { // get default format for the extension
196     if (isMemoryExport) return;
197
198     // ".brep" -> "BREP"
199     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
200     if (anExtension == "BREP" || anExtension == "BRP") {
201       aFormatName = "BREP";
202     } else if (anExtension == "STEP" || anExtension == "STP") {
203       aFormatName = "STEP";
204     } else if (anExtension == "IGES" || anExtension == "IGS") {
205       aFormatName = "IGES-5.1";
206     } else {
207       aFormatName = anExtension;
208     }
209   }
210
211   if (aFormatName == "XAO") {
212     exportXAO(theFileName, isMemoryExport);
213     return;
214   } else if (aFormatName == "STL") {
215     exportSTL(theFileName);
216     return;
217   }
218
219   // make shape for export from selected shapes
220   AttributeSelectionListPtr aSelectionListAttr =
221       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
222   std::list<GeomShapePtr> aShapes;
223   std::list<ResultPtr> aContexts;
224   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
225     AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
226
227     /// do not export pictures
228     ResultPtr aBodyContext =
229       std::dynamic_pointer_cast<ModelAPI_Result>(anAttrSelection->context());
230     if (aBodyContext.get() && aBodyContext->hasTexture())
231       continue;
232
233     std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
234     if (aCurShape.get() == NULL)
235       aCurShape = anAttrSelection->context()->shape();
236     if (aCurShape.get() != NULL)
237     {
238       aShapes.push_back(aCurShape);
239       aContexts.push_back(anAttrSelection->context());
240     }
241   }
242
243   // Store compound if we have more than one shape.
244   std::shared_ptr<GeomAPI_Shape> aShape =
245     aShapes.size() == 1 ? aShapes.front() : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
246
247   // Perform the export
248   std::string anError;
249   bool aResult = false;
250   if (aFormatName == "BREP") {
251     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
252   } else if (aFormatName == "STEP") {
253     aResult = STEPExport(theFileName, aShapes, aContexts, anError);
254   } else if (aFormatName.substr(0, 4) == "IGES") {
255     aResult = IGESExport(theFileName, aFormatName, aShape, anError);
256   } else {
257     anError = "Unsupported format: " + aFormatName;
258   }
259
260   if (!aResult || !anError.empty()) {
261     setError("An error occurred while exporting " + theFileName + ": " + anError);
262     return;
263   }
264 }
265
266 /// Returns XAO string by the value from the table
267 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
268                                const ModelAPI_AttributeTables::ValueType& theType)
269 {
270   std::ostringstream aStr; // the resulting string value
271   switch(theType) {
272   case ModelAPI_AttributeTables::BOOLEAN:
273     aStr<<(theVal.myBool ? "true" : "false");
274     break;
275   case ModelAPI_AttributeTables::INTEGER:
276     aStr<<theVal.myInt;
277     break;
278   case ModelAPI_AttributeTables::DOUBLE:
279     aStr<<theVal.myDouble;
280     break;
281   case ModelAPI_AttributeTables::STRING:
282     aStr<<theVal.myStr;
283     break;
284   }
285   return aStr.str();
286 }
287
288 void ExchangePlugin_ExportFeature::exportSTL(const std::string& theFileName)
289 {
290   // Get shape.
291   AttributeSelectionPtr aSelection = selection(STL_OBJECT_SELECTED());
292   GeomShapePtr aShape = aSelection->value();
293   if (!aShape.get()) {
294     aShape = aSelection->context()->shape();
295   }
296
297   // Get relative value and percent flag.
298   double aValue;
299   bool anIsRelative = false;
300   bool anIsASCII = false;
301
302   if (string(STL_DEFLECTION_TYPE())->value() == STL_DEFLECTION_TYPE_RELATIVE()) {
303     aValue = real(STL_RELATIVE())->value();
304     anIsRelative = true;
305   } else {
306     aValue = real(STL_ABSOLUTE())->value();
307   }
308
309   if (string(STL_FILE_TYPE())->value() == STL_FILE_TYPE_ASCII()) {
310     anIsASCII = true;
311   }
312   // Perform the export
313   std::string anError;
314   bool aResult = false;
315
316   aResult = STLExport(theFileName,
317                       aShape,
318                       aValue,
319                       anIsRelative,
320                       anIsASCII,
321                       anError);
322
323   if (!aResult || !anError.empty()) {
324     setError("An error occurred while exporting " + theFileName + ": " + anError);
325     return;
326   }
327 }
328
329
330 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName,
331                                              const bool         isMemoryExport)
332 {
333   try {
334
335   std::string anError;
336   XAO::Xao aXao;
337
338   // author
339
340   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
341   aXao.setAuthor(anAuthor);
342
343   // make shape for export from all results
344   std::list<GeomShapePtr> aShapes;
345   std::list<ResultPtr> aResults;
346   std::list<DocumentPtr> aDocuments; /// documents of Parts selected and used in export
347   std::map<DocumentPtr, GeomTrsfPtr> aDocTrsf; /// translation of the part
348
349   bool anExCludedIsImage = false;
350   AttributeSelectionListPtr aSelection = selectionList(XAO_SELECTION_LIST_ID());
351   bool aIsSelection = aSelection->isInitialized() && aSelection->size() > 0;
352   if (aIsSelection) { // a mode for export to geom result by result
353     for (int a = 0; a < aSelection->size(); a++) {
354       AttributeSelectionPtr anAttr = aSelection->value(a);
355       ResultPtr aBodyContext =
356         std::dynamic_pointer_cast<ModelAPI_Result>(anAttr->context());
357       if (aBodyContext.get() && !aBodyContext->isDisabled() && aBodyContext->shape().get()) {
358           /// do not export pictures
359           if (aBodyContext->hasTexture()){
360             anExCludedIsImage = true;
361             continue;
362           }
363         aResults.push_back(aBodyContext);
364         GeomShapePtr aShape = anAttr->value();
365         if (!aShape.get())
366           aShape = aBodyContext->shape();
367         aShapes.push_back(aShape);
368         if (aBodyContext->groupName() == ModelAPI_ResultPart::group()) {
369           ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aBodyContext);
370           DocumentPtr aPartDoc = aResPart->partDoc();
371           if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
372             std::string msg = "Can not export XAO for not loaded part";
373             Events_InfoMessage("ExportFeature", msg, this).send();
374             return;
375           } else {
376             aDocuments.push_back(aPartDoc);
377             aDocTrsf[aPartDoc] = aResPart->summaryTrsf();
378           }
379         }
380       }
381     }
382   } else {
383     int aBodyCount = document()->size(ModelAPI_ResultBody::group());
384     for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
385       ResultBodyPtr aResultBody =
386           std::dynamic_pointer_cast<ModelAPI_ResultBody>(
387               document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
388       if (!aResultBody.get())
389         continue;
390       aShapes.push_back(aResultBody->shape());
391       aResults.push_back(aResultBody);
392     }
393   }
394   if (aShapes.empty()) {
395     if(!anExCludedIsImage)
396       setError("No shapes to export");
397     return;
398   }
399
400
401   GeomShapePtr aShape = (aShapes.size() == 1)
402       ? *aShapes.begin()
403       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
404
405   SetShapeToXAO(aShape, &aXao, anError);
406
407   if (!anError.empty()) {
408     setError("An error occurred while exporting " + theFileName + ": " + anError);
409     return;
410   }
411
412   // geometry name
413   std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
414   if (aGeometryName.empty() && aResults.size() == 1) {
415     // get the name from the first result
416     ResultPtr aResultBody = *aResults.begin();
417     aGeometryName = Locale::Convert::toString(aResultBody->data()->name());
418     if (isMemoryExport) {
419       // for python dump
420       string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->setValue(aGeometryName);
421       // or
422       //data()->setName(Locale::Convert::toWString(aGeometryName));
423     }
424   }
425
426   aXao.getGeometry()->setName(aGeometryName);
427
428   std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
429
430   // [bos #38360] [CEA] improve performances of exportXAO and PublishToStudy
431   GeomAPI_IndexedMapOfShape aSubShapesMap;
432   bool isSubShapesMap = false; // we will init it only if required (for performance reason)
433
434   // iterate all documents used
435   if (aDocuments.empty())
436     aDocuments.push_back(document());
437   std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
438   for(; aDoc != aDocuments.end(); aDoc++) {
439     // groups
440     int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
441     for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
442       ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
443           (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
444       if (!aResultGroup.get() || !aResultGroup->shape().get())
445         continue;
446
447       FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
448
449       AttributeSelectionListPtr aSelectionList =
450           aGroupFeature->selectionList("group_list");
451       if (!ModelAPI_Tools::isInResults(aSelectionList,
452                                        aResults,
453                                        allResultsCashed))// skip group not used in result
454         continue;
455
456       // conversion of dimension
457       std::string aSelectionType = aSelectionList->selectionType();
458       GeomAPI_Shape::ShapeType aSelType = GeomAPI_Shape::shapeTypeByStr(aSelectionType);
459       std::string aDimensionString =
460         ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
461       XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
462
463       XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
464         Locale::Convert::toString(aResultGroup->data()->name()));
465
466       try {
467         GeomAPI_ShapeExplorer aGroupResExplorer(aResultGroup->shape(), aSelType);
468         for(; aGroupResExplorer.more(); aGroupResExplorer.next()) {
469           GeomShapePtr aGroupShape = aGroupResExplorer.current();
470           if (aDocTrsf.find(*aDoc) != aDocTrsf.end())
471             aGroupShape->move(aDocTrsf[*aDoc]);
472
473           if (!isSubShapesMap) {
474             aSubShapesMap.MapShapes(aShape);
475             isSubShapesMap = true;
476           }
477           int aReferenceID = aSubShapesMap.FindIndexEqualLocations(aGroupShape);
478           if (aReferenceID == 0) // selected value does not found in the exported shape
479             continue;
480           std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
481           int anElementID =
482             aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
483           aXaoGroup->add(anElementID);
484         }
485       } catch (XAO::XAO_Exception& e) {
486         // LCOV_EXCL_START
487         std::string msg = "An error occurred while exporting group " +
488           Locale::Convert::toString(aResultGroup->data()->name());
489         msg += ".\n";
490         msg += e.what();
491         msg += "\n";
492         msg += "=> skipping this group from XAO export.";
493         Events_InfoMessage("ExportFeature", msg, this).send();
494         aXao.removeGroup(aXaoGroup);
495         // LCOV_EXCL_STOP
496       }
497     }
498
499     // fields
500     int aFieldCount = (*aDoc)->size(ModelAPI_ResultField::group());
501     for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
502       ResultFieldPtr aResultField = std::dynamic_pointer_cast<ModelAPI_ResultField>(
503         (*aDoc)->object(ModelAPI_ResultField::group(), aFieldIndex));
504
505       FeaturePtr aFieldFeature = (*aDoc)->feature(aResultField);
506
507       AttributeSelectionListPtr aSelectionList =
508           aFieldFeature->selectionList("selected");
509       std::string aSelectionType = aSelectionList->selectionType();
510       bool isWholePart = aSelectionType == "part";
511       // skip field not used in results
512       if (!isWholePart &&
513           !ModelAPI_Tools::isInResults(aSelectionList, aResults, allResultsCashed))
514         continue;
515
516       // conversion of dimension
517       std::string aDimensionString =
518         ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
519       XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
520       // get tables and their type
521       std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
522       std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
523       XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
524
525       XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
526         Locale::Convert::toString(aResultField->data()->name()));
527
528
529       try {
530         // set components names
531         AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
532         for(int aComp = 0; aComp < aComponents->size(); aComp++) {
533           std::string aName = aComponents->value(aComp);
534           aXaoField->setComponentName(aComp, aName);
535         }
536
537         AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
538         for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
539           XAO::Step* aStep = aXaoField->addNewStep(aStepIndex + 1);
540           aStep->setStep(aStepIndex + 1);
541           int aStampIndex = aStamps->value(aStepIndex);
542           aStep->setStamp(aStampIndex);
543           int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
544           int aNumComps = aTables->columns();
545           std::set<int> aFilledIDs; // to fill the rest by defaults
546           // omit default values first row
547           for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
548             for(int aCol = 0; aCol < aNumComps; aCol++) {
549               int anElementID = 0;
550               if (!isWholePart) {
551                 // element index actually is the ID of the selection
552                 AttributeSelectionPtr aSel = aSelectionList->value(aRow - 1);
553                 if (!isSubShapesMap) {
554                   aSubShapesMap.MapShapes(aShape);
555                   isSubShapesMap = true;
556                 }
557                 int aReferenceID = aSubShapesMap.FindIndexEqualLocations(aSel->value());
558                 if (aReferenceID == 0) // selected value does not found in the exported shape
559                   continue;
560
561                 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
562                 anElementID = aXao.getGeometry()->
563                   getElementIndexByReference(aFieldDimension, aReferenceString);
564               }
565
566               ModelAPI_AttributeTables::Value aVal = aTables->value(
567                 isWholePart ? 0 : aRow, aCol, aStepIndex);
568               std::string aStrVal = valToString(aVal, aTables->type());
569               aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
570               aFilledIDs.insert(anElementID);
571             }
572           }
573           if (!isWholePart) { // fill the rest values by default ones
574             XAO::GeometricElementList::iterator allElem =
575               aXao.getGeometry()->begin(aFieldDimension);
576             for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
577               if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
578                 continue;
579               for(int aCol = 0; aCol < aNumComps; aCol++) {
580                 // default value
581                 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex);
582                 std::string aStrVal = valToString(aVal, aTables->type());
583                 aStep->setStringValue(allElem->first, aCol, aStrVal);
584               }
585             }
586           }
587         }
588       } catch (XAO::XAO_Exception& e) {
589         // LCOV_EXCL_START
590         std::string msg = "An error occurred while exporting field " +
591           Locale::Convert::toString(aResultField->data()->name());
592         msg += ".\n";
593         msg += e.what();
594         msg += "\n";
595         msg += "=> skipping this field from XAO export.";
596         Events_InfoMessage("ExportFeature", msg, this).send();
597         aXao.removeField(aXaoField);
598         // LCOV_EXCL_STOP
599       }
600     }
601   }
602
603   // exporting
604   if (isMemoryExport) {
605     string(ExchangePlugin_ExportFeature::MEMORY_BUFFER_ID())->setValue(XAOExportMem(&aXao, anError));
606   }
607   else {
608     XAOExport(theFileName, &aXao, anError);
609   }
610
611   if (!anError.empty()) {
612     setError("An error occurred while exporting " + theFileName + ": " + anError);
613     return;
614   }
615
616 // LCOV_EXCL_START
617   } catch (XAO::XAO_Exception& e) {
618     std::string anError = e.what();
619     setError("An error occurred while exporting " + theFileName + ": " + anError);
620     return;
621   }
622 // LCOV_EXCL_STOP
623 }
624
625 bool ExchangePlugin_ExportFeature::isMacro() const
626 {
627   if (!data().get() || !data()->isValid())
628     return false;
629   ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
630   AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
631   std::string aFormat(aFormatAttr.get() ? aFormatAttr->value() : "");
632
633   if (aFormat.empty()) { // get default format for the extension
634     AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
635     std::string aFilePath = aFilePathAttr->value();
636     if (!aFilePath.empty()) {
637       std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
638       aFormat = anExtension;
639     }
640   }
641
642   if (aFormat == "XAO") { // on export to GEOM the selection attribute is filled - this is
643                           // an exceptional case where export to XAO feature must be kept
644     AttributeSelectionListPtr aList = aThis->selectionList(XAO_SELECTION_LIST_ID());
645     return !aList->isInitialized() || aList->size() == 0;
646   }
647   return true;
648 }