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";
145 aFormatName = anExtension;
149 if (aFormatName == "XAO") {
150 exportXAO(theFileName);
154 // make shape for export from selected shapes
155 AttributeSelectionListPtr aSelectionListAttr =
156 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
157 std::list<GeomShapePtr> aShapes;
158 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
159 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
160 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
161 if (aCurShape.get() == NULL)
162 aCurShape = anAttrSelection->context()->shape();
163 if (aCurShape.get() != NULL)
164 aShapes.push_back(aCurShape);
167 // Store compound if we have more than one shape.
168 std::shared_ptr<GeomAPI_Shape> aShape =
169 aShapes.size() == 1 ? aShapes.front() : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
171 // Perform the export
173 bool aResult = false;
174 if (aFormatName == "BREP") {
175 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
176 } else if (aFormatName == "STEP") {
177 aResult = STEPExport(theFileName, aFormatName, aShape, anError);
178 } else if (aFormatName.substr(0, 4) == "IGES") {
179 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
181 anError = "Unsupported format: " + aFormatName;
184 if (!anError.empty()) {
185 setError("An error occurred while exporting " + theFileName + ": " + anError);
190 /// Returns XAO string by the value from the table
191 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
192 const ModelAPI_AttributeTables::ValueType& theType) {
193 std::ostringstream aStr; // the resulting string value
195 case ModelAPI_AttributeTables::BOOLEAN:
196 aStr<<(theVal.myBool ? "true" : "false");
198 case ModelAPI_AttributeTables::INTEGER:
201 case ModelAPI_AttributeTables::DOUBLE:
202 aStr<<theVal.myDouble;
204 case ModelAPI_AttributeTables::STRING:
211 /// Returns true if something in selection is presented in the results list
212 static bool isInResults(AttributeSelectionListPtr theSelection,
213 const std::list<ResultPtr>& theResults,
214 std::set<ResultPtr>& theCashedResults)
216 // collect all results into a cashed set
217 if (theCashedResults.empty()) {
218 std::list<ResultPtr> aResults;
219 std::list<ResultPtr>::const_iterator aRes = theResults.cbegin();
220 for(; aRes != theResults.cend(); aRes++) {
221 if (theCashedResults.count(*aRes))
224 theCashedResults.insert(*aRes);
225 if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
226 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
227 std::list<ResultPtr> aResults;
228 ModelAPI_Tools::allSubs(aResBody, aResults, false);
229 for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
230 theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
232 } else if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { // all results of the part
233 ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
234 DocumentPtr aPartDoc = aResPart->partDoc();
235 if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
238 int aBodyCount = aPartDoc->size(ModelAPI_ResultBody::group());
239 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
240 ResultBodyPtr aResBody =
241 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
242 aPartDoc->object(ModelAPI_ResultBody::group(), aBodyIndex));
243 if (aResBody.get()) {
244 theCashedResults.insert(aResBody);
245 std::list<ResultPtr> aResults;
246 ModelAPI_Tools::allSubs(aResBody, aResults, false);
247 for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
248 theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
255 // if context is in results, return true
256 for(int a = 0; a < theSelection->size(); a++) {
257 AttributeSelectionPtr anAttr = theSelection->value(a);
258 ResultBodyPtr aSelected= std::dynamic_pointer_cast<ModelAPI_ResultBody>(anAttr->context());
259 if (aSelected.get() && theCashedResults.count(aSelected))
265 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
274 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
275 aXao.setAuthor(anAuthor);
277 // make shape for export from all results
278 std::list<GeomShapePtr> aShapes;
279 std::list<ResultPtr> aResults;
280 std::list<DocumentPtr> aDocuments; /// documents of Parts selected and used in export
282 AttributeSelectionListPtr aSelection = selectionList(SELECTION_LIST_ID());
283 bool aIsSelection = aSelection->isInitialized() && aSelection->size() > 0;
284 if (aIsSelection) { // a mode for export to geom result by result
285 for(int a = 0; a < aSelection->size(); a++) {
286 AttributeSelectionPtr anAttr = aSelection->value(a);
287 ResultPtr aBodyContext =
288 std::dynamic_pointer_cast<ModelAPI_Result>(anAttr->context());
289 if (aBodyContext.get() && !aBodyContext->isDisabled() && aBodyContext->shape().get()) {
290 aResults.push_back(aBodyContext);
291 GeomShapePtr aShape = anAttr->value();
293 aShape = aBodyContext->shape();
294 aShapes.push_back(aShape);
295 if (aBodyContext->groupName() == ModelAPI_ResultPart::group()) {
296 ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aBodyContext);
297 DocumentPtr aPartDoc = aResPart->partDoc();
298 if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
299 std::string msg = "Can not export XAO for not loaded part";
300 Events_InfoMessage("ExportFeature", msg, this).send();
303 aDocuments.push_back(aPartDoc);
309 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
310 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
311 ResultBodyPtr aResultBody =
312 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
313 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
314 if (!aResultBody.get())
316 aShapes.push_back(aResultBody->shape());
317 aResults.push_back(aResultBody);
320 if (aShapes.empty()) {
321 setError("No shapes to export");
326 GeomShapePtr aShape = (aShapes.size() == 1)
328 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
330 SetShapeToXAO(aShape, &aXao, anError);
332 if (!anError.empty()) {
333 setError("An error occurred while exporting " + theFileName + ": " + anError);
338 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
339 if (aGeometryName.empty() && aResults.size() == 1) {
340 // get the name from the first result
341 ResultPtr aResultBody = *aResults.begin();
342 aGeometryName = aResultBody->data()->name();
345 aXao.getGeometry()->setName(aGeometryName);
347 std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
349 // iterate all documents used
350 if (aDocuments.empty())
351 aDocuments.push_back(document());
352 std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
353 for(; aDoc != aDocuments.end(); aDoc++) {
355 int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
356 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
357 ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
358 (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
360 FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
362 AttributeSelectionListPtr aSelectionList =
363 aGroupFeature->selectionList("group_list");
364 if (!isInResults(aSelectionList, aResults, allResultsCashed))// skip group not used in result
367 // conversion of dimension
368 std::string aSelectionType = aSelectionList->selectionType();
369 std::string aDimensionString =
370 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
371 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
373 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
374 aResultGroup->data()->name());
377 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex){
378 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
380 // complex conversion of reference id to element index
381 // gives bad id in case the selection is done from python script
382 // => using GeomAlgoAPI_CompoundBuilder::id instead
383 // int aReferenceID_old = aSelection->Id();
385 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
387 if (aReferenceID == 0) // selected value does not found in the exported shape
390 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
392 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
394 aXaoGroup->add(anElementID);
396 } catch (XAO::XAO_Exception& e) {
398 std::string msg = "An error occurred while exporting group " +
399 aResultGroup->data()->name();
403 msg += "=> skipping this group from XAO export.";
404 Events_InfoMessage("ExportFeature", msg, this).send();
405 aXao.removeGroup(aXaoGroup);
411 int aFieldCount = (*aDoc)->size(ModelAPI_ResultField::group());
412 for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
413 ResultFieldPtr aResultField = std::dynamic_pointer_cast<ModelAPI_ResultField>(
414 (*aDoc)->object(ModelAPI_ResultField::group(), aFieldIndex));
416 FeaturePtr aFieldFeature = (*aDoc)->feature(aResultField);
418 AttributeSelectionListPtr aSelectionList =
419 aFieldFeature->selectionList("selected");
420 std::string aSelectionType = aSelectionList->selectionType();
421 bool isWholePart = aSelectionType == "part";
422 // skip field not used in results
423 if (!isWholePart && !isInResults(aSelectionList, aResults, allResultsCashed))
426 // conversion of dimension
427 std::string aDimensionString =
428 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
429 XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
430 // get tables and their type
431 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
432 std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
433 XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
435 XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
436 aResultField->data()->name());
440 // set components names
441 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
442 for(int aComp = 0; aComp < aComponents->size(); aComp++) {
443 std::string aName = aComponents->value(aComp);
444 aXaoField->setComponentName(aComp, aName);
447 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
448 for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
449 XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
450 aStep->setStep(aStepIndex);
451 int aStampIndex = aStamps->value(aStepIndex);
452 aStep->setStamp(aStampIndex);
453 int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
454 int aNumComps = aTables->columns();
455 std::set<int> aFilledIDs; // to fill the rest by defaults
456 // omit default values first row
457 for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
458 for(int aCol = 0; aCol < aNumComps; aCol++) {
461 // element index actually is the ID of the selection
462 AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
463 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
464 if (aReferenceID == 0) // selected value does not found in the exported shape
467 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
468 anElementID = aXao.getGeometry()->
469 getElementIndexByReference(aFieldDimension, aReferenceString);
472 ModelAPI_AttributeTables::Value aVal = aTables->value(
473 isWholePart ? 0 : aRow, aCol, aStepIndex);
474 std::string aStrVal = valToString(aVal, aTables->type());
475 aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
476 aFilledIDs.insert(anElementID);
479 if (!isWholePart) { // fill the rest values by default ones
480 XAO::GeometricElementList::iterator allElem =
481 aXao.getGeometry()->begin(aFieldDimension);
482 for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
483 if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
485 for(int aCol = 0; aCol < aNumComps; aCol++) {
487 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex);
488 std::string aStrVal = valToString(aVal, aTables->type());
489 aStep->setStringValue(allElem->first, aCol, aStrVal);
494 } catch (XAO::XAO_Exception& e) {
496 std::string msg = "An error occurred while exporting field " +
497 aResultField->data()->name();
501 msg += "=> skipping this field from XAO export.";
502 Events_InfoMessage("ExportFeature", msg, this).send();
503 aXao.removeField(aXaoField);
510 XAOExport(theFileName, &aXao, anError);
512 if (!anError.empty()) {
513 setError("An error occurred while exporting " + theFileName + ": " + anError);
518 } catch (XAO::XAO_Exception& e) {
519 std::string anError = e.what();
520 setError("An error occurred while exporting " + theFileName + ": " + anError);
526 bool ExchangePlugin_ExportFeature::isMacro() const
528 if (!data().get() || !data()->isValid())
530 ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
531 AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
532 std::string aFormat(aFormatAttr.get() ? aFormatAttr->value() : "");
534 if (aFormat.empty()) { // get default format for the extension
535 AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
536 std::string aFilePath = aFilePathAttr->value();
537 if (!aFilePath.empty()) {
538 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
539 aFormat = anExtension;
543 if (aFormat == "XAO") { // on export to GEOm the selection attribute is filled - this is
544 // an exceptional case where export to XAO feature must be kept
545 AttributeSelectionListPtr aList = aThis->selectionList(SELECTION_LIST_ID());
546 return !aList->isInitialized() || aList->size() == 0;