]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
4060c3e0495948dfdeb3be2f51009fa93c1963e6
[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_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>
35
36 #include <XAO_Xao.hxx>
37 #include <XAO_Group.hxx>
38
39 #include <ExchangePlugin_Tools.h>
40
41 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
42 {
43 }
44
45 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
46 {
47   // TODO Auto-generated destructor stub
48 }
49
50 /*
51  * Request for initialization of data model of the feature: adding all attributes
52  */
53 void ExchangePlugin_ImportFeature::initAttributes()
54 {
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);
58
59   ModelAPI_Session::get()->validators()->registerNotObligatory(
60       getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
61 }
62
63 /*
64  * Computes or recomputes the results
65  */
66 void ExchangePlugin_ImportFeature::execute()
67 {
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.");
72     return;
73   }
74
75   importFile(aFilePath);
76 }
77
78 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
79     std::shared_ptr<GeomAPI_Shape> aGeomShape)
80 {
81   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
82   //LoadNamingDS of the imported shape
83   loadNamingDS(aGeomShape, aResultBody);
84   return aResultBody;
85 }
86
87 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
88 {
89   // "*.brep" -> "BREP"
90   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
91
92   if (anExtension == "XAO") {
93     importXAO(theFileName);
94     return;
95   }
96
97   // Perform the import
98   std::string anError;
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);
106   } else {
107     anError = "Unsupported format: " + anExtension;
108   }
109
110   // Check if shape is valid
111   if (!anError.empty()) {
112     setError("An error occurred while importing " + theFileName + ": " + anError);
113     return;
114   }
115
116   // Pass the results into the model
117   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
118   data()->setName(anObjectName);
119
120   setResult(createResultBody(aGeomShape));
121 }
122
123 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
124 {
125   try {
126   std::string anError;
127
128   XAO::Xao aXao;
129   std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
130
131   if (!anError.empty()) {
132     setError("An error occurred while importing " + theFileName + ": " + anError);
133     return;
134   }
135
136   XAO::Geometry* aXaoGeometry = aXao.getGeometry();
137
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);
143
144   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
145   setResult(aResultBody);
146
147   // Process groups
148   std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
149       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
150
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);
156     if (aFeature)
157       document()->removeFeature(aFeature);
158   }
159
160   // Create new groups
161   for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
162     XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
163
164     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
165
166     // group name
167     if (!aXaoGroup->getName().empty())
168       aGroupFeature->data()->setName(aXaoGroup->getName());
169
170     // fill selection
171     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
172
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);
177
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);
186
187       aSelectionList->value(anElementIndex)->setId(aReferenceID);
188     }
189   }
190
191   } catch (XAO::XAO_Exception& e) {
192     std::string anError = e.what();
193     setError("An error occurred while importing " + theFileName + ": " + anError);
194     return;
195   }
196 }
197
198 //============================================================================
199 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
200     std::string theID)
201 {
202   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
203   if (aNew)
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);
207   return aNew;
208 }
209
210 void ExchangePlugin_ImportFeature::removeFeature(
211     std::shared_ptr<ModelAPI_Feature> theFeature)
212 {
213   if (!data()->isValid())
214     return;
215   AttributeRefListPtr aList = reflist(FEATURES_ID());
216   // if the object is last, remove it from the list (needed to skip empty transaction on edit of sketch feature)
217   if (aList->object(aList->size(true) - 1, true) == theFeature) {
218     aList->remove(theFeature);
219   } else {
220     // to keep the persistent sub-elements indexing, do not remove elements from list,
221     // but substitute by nulls
222     aList->substitute(theFeature, ObjectPtr());
223   }
224 }
225
226 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
227 {
228   return data()->reflist(FEATURES_ID())->size(false);
229 }
230
231 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
232     const int theIndex, bool forTree)
233 {
234   ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
235   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
236   return aRes;
237 }
238
239 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
240 {
241   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
242       ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
243   std::list<ObjectPtr> aFeatures = aRefList->list();
244   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
245   int aResultIndex = 1; // number of the counted (created) features, started from 1
246   int aFeatureIndex = -1; // number of the not-empty features in the list
247   for (; anIt != aFeatures.end(); anIt++) {
248     if (anIt->get())
249       aFeatureIndex++;
250     if (aFeatureIndex == theIndex)
251       break;
252     aResultIndex++;
253   }
254   return aResultIndex;
255 }
256
257 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
258 {
259   // check is this feature of result
260   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
261   if (aFeature)
262     return data()->reflist(FEATURES_ID())->isInList(aFeature);
263   return false;
264 }
265
266 //============================================================================
267 void ExchangePlugin_ImportFeature::loadNamingDS(
268     std::shared_ptr<GeomAPI_Shape> theGeomShape,
269     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
270 {
271   //load result
272   theResultBody->store(theGeomShape);
273
274   int aTag(1);
275   std::string aNameMS = "Shape";
276   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
277 }