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