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>
17 #include <Config_Common.h>
18 #include <Config_PropManager.h>
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>
27 #include <GeomAPI_Shape.h>
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_Session.h>
36 #include <ModelAPI_Validator.h>
38 #include <XAO_Xao.hxx>
40 #include <ExchangePlugin_Tools.h>
42 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
46 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
48 // TODO Auto-generated destructor stub
52 * Request for initialization of data model of the feature: adding all attributes
54 void ExchangePlugin_ExportFeature::initAttributes()
56 data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
57 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
58 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
59 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
60 data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString::typeId());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
66 * Computes or recomputes the results
68 void ExchangePlugin_ExportFeature::execute()
70 AttributeStringPtr aFormatAttr =
71 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
72 std::string aFormat = aFormatAttr->value();
73 // Format may be empty. In this case look at extension.
74 // if (aFormat.empty())
77 AttributeStringPtr aFilePathAttr =
78 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
79 std::string aFilePath = aFilePathAttr->value();
80 if (aFilePath.empty())
83 AttributeSelectionListPtr aSelectionListAttr =
84 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
85 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
86 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
87 AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
88 std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
89 if (aCurShape.get() == NULL)
90 aCurShape = anAttrSelection->context()->shape();
91 if (aCurShape.get() != NULL)
92 aShapes.push_back(aCurShape);
95 // Store compound if we have more than one shape.
96 std::shared_ptr<GeomAPI_Shape> aShape;
97 if(aShapes.size() == 1) {
98 aShape = aShapes.front();
100 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
103 exportFile(aFilePath, aFormat, aShape);
106 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
107 const std::string& theFormat,
108 std::shared_ptr<GeomAPI_Shape> theShape)
110 std::string aFormatName = theFormat;
112 if (aFormatName.empty()) { // get default format for the 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";
121 } else if (anExtension == "XAO") {
124 aFormatName = anExtension;
128 if (aFormatName == "XAO") {
129 exportXAO(theFileName, theShape);
133 // Perform the export
135 bool aResult = false;
136 if (aFormatName == "BREP") {
137 aResult = BREPExport(theFileName, aFormatName, theShape, anError);
138 } else if (aFormatName == "STEP") {
139 aResult = STEPExport(theFileName, aFormatName, theShape, anError);
140 } else if (aFormatName.substr(0, 4) == "IGES") {
141 aResult = IGESExport(theFileName, aFormatName, theShape, anError);
143 anError = "Unsupported format: " + aFormatName;
146 if (!anError.empty()) {
147 setError("An error occurred while exporting " + theFileName + ": " + anError);
152 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName,
153 std::shared_ptr<GeomAPI_Shape> theShape)
155 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
157 XAO::Xao aXao(anAuthor, "1.0");
160 XAOExport(theFileName, theShape, &aXao, anError);
162 if (!anError.empty()) {
163 setError("An error occurred while exporting " + theFileName + ": " + anError);