1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ExchangePlugin_ExportFeature.h>
30 #include <Config_Common.h>
31 #include <Config_PropManager.h>
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>
41 #include <GeomAPI_Shape.h>
42 #include <GeomAPI_ShapeExplorer.h>
43 #include <GeomAPI_Trsf.h>
45 #include <Locale_Convert.h>
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_Data.h>
54 #include <ModelAPI_Document.h>
55 #include <ModelAPI_Object.h>
56 #include <ModelAPI_ResultBody.h>
57 #include <ModelAPI_ResultPart.h>
58 #include <ModelAPI_ResultGroup.h>
59 #include <ModelAPI_ResultField.h>
60 #include <ModelAPI_Session.h>
61 #include <ModelAPI_Validator.h>
62 #include <ModelAPI_Tools.h>
64 #include <Events_InfoMessage.h>
66 #include <XAO_Group.hxx>
67 #include <XAO_Field.hxx>
68 #include <XAO_Xao.hxx>
69 #include <XAO_Geometry.hxx>
71 #include <ExchangePlugin_Tools.h>
73 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
77 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
79 // TODO Auto-generated destructor stub
83 * Request for initialization of data model of the feature: adding all attributes
85 void ExchangePlugin_ExportFeature::initAttributes()
87 data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(),
88 ModelAPI_AttributeString::typeId());
89 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
90 ModelAPI_AttributeString::typeId());
91 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
92 ModelAPI_AttributeString::typeId());
93 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
94 ModelAPI_AttributeString::typeId());
95 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
96 ModelAPI_AttributeSelectionList::typeId());
97 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
98 ModelAPI_AttributeString::typeId());
99 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
100 ModelAPI_AttributeString::typeId());
101 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID(),
102 ModelAPI_AttributeSelectionList::typeId());
103 data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID(),
104 ModelAPI_AttributeString::typeId());
105 data()->addAttribute(ExchangePlugin_ExportFeature::STL_OBJECT_SELECTED(),
106 ModelAPI_AttributeSelection::typeId());
107 data()->addAttribute(ExchangePlugin_ExportFeature::STL_DEFLECTION_TYPE(),
108 ModelAPI_AttributeString::typeId());
109 data()->addAttribute(ExchangePlugin_ExportFeature::STL_RELATIVE(),
110 ModelAPI_AttributeDouble::typeId());
112 double defelection = Config_PropManager::real("Visualization", "body_deflection");
113 real(ExchangePlugin_ExportFeature::STL_RELATIVE())->setValue(defelection);
115 data()->addAttribute(ExchangePlugin_ExportFeature::STL_ABSOLUTE(),
116 ModelAPI_AttributeDouble::typeId());
117 data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE(),
118 ModelAPI_AttributeString::typeId());
120 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
121 ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
122 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
123 ExchangePlugin_ExportFeature::STL_FILE_PATH_ID());
124 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
125 ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
126 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
127 ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
128 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
129 ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID());
131 // to support previous version of document, move the selection list
132 // if the type of export operation is XAO
133 AttributeStringPtr aTypeAttr = string(EXPORT_TYPE_ID());
134 if (aTypeAttr->isInitialized() && aTypeAttr->value() == "XAO") {
135 bool aWasBlocked = data()->blockSendAttributeUpdated(true, false);
136 AttributeSelectionListPtr aSelList = selectionList(SELECTION_LIST_ID());
137 AttributeSelectionListPtr aXAOSelList = selectionList(XAO_SELECTION_LIST_ID());
138 if (aSelList->size() > 0 && aXAOSelList->size() == 0)
139 aSelList->copyTo(aXAOSelList);
141 data()->blockSendAttributeUpdated(aWasBlocked, false);
145 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
147 if (theID == XAO_FILE_PATH_ID()) {
148 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
149 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
151 else if (theID == STL_FILE_PATH_ID()) {
152 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
153 string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID())->value());
158 * Computes or recomputes the results
160 void ExchangePlugin_ExportFeature::execute()
162 AttributeStringPtr aFormatAttr =
163 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
164 std::string aFormat = aFormatAttr->value();
166 AttributeStringPtr aFilePathAttr =
167 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
168 std::string aFilePath = aFilePathAttr->value();
169 if (aFilePath.empty())
172 exportFile(aFilePath, aFormat);
175 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
176 const std::string& theFormat)
178 std::string aFormatName = theFormat;
180 if (aFormatName.empty()) { // get default format for the extension
182 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
183 if (anExtension == "BREP" || anExtension == "BRP") {
184 aFormatName = "BREP";
185 } else if (anExtension == "STEP" || anExtension == "STP") {
186 aFormatName = "STEP";
187 } else if (anExtension == "IGES" || anExtension == "IGS") {
188 aFormatName = "IGES-5.1";
190 aFormatName = anExtension;
194 if (aFormatName == "XAO") {
195 exportXAO(theFileName);
197 }else if (aFormatName == "STL") {
198 exportSTL(theFileName);
202 // make shape for export from selected shapes
203 AttributeSelectionListPtr aSelectionListAttr =
204 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
205 std::list<GeomShapePtr> aShapes;
206 std::list<ResultPtr> aContexts;
207 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
208 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
210 /// do not export pictures
211 ResultPtr aBodyContext =
212 std::dynamic_pointer_cast<ModelAPI_Result>(anAttrSelection->context());
213 if (aBodyContext.get() && aBodyContext->hasTexture())
216 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
217 if (aCurShape.get() == NULL)
218 aCurShape = anAttrSelection->context()->shape();
219 if (aCurShape.get() != NULL)
221 aShapes.push_back(aCurShape);
222 aContexts.push_back(anAttrSelection->context());
226 // Store compound if we have more than one shape.
227 std::shared_ptr<GeomAPI_Shape> aShape =
228 aShapes.size() == 1 ? aShapes.front() : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
230 // Perform the export
232 bool aResult = false;
233 if (aFormatName == "BREP") {
234 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
235 } else if (aFormatName == "STEP") {
236 aResult = STEPExport(theFileName, aShapes, aContexts, anError);
237 } else if (aFormatName.substr(0, 4) == "IGES") {
238 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
240 anError = "Unsupported format: " + aFormatName;
243 if (!aResult || !anError.empty()) {
244 setError("An error occurred while exporting " + theFileName + ": " + anError);
249 /// Returns XAO string by the value from the table
250 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
251 const ModelAPI_AttributeTables::ValueType& theType) {
252 std::ostringstream aStr; // the resulting string value
254 case ModelAPI_AttributeTables::BOOLEAN:
255 aStr<<(theVal.myBool ? "true" : "false");
257 case ModelAPI_AttributeTables::INTEGER:
260 case ModelAPI_AttributeTables::DOUBLE:
261 aStr<<theVal.myDouble;
263 case ModelAPI_AttributeTables::STRING:
270 void ExchangePlugin_ExportFeature::exportSTL(const std::string& theFileName)
273 AttributeSelectionPtr aSelection = selection(STL_OBJECT_SELECTED());
274 GeomShapePtr aShape = aSelection->value();
276 aShape = aSelection->context()->shape();
279 // Get relative value and percent flag.
281 bool anIsRelative = false;
282 bool anIsASCII = false;
284 if (string(STL_DEFLECTION_TYPE())->value() == STL_DEFLECTION_TYPE_RELATIVE()) {
285 aValue = real(STL_RELATIVE())->value();
288 aValue = real(STL_ABSOLUTE())->value();
291 if (string(STL_FILE_TYPE())->value() == STL_FILE_TYPE_ASCII()) {
294 // Perform the export
296 bool aResult = false;
298 aResult = STLExport(theFileName,
305 if (!aResult || !anError.empty()) {
306 setError("An error occurred while exporting " + theFileName + ": " + anError);
312 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
321 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
322 aXao.setAuthor(anAuthor);
324 // make shape for export from all results
325 std::list<GeomShapePtr> aShapes;
326 std::list<ResultPtr> aResults;
327 std::list<DocumentPtr> aDocuments; /// documents of Parts selected and used in export
328 std::map<DocumentPtr, GeomTrsfPtr> aDocTrsf; /// translation of the part
330 bool anExCludedIsImage = false;
331 AttributeSelectionListPtr aSelection = selectionList(XAO_SELECTION_LIST_ID());
332 bool aIsSelection = aSelection->isInitialized() && aSelection->size() > 0;
333 if (aIsSelection) { // a mode for export to geom result by result
334 for (int a = 0; a < aSelection->size(); a++) {
335 AttributeSelectionPtr anAttr = aSelection->value(a);
336 ResultPtr aBodyContext =
337 std::dynamic_pointer_cast<ModelAPI_Result>(anAttr->context());
338 if (aBodyContext.get() && !aBodyContext->isDisabled() && aBodyContext->shape().get()) {
339 /// do not export pictures
340 if (aBodyContext->hasTexture()){
341 anExCludedIsImage = true;
344 aResults.push_back(aBodyContext);
345 GeomShapePtr aShape = anAttr->value();
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();
357 aDocuments.push_back(aPartDoc);
358 aDocTrsf[aPartDoc] = aResPart->summaryTrsf();
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())
371 aShapes.push_back(aResultBody->shape());
372 aResults.push_back(aResultBody);
375 if (aShapes.empty()) {
376 if(!anExCludedIsImage)
377 setError("No shapes to export");
382 GeomShapePtr aShape = (aShapes.size() == 1)
384 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
386 SetShapeToXAO(aShape, &aXao, anError);
388 if (!anError.empty()) {
389 setError("An error occurred while exporting " + theFileName + ": " + anError);
394 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
395 if (aGeometryName.empty() && aResults.size() == 1) {
396 // get the name from the first result
397 ResultPtr aResultBody = *aResults.begin();
398 aGeometryName = Locale::Convert::toString(aResultBody->data()->name());
401 aXao.getGeometry()->setName(aGeometryName);
403 std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
405 // iterate all documents used
406 if (aDocuments.empty())
407 aDocuments.push_back(document());
408 std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
409 for(; aDoc != aDocuments.end(); aDoc++) {
411 int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
412 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
413 ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
414 (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
415 if (!aResultGroup.get() || !aResultGroup->shape().get())
418 FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
420 AttributeSelectionListPtr aSelectionList =
421 aGroupFeature->selectionList("group_list");
422 if (!ModelAPI_Tools::isInResults(aSelectionList,
424 allResultsCashed))// skip group not used in result
427 // conversion of dimension
428 std::string aSelectionType = aSelectionList->selectionType();
429 GeomAPI_Shape::ShapeType aSelType = GeomAPI_Shape::shapeTypeByStr(aSelectionType);
430 std::string aDimensionString =
431 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
432 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
434 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
435 Locale::Convert::toString(aResultGroup->data()->name()));
438 GeomAPI_ShapeExplorer aGroupResExplorer(aResultGroup->shape(), aSelType);
439 for(; aGroupResExplorer.more(); aGroupResExplorer.next()) {
440 GeomShapePtr aGroupShape = aGroupResExplorer.current();
441 if (aDocTrsf.find(*aDoc) != aDocTrsf.end())
442 aGroupShape->move(aDocTrsf[*aDoc]);
443 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aGroupShape);
444 if (aReferenceID == 0) // selected value does not found in the exported shape
446 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
448 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
449 aXaoGroup->add(anElementID);
451 } catch (XAO::XAO_Exception& e) {
453 std::string msg = "An error occurred while exporting group " +
454 Locale::Convert::toString(aResultGroup->data()->name());
458 msg += "=> skipping this group from XAO export.";
459 Events_InfoMessage("ExportFeature", msg, this).send();
460 aXao.removeGroup(aXaoGroup);
466 int aFieldCount = (*aDoc)->size(ModelAPI_ResultField::group());
467 for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
468 ResultFieldPtr aResultField = std::dynamic_pointer_cast<ModelAPI_ResultField>(
469 (*aDoc)->object(ModelAPI_ResultField::group(), aFieldIndex));
471 FeaturePtr aFieldFeature = (*aDoc)->feature(aResultField);
473 AttributeSelectionListPtr aSelectionList =
474 aFieldFeature->selectionList("selected");
475 std::string aSelectionType = aSelectionList->selectionType();
476 bool isWholePart = aSelectionType == "part";
477 // skip field not used in results
479 !ModelAPI_Tools::isInResults(aSelectionList, aResults, allResultsCashed))
482 // conversion of dimension
483 std::string aDimensionString =
484 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
485 XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
486 // get tables and their type
487 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
488 std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
489 XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
491 XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
492 Locale::Convert::toString(aResultField->data()->name()));
496 // set components names
497 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
498 for(int aComp = 0; aComp < aComponents->size(); aComp++) {
499 std::string aName = aComponents->value(aComp);
500 aXaoField->setComponentName(aComp, aName);
503 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
504 for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
505 XAO::Step* aStep = aXaoField->addNewStep(aStepIndex + 1);
506 aStep->setStep(aStepIndex + 1);
507 int aStampIndex = aStamps->value(aStepIndex);
508 aStep->setStamp(aStampIndex);
509 int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
510 int aNumComps = aTables->columns();
511 std::set<int> aFilledIDs; // to fill the rest by defaults
512 // omit default values first row
513 for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
514 for(int aCol = 0; aCol < aNumComps; aCol++) {
517 // element index actually is the ID of the selection
518 AttributeSelectionPtr aSel = aSelectionList->value(aRow - 1);
519 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSel->value());
520 if (aReferenceID == 0) // selected value does not found in the exported shape
523 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
524 anElementID = aXao.getGeometry()->
525 getElementIndexByReference(aFieldDimension, aReferenceString);
528 ModelAPI_AttributeTables::Value aVal = aTables->value(
529 isWholePart ? 0 : aRow, aCol, aStepIndex);
530 std::string aStrVal = valToString(aVal, aTables->type());
531 aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
532 aFilledIDs.insert(anElementID);
535 if (!isWholePart) { // fill the rest values by default ones
536 XAO::GeometricElementList::iterator allElem =
537 aXao.getGeometry()->begin(aFieldDimension);
538 for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
539 if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
541 for(int aCol = 0; aCol < aNumComps; aCol++) {
543 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex);
544 std::string aStrVal = valToString(aVal, aTables->type());
545 aStep->setStringValue(allElem->first, aCol, aStrVal);
550 } catch (XAO::XAO_Exception& e) {
552 std::string msg = "An error occurred while exporting field " +
553 Locale::Convert::toString(aResultField->data()->name());
557 msg += "=> skipping this field from XAO export.";
558 Events_InfoMessage("ExportFeature", msg, this).send();
559 aXao.removeField(aXaoField);
566 XAOExport(theFileName, &aXao, anError);
568 if (!anError.empty()) {
569 setError("An error occurred while exporting " + theFileName + ": " + anError);
574 } catch (XAO::XAO_Exception& e) {
575 std::string anError = e.what();
576 setError("An error occurred while exporting " + theFileName + ": " + anError);
582 bool ExchangePlugin_ExportFeature::isMacro() const
584 if (!data().get() || !data()->isValid())
586 ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
587 AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
588 std::string aFormat(aFormatAttr.get() ? aFormatAttr->value() : "");
590 if (aFormat.empty()) { // get default format for the extension
591 AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
592 std::string aFilePath = aFilePathAttr->value();
593 if (!aFilePath.empty()) {
594 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
595 aFormat = anExtension;
599 if (aFormat == "XAO") { // on export to GEOM the selection attribute is filled - this is
600 // an exceptional case where export to XAO feature must be kept
601 AttributeSelectionListPtr aList = aThis->selectionList(XAO_SELECTION_LIST_ID());
602 return !aList->isInitialized() || aList->size() == 0;