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>
31 #include <Config_Common.h>
32 #include <Config_PropManager.h>
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>
42 #include <GeomAPI_Shape.h>
43 #include <GeomAPI_ShapeExplorer.h>
44 #include <GeomAPI_Trsf.h>
46 #include <Locale_Convert.h>
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>
65 #include <Events_InfoMessage.h>
67 #include <XAO_Group.hxx>
68 #include <XAO_Field.hxx>
69 #include <XAO_Xao.hxx>
70 #include <XAO_Geometry.hxx>
72 #include <ExchangePlugin_Tools.h>
74 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
78 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
80 // TODO Auto-generated destructor stub
84 * Request for initialization of data model of the feature: adding all attributes
86 void ExchangePlugin_ExportFeature::initAttributes()
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());
113 double defelection = Config_PropManager::real("Visualization", "body_deflection");
114 real(ExchangePlugin_ExportFeature::STL_RELATIVE())->setValue(defelection);
116 data()->addAttribute(ExchangePlugin_ExportFeature::STL_ABSOLUTE(),
117 ModelAPI_AttributeDouble::typeId());
118 data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE(),
119 ModelAPI_AttributeString::typeId());
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());
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);
142 data()->blockSendAttributeUpdated(aWasBlocked, false);
146 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
148 if (theID == XAO_FILE_PATH_ID()) {
149 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
150 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
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());
160 * Computes or recomputes the results
162 void ExchangePlugin_ExportFeature::execute()
164 AttributeStringPtr aFormatAttr =
165 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
166 std::string aFormat = aFormatAttr->value();
168 AttributeStringPtr aFilePathAttr =
169 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
170 std::string aFilePath = aFilePathAttr->value();
171 if (aFilePath.empty())
174 exportFile(aFilePath, aFormat);
177 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
178 const std::string& theFormat)
180 std::string aFormatName = theFormat;
182 if (aFormatName.empty()) { // get default format for the extension
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";
192 aFormatName = anExtension;
196 if (aFormatName == "XAO") {
197 exportXAO(theFileName);
199 }else if (aFormatName == "STL") {
200 exportSTL(theFileName);
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);
211 /// do not export pictures
212 ResultPtr aBodyContext =
213 std::dynamic_pointer_cast<ModelAPI_Result>(anAttrSelection->context());
214 if (aBodyContext.get() && aBodyContext->hasTexture())
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);
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);
228 // Perform the export
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);
238 anError = "Unsupported format: " + aFormatName;
241 if (!aResult || !anError.empty()) {
242 setError("An error occurred while exporting " + theFileName + ": " + anError);
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
252 case ModelAPI_AttributeTables::BOOLEAN:
253 aStr<<(theVal.myBool ? "true" : "false");
255 case ModelAPI_AttributeTables::INTEGER:
258 case ModelAPI_AttributeTables::DOUBLE:
259 aStr<<theVal.myDouble;
261 case ModelAPI_AttributeTables::STRING:
268 void ExchangePlugin_ExportFeature::exportSTL(const std::string& theFileName)
271 AttributeSelectionPtr aSelection = selection(STL_OBJECT_SELECTED());
272 GeomShapePtr aShape = aSelection->value();
274 aShape = aSelection->context()->shape();
277 // Get relative value and percent flag.
279 bool anIsRelative = false;
280 bool anIsASCII = false;
282 if (string(STL_DEFLECTION_TYPE())->value() == STL_DEFLECTION_TYPE_RELATIVE()) {
283 aValue = real(STL_RELATIVE())->value();
286 aValue = real(STL_ABSOLUTE())->value();
289 if (string(STL_FILE_TYPE())->value() == STL_FILE_TYPE_ASCII()) {
292 // Perform the export
294 bool aResult = false;
296 aResult = STLExport(theFileName,
303 if (!aResult || !anError.empty()) {
304 setError("An error occurred while exporting " + theFileName + ": " + anError);
310 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
319 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
320 aXao.setAuthor(anAuthor);
322 // make shape for export from all results
323 std::list<GeomShapePtr> aShapes;
324 std::list<ResultPtr> aResults;
325 std::list<DocumentPtr> aDocuments; /// documents of Parts selected and used in export
326 std::map<DocumentPtr, GeomTrsfPtr> aDocTrsf; /// translation of the part
328 bool anExCludedIsImage = false;
329 AttributeSelectionListPtr aSelection = selectionList(XAO_SELECTION_LIST_ID());
330 bool aIsSelection = aSelection->isInitialized() && aSelection->size() > 0;
331 if (aIsSelection) { // a mode for export to geom result by result
332 for (int a = 0; a < aSelection->size(); a++) {
333 AttributeSelectionPtr anAttr = aSelection->value(a);
334 ResultPtr aBodyContext =
335 std::dynamic_pointer_cast<ModelAPI_Result>(anAttr->context());
336 if (aBodyContext.get() && !aBodyContext->isDisabled() && aBodyContext->shape().get()) {
337 /// do not export pictures
338 if (aBodyContext->hasTexture()){
339 anExCludedIsImage = true;
342 aResults.push_back(aBodyContext);
343 GeomShapePtr aShape = anAttr->value();
345 aShape = aBodyContext->shape();
346 aShapes.push_back(aShape);
347 if (aBodyContext->groupName() == ModelAPI_ResultPart::group()) {
348 ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aBodyContext);
349 DocumentPtr aPartDoc = aResPart->partDoc();
350 if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
351 std::string msg = "Can not export XAO for not loaded part";
352 Events_InfoMessage("ExportFeature", msg, this).send();
355 aDocuments.push_back(aPartDoc);
356 aDocTrsf[aPartDoc] = aResPart->summaryTrsf();
362 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
363 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
364 ResultBodyPtr aResultBody =
365 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
366 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
367 if (!aResultBody.get())
369 aShapes.push_back(aResultBody->shape());
370 aResults.push_back(aResultBody);
373 if (aShapes.empty()) {
374 if(!anExCludedIsImage)
375 setError("No shapes to export");
380 GeomShapePtr aShape = (aShapes.size() == 1)
382 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
384 SetShapeToXAO(aShape, &aXao, anError);
386 if (!anError.empty()) {
387 setError("An error occurred while exporting " + theFileName + ": " + anError);
392 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
393 if (aGeometryName.empty() && aResults.size() == 1) {
394 // get the name from the first result
395 ResultPtr aResultBody = *aResults.begin();
396 aGeometryName = Locale::Convert::toString(aResultBody->data()->name());
399 aXao.getGeometry()->setName(aGeometryName);
401 std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
403 // iterate all documents used
404 if (aDocuments.empty())
405 aDocuments.push_back(document());
406 std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
407 for(; aDoc != aDocuments.end(); aDoc++) {
409 int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
410 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
411 ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
412 (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
413 if (!aResultGroup.get() || !aResultGroup->shape().get())
416 FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
418 AttributeSelectionListPtr aSelectionList =
419 aGroupFeature->selectionList("group_list");
420 if (!ModelAPI_Tools::isInResults(aSelectionList,
422 allResultsCashed))// skip group not used in result
425 // conversion of dimension
426 std::string aSelectionType = aSelectionList->selectionType();
427 GeomAPI_Shape::ShapeType aSelType = GeomAPI_Shape::shapeTypeByStr(aSelectionType);
428 std::string aDimensionString =
429 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
430 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
432 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
433 Locale::Convert::toString(aResultGroup->data()->name()));
436 GeomAPI_ShapeExplorer aGroupResExplorer(aResultGroup->shape(), aSelType);
437 for(; aGroupResExplorer.more(); aGroupResExplorer.next()) {
438 GeomShapePtr aGroupShape = aGroupResExplorer.current();
439 if (aDocTrsf.find(*aDoc) != aDocTrsf.end())
440 aGroupShape->move(aDocTrsf[*aDoc]);
441 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aGroupShape);
442 if (aReferenceID == 0) // selected value does not found in the exported shape
444 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
446 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
447 aXaoGroup->add(anElementID);
449 } catch (XAO::XAO_Exception& e) {
451 std::string msg = "An error occurred while exporting group " +
452 Locale::Convert::toString(aResultGroup->data()->name());
456 msg += "=> skipping this group from XAO export.";
457 Events_InfoMessage("ExportFeature", msg, this).send();
458 aXao.removeGroup(aXaoGroup);
464 int aFieldCount = (*aDoc)->size(ModelAPI_ResultField::group());
465 for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
466 ResultFieldPtr aResultField = std::dynamic_pointer_cast<ModelAPI_ResultField>(
467 (*aDoc)->object(ModelAPI_ResultField::group(), aFieldIndex));
469 FeaturePtr aFieldFeature = (*aDoc)->feature(aResultField);
471 AttributeSelectionListPtr aSelectionList =
472 aFieldFeature->selectionList("selected");
473 std::string aSelectionType = aSelectionList->selectionType();
474 bool isWholePart = aSelectionType == "part";
475 // skip field not used in results
477 !ModelAPI_Tools::isInResults(aSelectionList, aResults, allResultsCashed))
480 // conversion of dimension
481 std::string aDimensionString =
482 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
483 XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
484 // get tables and their type
485 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
486 std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
487 XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
489 XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
490 Locale::Convert::toString(aResultField->data()->name()));
494 // set components names
495 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
496 for(int aComp = 0; aComp < aComponents->size(); aComp++) {
497 std::string aName = aComponents->value(aComp);
498 aXaoField->setComponentName(aComp, aName);
501 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
502 for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
503 XAO::Step* aStep = aXaoField->addNewStep(aStepIndex + 1);
504 aStep->setStep(aStepIndex + 1);
505 int aStampIndex = aStamps->value(aStepIndex);
506 aStep->setStamp(aStampIndex);
507 int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
508 int aNumComps = aTables->columns();
509 std::set<int> aFilledIDs; // to fill the rest by defaults
510 // omit default values first row
511 for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
512 for(int aCol = 0; aCol < aNumComps; aCol++) {
515 // element index actually is the ID of the selection
516 AttributeSelectionPtr aSel = aSelectionList->value(aRow - 1);
517 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSel->value());
518 if (aReferenceID == 0) // selected value does not found in the exported shape
521 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
522 anElementID = aXao.getGeometry()->
523 getElementIndexByReference(aFieldDimension, aReferenceString);
526 ModelAPI_AttributeTables::Value aVal = aTables->value(
527 isWholePart ? 0 : aRow, aCol, aStepIndex);
528 std::string aStrVal = valToString(aVal, aTables->type());
529 aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
530 aFilledIDs.insert(anElementID);
533 if (!isWholePart) { // fill the rest values by default ones
534 XAO::GeometricElementList::iterator allElem =
535 aXao.getGeometry()->begin(aFieldDimension);
536 for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
537 if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
539 for(int aCol = 0; aCol < aNumComps; aCol++) {
541 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex);
542 std::string aStrVal = valToString(aVal, aTables->type());
543 aStep->setStringValue(allElem->first, aCol, aStrVal);
548 } catch (XAO::XAO_Exception& e) {
550 std::string msg = "An error occurred while exporting field " +
551 Locale::Convert::toString(aResultField->data()->name());
555 msg += "=> skipping this field from XAO export.";
556 Events_InfoMessage("ExportFeature", msg, this).send();
557 aXao.removeField(aXaoField);
564 XAOExport(theFileName, &aXao, anError);
566 if (!anError.empty()) {
567 setError("An error occurred while exporting " + theFileName + ": " + anError);
572 } catch (XAO::XAO_Exception& e) {
573 std::string anError = e.what();
574 setError("An error occurred while exporting " + theFileName + ": " + anError);
580 bool ExchangePlugin_ExportFeature::isMacro() const
582 if (!data().get() || !data()->isValid())
584 ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
585 AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
586 std::string aFormat(aFormatAttr.get() ? aFormatAttr->value() : "");
588 if (aFormat.empty()) { // get default format for the extension
589 AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
590 std::string aFilePath = aFilePathAttr->value();
591 if (!aFilePath.empty()) {
592 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
593 aFormat = anExtension;
597 if (aFormat == "XAO") { // on export to GEOM the selection attribute is filled - this is
598 // an exceptional case where export to XAO feature must be kept
599 AttributeSelectionListPtr aList = aThis->selectionList(XAO_SELECTION_LIST_ID());
600 return !aList->isInitialized() || aList->size() == 0;