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::XAO_FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
61 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
62 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
63 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString::typeId());
64 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(), ModelAPI_AttributeString::typeId());
66 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
67 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
68 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
69 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
72 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
74 if (theID == XAO_FILE_PATH_ID()) {
75 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
76 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
81 * Computes or recomputes the results
83 void ExchangePlugin_ExportFeature::execute()
85 AttributeStringPtr aFormatAttr =
86 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
87 std::string aFormat = aFormatAttr->value();
89 AttributeStringPtr aFilePathAttr =
90 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
91 std::string aFilePath = aFilePathAttr->value();
92 if (aFilePath.empty())
95 exportFile(aFilePath, aFormat);
98 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
99 const std::string& theFormat)
101 std::string aFormatName = theFormat;
103 if (aFormatName.empty()) { // get default format for the extension
105 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
106 if (anExtension == "BREP" || anExtension == "BRP") {
107 aFormatName = "BREP";
108 } else if (anExtension == "STEP" || anExtension == "STP") {
109 aFormatName = "STEP";
110 } else if (anExtension == "IGES" || anExtension == "IGS") {
111 aFormatName = "IGES-5.1";
112 } else if (anExtension == "XAO") {
115 aFormatName = anExtension;
119 if (aFormatName == "XAO") {
120 exportXAO(theFileName);
124 // make shape for export from selected shapes
125 AttributeSelectionListPtr aSelectionListAttr =
126 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
127 std::list<GeomShapePtr> aShapes;
128 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
129 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
130 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
131 if (aCurShape.get() == NULL)
132 aCurShape = anAttrSelection->context()->shape();
133 if (aCurShape.get() != NULL)
134 aShapes.push_back(aCurShape);
137 // Store compound if we have more than one shape.
138 std::shared_ptr<GeomAPI_Shape> aShape;
139 if(aShapes.size() == 1) {
140 aShape = aShapes.front();
142 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
145 // Perform the export
147 bool aResult = false;
148 if (aFormatName == "BREP") {
149 aResult = BREPExport(theFileName, aFormatName, aShape, anError);
150 } else if (aFormatName == "STEP") {
151 aResult = STEPExport(theFileName, aFormatName, aShape, anError);
152 } else if (aFormatName.substr(0, 4) == "IGES") {
153 aResult = IGESExport(theFileName, aFormatName, aShape, anError);
155 anError = "Unsupported format: " + aFormatName;
158 if (!anError.empty()) {
159 setError("An error occurred while exporting " + theFileName + ": " + anError);
164 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
173 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
174 aXao.setAuthor(anAuthor);
176 // make shape for export from all results
177 std::list<GeomShapePtr> aShapes;
178 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
179 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
180 ResultBodyPtr aResultBody =
181 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
182 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
183 if (!aResultBody.get())
185 aShapes.push_back(aResultBody->shape());
187 GeomShapePtr aShape = (aShapes.size() == 1)
189 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
191 SetShapeToXAO(aShape, &aXao, anError);
193 if (!anError.empty()) {
194 setError("An error occurred while exporting " + theFileName + ": " + anError);
200 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
201 aXao.getGeometry()->setName(aGeometryName);
205 int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
206 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
207 ResultGroupPtr aResultGroup =
208 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
209 document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
211 FeaturePtr aGroupFeature = document()->feature(aResultGroup);
213 AttributeSelectionListPtr aSelectionList =
214 aGroupFeature->selectionList("group_list");
216 // conversion of dimension
217 std::string aSelectionType = aSelectionList->selectionType();
218 std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
219 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
221 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
222 aResultGroup->data()->name());
224 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
225 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
227 // complex conversion of reference id to element index
228 int aReferenceID = aSelection->Id();
229 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
230 int anElementID = aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
232 aXaoGroup->add(anElementID);
238 XAOExport(theFileName, &aXao, anError);
240 if (!anError.empty()) {
241 setError("An error occurred while exporting " + theFileName + ": " + anError);
245 } catch (XAO::XAO_Exception& e) {
246 std::string anError = e.what();
247 setError("An error occurred while importing " + theFileName + ": " + anError);