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>
41 #include <GeomAPI_ShapeExplorer.h>
42 #include <GeomAPI_Trsf.h>
44 #include <ModelAPI_AttributeSelectionList.h>
45 #include <ModelAPI_AttributeString.h>
46 #include <ModelAPI_AttributeStringArray.h>
47 #include <ModelAPI_AttributeIntArray.h>
48 #include <ModelAPI_AttributeTables.h>
49 #include <ModelAPI_Data.h>
50 #include <ModelAPI_Document.h>
51 #include <ModelAPI_Object.h>
52 #include <ModelAPI_ResultBody.h>
53 #include <ModelAPI_ResultPart.h>
54 #include <ModelAPI_ResultGroup.h>
55 #include <ModelAPI_ResultField.h>
56 #include <ModelAPI_Session.h>
57 #include <ModelAPI_Validator.h>
58 #include <ModelAPI_Tools.h>
60 #include <Events_InfoMessage.h>
62 #include <XAO_Group.hxx>
63 #include <XAO_Field.hxx>
64 #include <XAO_Xao.hxx>
65 #include <XAO_Geometry.hxx>
67 #include <ExchangePlugin_Tools.h>
69 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
73 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
75 // TODO Auto-generated destructor stub
79 * Request for initialization of data model of the feature: adding all attributes
81 void ExchangePlugin_ExportFeature::initAttributes()
83 data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(),
84 ModelAPI_AttributeString::typeId());
85 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
86 ModelAPI_AttributeString::typeId());
87 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
88 ModelAPI_AttributeString::typeId());
89 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
90 ModelAPI_AttributeString::typeId());
91 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
92 ModelAPI_AttributeSelectionList::typeId());
93 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
94 ModelAPI_AttributeString::typeId());
95 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
96 ModelAPI_AttributeString::typeId());
98 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
99 ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
100 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
101 ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
102 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
103 ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
106 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
108 if (theID == XAO_FILE_PATH_ID()) {
109 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
110 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
115 * Computes or recomputes the results
117 void ExchangePlugin_ExportFeature::execute()
119 AttributeStringPtr aFormatAttr =
120 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
121 std::string aFormat = aFormatAttr->value();
123 AttributeStringPtr aFilePathAttr =
124 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
125 std::string aFilePath = aFilePathAttr->value();
126 if (aFilePath.empty())
129 exportFile(aFilePath, aFormat);
132 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
133 const std::string& theFormat)
135 std::string aFormatName = theFormat;
137 if (aFormatName.empty()) { // get default format for the extension
139 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
140 if (anExtension == "BREP" || anExtension == "BRP") {
141 aFormatName = "BREP";
142 } else if (anExtension == "STEP" || anExtension == "STP") {
143 aFormatName = "STEP";
144 } else if (anExtension == "IGES" || anExtension == "IGS") {
145 aFormatName = "IGES-5.1";
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 aShapes.size() == 1 ? aShapes.front() : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
173 // Perform the export
175 bool aResult = false;
176 if (aFormatName == "BREP") {
177 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
178 } else if (aFormatName == "STEP") {
179 aResult = STEPExport(theFileName, aFormatName, aShape, anError);
180 } else if (aFormatName.substr(0, 4) == "IGES") {
181 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
183 anError = "Unsupported format: " + aFormatName;
186 if (!anError.empty()) {
187 setError("An error occurred while exporting " + theFileName + ": " + anError);
192 /// Returns XAO string by the value from the table
193 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
194 const ModelAPI_AttributeTables::ValueType& theType) {
195 std::ostringstream aStr; // the resulting string value
197 case ModelAPI_AttributeTables::BOOLEAN:
198 aStr<<(theVal.myBool ? "true" : "false");
200 case ModelAPI_AttributeTables::INTEGER:
203 case ModelAPI_AttributeTables::DOUBLE:
204 aStr<<theVal.myDouble;
206 case ModelAPI_AttributeTables::STRING:
213 /// Returns true if something in selection is presented in the results list
214 static bool isInResults(AttributeSelectionListPtr theSelection,
215 const std::list<ResultPtr>& theResults,
216 std::set<ResultPtr>& theCashedResults)
218 // collect all results into a cashed set
219 if (theCashedResults.empty()) {
220 std::list<ResultPtr> aResults;
221 std::list<ResultPtr>::const_iterator aRes = theResults.cbegin();
222 for(; aRes != theResults.cend(); aRes++) {
223 if (theCashedResults.count(*aRes))
226 theCashedResults.insert(*aRes);
227 if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
228 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
229 std::list<ResultPtr> aResults;
230 ModelAPI_Tools::allSubs(aResBody, aResults, false);
231 for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
232 theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
234 } else if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { // all results of the part
235 ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
236 DocumentPtr aPartDoc = aResPart->partDoc();
237 if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
240 int aBodyCount = aPartDoc->size(ModelAPI_ResultBody::group());
241 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
242 ResultBodyPtr aResBody =
243 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
244 aPartDoc->object(ModelAPI_ResultBody::group(), aBodyIndex));
245 if (aResBody.get()) {
246 theCashedResults.insert(aResBody);
247 std::list<ResultPtr> aResults;
248 ModelAPI_Tools::allSubs(aResBody, aResults, false);
249 for(std::list<ResultPtr>::iterator aR = aResults.begin(); aR != aResults.end(); aR++) {
250 theCashedResults.insert(std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aR));
257 // if context is in results, return true
258 for(int a = 0; a < theSelection->size(); a++) {
259 AttributeSelectionPtr anAttr = theSelection->value(a);
260 ResultPtr aContext = anAttr->context();
261 // check is it group selected for groups BOP
262 if (aContext.get() && aContext->groupName() == ModelAPI_ResultGroup::group()) {
263 // it is impossible by used results check which result is used in this group result,
264 // so check the results shapes is it in results of this document or not
265 FeaturePtr aSelFeature =
266 std::dynamic_pointer_cast<ModelAPI_Feature>(theSelection->owner());
267 if (!aSelFeature.get() || aSelFeature->results().empty())
269 GeomShapePtr aGroupResShape = aSelFeature->firstResult()->shape();
271 std::set<ResultPtr>::iterator allResultsIter = theCashedResults.begin();
272 for(; allResultsIter != theCashedResults.end(); allResultsIter++) {
273 GeomShapePtr aResultShape = (*allResultsIter)->shape();
275 GeomAPI_Shape::ShapeType aType =
276 GeomAPI_Shape::shapeTypeByStr(theSelection->selectionType());
277 GeomAPI_ShapeExplorer aGroupResExp(aGroupResShape, aType);
278 for(; aGroupResExp.more(); aGroupResExp.next()) {
279 if (aResultShape->isSubShape(aGroupResExp.current(), false))
280 return true; // at least one shape of the group is in the used results
284 ResultBodyPtr aSelected = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anAttr->context());
285 if (!aSelected.get()) { // try to get selected feature and all its results
286 FeaturePtr aContextFeature = anAttr->contextFeature();
287 if (aContextFeature.get() && !aContextFeature->results().empty()) {
288 const std::list<ResultPtr>& allResluts = aContextFeature->results();
289 std::list<ResultPtr>::const_iterator aResIter = allResluts.cbegin();
290 for(; aResIter != allResluts.cend(); aResIter++) {
291 if (aResIter->get() && theCashedResults.count(*aResIter))
295 } else if (aSelected.get() && theCashedResults.count(aSelected))
301 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
310 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
311 aXao.setAuthor(anAuthor);
313 // make shape for export from all results
314 std::list<GeomShapePtr> aShapes;
315 std::list<ResultPtr> aResults;
316 std::list<DocumentPtr> aDocuments; /// documents of Parts selected and used in export
317 std::map<DocumentPtr, GeomTrsfPtr> aDocTrsf; /// translation of the part
319 AttributeSelectionListPtr aSelection = selectionList(SELECTION_LIST_ID());
320 bool aIsSelection = aSelection->isInitialized() && aSelection->size() > 0;
321 if (aIsSelection) { // a mode for export to geom result by result
322 for(int a = 0; a < aSelection->size(); a++) {
323 AttributeSelectionPtr anAttr = aSelection->value(a);
324 ResultPtr aBodyContext =
325 std::dynamic_pointer_cast<ModelAPI_Result>(anAttr->context());
326 if (aBodyContext.get() && !aBodyContext->isDisabled() && aBodyContext->shape().get()) {
327 aResults.push_back(aBodyContext);
328 GeomShapePtr aShape = anAttr->value();
330 aShape = aBodyContext->shape();
331 aShapes.push_back(aShape);
332 if (aBodyContext->groupName() == ModelAPI_ResultPart::group()) {
333 ResultPartPtr aResPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aBodyContext);
334 DocumentPtr aPartDoc = aResPart->partDoc();
335 if (!aPartDoc.get() || !aPartDoc->isOpened()) { // document is not accessible
336 std::string msg = "Can not export XAO for not loaded part";
337 Events_InfoMessage("ExportFeature", msg, this).send();
340 aDocuments.push_back(aPartDoc);
341 aDocTrsf[aPartDoc] = aResPart->summaryTrsf();
347 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
348 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
349 ResultBodyPtr aResultBody =
350 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
351 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
352 if (!aResultBody.get())
354 aShapes.push_back(aResultBody->shape());
355 aResults.push_back(aResultBody);
358 if (aShapes.empty()) {
359 setError("No shapes to export");
364 GeomShapePtr aShape = (aShapes.size() == 1)
366 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
368 SetShapeToXAO(aShape, &aXao, anError);
370 if (!anError.empty()) {
371 setError("An error occurred while exporting " + theFileName + ": " + anError);
376 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
377 if (aGeometryName.empty() && aResults.size() == 1) {
378 // get the name from the first result
379 ResultPtr aResultBody = *aResults.begin();
380 aGeometryName = aResultBody->data()->name();
383 aXao.getGeometry()->setName(aGeometryName);
385 std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
387 // iterate all documents used
388 if (aDocuments.empty())
389 aDocuments.push_back(document());
390 std::list<DocumentPtr>::iterator aDoc = aDocuments.begin();
391 for(; aDoc != aDocuments.end(); aDoc++) {
393 int aGroupCount = (*aDoc)->size(ModelAPI_ResultGroup::group());
394 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
395 ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
396 (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
397 if (!aResultGroup.get() || !aResultGroup->shape().get())
400 FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
402 AttributeSelectionListPtr aSelectionList =
403 aGroupFeature->selectionList("group_list");
404 if (!isInResults(aSelectionList, aResults, allResultsCashed))// skip group not used in result
407 // conversion of dimension
408 std::string aSelectionType = aSelectionList->selectionType();
409 GeomAPI_Shape::ShapeType aSelType = GeomAPI_Shape::shapeTypeByStr(aSelectionType);
410 std::string aDimensionString =
411 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
412 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
414 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
415 aResultGroup->data()->name());
418 GeomAPI_ShapeExplorer aGroupResExplorer(aResultGroup->shape(), aSelType);
419 for(; aGroupResExplorer.more(); aGroupResExplorer.next()) {
420 GeomShapePtr aGroupShape = aGroupResExplorer.current();
421 if (aDocTrsf.find(*aDoc) != aDocTrsf.end())
422 aGroupShape->move(aDocTrsf[*aDoc]);
423 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aGroupShape);
424 if (aReferenceID == 0) // selected value does not found in the exported shape
426 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
428 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
429 aXaoGroup->add(anElementID);
431 } catch (XAO::XAO_Exception& e) {
433 std::string msg = "An error occurred while exporting group " +
434 aResultGroup->data()->name();
438 msg += "=> skipping this group from XAO export.";
439 Events_InfoMessage("ExportFeature", msg, this).send();
440 aXao.removeGroup(aXaoGroup);
446 int aFieldCount = (*aDoc)->size(ModelAPI_ResultField::group());
447 for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
448 ResultFieldPtr aResultField = std::dynamic_pointer_cast<ModelAPI_ResultField>(
449 (*aDoc)->object(ModelAPI_ResultField::group(), aFieldIndex));
451 FeaturePtr aFieldFeature = (*aDoc)->feature(aResultField);
453 AttributeSelectionListPtr aSelectionList =
454 aFieldFeature->selectionList("selected");
455 std::string aSelectionType = aSelectionList->selectionType();
456 bool isWholePart = aSelectionType == "part";
457 // skip field not used in results
458 if (!isWholePart && !isInResults(aSelectionList, aResults, allResultsCashed))
461 // conversion of dimension
462 std::string aDimensionString =
463 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
464 XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
465 // get tables and their type
466 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
467 std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
468 XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
470 XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
471 aResultField->data()->name());
475 // set components names
476 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
477 for(int aComp = 0; aComp < aComponents->size(); aComp++) {
478 std::string aName = aComponents->value(aComp);
479 aXaoField->setComponentName(aComp, aName);
482 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
483 for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
484 XAO::Step* aStep = aXaoField->addNewStep(aStepIndex + 1);
485 aStep->setStep(aStepIndex + 1);
486 int aStampIndex = aStamps->value(aStepIndex);
487 aStep->setStamp(aStampIndex);
488 int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
489 int aNumComps = aTables->columns();
490 std::set<int> aFilledIDs; // to fill the rest by defaults
491 // omit default values first row
492 for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
493 for(int aCol = 0; aCol < aNumComps; aCol++) {
496 // element index actually is the ID of the selection
497 AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
498 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
499 if (aReferenceID == 0) // selected value does not found in the exported shape
502 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
503 anElementID = aXao.getGeometry()->
504 getElementIndexByReference(aFieldDimension, aReferenceString);
507 ModelAPI_AttributeTables::Value aVal = aTables->value(
508 isWholePart ? 0 : aRow, aCol, aStepIndex);
509 std::string aStrVal = valToString(aVal, aTables->type());
510 aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
511 aFilledIDs.insert(anElementID);
514 if (!isWholePart) { // fill the rest values by default ones
515 XAO::GeometricElementList::iterator allElem =
516 aXao.getGeometry()->begin(aFieldDimension);
517 for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
518 if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
520 for(int aCol = 0; aCol < aNumComps; aCol++) {
522 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex);
523 std::string aStrVal = valToString(aVal, aTables->type());
524 aStep->setStringValue(allElem->first, aCol, aStrVal);
529 } catch (XAO::XAO_Exception& e) {
531 std::string msg = "An error occurred while exporting field " +
532 aResultField->data()->name();
536 msg += "=> skipping this field from XAO export.";
537 Events_InfoMessage("ExportFeature", msg, this).send();
538 aXao.removeField(aXaoField);
545 XAOExport(theFileName, &aXao, anError);
547 if (!anError.empty()) {
548 setError("An error occurred while exporting " + theFileName + ": " + anError);
553 } catch (XAO::XAO_Exception& e) {
554 std::string anError = e.what();
555 setError("An error occurred while exporting " + theFileName + ": " + anError);
561 bool ExchangePlugin_ExportFeature::isMacro() const
563 if (!data().get() || !data()->isValid())
565 ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
566 AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
567 std::string aFormat(aFormatAttr.get() ? aFormatAttr->value() : "");
569 if (aFormat.empty()) { // get default format for the extension
570 AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
571 std::string aFilePath = aFilePathAttr->value();
572 if (!aFilePath.empty()) {
573 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
574 aFormat = anExtension;
578 if (aFormat == "XAO") { // on export to GEOm the selection attribute is filled - this is
579 // an exceptional case where export to XAO feature must be kept
580 AttributeSelectionListPtr aList = aThis->selectionList(SELECTION_LIST_ID());
581 return !aList->isInitialized() || aList->size() == 0;