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 AttributePtr aFeaturesAttribute = data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
57 aFeaturesAttribute->setIsArgument(false);
59 ModelAPI_Session::get()->validators()->registerNotObligatory(
60 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
64 * Computes or recomputes the results
66 void ExchangePlugin_ImportFeature::execute()
68 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
69 std::string aFilePath = aFilePathAttr->value();
70 if (aFilePath.empty()) {
71 setError("File path is empty.");
75 importFile(aFilePath);
78 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
79 std::shared_ptr<GeomAPI_Shape> aGeomShape)
81 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
82 //LoadNamingDS of the imported shape
83 loadNamingDS(aGeomShape, aResultBody);
87 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
90 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
92 if (anExtension == "XAO") {
93 importXAO(theFileName);
99 std::shared_ptr<GeomAPI_Shape> aGeomShape;
100 if (anExtension == "BREP" || anExtension == "BRP") {
101 aGeomShape = BREPImport(theFileName, anExtension, anError);
102 } else if (anExtension == "STEP" || anExtension == "STP") {
103 aGeomShape = STEPImport(theFileName, anExtension, anError);
104 } else if (anExtension == "IGES" || anExtension == "IGS") {
105 aGeomShape = IGESImport(theFileName, anExtension, anError);
107 anError = "Unsupported format: " + anExtension;
110 // Check if shape is valid
111 if (!anError.empty()) {
112 setError("An error occurred while importing " + theFileName + ": " + anError);
116 // Pass the results into the model
117 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
118 data()->setName(anObjectName);
120 setResult(createResultBody(aGeomShape));
123 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
129 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
131 if (!anError.empty()) {
132 setError("An error occurred while importing " + theFileName + ": " + anError);
136 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
138 // use the geometry name or the file name for the feature
139 std::string aBodyName = aXaoGeometry->getName();
140 if (aBodyName.empty())
141 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
142 data()->setName(aBodyName);
144 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
145 setResult(aResultBody);
148 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
149 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
151 // Remove previous groups stored in RefList
152 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
153 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
154 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
155 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
157 document()->removeFeature(aFeature);
161 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
162 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
164 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
167 if (!aXaoGroup->getName().empty())
168 aGroupFeature->data()->setName(aXaoGroup->getName());
171 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
173 // conversion of dimension
174 XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
175 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
176 std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
178 aSelectionList->setSelectionType(aSelectionType);
179 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
180 aSelectionList->append(aResultBody, GeomShapePtr());
181 // complex conversion of element index to reference id
182 int anElementID = aXaoGroup->get(anElementIndex);
183 std::string aReferenceString =
184 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
185 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
187 aSelectionList->value(anElementIndex)->setId(aReferenceID);
191 } catch (XAO::XAO_Exception& e) {
192 std::string anError = e.what();
193 setError("An error occurred while importing " + theFileName + ": " + anError);
198 //============================================================================
199 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
202 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
204 data()->reflist(FEATURES_ID())->append(aNew);
205 // set as current also after it becomes sub to set correctly enabled for other subs
206 //document()->setCurrentFeature(aNew, false);
210 void ExchangePlugin_ImportFeature::removeFeature(
211 std::shared_ptr<ModelAPI_Feature> theFeature)
213 if (!data()->isValid())
215 AttributeRefListPtr aList = reflist(FEATURES_ID());
216 aList->remove(theFeature);
219 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
221 return data()->reflist(FEATURES_ID())->size(true);
224 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
225 const int theIndex, bool forTree)
227 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
228 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
232 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
234 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
235 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
236 std::list<ObjectPtr> aFeatures = aRefList->list();
237 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
238 int aResultIndex = 1; // number of the counted (created) features, started from 1
239 int aFeatureIndex = -1; // number of the not-empty features in the list
240 for (; anIt != aFeatures.end(); anIt++) {
243 if (aFeatureIndex == theIndex)
250 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
252 // check is this feature of result
253 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
255 return data()->reflist(FEATURES_ID())->isInList(aFeature);
259 //============================================================================
260 void ExchangePlugin_ImportFeature::loadNamingDS(
261 std::shared_ptr<GeomAPI_Shape> theGeomShape,
262 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
265 theResultBody->store(theGeomShape);
268 std::string aNameMS = "Shape";
269 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);