]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp
Salome HOME
Issue #1834: Fix length of lines
[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(),
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());
72
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());
79 }
80
81 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
82 {
83   if (theID == XAO_FILE_PATH_ID()) {
84     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
85       string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
86   }
87 }
88
89 /*
90  * Computes or recomputes the results
91  */
92 void ExchangePlugin_ExportFeature::execute()
93 {
94   AttributeStringPtr aFormatAttr =
95       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
96   std::string aFormat = aFormatAttr->value();
97
98   AttributeStringPtr aFilePathAttr =
99       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
100   std::string aFilePath = aFilePathAttr->value();
101   if (aFilePath.empty())
102     return;
103
104   exportFile(aFilePath, aFormat);
105 }
106
107 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
108                                               const std::string& theFormat)
109 {
110   std::string aFormatName = theFormat;
111
112   if (aFormatName.empty()) { // get default format for the extension
113     // ".brep" -> "BREP"
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") {
122       aFormatName = "XAO";
123     } else {
124       aFormatName = anExtension;
125     }
126   }
127
128   if (aFormatName == "XAO") {
129     exportXAO(theFileName);
130     return;
131   }
132
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);
144   }
145
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();
150   } else {
151     aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
152   }
153
154   // Perform the export
155   std::string anError;
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);
163   } else {
164     anError = "Unsupported format: " + aFormatName;
165   }
166
167   if (!anError.empty()) {
168     setError("An error occurred while exporting " + theFileName + ": " + anError);
169     return;
170   }
171 }
172
173 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
174 {
175   try {
176
177   std::string anError;
178   XAO::Xao aXao;
179
180   // author
181
182   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
183   aXao.setAuthor(anAuthor);
184
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())
193       continue;
194     aShapes.push_back(aResultBody->shape());
195   }
196   GeomShapePtr aShape = (aShapes.size() == 1)
197       ? *aShapes.begin()
198       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
199
200   SetShapeToXAO(aShape, &aXao, anError);
201
202   if (!anError.empty()) {
203     setError("An error occurred while exporting " + theFileName + ": " + anError);
204     return;
205   }
206
207   // geometry name
208
209   std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
210   aXao.getGeometry()->setName(aGeometryName);
211
212   // groups
213
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));
219
220     FeaturePtr aGroupFeature = document()->feature(aResultGroup);
221
222     AttributeSelectionListPtr aSelectionList =
223         aGroupFeature->selectionList("group_list");
224
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);
229
230     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
231                                           aResultGroup->data()->name());
232
233     for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
234       AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
235
236       // complex conversion of reference id to element index
237       int aReferenceID = aSelection->Id();
238       std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
239       int anElementID = 
240         aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
241
242       aXaoGroup->add(anElementID);
243     }
244   }
245
246   // exporting
247
248   XAOExport(theFileName, &aXao, anError);
249
250   if (!anError.empty()) {
251     setError("An error occurred while exporting " + theFileName + ": " + anError);
252     return;
253   }
254
255   } catch (XAO::XAO_Exception& e) {
256     std::string anError = e.what();
257     setError("An error occurred while importing " + theFileName + ": " + anError);
258     return;
259   }
260 }