Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ExchangePlugin_ExportFeature.h>
22
23 #include <algorithm>
24 #include <iterator>
25 #include <string>
26 #ifdef _DEBUG
27 #include <iostream>
28 #include <ostream>
29 #endif
30
31 #include <Config_Common.h>
32 #include <Config_PropManager.h>
33
34 #include <GeomAlgoAPI_BREPExport.h>
35 #include <GeomAlgoAPI_CompoundBuilder.h>
36 #include <GeomAlgoAPI_IGESExport.h>
37 #include <GeomAlgoAPI_STEPExport.h>
38 #include <GeomAlgoAPI_Tools.h>
39 #include <GeomAlgoAPI_XAOExport.h>
40
41 #include <GeomAPI_Shape.h>
42
43 #include <ModelAPI_AttributeSelectionList.h>
44 #include <ModelAPI_AttributeString.h>
45 #include <ModelAPI_AttributeStringArray.h>
46 #include <ModelAPI_AttributeIntArray.h>
47 #include <ModelAPI_AttributeTables.h>
48 #include <ModelAPI_Data.h>
49 #include <ModelAPI_Document.h>
50 #include <ModelAPI_Object.h>
51 #include <ModelAPI_ResultBody.h>
52 #include <ModelAPI_ResultGroup.h>
53 #include <ModelAPI_ResultField.h>
54 #include <ModelAPI_Session.h>
55 #include <ModelAPI_Validator.h>
56
57 #include <XAO_Group.hxx>
58 #include <XAO_Field.hxx>
59 #include <XAO_Xao.hxx>
60 #include <XAO_Geometry.hxx>
61
62 #include <ExchangePlugin_Tools.h>
63
64 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
65 {
66 }
67
68 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
69 {
70   // TODO Auto-generated destructor stub
71 }
72
73 /*
74  * Request for initialization of data model of the feature: adding all attributes
75  */
76 void ExchangePlugin_ExportFeature::initAttributes()
77 {
78   data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(),
79     ModelAPI_AttributeString::typeId());
80   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
81     ModelAPI_AttributeString::typeId());
82   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
83     ModelAPI_AttributeString::typeId());
84   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
85     ModelAPI_AttributeString::typeId());
86   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
87     ModelAPI_AttributeSelectionList::typeId());
88   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
89     ModelAPI_AttributeString::typeId());
90   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
91     ModelAPI_AttributeString::typeId());
92
93   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
94     ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
95   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
96     ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
97   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
98     ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
99 }
100
101 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
102 {
103   if (theID == XAO_FILE_PATH_ID()) {
104     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
105       string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
106   }
107 }
108
109 /*
110  * Computes or recomputes the results
111  */
112 void ExchangePlugin_ExportFeature::execute()
113 {
114   AttributeStringPtr aFormatAttr =
115       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
116   std::string aFormat = aFormatAttr->value();
117
118   AttributeStringPtr aFilePathAttr =
119       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
120   std::string aFilePath = aFilePathAttr->value();
121   if (aFilePath.empty())
122     return;
123
124   exportFile(aFilePath, aFormat);
125 }
126
127 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
128                                               const std::string& theFormat)
129 {
130   std::string aFormatName = theFormat;
131
132   if (aFormatName.empty()) { // get default format for the extension
133     // ".brep" -> "BREP"
134     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
135     if (anExtension == "BREP" || anExtension == "BRP") {
136       aFormatName = "BREP";
137     } else if (anExtension == "STEP" || anExtension == "STP") {
138       aFormatName = "STEP";
139     } else if (anExtension == "IGES" || anExtension == "IGS") {
140       aFormatName = "IGES-5.1";
141     } else if (anExtension == "XAO") {
142       aFormatName = "XAO";
143     } else {
144       aFormatName = anExtension;
145     }
146   }
147
148   if (aFormatName == "XAO") {
149     exportXAO(theFileName);
150     return;
151   }
152
153   // make shape for export from selected shapes
154   AttributeSelectionListPtr aSelectionListAttr =
155       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
156   std::list<GeomShapePtr> aShapes;
157   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
158     AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
159     std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
160     if (aCurShape.get() == NULL)
161       aCurShape = anAttrSelection->context()->shape();
162     if (aCurShape.get() != NULL)
163       aShapes.push_back(aCurShape);
164   }
165
166   // Store compound if we have more than one shape.
167   std::shared_ptr<GeomAPI_Shape> aShape;
168   if(aShapes.size() == 1) {
169     aShape = aShapes.front();
170   } else {
171     aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
172   }
173
174   // Perform the export
175   std::string anError;
176   bool aResult = false;
177   if (aFormatName == "BREP") {
178     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
179   } else if (aFormatName == "STEP") {
180     aResult = STEPExport(theFileName, aFormatName, aShape, anError);
181   } else if (aFormatName.substr(0, 4) == "IGES") {
182     aResult = IGESExport(theFileName, aFormatName, aShape, anError);
183   } else {
184     anError = "Unsupported format: " + aFormatName;
185   }
186
187   if (!anError.empty()) {
188     setError("An error occurred while exporting " + theFileName + ": " + anError);
189     return;
190   }
191 }
192
193 /// Returns XAO string by the value from the table
194 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
195   const ModelAPI_AttributeTables::ValueType& theType) {
196   std::ostringstream aStr; // the resulting string value
197   switch(theType) {
198   case ModelAPI_AttributeTables::BOOLEAN:
199     aStr<<(theVal.myBool ? "true" : "false");
200     break;
201   case ModelAPI_AttributeTables::INTEGER:
202     aStr<<theVal.myInt;
203     break;
204   case ModelAPI_AttributeTables::DOUBLE:
205     aStr<<theVal.myDouble;
206     break;
207   case ModelAPI_AttributeTables::STRING:
208     aStr<<theVal.myStr;
209     break;
210   }
211   return aStr.str();
212 }
213
214 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
215 {
216   try {
217
218   std::string anError;
219   XAO::Xao aXao;
220
221   // author
222
223   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
224   aXao.setAuthor(anAuthor);
225
226   // make shape for export from all results
227   std::list<GeomShapePtr> aShapes;
228   int aBodyCount = document()->size(ModelAPI_ResultBody::group());
229   for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
230     ResultBodyPtr aResultBody =
231         std::dynamic_pointer_cast<ModelAPI_ResultBody>(
232             document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
233     if (!aResultBody.get())
234       continue;
235     aShapes.push_back(aResultBody->shape());
236   }
237   GeomShapePtr aShape = (aShapes.size() == 1)
238       ? *aShapes.begin()
239       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
240
241   SetShapeToXAO(aShape, &aXao, anError);
242
243   if (!anError.empty()) {
244     setError("An error occurred while exporting " + theFileName + ": " + anError);
245     return;
246   }
247
248   // geometry name
249
250   std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
251   aXao.getGeometry()->setName(aGeometryName);
252
253   // groups
254
255   int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
256   for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
257     ResultGroupPtr aResultGroup =
258         std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
259             document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
260
261     FeaturePtr aGroupFeature = document()->feature(aResultGroup);
262
263     AttributeSelectionListPtr aSelectionList =
264         aGroupFeature->selectionList("group_list");
265
266     // conversion of dimension
267     std::string aSelectionType = aSelectionList->selectionType();
268     std::string aDimensionString =
269       ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
270     XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
271
272     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
273                                           aResultGroup->data()->name());
274
275     for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
276       AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
277
278       // complex conversion of reference id to element index
279       int aReferenceID = aSelection->Id();
280       std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
281       int anElementID =
282         aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
283
284       aXaoGroup->add(anElementID);
285     }
286   }
287
288   // fields
289   int aFieldCount = document()->size(ModelAPI_ResultField::group());
290   for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
291     ResultFieldPtr aResultField =
292         std::dynamic_pointer_cast<ModelAPI_ResultField>(
293             document()->object(ModelAPI_ResultField::group(), aFieldIndex));
294
295     FeaturePtr aFieldFeature = document()->feature(aResultField);
296
297     AttributeSelectionListPtr aSelectionList =
298         aFieldFeature->selectionList("selected");
299
300     // conversion of dimension
301     std::string aSelectionType = aSelectionList->selectionType();
302     std::string aDimensionString =
303       ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
304     XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
305     bool isWholePart = aSelectionType == "part";
306     // get tables and their type
307     std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
308     std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
309     XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
310
311     XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
312                                           aResultField->data()->name());
313     // set components names
314     AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
315     for(int aComp = 0; aComp < aComponents->size(); aComp++) {
316       std::string aName = aComponents->value(aComp);
317       aXaoField->setComponentName(aComp, aName);
318     }
319
320     AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
321     for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
322       XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
323       aStep->setStep(aStepIndex);
324       int aStampIndex = aStamps->value(aStepIndex);
325       aStep->setStamp(aStampIndex);
326       int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
327       int aNumComps = aTables->columns();
328       std::set<int> aFilledIDs; // to fill the rest by defaults
329       // omit default values first row
330       for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
331         for(int aCol = 0; aCol < aNumComps; aCol++) {
332           int anElementID = 0;
333           if (!isWholePart) {
334             // element index actually is the ID of the selection
335             AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
336
337             // complex conversion of reference id to element index
338             int aReferenceID = aSelection->Id();
339             std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
340             anElementID =
341               aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString);
342           }
343
344           ModelAPI_AttributeTables::Value aVal = aTables->value(
345             isWholePart ? 0 : aRow, aCol, aStepIndex);
346           std::string aStrVal = valToString(aVal, aTables->type());
347           aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
348           aFilledIDs.insert(anElementID);
349         }
350       }
351       if (!isWholePart) { // fill the rest values by default ones
352         XAO::GeometricElementList::iterator allElem = aXao.getGeometry()->begin(aFieldDimension);
353         for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
354           if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
355             continue;
356           for(int aCol = 0; aCol < aNumComps; aCol++) {
357             ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex); // default
358             std::string aStrVal = valToString(aVal, aTables->type());
359             aStep->setStringValue(allElem->first, aCol, aStrVal);
360           }
361         }
362       }
363     }
364   }
365
366   // exporting
367   XAOExport(theFileName, &aXao, anError);
368
369   if (!anError.empty()) {
370     setError("An error occurred while exporting " + theFileName + ": " + anError);
371     return;
372   }
373
374   } catch (XAO::XAO_Exception& e) {
375     std::string anError = e.what();
376     setError("An error occurred while exporting " + theFileName + ": " + anError);
377     return;
378   }
379 }