1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ExchangePlugin_ExportFeature.h>
31 #include <Config_Common.h>
32 #include <Config_PropManager.h>
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>
41 #include <GeomAPI_Shape.h>
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>
57 #include <XAO_Group.hxx>
58 #include <XAO_Field.hxx>
59 #include <XAO_Xao.hxx>
60 #include <XAO_Geometry.hxx>
62 #include <ExchangePlugin_Tools.h>
64 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
68 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
70 // TODO Auto-generated destructor stub
74 * Request for initialization of data model of the feature: adding all attributes
76 void ExchangePlugin_ExportFeature::initAttributes()
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());
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());
101 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
103 if (theID == XAO_FILE_PATH_ID()) {
104 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
105 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
110 * Computes or recomputes the results
112 void ExchangePlugin_ExportFeature::execute()
114 AttributeStringPtr aFormatAttr =
115 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
116 std::string aFormat = aFormatAttr->value();
118 AttributeStringPtr aFilePathAttr =
119 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
120 std::string aFilePath = aFilePathAttr->value();
121 if (aFilePath.empty())
124 exportFile(aFilePath, aFormat);
127 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
128 const std::string& theFormat)
130 std::string aFormatName = theFormat;
132 if (aFormatName.empty()) { // get default format for the extension
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") {
144 aFormatName = anExtension;
148 if (aFormatName == "XAO") {
149 exportXAO(theFileName);
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);
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();
171 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
174 // Perform the export
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);
184 anError = "Unsupported format: " + aFormatName;
187 if (!anError.empty()) {
188 setError("An error occurred while exporting " + theFileName + ": " + anError);
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
198 case ModelAPI_AttributeTables::BOOLEAN:
199 aStr<<(theVal.myBool ? "true" : "false");
201 case ModelAPI_AttributeTables::INTEGER:
204 case ModelAPI_AttributeTables::DOUBLE:
205 aStr<<theVal.myDouble;
207 case ModelAPI_AttributeTables::STRING:
214 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
223 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
224 aXao.setAuthor(anAuthor);
226 // make shape for export from all results
227 std::list<GeomShapePtr> aShapes;
228 std::list<ResultBodyPtr> aResults;
229 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
230 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
231 ResultBodyPtr aResultBody =
232 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
233 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
234 if (!aResultBody.get())
236 aShapes.push_back(aResultBody->shape());
237 aResults.push_back(aResultBody);
239 GeomShapePtr aShape = (aShapes.size() == 1)
241 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
243 SetShapeToXAO(aShape, &aXao, anError);
245 if (!anError.empty()) {
246 setError("An error occurred while exporting " + theFileName + ": " + anError);
251 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
252 if (aGeometryName.empty() && aBodyCount == 1) {
253 // get the name from the first result
254 ResultBodyPtr aResultBody = *aResults.begin();
255 aGeometryName = aResultBody->data()->name();
258 aXao.getGeometry()->setName(aGeometryName);
262 int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
263 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
264 ResultGroupPtr aResultGroup =
265 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
266 document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
268 FeaturePtr aGroupFeature = document()->feature(aResultGroup);
270 AttributeSelectionListPtr aSelectionList =
271 aGroupFeature->selectionList("group_list");
273 // conversion of dimension
274 std::string aSelectionType = aSelectionList->selectionType();
275 std::string aDimensionString =
276 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
277 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
279 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
280 aResultGroup->data()->name());
282 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
283 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
285 // complex conversion of reference id to element index
286 // gives bad id in case the selection is done from python script
287 // => using GeomAlgoAPI_CompoundBuilder::id instead
288 // int aReferenceID_old = aSelection->Id();
290 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
292 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
294 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
296 aXaoGroup->add(anElementID);
301 int aFieldCount = document()->size(ModelAPI_ResultField::group());
302 for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
303 ResultFieldPtr aResultField =
304 std::dynamic_pointer_cast<ModelAPI_ResultField>(
305 document()->object(ModelAPI_ResultField::group(), aFieldIndex));
307 FeaturePtr aFieldFeature = document()->feature(aResultField);
309 AttributeSelectionListPtr aSelectionList =
310 aFieldFeature->selectionList("selected");
312 // conversion of dimension
313 std::string aSelectionType = aSelectionList->selectionType();
314 std::string aDimensionString =
315 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
316 XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
317 bool isWholePart = aSelectionType == "part";
318 // get tables and their type
319 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
320 std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
321 XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
323 XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
324 aResultField->data()->name());
325 // set components names
326 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
327 for(int aComp = 0; aComp < aComponents->size(); aComp++) {
328 std::string aName = aComponents->value(aComp);
329 aXaoField->setComponentName(aComp, aName);
332 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
333 for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
334 XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
335 aStep->setStep(aStepIndex);
336 int aStampIndex = aStamps->value(aStepIndex);
337 aStep->setStamp(aStampIndex);
338 int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
339 int aNumComps = aTables->columns();
340 std::set<int> aFilledIDs; // to fill the rest by defaults
341 // omit default values first row
342 for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
343 for(int aCol = 0; aCol < aNumComps; aCol++) {
346 // element index actually is the ID of the selection
347 AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
349 // complex conversion of reference id to element index
350 // gives bad id in case the selection is done from python script
351 // => using GeomAlgoAPI_CompoundBuilder::id instead
352 //int aReferenceID_old = aSelection->Id();
354 int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
356 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
358 aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString);
361 ModelAPI_AttributeTables::Value aVal = aTables->value(
362 isWholePart ? 0 : aRow, aCol, aStepIndex);
363 std::string aStrVal = valToString(aVal, aTables->type());
364 aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
365 aFilledIDs.insert(anElementID);
368 if (!isWholePart) { // fill the rest values by default ones
369 XAO::GeometricElementList::iterator allElem = aXao.getGeometry()->begin(aFieldDimension);
370 for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
371 if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
373 for(int aCol = 0; aCol < aNumComps; aCol++) {
374 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex); // default
375 std::string aStrVal = valToString(aVal, aTables->type());
376 aStep->setStringValue(allElem->first, aCol, aStrVal);
384 XAOExport(theFileName, &aXao, anError);
386 if (!anError.empty()) {
387 setError("An error occurred while exporting " + theFileName + ": " + anError);
391 } catch (XAO::XAO_Exception& e) {
392 std::string anError = e.what();
393 setError("An error occurred while exporting " + theFileName + ": " + anError);