1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ExchangePlugin_ImportFeature.cpp
4 // Created: Aug 28, 2014
5 // Authors: Sergey BELASH, Sergey POKHODENKO
7 #include <ExchangePlugin_ImportFeature.h>
12 #include <Config_Common.h>
13 #include <Config_PropManager.h>
15 #include <GeomAlgoAPI_BREPImport.h>
16 #include <GeomAlgoAPI_IGESImport.h>
17 #include <GeomAlgoAPI_STEPImport.h>
18 #include <GeomAlgoAPI_Tools.h>
19 #include <GeomAlgoAPI_XAOImport.h>
21 #include <GeomAPI_Shape.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_BodyBuilder.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Object.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_ResultGroup.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
36 #include <XAO_Xao.hxx>
37 #include <XAO_Group.hxx>
39 #include <ExchangePlugin_Tools.h>
41 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
45 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
47 // TODO Auto-generated destructor stub
51 * Request for initialization of data model of the feature: adding all attributes
53 void ExchangePlugin_ImportFeature::initAttributes()
55 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
56 data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
58 ModelAPI_Session::get()->validators()->registerNotObligatory(
59 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
63 * Computes or recomputes the results
65 void ExchangePlugin_ImportFeature::execute()
67 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
68 std::string aFilePath = aFilePathAttr->value();
69 if (aFilePath.empty()) {
70 setError("File path is empty.");
74 importFile(aFilePath);
77 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
78 std::shared_ptr<GeomAPI_Shape> aGeomShape)
80 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
81 //LoadNamingDS of the imported shape
82 loadNamingDS(aGeomShape, aResultBody);
86 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
89 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
91 if (anExtension == "XAO") {
92 importXAO(theFileName);
98 std::shared_ptr<GeomAPI_Shape> aGeomShape;
99 if (anExtension == "BREP" || anExtension == "BRP") {
100 aGeomShape = BREPImport(theFileName, anExtension, anError);
101 } else if (anExtension == "STEP" || anExtension == "STP") {
102 aGeomShape = STEPImport(theFileName, anExtension, anError);
103 } else if (anExtension == "IGES" || anExtension == "IGS") {
104 aGeomShape = IGESImport(theFileName, anExtension, anError);
106 anError = "Unsupported format: " + anExtension;
109 // Check if shape is valid
110 if (!anError.empty()) {
111 setError("An error occurred while importing " + theFileName + ": " + anError);
115 // Pass the results into the model
116 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
117 data()->setName(anObjectName);
119 setResult(createResultBody(aGeomShape));
122 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
128 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
130 if (!anError.empty()) {
131 setError("An error occurred while importing " + theFileName + ": " + anError);
135 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
137 // use the geometry name or the file name for the feature
138 std::string aBodyName = aXaoGeometry->getName();
139 if (aBodyName.empty())
140 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
141 data()->setName(aBodyName);
143 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
144 setResult(aResultBody);
147 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
148 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
150 // Remove previous groups stored in RefList
151 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
152 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
153 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
154 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
156 document()->removeFeature(aFeature);
160 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
161 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
163 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
166 if (!aXaoGroup->getName().empty())
167 aGroupFeature->data()->setName(aXaoGroup->getName());
170 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
172 // conversion of dimension
173 XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
174 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
175 std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
177 aSelectionList->setSelectionType(aSelectionType);
178 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
179 aSelectionList->append(aResultBody, GeomShapePtr());
180 // complex conversion of element index to reference id
181 int anElementID = aXaoGroup->get(anElementIndex);
182 std::string aReferenceString =
183 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
184 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
186 aSelectionList->value(anElementIndex)->setId(aReferenceID);
189 // aRefListOfGroups->append(aGroupFeature);
191 // // hide the group in the history
192 // document()->setCurrentFeature(aGroupFeature, false);
193 // // groups features is internal part of the import
194 // aGroupFeature->setInHistory(aGroupFeature, false);
197 } catch (XAO::XAO_Exception& e) {
198 std::string anError = e.what();
199 setError("An error occurred while importing " + theFileName + ": " + anError);
204 //============================================================================
205 void ExchangePlugin_ImportFeature::loadNamingDS(
206 std::shared_ptr<GeomAPI_Shape> theGeomShape,
207 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
210 theResultBody->store(theGeomShape);
213 std::string aNameMS = "Shape";
214 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);