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