Salome HOME
6ae4199ec4da72ef2a973b569165cd84c4181210
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
1 // Copyright (C) 2014-2021  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
31 #include <Config_Common.h>
32 #include <Config_PropManager.h>
33
34 #include <GeomAlgoAPI_BREPExport.h>
35 #include <GeomAlgoAPI_CompoundBuilder.h>
36 #include <GeomAlgoAPI_IGESExport.h>
37 #include <GeomAlgoAPI_STEPExport.h>
38 #include <GeomAlgoAPI_STLExport.h>
39 #include <GeomAlgoAPI_Tools.h>
40 #include <GeomAlgoAPI_XAOExport.h>
41
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_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   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
122     ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
123   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
124     ExchangePlugin_ExportFeature::STL_FILE_PATH_ID());
125   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
126     ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
127   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
128     ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
129   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
130     ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID());
131
132   // to support previous version of document, move the selection list
133   // if the type of export operation is XAO
134   AttributeStringPtr aTypeAttr = string(EXPORT_TYPE_ID());
135   if (aTypeAttr->isInitialized() && aTypeAttr->value() == "XAO") {
136     bool aWasBlocked = data()->blockSendAttributeUpdated(true, false);
137     AttributeSelectionListPtr aSelList = selectionList(SELECTION_LIST_ID());
138     AttributeSelectionListPtr aXAOSelList = selectionList(XAO_SELECTION_LIST_ID());
139     if (aSelList->size() > 0 && aXAOSelList->size() == 0)
140       aSelList->copyTo(aXAOSelList);
141     aSelList->clear();
142     data()->blockSendAttributeUpdated(aWasBlocked, false);
143   }
144 }
145
146 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
147 {
148   if (theID == XAO_FILE_PATH_ID()) {
149     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
150       string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
151   }
152   else if (theID == STL_FILE_PATH_ID()) {
153     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
154       string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID())->value());
155   }
156
157 }
158
159 /*
160  * Computes or recomputes the results
161  */
162 void ExchangePlugin_ExportFeature::execute()
163 {
164   AttributeStringPtr aFormatAttr =
165       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
166   std::string aFormat = aFormatAttr->value();
167
168   AttributeStringPtr aFilePathAttr =
169       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
170   std::string aFilePath = aFilePathAttr->value();
171   if (aFilePath.empty())
172     return;
173
174   exportFile(aFilePath, aFormat);
175 }
176
177 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
178                                               const std::string& theFormat)
179 {
180   std::string aFormatName = theFormat;
181
182   if (aFormatName.empty()) { // get default format for the extension
183     // ".brep" -> "BREP"
184     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
185     if (anExtension == "BREP" || anExtension == "BRP") {
186       aFormatName = "BREP";
187     } else if (anExtension == "STEP" || anExtension == "STP") {
188       aFormatName = "STEP";
189     } else if (anExtension == "IGES" || anExtension == "IGS") {
190       aFormatName = "IGES-5.1";
191     } else {
192       aFormatName = anExtension;
193     }
194   }
195
196   if (aFormatName == "XAO") {
197     exportXAO(theFileName);
198     return;
199   }else if (aFormatName == "STL") {
200     exportSTL(theFileName);
201     return;
202   }
203
204   // make shape for export from selected shapes
205   AttributeSelectionListPtr aSelectionListAttr =
206       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
207   std::list<GeomShapePtr> aShapes;
208   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
209     AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
210
211     /// do not export pictures
212     ResultPtr aBodyContext =
213       std::dynamic_pointer_cast<ModelAPI_Result>(anAttrSelection->context());
214     if(aBodyContext->hasTextureFile())
215       continue;
216
217     std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
218     if (aCurShape.get() == NULL)
219       aCurShape = anAttrSelection->context()->shape();
220     if (aCurShape.get() != NULL)
221       aShapes.push_back(aCurShape);
222   }
223
224   // Store compound if we have more than one shape.
225   std::shared_ptr<GeomAPI_Shape> aShape =
226     aShapes.size() == 1 ? aShapes.front() : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
227
228   // Perform the export
229   std::string anError;
230   bool aResult = false;
231   if (aFormatName == "BREP") {
232     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
233   } else if (aFormatName == "STEP") {
234     aResult = STEPExport(theFileName, aFormatName, aShape, anError);
235   } else if (aFormatName.substr(0, 4) == "IGES") {
236     aResult = IGESExport(theFileName, aFormatName, aShape, anError);
237   } else {
238     anError = "Unsupported format: " + aFormatName;
239   }
240
241   if (!aResult || !anError.empty()) {
242     setError("An error occurred while exporting " + theFileName + ": " + anError);
243     return;
244   }
245 }
246
247 /// Returns XAO string by the value from the table
248 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
249   const ModelAPI_AttributeTables::ValueType& theType) {
250   std::ostringstream aStr; // the resulting string value
251   switch(theType) {
252   case ModelAPI_AttributeTables::BOOLEAN:
253     aStr<<(theVal.myBool ? "true" : "false");
254     break;
255   case ModelAPI_AttributeTables::INTEGER:
256     aStr<<theVal.myInt;
257     break;
258   case ModelAPI_AttributeTables::DOUBLE:
259     aStr<<theVal.myDouble;
260     break;
261   case ModelAPI_AttributeTables::STRING:
262     aStr<<theVal.myStr;
263     break;
264   }
265   return aStr.str();
266 }
267
268 /// Returns true if something in selection is presented in the results list
269 static bool isInResults(AttributeSelectionListPtr theSelection,
270                         const std::list<ResultPtr>& theResults,
271                         std::set<ResultPtr>& theCashedResults)
272 {
273   // collect all results into a cashed set
274   if (theCashedResults.empty()) {
275     std::list<ResultPtr>::const_iterator aRes = theResults.cbegin();
276     for(; aRes != theResults.cend(); aRes++) {
277       if (theCashedResults.count(*aRes))
278         continue;
279       else
280         theCashedResults.insert(*aRes);
281       if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
282         ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
283         std::list<ResultPtr> aResults;
284         ModelAPI_Tools::allSubs(aResBody, aResults, false);
285         for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
286           theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
287         }
288       } else if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { // all results of the part
289         ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
290         DocumentPtr aPartDoc = aResPart->partDoc();
291         if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
292           return false;
293         }
294         int aBodyCount = aPartDoc->size(ModelAPI_ResultBody::group());
295         for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
296           ResultBodyPtr aResBody =
297             std::dynamic_pointer_cast<ModelAPI_ResultBody>(
298               aPartDoc->object(ModelAPI_ResultBody::group(), aBodyIndex));
299           if (aResBody.get()) {
300             theCashedResults.insert(aResBody);
301             std::list<ResultPtr> aResults;
302             ModelAPI_Tools::allSubs(aResBody, aResults, false);
303             for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
304               theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
305             }
306           }
307         }
308       }
309     }
310   }
311   // if context is in results, return true
312   for(int a = 0; a < theSelection->size(); a++) {
313     AttributeSelectionPtr anAttr = theSelection->value(a);
314     ResultPtr aContext = anAttr->context();
315     // check is it group selected for groups BOP
316     if (aContext.get() && aContext->groupName() == ModelAPI_ResultGroup::group()) {
317       // it is impossible by used results check which result is used in this group result,
318       // so check the results shapes is it in results of this document or not
319       FeaturePtr aSelFeature =
320         std::dynamic_pointer_cast<ModelAPI_Feature>(theSelection->owner());
321       if (!aSelFeature.get() || aSelFeature->results().empty())
322         continue;
323       GeomShapePtr aGroupResShape = aSelFeature->firstResult()->shape();
324
325       std::set<ResultPtr>::iterator allResultsIter = theCashedResults.begin();
326       for(; allResultsIter != theCashedResults.end(); allResultsIter++) {
327         GeomShapePtr aResultShape = (*allResultsIter)->shape();
328
329         GeomAPI_Shape::ShapeType aType =
330           GeomAPI_Shape::shapeTypeByStr(theSelection->selectionType());
331         GeomAPI_ShapeExplorer aGroupResExp(aGroupResShape, aType);
332         for(; aGroupResExp.more(); aGroupResExp.next()) {
333           if (aResultShape->isSubShape(aGroupResExp.current(), false))
334             return true; // at least one shape of the group is in the used results
335         }
336       }
337     }
338     ResultBodyPtr aSelected = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anAttr->context());
339     if (!aSelected.get()) { // try to get selected feature and all its results
340       FeaturePtr aContextFeature = anAttr->contextFeature();
341       if (aContextFeature.get() && !aContextFeature->results().empty()) {
342         const std::list<ResultPtr>& allResluts = aContextFeature->results();
343         std::list<ResultPtr>::const_iterator aResIter = allResluts.cbegin();
344         for(; aResIter != allResluts.cend(); aResIter++) {
345           if (aResIter->get() && theCashedResults.count(*aResIter))
346             return true;
347         }
348       }
349     } else if (aSelected.get() && theCashedResults.count(aSelected))
350       return true;
351   }
352   return false;
353 }
354
355 void ExchangePlugin_ExportFeature::exportSTL(const std::string& theFileName)
356 {
357   // Get shape.
358   AttributeSelectionPtr aSelection = selection(STL_OBJECT_SELECTED());
359   GeomShapePtr aShape = aSelection->value();
360   if (!aShape.get()) {
361     aShape = aSelection->context()->shape();
362   }
363
364   // Get relative value and percent flag.
365   double aValue;
366   bool anIsRelative = false;
367   bool anIsASCII = false;
368
369   if (string(STL_DEFLECTION_TYPE())->value() == STL_DEFLECTION_TYPE_RELATIVE()) {
370     aValue = real(STL_RELATIVE())->value();
371     anIsRelative = true;
372   } else {
373     aValue = real(STL_ABSOLUTE())->value();
374   }
375
376   if (string(STL_FILE_TYPE())->value() == STL_FILE_TYPE_ASCII()) {
377     anIsASCII = true;
378   }
379   // Perform the export
380   std::string anError;
381   bool aResult = false;
382
383   aResult = STLExport(theFileName,
384                       aShape,
385                       aValue,
386                       anIsRelative,
387                       anIsASCII,
388                       anError);
389
390   if (!aResult || !anError.empty()) {
391     setError("An error occurred while exporting " + theFileName + ": " + anError);
392     return;
393   }
394 }
395
396
397 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
398 {
399   try {
400
401   std::string anError;
402   XAO::Xao aXao;
403
404   // author
405
406   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
407   aXao.setAuthor(anAuthor);
408
409   // make shape for export from all results
410   std::list<GeomShapePtr> aShapes;
411   std::list<ResultPtr> aResults;
412   std::list<DocumentPtr> aDocuments; /// documents of Parts selected and used in export
413   std::map<DocumentPtr, GeomTrsfPtr> aDocTrsf; /// translation of the part
414
415   bool anExCludedIsImage = false;
416   AttributeSelectionListPtr aSelection = selectionList(XAO_SELECTION_LIST_ID());
417   bool aIsSelection = aSelection->isInitialized() && aSelection->size() > 0;
418   if (aIsSelection) { // a mode for export to geom result by result
419     for(int a = 0; a < aSelection->size(); a++) {
420       AttributeSelectionPtr anAttr = aSelection->value(a);
421       ResultPtr aBodyContext =
422         std::dynamic_pointer_cast<ModelAPI_Result>(anAttr->context());
423       if (aBodyContext.get() && !aBodyContext->isDisabled() && aBodyContext->shape().get()) {
424           /// do not export pictures
425           if(aBodyContext->hasTextureFile()){
426             anExCludedIsImage = true;
427             continue;
428           }
429         aResults.push_back(aBodyContext);
430         GeomShapePtr aShape = anAttr->value();
431         if (!aShape.get())
432           aShape = aBodyContext->shape();
433         aShapes.push_back(aShape);
434         if (aBodyContext->groupName() == ModelAPI_ResultPart::group()) {
435           ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aBodyContext);
436           DocumentPtr aPartDoc = aResPart->partDoc();
437           if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
438             std::string msg = "Can not export XAO for not loaded part";
439             Events_InfoMessage("ExportFeature", msg, this).send();
440             return;
441           } else {
442             aDocuments.push_back(aPartDoc);
443             aDocTrsf[aPartDoc] = aResPart->summaryTrsf();
444           }
445         }
446       }
447     }
448   } else {
449     int aBodyCount = document()->size(ModelAPI_ResultBody::group());
450     for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
451       ResultBodyPtr aResultBody =
452           std::dynamic_pointer_cast<ModelAPI_ResultBody>(
453               document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
454       if (!aResultBody.get())
455         continue;
456       aShapes.push_back(aResultBody->shape());
457       aResults.push_back(aResultBody);
458     }
459   }
460   if (aShapes.empty()) {
461     if(!anExCludedIsImage)
462       setError("No shapes to export");
463     return;
464   }
465
466
467   GeomShapePtr aShape = (aShapes.size() == 1)
468       ? *aShapes.begin()
469       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
470
471   SetShapeToXAO(aShape, &aXao, anError);
472
473   if (!anError.empty()) {
474     setError("An error occurred while exporting " + theFileName + ": " + anError);
475     return;
476   }
477
478   // geometry name
479   std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
480   if (aGeometryName.empty() && aResults.size() == 1) {
481     // get the name from the first result
482     ResultPtr aResultBody = *aResults.begin();
483     aGeometryName = Locale::Convert::toString(aResultBody->data()->name());
484   }
485
486   aXao.getGeometry()->setName(aGeometryName);
487
488   std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
489
490   // iterate all documents used
491   if (aDocuments.empty())
492     aDocuments.push_back(document());
493   std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
494   for(; aDoc != aDocuments.end(); aDoc++) {
495     // groups
496     int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
497     for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
498       ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
499           (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
500       if (!aResultGroup.get() || !aResultGroup->shape().get())
501         continue;
502
503       FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
504
505       AttributeSelectionListPtr aSelectionList =
506           aGroupFeature->selectionList("group_list");
507       if (!isInResults(aSelectionList, aResults, allResultsCashed))// skip group not used in result
508         continue;
509
510       // conversion of dimension
511       std::string aSelectionType = aSelectionList->selectionType();
512       GeomAPI_Shape::ShapeType aSelType = GeomAPI_Shape::shapeTypeByStr(aSelectionType);
513       std::string aDimensionString =
514         ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
515       XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
516
517       XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
518         Locale::Convert::toString(aResultGroup->data()->name()));
519
520       try {
521         GeomAPI_ShapeExplorer aGroupResExplorer(aResultGroup->shape(), aSelType);
522         for(; aGroupResExplorer.more(); aGroupResExplorer.next()) {
523           GeomShapePtr aGroupShape = aGroupResExplorer.current();
524           if (aDocTrsf.find(*aDoc) != aDocTrsf.end())
525             aGroupShape->move(aDocTrsf[*aDoc]);
526           int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aGroupShape);
527           if (aReferenceID == 0) // selected value does not found in the exported shape
528             continue;
529           std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
530           int anElementID =
531             aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
532           aXaoGroup->add(anElementID);
533         }
534       } catch (XAO::XAO_Exception& e) {
535         // LCOV_EXCL_START
536         std::string msg = "An error occurred while exporting group " +
537           Locale::Convert::toString(aResultGroup->data()->name());
538         msg += ".\n";
539         msg += e.what();
540         msg += "\n";
541         msg += "=> skipping this group from XAO export.";
542         Events_InfoMessage("ExportFeature", msg, this).send();
543         aXao.removeGroup(aXaoGroup);
544         // LCOV_EXCL_STOP
545       }
546     }
547
548     // fields
549     int aFieldCount = (*aDoc)->size(ModelAPI_ResultField::group());
550     for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
551       ResultFieldPtr aResultField = std::dynamic_pointer_cast<ModelAPI_ResultField>(
552         (*aDoc)->object(ModelAPI_ResultField::group(), aFieldIndex));
553
554       FeaturePtr aFieldFeature = (*aDoc)->feature(aResultField);
555
556       AttributeSelectionListPtr aSelectionList =
557           aFieldFeature->selectionList("selected");
558       std::string aSelectionType = aSelectionList->selectionType();
559       bool isWholePart = aSelectionType == "part";
560       // skip field not used in results
561       if (!isWholePart && !isInResults(aSelectionList, aResults, allResultsCashed))
562         continue;
563
564       // conversion of dimension
565       std::string aDimensionString =
566         ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
567       XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
568       // get tables and their type
569       std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
570       std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
571       XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
572
573       XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
574         Locale::Convert::toString(aResultField->data()->name()));
575
576
577       try {
578         // set components names
579         AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
580         for(int aComp = 0; aComp < aComponents->size(); aComp++) {
581           std::string aName = aComponents->value(aComp);
582           aXaoField->setComponentName(aComp, aName);
583         }
584
585         AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
586         for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
587           XAO::Step* aStep = aXaoField->addNewStep(aStepIndex + 1);
588           aStep->setStep(aStepIndex + 1);
589           int aStampIndex = aStamps->value(aStepIndex);
590           aStep->setStamp(aStampIndex);
591           int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
592           int aNumComps = aTables->columns();
593           std::set<int> aFilledIDs; // to fill the rest by defaults
594           // omit default values first row
595           for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
596             for(int aCol = 0; aCol < aNumComps; aCol++) {
597               int anElementID = 0;
598               if (!isWholePart) {
599                 // element index actually is the ID of the selection
600                 AttributeSelectionPtr aSel = aSelectionList->value(aRow - 1);
601                 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSel->value());
602                 if (aReferenceID == 0) // selected value does not found in the exported shape
603                   continue;
604
605                 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
606                 anElementID = aXao.getGeometry()->
607                   getElementIndexByReference(aFieldDimension, aReferenceString);
608               }
609
610               ModelAPI_AttributeTables::Value aVal = aTables->value(
611                 isWholePart ? 0 : aRow, aCol, aStepIndex);
612               std::string aStrVal = valToString(aVal, aTables->type());
613               aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
614               aFilledIDs.insert(anElementID);
615             }
616           }
617           if (!isWholePart) { // fill the rest values by default ones
618             XAO::GeometricElementList::iterator allElem =
619               aXao.getGeometry()->begin(aFieldDimension);
620             for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
621               if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
622                 continue;
623               for(int aCol = 0; aCol < aNumComps; aCol++) {
624                 // default value
625                 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex);
626                 std::string aStrVal = valToString(aVal, aTables->type());
627                 aStep->setStringValue(allElem->first, aCol, aStrVal);
628               }
629             }
630           }
631         }
632       } catch (XAO::XAO_Exception& e) {
633         // LCOV_EXCL_START
634         std::string msg = "An error occurred while exporting field " +
635           Locale::Convert::toString(aResultField->data()->name());
636         msg += ".\n";
637         msg += e.what();
638         msg += "\n";
639         msg += "=> skipping this field from XAO export.";
640         Events_InfoMessage("ExportFeature", msg, this).send();
641         aXao.removeField(aXaoField);
642         // LCOV_EXCL_STOP
643       }
644     }
645   }
646
647   // exporting
648   XAOExport(theFileName, &aXao, anError);
649
650   if (!anError.empty()) {
651     setError("An error occurred while exporting " + theFileName + ": " + anError);
652     return;
653   }
654
655 // LCOV_EXCL_START
656   } catch (XAO::XAO_Exception& e) {
657     std::string anError = e.what();
658     setError("An error occurred while exporting " + theFileName + ": " + anError);
659     return;
660   }
661 // LCOV_EXCL_STOP
662 }
663
664 bool ExchangePlugin_ExportFeature::isMacro() const
665 {
666   if (!data().get() || !data()->isValid())
667     return false;
668   ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
669   AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
670   std::string aFormat(aFormatAttr.get() ? aFormatAttr->value() : "");
671
672   if (aFormat.empty()) { // get default format for the extension
673     AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
674     std::string aFilePath = aFilePathAttr->value();
675     if (!aFilePath.empty()) {
676       std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
677       aFormat = anExtension;
678     }
679   }
680
681   if (aFormat == "XAO") { // on export to GEOM the selection attribute is filled - this is
682                           // an exceptional case where export to XAO feature must be kept
683     AttributeSelectionListPtr aList = aThis->selectionList(XAO_SELECTION_LIST_ID());
684     return !aList->isInitialized() || aList->size() == 0;
685   }
686   return true;
687 }