]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
Issue #1865 : implementation of import/export fields in XAO format
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    ExchangePlugin_ImportFeature.cpp
4 // Created: Aug 28, 2014
5 // Authors:  Sergey BELASH, Sergey POKHODENKO
6
7 #include <ExchangePlugin_ImportFeature.h>
8
9 #include <algorithm>
10 #include <string>
11
12 #include <Config_Common.h>
13 #include <Config_PropManager.h>
14
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>
20
21 #include <GeomAPI_Shape.h>
22
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>
38
39 #include <XAO_Xao.hxx>
40 #include <XAO_Group.hxx>
41 #include <XAO_Field.hxx>
42 #include <XAO_Step.hxx>
43
44 #include <ExchangePlugin_Tools.h>
45
46 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
47 {
48 }
49
50 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
51 {
52   // TODO Auto-generated destructor stub
53 }
54
55 /*
56  * Request for initialization of data model of the feature: adding all attributes
57  */
58 void ExchangePlugin_ImportFeature::initAttributes()
59 {
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);
66
67   ModelAPI_Session::get()->validators()->registerNotObligatory(
68       getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
69 }
70
71 /*
72  * Computes or recomputes the results
73  */
74 void ExchangePlugin_ImportFeature::execute()
75 {
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.");
80     return;
81   }
82
83   importFile(aFilePath);
84 }
85
86 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
87     std::shared_ptr<GeomAPI_Shape> aGeomShape)
88 {
89   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
90   //LoadNamingDS of the imported shape
91   loadNamingDS(aGeomShape, aResultBody);
92   return aResultBody;
93 }
94
95 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
96 {
97   // "*.brep" -> "BREP"
98   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
99
100   if (anExtension == "XAO") {
101     importXAO(theFileName);
102     return;
103   }
104
105   // Perform the import
106   std::string anError;
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);
114   } else {
115     anError = "Unsupported format: " + anExtension;
116   }
117
118   // Check if shape is valid
119   if (!anError.empty()) {
120     setError("An error occurred while importing " + theFileName + ": " + anError);
121     return;
122   }
123
124   // Pass the results into the model
125   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
126   data()->setName(anObjectName);
127
128   setResult(createResultBody(aGeomShape));
129 }
130
131 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
132 {
133   try {
134   std::string anError;
135
136   XAO::Xao aXao;
137   std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
138
139   if (!anError.empty()) {
140     setError("An error occurred while importing " + theFileName + ": " + anError);
141     return;
142   }
143
144   XAO::Geometry* aXaoGeometry = aXao.getGeometry();
145
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);
151
152   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
153   setResult(aResultBody);
154
155   // Process groups/fields
156   std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
157       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
158
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);
164     if (aFeature)
165       document()->removeFeature(aFeature);
166   }
167
168   // Create new groups
169   for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
170     XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
171
172     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
173
174     // group name
175     if (!aXaoGroup->getName().empty())
176       aGroupFeature->data()->setName(aXaoGroup->getName());
177
178     // fill selection
179     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
180
181     // conversion of dimension
182     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
183     std::string aSelectionType =
184       ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
185
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);
194
195       aSelectionList->value(anElementIndex)->setId(aReferenceID);
196     }
197   }
198   // Create new fields
199   for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
200     XAO::Field* aXaoField = aXao.getField(aFieldIndex);
201
202     std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
203
204     // group name
205     if (!aXaoField->getName().empty())
206       aFieldFeature->data()->setName(aXaoField->getName());
207
208     // fill selection
209     AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
210
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));
226     }
227
228     AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
229     aStamps->setSize(aXaoField->countSteps());
230     std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
231     aTables->setSize(
232       aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
233     aTables->setType(aType);
234     // iterate steps
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);
242           switch(aType) {
243           case ModelAPI_AttributeTables::BOOLEAN:
244             aVal.myBool = aValStr == "True";
245             break;
246           case ModelAPI_AttributeTables::INTEGER:
247             aVal.myInt = atoi(aValStr.c_str());
248             break;
249           case ModelAPI_AttributeTables::DOUBLE:
250             aVal.myDouble = atof(aValStr.c_str());
251             break;
252           case ModelAPI_AttributeTables::STRING:
253             aVal.myStr = aValStr;
254             break;
255           }
256           aTables->setValue(aVal, aRow, aCol, aStepIndex);
257         }
258       }
259     }
260   }
261   // Top avoid problems in Object Browser update: issue #1647.
262   ModelAPI_EventCreator::get()->sendReordered(
263     std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
264
265   } catch (XAO::XAO_Exception& e) {
266     std::string anError = e.what();
267     setError("An error occurred while importing " + theFileName + ": " + anError);
268     return;
269   }
270 }
271
272 //============================================================================
273 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
274     std::string theID)
275 {
276   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
277   if (aNew)
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);
281   return aNew;
282 }
283
284 void ExchangePlugin_ImportFeature::removeFeature(
285     std::shared_ptr<ModelAPI_Feature> theFeature)
286 {
287   if (!data()->isValid())
288     return;
289   AttributeRefListPtr aList = reflist(FEATURES_ID());
290   aList->remove(theFeature);
291 }
292
293 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
294 {
295   return data()->reflist(FEATURES_ID())->size(true);
296 }
297
298 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
299     const int theIndex, bool forTree)
300 {
301   ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
302   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
303   return aRes;
304 }
305
306 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
307 {
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++) {
315     if (anIt->get())
316       aFeatureIndex++;
317     if (aFeatureIndex == theIndex)
318       break;
319     aResultIndex++;
320   }
321   return aResultIndex;
322 }
323
324 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
325 {
326   // check is this feature of result
327   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
328   if (aFeature)
329     return data()->reflist(FEATURES_ID())->isInList(aFeature);
330   return false;
331 }
332
333 //============================================================================
334 void ExchangePlugin_ImportFeature::loadNamingDS(
335     std::shared_ptr<GeomAPI_Shape> theGeomShape,
336     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
337 {
338   //load result
339   theResultBody->store(theGeomShape);
340
341   int aTag(1);
342   std::string aNameMS = "Shape";
343   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
344 }