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());
63 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(), ModelAPI_AttributeString::typeId());
65 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
66 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
67 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
71 * Computes or recomputes the results
73 void ExchangePlugin_ExportFeature::execute()
75 AttributeStringPtr aFormatAttr =
76 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
77 std::string aFormat = aFormatAttr->value();
79 AttributeStringPtr aFilePathAttr =
80 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
81 std::string aFilePath = aFilePathAttr->value();
82 if (aFilePath.empty())
85 exportFile(aFilePath, aFormat);
88 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
89 const std::string& theFormat)
91 std::string aFormatName = theFormat;
93 if (aFormatName.empty()) { // get default format for the extension
95 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
96 if (anExtension == "BREP" || anExtension == "BRP") {
98 } else if (anExtension == "STEP" || anExtension == "STP") {
100 } else if (anExtension == "IGES" || anExtension == "IGS") {
101 aFormatName = "IGES-5.1";
102 } else if (anExtension == "XAO") {
105 aFormatName = anExtension;
109 if (aFormatName == "XAO") {
110 exportXAO(theFileName);
114 // make shape for export from selected shapes
115 AttributeSelectionListPtr aSelectionListAttr =
116 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
117 std::list<GeomShapePtr> aShapes;
118 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
119 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
120 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
121 if (aCurShape.get() == NULL)
122 aCurShape = anAttrSelection->context()->shape();
123 if (aCurShape.get() != NULL)
124 aShapes.push_back(aCurShape);
127 // Store compound if we have more than one shape.
128 std::shared_ptr<GeomAPI_Shape> aShape;
129 if(aShapes.size() == 1) {
130 aShape = aShapes.front();
132 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
135 // Perform the export
137 bool aResult = false;
138 if (aFormatName == "BREP") {
139 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
140 } else if (aFormatName == "STEP") {
141 aResult = STEPExport(theFileName, aFormatName, aShape, anError);
142 } else if (aFormatName.substr(0, 4) == "IGES") {
143 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
145 anError = "Unsupported format: " + aFormatName;
148 if (!anError.empty()) {
149 setError("An error occurred while exporting " + theFileName + ": " + anError);
154 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
163 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
164 aXao.setAuthor(anAuthor);
166 // make shape for export from all results
167 std::list<GeomShapePtr> aShapes;
168 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
169 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
170 ResultBodyPtr aResultBody =
171 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
172 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
173 if (!aResultBody.get())
175 aShapes.push_back(aResultBody->shape());
177 GeomShapePtr aShape = (aShapes.size() == 1)
179 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
181 SetShapeToXAO(aShape, &aXao, anError);
183 if (!anError.empty()) {
184 setError("An error occurred while exporting " + theFileName + ": " + anError);
190 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
191 aXao.getGeometry()->setName(aGeometryName);
195 int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
196 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
197 ResultGroupPtr aResultGroup =
198 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
199 document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
201 FeaturePtr aGroupFeature = document()->feature(aResultGroup);
203 AttributeSelectionListPtr aSelectionList =
204 aGroupFeature->selectionList("group_list");
206 // conversion of dimension
207 std::string aSelectionType = aSelectionList->selectionType();
208 std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
209 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
211 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
212 aResultGroup->data()->name());
214 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
215 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
217 // complex conversion of reference id to element index
218 int aReferenceID = aSelection->Id();
219 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
220 int anElementID = aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
222 aXaoGroup->add(anElementID);
228 XAOExport(theFileName, &aXao, anError);
230 if (!anError.empty()) {
231 setError("An error occurred while exporting " + theFileName + ": " + anError);
235 } catch (XAO::XAO_Exception& e) {
236 std::string anError = e.what();
237 setError("An error occurred while importing " + theFileName + ": " + anError);