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(),
56 ModelAPI_AttributeString::typeId());
57 AttributePtr aFeaturesAttribute =
58 data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(),
59 ModelAPI_AttributeRefList::typeId());
60 aFeaturesAttribute->setIsArgument(false);
62 ModelAPI_Session::get()->validators()->registerNotObligatory(
63 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
67 * Computes or recomputes the results
69 void ExchangePlugin_ImportFeature::execute()
71 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
72 std::string aFilePath = aFilePathAttr->value();
73 if (aFilePath.empty()) {
74 setError("File path is empty.");
78 importFile(aFilePath);
81 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
82 std::shared_ptr<GeomAPI_Shape> aGeomShape)
84 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
85 //LoadNamingDS of the imported shape
86 loadNamingDS(aGeomShape, aResultBody);
90 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
93 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
95 if (anExtension == "XAO") {
96 importXAO(theFileName);
100 // Perform the import
102 std::shared_ptr<GeomAPI_Shape> aGeomShape;
103 if (anExtension == "BREP" || anExtension == "BRP") {
104 aGeomShape = BREPImport(theFileName, anExtension, anError);
105 } else if (anExtension == "STEP" || anExtension == "STP") {
106 aGeomShape = STEPImport(theFileName, anExtension, anError);
107 } else if (anExtension == "IGES" || anExtension == "IGS") {
108 aGeomShape = IGESImport(theFileName, anExtension, anError);
110 anError = "Unsupported format: " + anExtension;
113 // Check if shape is valid
114 if (!anError.empty()) {
115 setError("An error occurred while importing " + theFileName + ": " + anError);
119 // Pass the results into the model
120 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
121 data()->setName(anObjectName);
123 setResult(createResultBody(aGeomShape));
126 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
132 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
134 if (!anError.empty()) {
135 setError("An error occurred while importing " + theFileName + ": " + anError);
139 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
141 // use the geometry name or the file name for the feature
142 std::string aBodyName = aXaoGeometry->getName();
143 if (aBodyName.empty())
144 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
145 data()->setName(aBodyName);
147 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
148 setResult(aResultBody);
151 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
152 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
154 // Remove previous groups stored in RefList
155 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
156 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
157 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
158 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
160 document()->removeFeature(aFeature);
164 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
165 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
167 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
170 if (!aXaoGroup->getName().empty())
171 aGroupFeature->data()->setName(aXaoGroup->getName());
174 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
176 // conversion of dimension
177 XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
178 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
179 std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
181 aSelectionList->setSelectionType(aSelectionType);
182 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
183 aSelectionList->append(aResultBody, GeomShapePtr());
184 // complex conversion of element index to reference id
185 int anElementID = aXaoGroup->get(anElementIndex);
186 std::string aReferenceString =
187 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
188 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
190 aSelectionList->value(anElementIndex)->setId(aReferenceID);
193 // Top avoid problems in Object Browser update: issue #1647.
194 ModelAPI_EventCreator::get()->sendReordered(
195 std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
197 } catch (XAO::XAO_Exception& e) {
198 std::string anError = e.what();
199 setError("An error occurred while importing " + theFileName + ": " + anError);
204 //============================================================================
205 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
208 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
210 data()->reflist(FEATURES_ID())->append(aNew);
211 // set as current also after it becomes sub to set correctly enabled for other subs
212 //document()->setCurrentFeature(aNew, false);
216 void ExchangePlugin_ImportFeature::removeFeature(
217 std::shared_ptr<ModelAPI_Feature> theFeature)
219 if (!data()->isValid())
221 AttributeRefListPtr aList = reflist(FEATURES_ID());
222 aList->remove(theFeature);
225 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
227 return data()->reflist(FEATURES_ID())->size(true);
230 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
231 const int theIndex, bool forTree)
233 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
234 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
238 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
240 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
241 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
242 std::list<ObjectPtr> aFeatures = aRefList->list();
243 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
244 int aResultIndex = 1; // number of the counted (created) features, started from 1
245 int aFeatureIndex = -1; // number of the not-empty features in the list
246 for (; anIt != aFeatures.end(); anIt++) {
249 if (aFeatureIndex == theIndex)
256 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
258 // check is this feature of result
259 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
261 return data()->reflist(FEATURES_ID())->isInList(aFeature);
265 //============================================================================
266 void ExchangePlugin_ImportFeature::loadNamingDS(
267 std::shared_ptr<GeomAPI_Shape> theGeomShape,
268 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
271 theResultBody->store(theGeomShape);
274 std::string aNameMS = "Shape";
275 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);