Salome HOME
Copyright update 2021
[modules/shaper_study.git] / src / StudyData / StudyData_XAO.cpp
1 // Copyright (C) 2019-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "StudyData_XAO.h"
21
22 #include <XAO_Group.hxx>
23 #include <XAO_Field.hxx>
24 #include <XAO_Geometry.hxx>
25 #include <XAO_XaoExporter.hxx>
26 #include <XAO_BrepGeometry.hxx>
27
28
29 StudyData_XAO::StudyData_XAO() : myExport(NULL), myImport(NULL), myCurrentElementID(0)
30 {}
31
32 void StudyData_XAO::SetShape(const long long theShapePtr)
33 {
34   myShape = (TopoDS_Shape*)(theShapePtr);
35   if (!myExport)
36     myExport = new XAO::Xao();
37   XAO::BrepGeometry* aGeometry = new XAO::BrepGeometry;
38   myExport->setGeometry(aGeometry);
39   aGeometry->setTopoDS_Shape(*myShape);
40 }
41
42 static XAO::Dimension GetDimension(const int theSelType) {
43   switch(theSelType) {
44   case 7: return XAO::VERTEX;
45   case 6: return XAO::EDGE;
46   case 4: return XAO::FACE;
47   case 2: return XAO::SOLID;
48   default: return XAO::WHOLE;
49   };
50   return XAO::WHOLE;
51 }
52
53 int StudyData_XAO::AddGroup(const int theSelType, const std::string theGroupName)
54 {
55   XAO::Group* aNewGroup = myExport->addGroup(GetDimension(theSelType), theGroupName);
56   int anID = (int)myGroups.size();
57   myGroups[anID] = aNewGroup;
58   return anID;
59 }
60
61 void StudyData_XAO::AddGroupSelection(const int theGroupID, const int theSelection)
62 {
63   XAO::Group* aGroup = myGroups[theGroupID];
64   aGroup->add(theSelection);
65 }
66
67 void StudyData_XAO::Export(const std::string theFileName)
68 {
69   myExport->setAuthor("ShaperStudy");
70
71   XAO::XaoExporter::saveToFile(myExport, theFileName, "");
72 }
73
74 int StudyData_XAO::AddField(const int theValType, const int theSelType,
75   const int theCompsNum, const std::string theFieldName)
76 {
77   XAO::Field* aNewField = myExport->addField(
78     XAO::Type(theValType), GetDimension(theSelType), theCompsNum, theFieldName);
79   int anID = (int)myFields.size();
80   myFields[anID] = aNewField;
81   return anID;
82 }
83
84 void StudyData_XAO::SetFieldComponent(const int theFieldID, const int theCompIndex,
85   const std::string theCompName)
86 {
87   myFields[theFieldID]->setComponentName(theCompIndex, theCompName);
88 }
89
90 void StudyData_XAO::AddStep(const int theFieldID, const int theStepID, const int theStampID)
91 {
92   XAO::Step* aNewStep = myFields[theFieldID]->addNewStep(theStepID);
93   aNewStep->setStamp(theStampID);
94   if (mySteps.find(theFieldID) == mySteps.end())
95     mySteps[theFieldID] = std::map<int, XAO::Step*>();
96   mySteps[theFieldID][theStepID] = aNewStep;
97   myCurrentElementID = 0;
98 }
99
100 void StudyData_XAO::AddStepValue(
101   const int theFieldID, const int theStepID, std::string theValue)
102 {
103   int aColumns = myFields[theFieldID]->countComponents();
104   mySteps[theFieldID][theStepID]->setStringValue(
105     myCurrentElementID / aColumns, myCurrentElementID % aColumns, theValue);
106   myCurrentElementID++;
107 }
108
109 std::string StudyData_XAO::Import(const std::string theFileName)
110 {
111   std::string anError;
112   myImport = new XAO::Xao();
113   try {
114     if (XAO::XaoExporter::readFromFile(theFileName, myImport)) {
115       XAO::Geometry* aGeometry = myImport->getGeometry();
116       XAO::Format aFormat = aGeometry->getFormat();
117       if (aFormat == XAO::BREP) {
118         if (XAO::BrepGeometry* aBrepGeometry = dynamic_cast<XAO::BrepGeometry*>(aGeometry))
119           myShape = new TopoDS_Shape(aBrepGeometry->getTopoDS_Shape());
120       } else {
121         anError = "Unsupported XAO geometry format:" + XAO::XaoUtils::shapeFormatToString(aFormat);
122       }
123     } else {
124       anError = "XAO object was not read successful";
125     }
126   } catch (XAO::XAO_Exception& e) {
127     anError = e.what();
128   }
129
130   return anError;
131 }
132
133 long long StudyData_XAO::GetShape()
134 {
135   return (long long)(myShape);
136 }
137
138 static int GetSelectionTypeInt(const XAO::Dimension theDimension) {
139   switch(theDimension) {
140   case XAO::VERTEX: return 7;
141   case XAO::EDGE: return 6;
142   case XAO::FACE: return 4;
143   case XAO::SOLID: return 2;
144   default: return -1;
145   }
146   return -1;
147 }
148
149 int StudyData_XAO::GetGroupDimension(const int theGroupID)
150 {
151   XAO::Group* aXaoGroup = myImport->getGroup(theGroupID);
152   return GetSelectionTypeInt(aXaoGroup->getDimension());
153 }
154
155 std::list<long> StudyData_XAO::GetGroupSelection(const int theGroupID)
156 {
157   XAO::Group* aXaoGroup = myImport->getGroup(theGroupID);
158   std::list<long> aResult;
159   for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
160     aResult.push_back(aXaoGroup->get(anElementIndex));
161   }
162   return aResult;
163 }
164
165 int StudyData_XAO::GetValuesType(const int theFieldID)
166 {
167   XAO::Field* aField = myImport->getField(theFieldID);
168   return (int)(aField->getType());
169 }
170
171 int StudyData_XAO::GetSelectionType(const int theFieldID)
172 {
173   XAO::Field* aField = myImport->getField(theFieldID);
174   return GetSelectionTypeInt(aField->getDimension());
175 }
176
177 std::list<std::string> StudyData_XAO::GetComponents(const int theFieldID)
178 {
179   XAO::Field* aField = myImport->getField(theFieldID);
180   std::list<std::string> aResult;
181   int aNum = aField->countComponents();
182   for(int a = 0; a < aNum; a++) {
183     aResult.push_back(aField->getComponentName(a));
184   }
185   return aResult;
186 }
187
188 void StudyData_XAO::BeginSteps(const int theFieldID)
189 {
190   myStepIterator = myImport->getField(theFieldID)->begin();
191 }
192
193 bool StudyData_XAO::More(const int theFieldID)
194 {
195   return myStepIterator != myImport->getField(theFieldID)->end();
196 }
197
198 void StudyData_XAO::Next()
199 {
200   myStepIterator++;
201 }
202
203 int StudyData_XAO::GetStamp()
204 {
205   return (*myStepIterator)->getStamp();
206 }
207
208 int StudyData_XAO::GetStepIndex()
209 {
210   return (*myStepIterator)->getStep();
211 }
212
213 std::list<std::string> StudyData_XAO::GetValues()
214 {
215   std::list<std::string> aResult;
216   int aComps = (*myStepIterator)->countComponents();
217   int aVals = (*myStepIterator)->countValues();
218   for(int a = 0; a < aVals; a++) {
219     aResult.push_back((*myStepIterator)->getStringValue(a / aComps, a % aComps));
220   }
221
222   return aResult;
223 }