]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
Make export XAO with groups
[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 // Author:  Sergey BELASH
6
7 #include <ExchangePlugin_ImportFeature.h>
8
9 #include <algorithm>
10 #include <string>
11 #ifdef _DEBUG
12 #include <iostream>
13 #include <ostream>
14 #endif
15
16 #include <Config_Common.h>
17 #include <Config_PropManager.h>
18
19 #include <GeomAlgoAPI_BREPImport.h>
20 #include <GeomAlgoAPI_IGESImport.h>
21 #include <GeomAlgoAPI_STEPImport.h>
22 #include <GeomAlgoAPI_Tools.h>
23 #include <GeomAlgoAPI_XAOImport.h>
24
25 #include <GeomAPI_Shape.h>
26
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_BodyBuilder.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Document.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Object.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_ResultGroup.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Validator.h>
39
40 #include <XAO_Xao.hxx>
41 #include <XAO_Group.hxx>
42
43 #include <ExchangePlugin_Tools.h>
44
45 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
46 {
47 }
48
49 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
50 {
51   // TODO Auto-generated destructor stub
52 }
53
54 /*
55  * Request for initialization of data model of the feature: adding all attributes
56  */
57 void ExchangePlugin_ImportFeature::initAttributes()
58 {
59   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
60   data()->addAttribute(ExchangePlugin_ImportFeature::GROUP_LIST_ID(), ModelAPI_AttributeRefList::typeId());
61
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ImportFeature::GROUP_LIST_ID());
63 }
64
65 /*
66  * Computes or recomputes the results
67  */
68 void ExchangePlugin_ImportFeature::execute()
69 {
70   AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
71   std::string aFilePath = aFilePathAttr->value();
72   if (aFilePath.empty()) {
73     setError("File path is empty.");
74     return;
75   }
76
77   importFile(aFilePath);
78 }
79
80 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
81     std::shared_ptr<GeomAPI_Shape> aGeomShape)
82 {
83   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
84   //LoadNamingDS of the imported shape
85   loadNamingDS(aGeomShape, aResultBody);
86   return aResultBody;
87 }
88
89 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
90 {
91   // "*.brep" -> "BREP"
92   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
93
94   if (anExtension == "XAO") {
95     importXAO(theFileName);
96     return;
97   }
98
99   // Perform the import
100   std::string anError;
101   std::shared_ptr<GeomAPI_Shape> aGeomShape;
102   if (anExtension == "BREP" || anExtension == "BRP") {
103     aGeomShape = BREPImport(theFileName, anExtension, anError);
104   } else if (anExtension == "STEP" || anExtension == "STP") {
105     aGeomShape = STEPImport(theFileName, anExtension, anError);
106   } else if (anExtension == "IGES" || anExtension == "IGS") {
107     aGeomShape = IGESImport(theFileName, anExtension, anError);
108   } else {
109     anError = "Unsupported format: " + anExtension;
110   }
111
112   // Check if shape is valid
113   if (!anError.empty()) {
114     setError("An error occurred while importing " + theFileName + ": " + anError);
115     return;
116   }
117
118   // Pass the results into the model
119   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
120   data()->setName(anObjectName);
121
122   setResult(createResultBody(aGeomShape));
123 }
124
125 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
126 {
127   try {
128   std::string anError;
129
130   XAO::Xao aXao;
131   std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
132
133   if (!anError.empty()) {
134     setError("An error occurred while importing " + theFileName + ": " + anError);
135     return;
136   }
137
138   XAO::Geometry* aXaoGeometry = aXao.getGeometry();
139
140   // use the geometry name or the file name for the feature
141   std::string aBodyName = aXaoGeometry->getName().empty()
142       ? GeomAlgoAPI_Tools::File_Tools::name(theFileName)
143       : aXaoGeometry->getName();
144   data()->setName(aBodyName);
145
146   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
147   setResult(aResultBody);
148
149   // Process groups
150   AttributeRefListPtr aRefListOfGroups = reflist(ExchangePlugin_ImportFeature::GROUP_LIST_ID());
151
152   // Remove previous groups stored in RefList
153   std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
154   std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
155   for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
156     std::shared_ptr<ModelAPI_Feature> aFeature =
157         std::dynamic_pointer_cast<ModelAPI_Feature>(*anGroupIt);
158     if (aFeature)
159       document()->removeFeature(aFeature);
160   }
161
162   // Create new groups
163   for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
164     XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
165
166     std::shared_ptr<ModelAPI_Feature> aGroupFeature = document()->addFeature("Group", false);
167
168     // group name
169     if (!aXaoGroup->getName().empty())
170       aGroupFeature->data()->setName(aXaoGroup->getName());
171
172     // fill selection
173     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
174
175     // conversion of dimension
176     XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
177     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
178     std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
179
180     aSelectionList->setSelectionType(aSelectionType);
181     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
182       aSelectionList->append(aResultBody, GeomShapePtr());
183       // complex conversion of element index to reference id
184       int anElementID = aXaoGroup->get(anElementIndex);
185       std::string aReferenceString =
186           aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
187       int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
188
189       aSelectionList->value(anElementIndex)->setId(aReferenceID);
190     }
191
192     aRefListOfGroups->append(aGroupFeature);
193
194     document()->setCurrentFeature(aGroupFeature, true);
195   }
196
197   } catch (XAO::XAO_Exception& e) {
198     std::string anError = e.what();
199     setError("An error occurred while importing " + theFileName + ": " + anError);
200     return;
201   }
202 }
203
204 //============================================================================
205 void ExchangePlugin_ImportFeature::loadNamingDS(
206     std::shared_ptr<GeomAPI_Shape> theGeomShape,
207     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
208 {
209   //load result
210   theResultBody->store(theGeomShape);
211
212   int aTag(1);
213   std::string aNameMS = "Shape";
214   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
215 }