1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ExchangePlugin_ExportFeature.cpp
4 // Created: May 14, 2015
5 // Author: Sergey POKHODENKO
7 #include <ExchangePlugin_ExportFeature.h>
9 #include <ExchangePlugin_Tools.h>
11 #include <GeomAlgoAPI_BREPExport.h>
12 #include <GeomAlgoAPI_CompoundBuilder.h>
13 #include <GeomAlgoAPI_IGESExport.h>
14 #include <GeomAlgoAPI_STEPExport.h>
15 #include <GeomAlgoAPI_Tools.h>
17 #include <Config_Common.h>
18 #include <Config_PropManager.h>
20 #include <GeomAPI_Shape.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_ResultBody.h>
37 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
41 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
43 // TODO Auto-generated destructor stub
47 * Returns the unique kind of a feature
49 const std::string& ExchangePlugin_ExportFeature::getKind()
51 return ExchangePlugin_ExportFeature::ID();
55 * Request for initialization of data model of the feature: adding all attributes
57 void ExchangePlugin_ExportFeature::initAttributes()
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());
65 * Computes or recomputes the results
67 void ExchangePlugin_ExportFeature::execute()
69 AttributeStringPtr aFormatAttr =
70 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
71 std::string aFormat = aFormatAttr->value();
72 // Format may be empty. In this case look at extension.
73 // if (aFormat.empty())
76 AttributeStringPtr aFilePathAttr =
77 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
78 std::string aFilePath = aFilePathAttr->value();
79 if (aFilePath.empty())
82 AttributeSelectionListPtr aSelectionListAttr =
83 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
84 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
85 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
86 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
87 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
88 if (aCurShape.get() == NULL)
89 aCurShape = anAttrSelection->context()->shape();
90 if (aCurShape.get() != NULL)
91 aShapes.push_back(aCurShape);
94 // Store compound if we have more than one shape.
95 std::shared_ptr<GeomAPI_Shape> aShape;
96 if(aShapes.size() == 1) {
97 aShape = aShapes.front();
99 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
102 exportFile(aFilePath, aFormat, aShape);
105 bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
106 const std::string& theFormat,
107 std::shared_ptr<GeomAPI_Shape> theShape)
109 // retrieve the file and plugin library names
110 std::string aFormatName = theFormat;
112 if (theFormat.empty()) { // look at extension
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";
122 aFormatName = anExtension;
126 // Perform the export
128 bool aResult = false;
129 if (aFormatName == "BREP") {
130 aResult = BREPExport(theFileName, aFormatName, theShape, anError);
131 } else if (aFormatName == "STEP") {
132 aResult = STEPExport(theFileName, aFormatName, theShape, anError);
133 } else if (aFormatName.substr(0, 4) == "IGES") {
134 aResult = IGESExport(theFileName, aFormatName, theShape, anError);
136 anError = "Unsupported format " + aFormatName;
140 std::string aShapeError =
141 "An error occurred while exporting " + theFileName + ": " + anError;
142 setError(aShapeError);