1 // Copyright (C) 2014-2019 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_Tools.h>
38 #include <GeomAlgoAPI_XAOExport.h>
40 #include <GeomAPI_Shape.h>
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_AttributeIntArray.h>
46 #include <ModelAPI_AttributeTables.h>
47 #include <ModelAPI_Data.h>
48 #include <ModelAPI_Document.h>
49 #include <ModelAPI_Object.h>
50 #include <ModelAPI_ResultBody.h>
51 #include <ModelAPI_ResultPart.h>
52 #include <ModelAPI_ResultGroup.h>
53 #include <ModelAPI_ResultField.h>
54 #include <ModelAPI_Session.h>
55 #include <ModelAPI_Validator.h>
56 #include <ModelAPI_Tools.h>
58 #include <Events_InfoMessage.h>
60 #include <XAO_Group.hxx>
61 #include <XAO_Field.hxx>
62 #include <XAO_Xao.hxx>
63 #include <XAO_Geometry.hxx>
65 #include <ExchangePlugin_Tools.h>
67 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
71 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
73 // TODO Auto-generated destructor stub
77 * Request for initialization of data model of the feature: adding all attributes
79 void ExchangePlugin_ExportFeature::initAttributes()
81 data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(),
82 ModelAPI_AttributeString::typeId());
83 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
84 ModelAPI_AttributeString::typeId());
85 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
86 ModelAPI_AttributeString::typeId());
87 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
88 ModelAPI_AttributeString::typeId());
89 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
90 ModelAPI_AttributeSelectionList::typeId());
91 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
92 ModelAPI_AttributeString::typeId());
93 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
94 ModelAPI_AttributeString::typeId());
96 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
97 ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
98 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
99 ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
100 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
101 ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
104 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
106 if (theID == XAO_FILE_PATH_ID()) {
107 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
108 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
113 * Computes or recomputes the results
115 void ExchangePlugin_ExportFeature::execute()
117 AttributeStringPtr aFormatAttr =
118 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
119 std::string aFormat = aFormatAttr->value();
121 AttributeStringPtr aFilePathAttr =
122 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
123 std::string aFilePath = aFilePathAttr->value();
124 if (aFilePath.empty())
127 exportFile(aFilePath, aFormat);
130 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
131 const std::string& theFormat)
133 std::string aFormatName = theFormat;
135 if (aFormatName.empty()) { // get default format for the extension
137 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
138 if (anExtension == "BREP" || anExtension == "BRP") {
139 aFormatName = "BREP";
140 } else if (anExtension == "STEP" || anExtension == "STP") {
141 aFormatName = "STEP";
142 } else if (anExtension == "IGES" || anExtension == "IGS") {
143 aFormatName = "IGES-5.1";
144 } else if (anExtension == "XAO") {
147 aFormatName = anExtension;
151 if (aFormatName == "XAO") {
152 exportXAO(theFileName);
156 // make shape for export from selected shapes
157 AttributeSelectionListPtr aSelectionListAttr =
158 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
159 std::list<GeomShapePtr> aShapes;
160 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
161 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
162 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
163 if (aCurShape.get() == NULL)
164 aCurShape = anAttrSelection->context()->shape();
165 if (aCurShape.get() != NULL)
166 aShapes.push_back(aCurShape);
169 // Store compound if we have more than one shape.
170 std::shared_ptr<GeomAPI_Shape> aShape;
171 if(aShapes.size() == 1) {
172 aShape = aShapes.front();
174 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
177 // Perform the export
179 bool aResult = false;
180 if (aFormatName == "BREP") {
181 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
182 } else if (aFormatName == "STEP") {
183 aResult = STEPExport(theFileName, aFormatName, aShape, anError);
184 } else if (aFormatName.substr(0, 4) == "IGES") {
185 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
187 anError = "Unsupported format: " + aFormatName;
190 if (!anError.empty()) {
191 setError("An error occurred while exporting " + theFileName + ": " + anError);
196 /// Returns XAO string by the value from the table
197 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
198 const ModelAPI_AttributeTables::ValueType& theType) {
199 std::ostringstream aStr; // the resulting string value
201 case ModelAPI_AttributeTables::BOOLEAN:
202 aStr<<(theVal.myBool ? "true" : "false");
204 case ModelAPI_AttributeTables::INTEGER:
207 case ModelAPI_AttributeTables::DOUBLE:
208 aStr<<theVal.myDouble;
210 case ModelAPI_AttributeTables::STRING:
217 /// Returns true if something in selection is presented in the results list
218 static bool isInResults(AttributeSelectionListPtr theSelection,
219 const std::list<ResultPtr>& theResults,
220 std::set<ResultPtr>& theCashedResults)
222 // collect all results into a cashed set
223 if (theCashedResults.empty()) {
224 std::list<ResultPtr> aResults;
225 std::list<ResultPtr>::const_iterator aRes = theResults.cbegin();
226 for(; aRes != theResults.cend(); aRes++) {
227 if (theCashedResults.count(*aRes))
230 theCashedResults.insert(*aRes);
231 if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
232 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
233 std::list<ResultPtr> aResults;
234 ModelAPI_Tools::allSubs(aResBody, aResults, false);
235 for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
236 theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
238 } else if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { // all results of the part
239 ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
240 DocumentPtr aPartDoc = aResPart->partDoc();
241 if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
244 int aBodyCount = aPartDoc->size(ModelAPI_ResultBody::group());
245 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
246 ResultBodyPtr aResBody =
247 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
248 aPartDoc->object(ModelAPI_ResultBody::group(), aBodyIndex));
249 if (aResBody.get()) {
250 theCashedResults.insert(aResBody);
251 std::list<ResultPtr> aResults;
252 ModelAPI_Tools::allSubs(aResBody, aResults, false);
253 for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
254 theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
261 // if context is in results, return true
262 for(int a = 0; a < theSelection->size(); a++) {
263 AttributeSelectionPtr anAttr = theSelection->value(a);
264 ResultBodyPtr aSelected= std::dynamic_pointer_cast<ModelAPI_ResultBody>(anAttr->context());
265 if (aSelected.get() && theCashedResults.count(aSelected))
271 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
280 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
281 aXao.setAuthor(anAuthor);
283 // make shape for export from all results
284 std::list<GeomShapePtr> aShapes;
285 std::list<ResultPtr> aResults;
286 std::list<DocumentPtr> aDocuments; /// documents of Parts selected and used in export
288 AttributeSelectionListPtr aSelection = selectionList(SELECTION_LIST_ID());
289 bool aIsSelection = aSelection->isInitialized() && aSelection->size() > 0;
290 if (aIsSelection) { // a mode for export to geom result by result
291 for(int a = 0; a < aSelection->size(); a++) {
292 AttributeSelectionPtr anAttr = aSelection->value(a);
293 ResultPtr aBodyContext =
294 std::dynamic_pointer_cast<ModelAPI_Result>(anAttr->context());
295 if (aBodyContext.get() && !aBodyContext->isDisabled() && aBodyContext->shape().get()) {
296 aResults.push_back(aBodyContext);
297 GeomShapePtr aShape = anAttr->value();
299 aShape = aBodyContext->shape();
300 aShapes.push_back(aShape);
301 if (aBodyContext->groupName() == ModelAPI_ResultPart::group()) {
302 ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aBodyContext);
303 DocumentPtr aPartDoc = aResPart->partDoc();
304 if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
305 std::string msg = "Can not export XAO for not loaded part";
306 Events_InfoMessage("ExportFeature", msg, this).send();
309 aDocuments.push_back(aPartDoc);
315 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
316 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
317 ResultBodyPtr aResultBody =
318 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
319 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
320 if (!aResultBody.get())
322 aShapes.push_back(aResultBody->shape());
323 aResults.push_back(aResultBody);
326 if (aShapes.empty()) {
327 setError("No shapes to export");
332 GeomShapePtr aShape = (aShapes.size() == 1)
334 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
336 SetShapeToXAO(aShape, &aXao, anError);
338 if (!anError.empty()) {
339 setError("An error occurred while exporting " + theFileName + ": " + anError);
344 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
345 if (aGeometryName.empty() && aResults.size() == 1) {
346 // get the name from the first result
347 ResultPtr aResultBody = *aResults.begin();
348 aGeometryName = aResultBody->data()->name();
351 aXao.getGeometry()->setName(aGeometryName);
353 std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
355 // iterate all documents used
356 if (aDocuments.empty())
357 aDocuments.push_back(document());
358 std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
359 for(; aDoc != aDocuments.end(); aDoc++) {
361 int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
362 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
363 ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
364 (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
366 FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
368 AttributeSelectionListPtr aSelectionList =
369 aGroupFeature->selectionList("group_list");
370 if (!isInResults(aSelectionList, aResults, allResultsCashed))// skip group not used in result
373 // conversion of dimension
374 std::string aSelectionType = aSelectionList->selectionType();
375 std::string aDimensionString =
376 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
377 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
379 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
380 aResultGroup->data()->name());
383 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex){
384 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
386 // complex conversion of reference id to element index
387 // gives bad id in case the selection is done from python script
388 // => using GeomAlgoAPI_CompoundBuilder::id instead
389 // int aReferenceID_old = aSelection->Id();
391 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
393 if (aReferenceID == 0) // selected value does not found in the exported shape
396 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
398 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
400 aXaoGroup->add(anElementID);
402 } catch (XAO::XAO_Exception& e) {
404 std::string msg = "An error occurred while exporting group " +
405 aResultGroup->data()->name();
409 msg += "=> skipping this group from XAO export.";
410 Events_InfoMessage("ExportFeature", msg, this).send();
411 aXao.removeGroup(aXaoGroup);
417 int aFieldCount = (*aDoc)->size(ModelAPI_ResultField::group());
418 for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
419 ResultFieldPtr aResultField = std::dynamic_pointer_cast<ModelAPI_ResultField>(
420 (*aDoc)->object(ModelAPI_ResultField::group(), aFieldIndex));
422 FeaturePtr aFieldFeature = (*aDoc)->feature(aResultField);
424 AttributeSelectionListPtr aSelectionList =
425 aFieldFeature->selectionList("selected");
426 std::string aSelectionType = aSelectionList->selectionType();
427 bool isWholePart = aSelectionType == "part";
428 // skip field not used in results
429 if (!isWholePart && !isInResults(aSelectionList, aResults, allResultsCashed))
432 // conversion of dimension
433 std::string aDimensionString =
434 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
435 XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
436 // get tables and their type
437 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
438 std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
439 XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
441 XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
442 aResultField->data()->name());
446 // set components names
447 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
448 for(int aComp = 0; aComp < aComponents->size(); aComp++) {
449 std::string aName = aComponents->value(aComp);
450 aXaoField->setComponentName(aComp, aName);
453 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
454 for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
455 XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
456 aStep->setStep(aStepIndex);
457 int aStampIndex = aStamps->value(aStepIndex);
458 aStep->setStamp(aStampIndex);
459 int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
460 int aNumComps = aTables->columns();
461 std::set<int> aFilledIDs; // to fill the rest by defaults
462 // omit default values first row
463 for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
464 for(int aCol = 0; aCol < aNumComps; aCol++) {
467 // element index actually is the ID of the selection
468 AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
469 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
470 if (aReferenceID == 0) // selected value does not found in the exported shape
473 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
474 anElementID = aXao.getGeometry()->
475 getElementIndexByReference(aFieldDimension, aReferenceString);
478 ModelAPI_AttributeTables::Value aVal = aTables->value(
479 isWholePart ? 0 : aRow, aCol, aStepIndex);
480 std::string aStrVal = valToString(aVal, aTables->type());
481 aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
482 aFilledIDs.insert(anElementID);
485 if (!isWholePart) { // fill the rest values by default ones
486 XAO::GeometricElementList::iterator allElem =
487 aXao.getGeometry()->begin(aFieldDimension);
488 for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
489 if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
491 for(int aCol = 0; aCol < aNumComps; aCol++) {
493 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex);
494 std::string aStrVal = valToString(aVal, aTables->type());
495 aStep->setStringValue(allElem->first, aCol, aStrVal);
500 } catch (XAO::XAO_Exception& e) {
502 std::string msg = "An error occurred while exporting field " +
503 aResultField->data()->name();
507 msg += "=> skipping this field from XAO export.";
508 Events_InfoMessage("ExportFeature", msg, this).send();
509 aXao.removeField(aXaoField);
516 XAOExport(theFileName, &aXao, anError);
518 if (!anError.empty()) {
519 setError("An error occurred while exporting " + theFileName + ": " + anError);
524 } catch (XAO::XAO_Exception& e) {
525 std::string anError = e.what();
526 setError("An error occurred while exporting " + theFileName + ": " + anError);
532 bool ExchangePlugin_ExportFeature::isMacro() const
534 if (!data().get() || !data()->isValid())
536 ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
537 AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
538 if (!aFormatAttr.get())
540 std::string aFormat = aFormatAttr->value();
542 if (aFormat.empty()) { // get default format for the extension
543 AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
544 std::string aFilePath = aFilePathAttr->value();
545 if (!aFilePath.empty()) {
546 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
547 if (anExtension == "XAO") {
553 if (aFormat == "XAO") { // on export to GEOm the selection attribute is filled - this is
554 // an exceptional case where export to XAO feature must be kept
555 AttributeSelectionListPtr aList = aThis->selectionList(SELECTION_LIST_ID());
556 return !aList->isInitialized() || aList->size() == 0;