]> SALOME platform Git repositories - modules/shaper.git/blob - src/Selector/Selector_Modify.cpp
Salome HOME
Implementation and part of tests of High Level Objects History task for Common, Cut...
[modules/shaper.git] / src / Selector / Selector_Modify.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Selector_Modify.h>
22
23 #include <Selector_NameGenerator.h>
24 #include <Selector_NExplode.h>
25
26 #include <TNaming_NamedShape.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TNaming_SameShapeIterator.hxx>
29 #include <TNaming_NewShapeIterator.hxx>
30 #include <TNaming_Tool.hxx>
31 #include <TDataStd_Name.hxx>
32 #include <TDataStd_Integer.hxx>
33 #include <TDF_ChildIterator.hxx>
34 #include <TopTools_MapOfShape.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <BRep_Tool.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Builder.hxx>
39 #include <TopoDS_Compound.hxx>
40
41 Selector_Modify::Selector_Modify() : Selector_Algo()
42 {
43   myWeakIndex = -1; // no index by default
44 }
45
46 // adds to theResult all labels that contain initial shapes for theValue located in theFinal
47 static void findBases(TDF_Label theAccess, Handle(TNaming_NamedShape) theFinal,
48   const TopoDS_Shape& theValue,
49   bool aMustBeAtFinal, const TDF_Label& theAdditionalDocument, TDF_LabelList& theResult)
50 {
51   bool aFoundAnyShape = false;
52   TNaming_SameShapeIterator aLabIter(theValue, theAccess);
53   for(; aLabIter.More(); aLabIter.Next()) {
54     Handle(TNaming_NamedShape) aNS;
55     if (aLabIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
56       if (aMustBeAtFinal && aNS != theFinal)
57         continue; // looking for old at the same final label only
58       TNaming_Evolution anEvolution = aNS->Evolution();
59       if (anEvolution == TNaming_PRIMITIVE) {
60         // check that this is not in the results already
61         const TDF_Label aResult = aNS->Label();
62         TDF_LabelList::Iterator aResIter(theResult);
63         for(; aResIter.More(); aResIter.Next()) {
64           if (aResIter.Value().IsEqual(aResult))
65             break;
66         }
67         if (!aResIter.More()) // not found, so add this new
68           theResult.Append(aResult);
69         aFoundAnyShape = true;
70       }
71       if (anEvolution == TNaming_GENERATED || anEvolution == TNaming_MODIFY) {
72         for(TNaming_Iterator aThisIter(aNS); aThisIter.More(); aThisIter.Next()) {
73           if (aThisIter.NewShape().IsSame(theValue)) {
74             // continue recursively, null NS means that any NS are ok
75             findBases(theAccess, theFinal, aThisIter.OldShape(),
76               false, theAdditionalDocument, theResult);
77             aFoundAnyShape = true;
78           }
79         }
80       }
81     }
82   }
83   if (!aFoundAnyShape && !theAdditionalDocument.IsNull()) { // try to find in additional document
84     static TDF_Label anEmpty;
85     if (TNaming_Tool::HasLabel(theAdditionalDocument, theValue))
86       findBases(theAdditionalDocument, Handle(TNaming_NamedShape)(), theValue,
87         false, anEmpty, theResult);
88   }
89 }
90
91 /// Returns in theResults all shapes with history started in theBase and ended in theFinal
92 static void findFinals(const TDF_Label& anAccess, const TopoDS_Shape& theBase,
93   const TDF_Label& theFinal,
94   const TDF_Label& theAdditionalDoc, TopTools_MapOfShape& thePass, TopTools_MapOfShape& theResults)
95 {
96   if (TNaming_Tool::HasLabel(anAccess, theBase)) {
97     for(TNaming_NewShapeIterator aBaseIter(theBase, anAccess); aBaseIter.More(); aBaseIter.Next())
98     {
99       TNaming_Evolution anEvolution = aBaseIter.NamedShape()->Evolution();
100       if (anEvolution == TNaming_GENERATED || anEvolution == TNaming_MODIFY) {
101         if (aBaseIter.NamedShape()->Label().IsEqual(theFinal)) {
102           theResults.Add(aBaseIter.Shape());
103         } else {
104           if (thePass.Add(aBaseIter.Shape()))
105             findFinals(
106               anAccess, aBaseIter.Shape(), theFinal, theAdditionalDoc, thePass, theResults);
107         }
108       }
109     }
110   }
111   if (!theAdditionalDoc.IsNull()) { // search additionally by the additional access label
112     static TDF_Label anEmpty;
113     TopTools_MapOfShape aPass;
114     findFinals(theAdditionalDoc, theBase, theFinal, anEmpty, aPass, theResults);
115   }
116 }
117
118 void Selector_Modify::findModificationResult(TopoDS_ListOfShape& theCommon) {
119   for(TDF_LabelList::Iterator aBase(myBases); aBase.More(); aBase.Next()) {
120     TDF_Label anAdditionalDoc; // this document if base is started in extra document
121     if (aBase.Value().Root() != label().Root()) {
122       anAdditionalDoc = label();
123     }
124     TopTools_MapOfShape aFinals;
125     TopTools_MapOfShape aPass;
126     for(TNaming_Iterator aBaseShape(aBase.Value()); aBaseShape.More(); aBaseShape.Next()) {
127       findFinals(aBase.Value(), aBaseShape.NewShape(), myFinal, anAdditionalDoc, aPass, aFinals);
128     }
129     if (!aFinals.IsEmpty()) {
130       if (theCommon.IsEmpty()) { // just copy all to common
131         for(TopTools_MapOfShape::Iterator aFinal(aFinals); aFinal.More(); aFinal.Next()) {
132           theCommon.Append(aFinal.Key());
133         }
134       } else { // keep only shapes presented in both lists
135         for(TopoDS_ListOfShape::Iterator aCommon(theCommon); aCommon.More(); ) {
136           if (aFinals.Contains(aCommon.Value())) {
137             aCommon.Next();
138           } else { // common is not found, remove it
139             theCommon.Remove(aCommon);
140           }
141         }
142       }
143     }
144   }
145 }
146
147 bool Selector_Modify::select(NCollection_List<Handle(TNaming_NamedShape)>& theModifList,
148   const TopoDS_Shape theContext, const TopoDS_Shape theValue)
149 {
150   if (theModifList.Extent() > 1) { // searching for the best modification result: by context
151     Handle(TNaming_NamedShape) aCandidate;
152     NCollection_List<Handle(TNaming_NamedShape)>::Iterator aModIter(theModifList);
153     for (; !theModifList.IsEmpty() && aModIter.More(); aModIter.Next()) {
154       aCandidate = aModIter.Value();
155       TDF_Label aFatherLab = aCandidate->Label().Father();
156       Handle(TNaming_NamedShape) aFatherNS;
157       if (aFatherLab.FindAttribute(TNaming_NamedShape::GetID(), aFatherNS)) {
158         for (TNaming_Iterator anIter(aFatherNS); anIter.More(); anIter.Next()) {
159           if (theContext.IsSame(anIter.NewShape())) { // found the best modification
160             theModifList.Clear();
161             break;
162           }
163         }
164       }
165     }
166     // take the best candidate, or the last in the iteration
167     theModifList.Clear();
168     theModifList.Append(aCandidate);
169   }
170
171   if (!theModifList.IsEmpty()) {
172     // searching for all the base shapes of this modification
173     findBases(label(), theModifList.First(), theValue, true, baseDocument(), myBases);
174     if (!myBases.IsEmpty()) {
175       myFinal = theModifList.First()->Label();
176       TopoDS_ListOfShape aCommon;
177       findModificationResult(aCommon);
178       // trying to search by neighbors
179       if (aCommon.Extent() > 1) { // more complicated selection
180         if (alwaysGeometricalNaming()) {
181           TopoDS_ListOfShape::Iterator aCommonIter(aCommon);
182           TopoDS_Shape aFirst = aCommonIter.Value();
183           for (aCommonIter.Next(); aCommonIter.More(); aCommonIter.Next()) {
184             if (!sameGeometry(aFirst, aCommonIter.Value()))
185               break;
186           }
187           if (!aCommonIter.More()) { // all geometry is same, result is a compound
188             return true;
189           }
190         }
191       } else if (aCommon.Extent() == 1) {
192         return true; // simple modification
193       }
194       if (useNeighbors()) { // optimization: for the current moment only in one case this method is
195                             //  called where this is not needed if neighbors option is disabled
196         // weak naming between the common results
197         Selector_NExplode aNexp(aCommon);
198         myWeakIndex = aNexp.index(theValue);
199       } else
200         myWeakIndex = 0;
201       return myWeakIndex != -1;
202     }
203     // weak naming case
204     TopoDS_ListOfShape aCommon;
205     myFinal = theModifList.First()->Label();
206     Handle(TNaming_NamedShape) aNS;
207     if (myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
208       for(TNaming_Iterator aFinalIter(aNS); aFinalIter.More(); aFinalIter.Next()) {
209         const TopoDS_Shape& aNewShape = aFinalIter.NewShape();
210         if (!aNewShape.IsNull())
211           aCommon.Append(aNewShape);
212       }
213     }
214     Selector_NExplode aNexp(aCommon);
215     myWeakIndex = aNexp.index(theValue);
216     return myWeakIndex != -1;
217   }
218   return false;
219 }
220
221 void Selector_Modify::store()
222 {
223   storeType(Selector_Algo::SELTYPE_MODIFICATION);
224   storeBaseArray(myBases, myFinal);
225   if (myWeakIndex != -1) {
226     TDataStd_Integer::Set(label(), weakID(), myWeakIndex);
227   }
228 }
229
230 bool Selector_Modify::restore()
231 {
232   if (restoreBaseArray(myBases, myFinal)) {
233     Handle(TDataStd_Integer) aWeakInt;
234     if (label().FindAttribute(weakID(), aWeakInt)) {
235       myWeakIndex = aWeakInt->Get();
236     }
237     return true;
238   }
239   return false;
240 }
241
242 TDF_Label Selector_Modify::restoreByName(std::string theName,
243   const TopAbs_ShapeEnum theShapeType, Selector_NameGenerator* theNameGenerator)
244 {
245   TDF_Label aContext;
246   for(size_t anEnd, aStart = 0; aStart != std::string::npos; aStart = anEnd) {
247     if (aStart != 0)
248       aStart++;
249     anEnd = theName.find('&', aStart);
250     std::string aSubStr =
251       theName.substr(aStart, anEnd == std::string::npos ? anEnd : anEnd - aStart);
252     if (aSubStr.find(weakNameID()) == 0) { // weak name identifier
253       std::string aWeakIndex = aSubStr.substr(weakNameID().size());
254       myWeakIndex = atoi(aWeakIndex.c_str());
255       continue;
256     }
257     TDF_Label aSubContext, aValue;
258     if (!theNameGenerator->restoreContext(aSubStr, aSubContext, aValue))
259       return TDF_Label(); // can not restore
260     if(aSubContext.IsNull() || aValue.IsNull())
261       return TDF_Label(); // can not restore
262     if (myFinal.IsNull()) {
263       myFinal = aValue;
264       aContext = aSubContext;
265     } else
266       myBases.Append(aValue);
267   }
268   return aContext;
269 }
270
271 bool Selector_Modify::solve(const TopoDS_Shape& theContext)
272 {
273   TopoDS_Shape aResult;
274   if (myBases.IsEmpty() && myWeakIndex > 0) { // weak name by the final shapes index
275     TopoDS_ListOfShape aCommon;
276     Handle(TNaming_NamedShape) aNS;
277     if (myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
278       for(TNaming_Iterator aFinalIter(aNS); aFinalIter.More(); aFinalIter.Next()) {
279         const TopoDS_Shape& aNewShape = aFinalIter.NewShape();
280         if (!aNewShape.IsNull())
281           aCommon.Append(aNewShape);
282       }
283     }
284     Selector_NExplode aNexp(aCommon);
285     aResult = aNexp.shape(myWeakIndex);
286   } else { // standard case
287     TopoDS_ListOfShape aFinalsCommon; // final shapes presented in all results from bases
288     findModificationResult(aFinalsCommon);
289     if (aFinalsCommon.Extent() == 1) { // result is valid: found only one shape
290       aResult = aFinalsCommon.First();
291       findNewVersion(theContext, aResult);
292     } else if (aFinalsCommon.Extent() > 1 && myWeakIndex > 0) {
293       Selector_NExplode aNExp(aFinalsCommon);
294       aResult = aNExp.shape(myWeakIndex);
295       findNewVersion(theContext, aResult);
296     } else if (aFinalsCommon.Extent() > 1 && geometricalNaming()) {// if same geometry - compound
297       TopoDS_ListOfShape::Iterator aCommonIter(aFinalsCommon);
298       TopoDS_Shape aFirst = aCommonIter.Value();
299       for(aCommonIter.Next(); aCommonIter.More(); aCommonIter.Next()) {
300         if (!sameGeometry(aFirst, aCommonIter.Value()))
301           break;
302       }
303       if (!aCommonIter.More()) { // all geometry is same, create a result compound
304         TopoDS_Builder aBuilder;
305         TopoDS_Compound aCompound;
306         aBuilder.MakeCompound(aCompound);
307         for(aCommonIter.Initialize(aFinalsCommon); aCommonIter.More(); aCommonIter.Next()) {
308           TopoDS_Shape aSub = aCommonIter.Value();
309           findNewVersion(theContext, aSub);
310           aBuilder.Add(aCompound, aSub);
311         }
312         aResult = aCompound;
313       }
314
315     }
316   }
317
318   if (!aResult.IsNull()) {
319     Selector_Algo::store(aResult);
320     return true;
321   }
322   return false;
323 }
324
325 std::string Selector_Modify::name(Selector_NameGenerator* theNameGenerator)
326 {
327   // final&base1&base2 +optionally: [weak_name_1]
328   std::string aResult;
329   Handle(TDataStd_Name) aName;
330   if (!myFinal.FindAttribute(TDataStd_Name::GetID(), aName))
331     return "";
332   aResult += theNameGenerator->contextName(myFinal) + "/" +
333     std::string(TCollection_AsciiString(aName->Get()).ToCString());
334   for(TDF_LabelList::iterator aBase = myBases.begin(); aBase != myBases.end(); aBase++) {
335     if (!aBase->FindAttribute(TDataStd_Name::GetID(), aName))
336       return "";
337     aResult += "&";
338     aResult += theNameGenerator->contextName(*aBase) + "/" +
339       std::string(TCollection_AsciiString(aName->Get()).ToCString());
340   }
341   if (myWeakIndex != -1) {
342     std::ostringstream aWeakStr;
343     aWeakStr<<"&"<<weakNameID()<<myWeakIndex;
344     aResult += aWeakStr.str();
345   }
346   return aResult;
347 }