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