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(),
59 ModelAPI_AttributeString::typeId());
60 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
61 ModelAPI_AttributeString::typeId());
62 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
63 ModelAPI_AttributeString::typeId());
64 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
65 ModelAPI_AttributeString::typeId());
66 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
67 ModelAPI_AttributeSelectionList::typeId());
68 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
69 ModelAPI_AttributeString::typeId());
70 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
71 ModelAPI_AttributeString::typeId());
73 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
74 ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
75 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
76 ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
77 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
78 ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
81 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
83 if (theID == XAO_FILE_PATH_ID()) {
84 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
85 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
90 * Computes or recomputes the results
92 void ExchangePlugin_ExportFeature::execute()
94 AttributeStringPtr aFormatAttr =
95 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
96 std::string aFormat = aFormatAttr->value();
98 AttributeStringPtr aFilePathAttr =
99 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
100 std::string aFilePath = aFilePathAttr->value();
101 if (aFilePath.empty())
104 exportFile(aFilePath, aFormat);
107 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
108 const std::string& theFormat)
110 std::string aFormatName = theFormat;
112 if (aFormatName.empty()) { // get default format for the extension
114 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
115 if (anExtension == "BREP" || anExtension == "BRP") {
116 aFormatName = "BREP";
117 } else if (anExtension == "STEP" || anExtension == "STP") {
118 aFormatName = "STEP";
119 } else if (anExtension == "IGES" || anExtension == "IGS") {
120 aFormatName = "IGES-5.1";
121 } else if (anExtension == "XAO") {
124 aFormatName = anExtension;
128 if (aFormatName == "XAO") {
129 exportXAO(theFileName);
133 // make shape for export from selected shapes
134 AttributeSelectionListPtr aSelectionListAttr =
135 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
136 std::list<GeomShapePtr> aShapes;
137 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
138 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
139 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
140 if (aCurShape.get() == NULL)
141 aCurShape = anAttrSelection->context()->shape();
142 if (aCurShape.get() != NULL)
143 aShapes.push_back(aCurShape);
146 // Store compound if we have more than one shape.
147 std::shared_ptr<GeomAPI_Shape> aShape;
148 if(aShapes.size() == 1) {
149 aShape = aShapes.front();
151 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
154 // Perform the export
156 bool aResult = false;
157 if (aFormatName == "BREP") {
158 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
159 } else if (aFormatName == "STEP") {
160 aResult = STEPExport(theFileName, aFormatName, aShape, anError);
161 } else if (aFormatName.substr(0, 4) == "IGES") {
162 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
164 anError = "Unsupported format: " + aFormatName;
167 if (!anError.empty()) {
168 setError("An error occurred while exporting " + theFileName + ": " + anError);
173 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
182 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
183 aXao.setAuthor(anAuthor);
185 // make shape for export from all results
186 std::list<GeomShapePtr> aShapes;
187 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
188 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
189 ResultBodyPtr aResultBody =
190 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
191 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
192 if (!aResultBody.get())
194 aShapes.push_back(aResultBody->shape());
196 GeomShapePtr aShape = (aShapes.size() == 1)
198 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
200 SetShapeToXAO(aShape, &aXao, anError);
202 if (!anError.empty()) {
203 setError("An error occurred while exporting " + theFileName + ": " + anError);
209 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
210 aXao.getGeometry()->setName(aGeometryName);
214 int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
215 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
216 ResultGroupPtr aResultGroup =
217 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
218 document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
220 FeaturePtr aGroupFeature = document()->feature(aResultGroup);
222 AttributeSelectionListPtr aSelectionList =
223 aGroupFeature->selectionList("group_list");
225 // conversion of dimension
226 std::string aSelectionType = aSelectionList->selectionType();
227 std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
228 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
230 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
231 aResultGroup->data()->name());
233 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
234 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
236 // complex conversion of reference id to element index
237 int aReferenceID = aSelection->Id();
238 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
240 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
242 aXaoGroup->add(anElementID);
248 XAOExport(theFileName, &aXao, anError);
250 if (!anError.empty()) {
251 setError("An error occurred while exporting " + theFileName + ": " + anError);
255 } catch (XAO::XAO_Exception& e) {
256 std::string anError = e.what();
257 setError("An error occurred while importing " + theFileName + ": " + anError);