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