]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp
Salome HOME
81c62671d95e8ffb77f6ba63cee5a25ea0c840c7
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    ExchangePlugin_ExportFeature.cpp
4 // Created: May 14, 2015
5 // Author:  Sergey POKHODENKO
6
7 #include <ExchangePlugin_ExportFeature.h>
8
9 #include <algorithm>
10 #include <iterator>
11 #include <string>
12 #ifdef _DEBUG
13 #include <iostream>
14 #include <ostream>
15 #endif
16
17 #include <Config_Common.h>
18 #include <Config_PropManager.h>
19
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>
26
27 #include <GeomAPI_Shape.h>
28
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>
38
39 #include <XAO_Group.hxx>
40 #include <XAO_Xao.hxx>
41
42 #include <ExchangePlugin_Tools.h>
43
44 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
45 {
46 }
47
48 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
49 {
50   // TODO Auto-generated destructor stub
51 }
52
53 /*
54  * Request for initialization of data model of the feature: adding all attributes
55  */
56 void ExchangePlugin_ExportFeature::initAttributes()
57 {
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());
64
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());
68 }
69
70 /*
71  * Computes or recomputes the results
72  */
73 void ExchangePlugin_ExportFeature::execute()
74 {
75   AttributeStringPtr aFormatAttr =
76       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
77   std::string aFormat = aFormatAttr->value();
78
79   AttributeStringPtr aFilePathAttr =
80       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
81   std::string aFilePath = aFilePathAttr->value();
82   if (aFilePath.empty())
83     return;
84
85   exportFile(aFilePath, aFormat);
86 }
87
88 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
89                                               const std::string& theFormat)
90 {
91   std::string aFormatName = theFormat;
92
93   if (aFormatName.empty()) { // get default format for the extension
94     // ".brep" -> "BREP"
95     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
96     if (anExtension == "BREP" || anExtension == "BRP") {
97       aFormatName = "BREP";
98     } else if (anExtension == "STEP" || anExtension == "STP") {
99       aFormatName = "STEP";
100     } else if (anExtension == "IGES" || anExtension == "IGS") {
101       aFormatName = "IGES-5.1";
102     } else if (anExtension == "XAO") {
103       aFormatName = "XAO";
104     } else {
105       aFormatName = anExtension;
106     }
107   }
108
109   if (aFormatName == "XAO") {
110     exportXAO(theFileName);
111     return;
112   }
113
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);
125   }
126
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();
131   } else {
132     aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
133   }
134
135   // Perform the export
136   std::string anError;
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);
144   } else {
145     anError = "Unsupported format: " + aFormatName;
146   }
147
148   if (!anError.empty()) {
149     setError("An error occurred while exporting " + theFileName + ": " + anError);
150     return;
151   }
152 }
153
154 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
155 {
156   try {
157
158   std::string anError;
159   XAO::Xao aXao;
160
161   // author
162
163   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
164   aXao.setAuthor(anAuthor);
165
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())
174       continue;
175     aShapes.push_back(aResultBody->shape());
176   }
177   GeomShapePtr aShape = (aShapes.size() == 1)
178       ? *aShapes.begin()
179       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
180
181   SetShapeToXAO(aShape, &aXao, anError);
182
183   if (!anError.empty()) {
184     setError("An error occurred while exporting " + theFileName + ": " + anError);
185     return;
186   }
187
188   // geometry name
189
190   std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
191   aXao.getGeometry()->setName(aGeometryName);
192
193   // groups
194
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));
200
201     FeaturePtr aGroupFeature = document()->feature(aResultGroup);
202
203     AttributeSelectionListPtr aSelectionList =
204         aGroupFeature->selectionList("group_list");
205
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);
210
211     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
212                                           aResultGroup->data()->name());
213
214     for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
215       AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
216
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);
221
222       aXaoGroup->add(anElementID);
223     }
224   }
225
226   // exporting
227
228   XAOExport(theFileName, &aXao, anError);
229
230   if (!anError.empty()) {
231     setError("An error occurred while exporting " + theFileName + ": " + anError);
232     return;
233   }
234
235   } catch (XAO::XAO_Exception& e) {
236     std::string anError = e.what();
237     setError("An error occurred while importing " + theFileName + ": " + anError);
238     return;
239   }
240 }