1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ExchangePlugin_ExportFeature.cpp
4 // Created: May 14, 2015
5 // Author: Sergey POKHODENKO
7 #include <ExchangePlugin_ExportFeature.h>
17 #include <Config_Common.h>
18 #include <Config_PropManager.h>
20 #include <GeomAlgoAPI_BREPExport.h>
21 #include <GeomAlgoAPI_CompoundBuilder.h>
22 #include <GeomAlgoAPI_IGESExport.h>
23 #include <GeomAlgoAPI_STEPExport.h>
24 #include <GeomAlgoAPI_Tools.h>
25 #include <GeomAlgoAPI_XAOExport.h>
27 #include <GeomAPI_Shape.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_AttributeString.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Document.h>
33 #include <ModelAPI_Object.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_ResultGroup.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
39 #include <XAO_Group.hxx>
40 #include <XAO_Xao.hxx>
42 #include <ExchangePlugin_Tools.h>
44 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
48 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
50 // TODO Auto-generated destructor stub
54 * Request for initialization of data model of the feature: adding all attributes
56 void ExchangePlugin_ExportFeature::initAttributes()
58 data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
59 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
60 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
61 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
62 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString::typeId());
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
65 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
69 * Computes or recomputes the results
71 void ExchangePlugin_ExportFeature::execute()
73 AttributeStringPtr aFormatAttr =
74 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
75 std::string aFormat = aFormatAttr->value();
77 AttributeStringPtr aFilePathAttr =
78 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
79 std::string aFilePath = aFilePathAttr->value();
80 if (aFilePath.empty())
83 exportFile(aFilePath, aFormat);
86 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
87 const std::string& theFormat)
89 std::string aFormatName = theFormat;
91 if (aFormatName.empty()) { // get default format for the extension
93 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
94 if (anExtension == "BREP" || anExtension == "BRP") {
96 } else if (anExtension == "STEP" || anExtension == "STP") {
98 } else if (anExtension == "IGES" || anExtension == "IGS") {
99 aFormatName = "IGES-5.1";
100 } else if (anExtension == "XAO") {
103 aFormatName = anExtension;
107 if (aFormatName == "XAO") {
108 exportXAO(theFileName);
112 // make shape for export from selected shapes
113 AttributeSelectionListPtr aSelectionListAttr =
114 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
115 std::list<GeomShapePtr> aShapes;
116 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
117 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
118 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
119 if (aCurShape.get() == NULL)
120 aCurShape = anAttrSelection->context()->shape();
121 if (aCurShape.get() != NULL)
122 aShapes.push_back(aCurShape);
125 // Store compound if we have more than one shape.
126 std::shared_ptr<GeomAPI_Shape> aShape;
127 if(aShapes.size() == 1) {
128 aShape = aShapes.front();
130 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
133 // Perform the export
135 bool aResult = false;
136 if (aFormatName == "BREP") {
137 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
138 } else if (aFormatName == "STEP") {
139 aResult = STEPExport(theFileName, aFormatName, aShape, anError);
140 } else if (aFormatName.substr(0, 4) == "IGES") {
141 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
143 anError = "Unsupported format: " + aFormatName;
146 if (!anError.empty()) {
147 setError("An error occurred while exporting " + theFileName + ": " + anError);
152 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
161 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
162 aXao.setAuthor(anAuthor);
164 // make shape for export from all results
165 std::list<GeomShapePtr> aShapes;
166 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
167 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
168 ResultBodyPtr aResultBody =
169 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
170 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
171 if (!aResultBody.get())
173 aShapes.push_back(aResultBody->shape());
175 GeomShapePtr aShape = (aShapes.size() == 1)
177 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
179 SetShapeToXAO(aShape, &aXao, anError);
181 if (!anError.empty()) {
182 setError("An error occurred while exporting " + theFileName + ": " + anError);
188 int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
189 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
190 ResultGroupPtr aResultGroup =
191 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
192 document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
194 FeaturePtr aGroupFeature = document()->feature(aResultGroup);
196 AttributeSelectionListPtr aSelectionList =
197 aGroupFeature->selectionList("group_list");
199 // conversion of dimension
200 std::string aSelectionType = aSelectionList->selectionType();
201 std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
202 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
204 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
205 aResultGroup->data()->name());
207 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
208 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
210 // complex conversion of reference id to element index
211 int aReferenceID = aSelection->Id();
212 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
213 int anElementID = aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
215 aXaoGroup->add(anElementID);
221 XAOExport(theFileName, &aXao, anError);
223 if (!anError.empty()) {
224 setError("An error occurred while exporting " + theFileName + ": " + anError);
228 } catch (XAO::XAO_Exception& e) {
229 std::string anError = e.what();
230 setError("An error occurred while importing " + theFileName + ": " + anError);