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_AttributeStringArray.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_AttributeTables.h>
29 #include <ModelAPI_BodyBuilder.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_Document.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_Object.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_ResultGroup.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
39 #include <XAO_Xao.hxx>
40 #include <XAO_Group.hxx>
41 #include <XAO_Field.hxx>
42 #include <XAO_Step.hxx>
44 #include <ExchangePlugin_Tools.h>
46 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
50 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
52 // TODO Auto-generated destructor stub
56 * Request for initialization of data model of the feature: adding all attributes
58 void ExchangePlugin_ImportFeature::initAttributes()
60 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(),
61 ModelAPI_AttributeString::typeId());
62 AttributePtr aFeaturesAttribute =
63 data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(),
64 ModelAPI_AttributeRefList::typeId());
65 aFeaturesAttribute->setIsArgument(false);
67 ModelAPI_Session::get()->validators()->registerNotObligatory(
68 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
72 * Computes or recomputes the results
74 void ExchangePlugin_ImportFeature::execute()
76 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
77 std::string aFilePath = aFilePathAttr->value();
78 if (aFilePath.empty()) {
79 setError("File path is empty.");
83 importFile(aFilePath);
86 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
87 std::shared_ptr<GeomAPI_Shape> aGeomShape)
89 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
90 //LoadNamingDS of the imported shape
91 loadNamingDS(aGeomShape, aResultBody);
95 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
98 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
100 if (anExtension == "XAO") {
101 importXAO(theFileName);
105 // Perform the import
107 std::shared_ptr<GeomAPI_Shape> aGeomShape;
108 if (anExtension == "BREP" || anExtension == "BRP") {
109 aGeomShape = BREPImport(theFileName, anExtension, anError);
110 } else if (anExtension == "STEP" || anExtension == "STP") {
111 aGeomShape = STEPImport(theFileName, anExtension, anError);
112 } else if (anExtension == "IGES" || anExtension == "IGS") {
113 aGeomShape = IGESImport(theFileName, anExtension, anError);
115 anError = "Unsupported format: " + anExtension;
118 // Check if shape is valid
119 if (!anError.empty()) {
120 setError("An error occurred while importing " + theFileName + ": " + anError);
124 // Pass the results into the model
125 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
126 data()->setName(anObjectName);
128 setResult(createResultBody(aGeomShape));
131 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
137 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
139 if (!anError.empty()) {
140 setError("An error occurred while importing " + theFileName + ": " + anError);
144 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
146 // use the geometry name or the file name for the feature
147 std::string aBodyName = aXaoGeometry->getName();
148 if (aBodyName.empty())
149 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
150 data()->setName(aBodyName);
152 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
153 setResult(aResultBody);
155 // Process groups/fields
156 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
157 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
159 // Remove previous groups/fields stored in RefList
160 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
161 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
162 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
163 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
165 document()->removeFeature(aFeature);
169 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
170 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
172 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
175 if (!aXaoGroup->getName().empty())
176 aGroupFeature->data()->setName(aXaoGroup->getName());
179 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
181 // conversion of dimension
182 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
183 std::string aSelectionType =
184 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
186 aSelectionList->setSelectionType(aSelectionType);
187 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
188 aSelectionList->append(aResultBody, GeomShapePtr());
189 // complex conversion of element index to reference id
190 int anElementID = aXaoGroup->get(anElementIndex);
191 std::string aReferenceString =
192 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
193 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
195 aSelectionList->value(anElementIndex)->setId(aReferenceID);
199 for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
200 XAO::Field* aXaoField = aXao.getField(aFieldIndex);
202 std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
205 if (!aXaoField->getName().empty())
206 aFieldFeature->data()->setName(aXaoField->getName());
209 AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
211 // conversion of dimension
212 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
213 std::string aSelectionType =
214 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
215 aSelectionList->setSelectionType(aSelectionType);
216 // conversion of type
217 XAO::Type aFieldType = aXaoField->getType();
218 std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
219 ModelAPI_AttributeTables::ValueType aType =
220 ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
221 // set components names
222 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
223 aComponents->setSize(aXaoField->countComponents());
224 for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
225 aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
228 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
229 aStamps->setSize(aXaoField->countSteps());
230 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
232 aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
233 aTables->setType(aType);
235 XAO::stepIterator aStepIter = aXaoField->begin();
236 for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
237 aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
238 for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
239 for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
240 ModelAPI_AttributeTables::Value aVal;
241 std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
243 case ModelAPI_AttributeTables::BOOLEAN:
244 aVal.myBool = aValStr == "True";
246 case ModelAPI_AttributeTables::INTEGER:
247 aVal.myInt = atoi(aValStr.c_str());
249 case ModelAPI_AttributeTables::DOUBLE:
250 aVal.myDouble = atof(aValStr.c_str());
252 case ModelAPI_AttributeTables::STRING:
253 aVal.myStr = aValStr;
256 aTables->setValue(aVal, aRow, aCol, aStepIndex);
261 // Top avoid problems in Object Browser update: issue #1647.
262 ModelAPI_EventCreator::get()->sendReordered(
263 std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
265 } catch (XAO::XAO_Exception& e) {
266 std::string anError = e.what();
267 setError("An error occurred while importing " + theFileName + ": " + anError);
272 //============================================================================
273 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
276 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
278 data()->reflist(FEATURES_ID())->append(aNew);
279 // set as current also after it becomes sub to set correctly enabled for other subs
280 //document()->setCurrentFeature(aNew, false);
284 void ExchangePlugin_ImportFeature::removeFeature(
285 std::shared_ptr<ModelAPI_Feature> theFeature)
287 if (!data()->isValid())
289 AttributeRefListPtr aList = reflist(FEATURES_ID());
290 aList->remove(theFeature);
293 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
295 return data()->reflist(FEATURES_ID())->size(true);
298 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
299 const int theIndex, bool forTree)
301 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
302 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
306 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
308 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
309 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
310 std::list<ObjectPtr> aFeatures = aRefList->list();
311 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
312 int aResultIndex = 1; // number of the counted (created) features, started from 1
313 int aFeatureIndex = -1; // number of the not-empty features in the list
314 for (; anIt != aFeatures.end(); anIt++) {
317 if (aFeatureIndex == theIndex)
324 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
326 // check is this feature of result
327 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
329 return data()->reflist(FEATURES_ID())->isInList(aFeature);
333 //============================================================================
334 void ExchangePlugin_ImportFeature::loadNamingDS(
335 std::shared_ptr<GeomAPI_Shape> theGeomShape,
336 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
339 theResultBody->store(theGeomShape);
342 std::string aNameMS = "Shape";
343 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);