]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp
Salome HOME
Fixed validators for Boolean and Intersection
[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::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());
65
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());
70 }
71
72 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
73 {
74   if (theID == XAO_FILE_PATH_ID()) {
75     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
76       string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
77   }
78 }
79
80 /*
81  * Computes or recomputes the results
82  */
83 void ExchangePlugin_ExportFeature::execute()
84 {
85   AttributeStringPtr aFormatAttr =
86       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
87   std::string aFormat = aFormatAttr->value();
88
89   AttributeStringPtr aFilePathAttr =
90       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
91   std::string aFilePath = aFilePathAttr->value();
92   if (aFilePath.empty())
93     return;
94
95   exportFile(aFilePath, aFormat);
96 }
97
98 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
99                                               const std::string& theFormat)
100 {
101   std::string aFormatName = theFormat;
102
103   if (aFormatName.empty()) { // get default format for the extension
104     // ".brep" -> "BREP"
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") {
113       aFormatName = "XAO";
114     } else {
115       aFormatName = anExtension;
116     }
117   }
118
119   if (aFormatName == "XAO") {
120     exportXAO(theFileName);
121     return;
122   }
123
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);
135   }
136
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();
141   } else {
142     aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
143   }
144
145   // Perform the export
146   std::string anError;
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);
154   } else {
155     anError = "Unsupported format: " + aFormatName;
156   }
157
158   if (!anError.empty()) {
159     setError("An error occurred while exporting " + theFileName + ": " + anError);
160     return;
161   }
162 }
163
164 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
165 {
166   try {
167
168   std::string anError;
169   XAO::Xao aXao;
170
171   // author
172
173   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
174   aXao.setAuthor(anAuthor);
175
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())
184       continue;
185     aShapes.push_back(aResultBody->shape());
186   }
187   GeomShapePtr aShape = (aShapes.size() == 1)
188       ? *aShapes.begin()
189       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
190
191   SetShapeToXAO(aShape, &aXao, anError);
192
193   if (!anError.empty()) {
194     setError("An error occurred while exporting " + theFileName + ": " + anError);
195     return;
196   }
197
198   // geometry name
199
200   std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
201   aXao.getGeometry()->setName(aGeometryName);
202
203   // groups
204
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));
210
211     FeaturePtr aGroupFeature = document()->feature(aResultGroup);
212
213     AttributeSelectionListPtr aSelectionList =
214         aGroupFeature->selectionList("group_list");
215
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);
220
221     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
222                                           aResultGroup->data()->name());
223
224     for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
225       AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
226
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);
231
232       aXaoGroup->add(anElementID);
233     }
234   }
235
236   // exporting
237
238   XAOExport(theFileName, &aXao, anError);
239
240   if (!anError.empty()) {
241     setError("An error occurred while exporting " + theFileName + ": " + anError);
242     return;
243   }
244
245   } catch (XAO::XAO_Exception& e) {
246     std::string anError = e.what();
247     setError("An error occurred while importing " + theFileName + ": " + anError);
248     return;
249   }
250 }