]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultConstruction.cpp
Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / Model / Model_ResultConstruction.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 <Model_ResultConstruction.h>
22
23 #include <Model_Data.h>
24 #include <ModelAPI_CompositeFeature.h>
25 #include <Model_SelectionNaming.h>
26 #include <ModelAPI_Events.h>
27 #include <Config_PropManager.h>
28 #include <GeomAPI_PlanarEdges.h>
29 #include <GeomAPI_Shape.h>
30 #include <GeomAlgoAPI_SketchBuilder.h>
31 #include <Events_Loop.h>
32
33 #include <TDF_Reference.hxx>
34 #include <TDF_ChildIterator.hxx>
35 #include <TNaming_NamedShape.hxx>
36 #include <TNaming_Builder.hxx>
37 #include <TDataStd_Integer.hxx>
38 #include <TDataStd_IntPackedMap.hxx>
39 #include <TDataStd_Name.hxx>
40 #include <TDataStd_UAttribute.hxx>
41 #include <TColStd_MapOfTransient.hxx>
42 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRep_Builder.hxx>
45 #include <TopoDS.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TNaming_Tool.hxx>
51 #include <Precision.hxx>
52
53 // identifier that it is full result selected, but in external document (for internal index is 0)
54 Standard_GUID kFULL_RESULT_ID("ee87e529-da6f-46af-be25-5e0fefde52f7");
55
56
57 void Model_ResultConstruction::colorConfigInfo(std::string& theSection, std::string& theName,
58                                        std::string& theDefault)
59 {
60   theSection = "Visualization";
61   theName = "result_construction_color";
62   theDefault = DEFAULT_COLOR();
63 }
64
65 void Model_ResultConstruction::setShape(std::shared_ptr<GeomAPI_Shape> theShape)
66 {
67   if (myShape != theShape) {
68     if (!theShape.get() || !theShape->isEqual(myShape)) {
69         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
70         ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
71     }
72     myShape = theShape;
73     if (theShape.get()) {
74       myFacesUpToDate = false;
75       myFaces.clear();
76     }
77   }
78 }
79
80 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape()
81 {
82   return myShape;
83 }
84
85 Model_ResultConstruction::Model_ResultConstruction()
86 {
87   myIsInHistory = true;
88   myIsInfinite = false;
89   myFacesUpToDate = false;
90 }
91
92 void Model_ResultConstruction::setIsInHistory(const bool isInHistory)
93 {
94   myIsInHistory = isInHistory;
95 }
96
97 int Model_ResultConstruction::facesNum()
98 {
99   if (!myFacesUpToDate) {
100     std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
101       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(myShape);
102     if (aWirePtr.get()) {
103       std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
104       GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
105         aWirePtr->norm(), aWirePtr, aFaces);
106       std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFIter = aFaces.begin();
107       for(; aFIter != aFaces.end(); aFIter++) {
108         std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(*aFIter));
109         if (aFace.get() && !aFace->isNull())
110           myFaces.push_back(aFace);
111       }
112     }
113     myFacesUpToDate = true;
114   }
115   return int(myFaces.size());
116 }
117
118 std::shared_ptr<GeomAPI_Face> Model_ResultConstruction::face(const int theIndex)
119 {
120   return myFaces[theIndex];
121 }
122
123 bool Model_ResultConstruction::isInfinite()
124 {
125   return myIsInfinite;
126 }
127
128 void Model_ResultConstruction::setInfinite(const bool theInfinite)
129 {
130   myIsInfinite = theInfinite;
131 }
132
133 void Model_ResultConstruction::setIsConcealed(const bool theValue)
134 {
135   // do nothing: the construction element is never consealed
136 }
137
138 static const int kSTART_VERTEX_DELTA = 1000000;
139
140 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape, std::string theFullName,
141   const int theID, std::shared_ptr<Model_Document> theDoc,
142   bool theSelectionMode)
143 {
144   TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
145   TNaming_Builder aBuilder(aLab);
146   // wire never happens as sub, it must be generated to be found
147   // by SelectionNaming TNaming_Tool::NamedShape
148   if (theSelectionMode && theShape.ShapeType() != TopAbs_WIRE)
149     aBuilder.Select(theShape, theShape);
150   else
151     aBuilder.Generated(theShape);
152
153   theDoc->addNamingName(aLab, theFullName);
154   TDataStd_Name::Set(aLab, theFullName.c_str());
155 }
156
157 // generates a full-name for sub-element of the composite feature (sketch)
158 std::string fullName(CompositeFeaturePtr theComposite, const TopoDS_Shape& theSubShape,
159   Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)())
160 {
161   TopAbs_ShapeEnum aShapeType = theSubShape.ShapeType();
162   gp_Pnt aVertexPos;
163   TColStd_MapOfTransient allCurves;
164   if (aShapeType == TopAbs_VERTEX) { // compare positions
165     aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(theSubShape));
166   } else {
167     for(TopExp_Explorer anEdgeExp(theSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
168       TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
169       Standard_Real aFirst, aLast;
170       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
171       allCurves.Add(aCurve);
172     }
173   }
174   std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
175   std::map<int, std::string> aSubNames; //map from edges IDs to names of edges
176   TColStd_PackedMapOfInteger aRefs; // indixes of sub-elements in composite
177
178   const int aSubNum = theComposite->numberOfSubs();
179   for(int a = 0; a < aSubNum; a++) {
180     FeaturePtr aSub = theComposite->subFeature(a);
181     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
182     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
183     // there may be many shapes (circle and center): register if at least one is in selection
184     for(; aRes != aResults.cend(); aRes++) {
185       ResultConstructionPtr aConstr =
186         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
187       if (!aConstr->shape()) {
188         continue;
189       }
190       if (aShapeType == TopAbs_VERTEX) {
191         if (aConstr->shape()->isVertex()) { // compare vertices positions
192           const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
193           gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
194           if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
195             aRefs.Add(theComposite->subFeatureId(a));
196             aSubNames[theComposite->subFeatureId(a)] = Model_SelectionNaming::shortName(aConstr);
197           }
198         } else { // get first or last vertex of the edge: last is stored with additional delta
199           const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
200           int aDelta = kSTART_VERTEX_DELTA;
201           for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
202             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
203             if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
204               aRefs.Add(aDelta + theComposite->subFeatureId(a));
205               aSubNames[aDelta + theComposite->subFeatureId(a)] =
206                 Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
207               break;
208             }
209             aDelta += kSTART_VERTEX_DELTA;
210           }
211         }
212       } else {
213         if (aConstr->shape()->isEdge()) {
214           const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
215           TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
216           if (!anEdge.IsNull()) {
217             Standard_Real aFirst, aLast;
218             Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
219             if (allCurves.Contains(aCurve)) {
220               int anID = theComposite->subFeatureId(a);
221               aRefs.Add(anID);
222               aSubNames[anID] = Model_SelectionNaming::shortName(aConstr);
223               if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
224                 // add edges to sub-label to support naming for edges selection
225                 TopExp_Explorer anEdgeExp(theSubShape, TopAbs_EDGE);
226                 for(; anEdgeExp.More(); anEdgeExp.Next()) {
227                   TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
228                   Standard_Real aFirst, aLast;
229                   Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
230                   if (aFaceCurve == aCurve) {
231                     int anOrient = Model_SelectionNaming::edgeOrientation(theSubShape, anEdge);
232                     anOrientations[anID] = anOrient;
233                   }
234                 }
235               }
236             }
237           }
238         }
239       }
240     }
241   }
242   std::stringstream aName;
243   // #1839 : do not store name of the feature in the tree, since this name could be changed
244   if (theSubShape.ShapeType() != TopAbs_COMPOUND) { // compound means the whole construction result
245     if (theSubShape.ShapeType() == TopAbs_FACE) aName<<"Face";
246     else if (theSubShape.ShapeType() == TopAbs_WIRE) aName<<"Wire";
247     else if (theSubShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
248     else if (theSubShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
249
250     // make a composite name from all sub-elements indexes: "1_2_3_4"
251     TColStd_MapIteratorOfPackedMapOfInteger aRef(aRefs);
252     for(; aRef.More(); aRef.Next()) {
253       aName<<"-"<<aSubNames[aRef.Key()];
254       if (anOrientations.find(aRef.Key()) != anOrientations.end()) {
255         if (anOrientations[aRef.Key()] == 1)
256           aName<<"f";
257         else if (anOrientations[aRef.Key()] == -1)
258           aName<<"r";
259       }
260     }
261   }
262   if (!theRefs.IsNull()) {
263     Handle(TColStd_HPackedMapOfInteger) aMap = new TColStd_HPackedMapOfInteger(aRefs);
264     theRefs->ChangeMap(aMap);
265   }
266   return aName.str();
267 }
268
269 // stores shape and name on sub-label of the main stored shape
270 static void saveSubName(TDF_Label& theLab, const bool isSelectionMode, const TopoDS_Shape& aSub,
271   std::shared_ptr<Model_Document> theDoc, std::string theFullName)
272 {
273   TNaming_Builder aBuilder(theLab);
274   if (isSelectionMode)
275     aBuilder.Select(aSub, aSub);
276   else
277     aBuilder.Generated(aSub);
278   theDoc->addNamingName(theLab, theFullName.c_str());
279   TDataStd_Name::Set(theLab, theFullName.c_str());
280 }
281
282
283 TDF_Label Model_ResultConstruction::startLabel(
284   const std::shared_ptr<ModelAPI_Document> theExtDoc, bool& theExternal)
285 {
286   theExternal = theExtDoc.get() && theExtDoc != document();
287   if (theExternal) { // external document is used
288     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theExtDoc);
289     return aDoc->extConstructionsLabel();
290   }
291   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
292   return aData->label();
293 }
294
295 int Model_ResultConstruction::select(const std::shared_ptr<GeomAPI_Shape>& theSubShape,
296   const std::shared_ptr<ModelAPI_Document> theExtDoc, const int theIndex)
297 {
298   int anIndex; // resulting index of the sub-label
299   TopoDS_Shape aSubShape;
300   if (theSubShape.get()) {
301     aSubShape = theSubShape->impl<TopoDS_Shape>();
302   } else if (shape().get()) {
303     aSubShape = shape()->impl<TopoDS_Shape>();
304   }
305   // if external document requires this selection, put the naming structures to this doc
306   // to support the naming mechanism in this document correctly
307   bool anExternal;
308   TDF_Label aDataLab = startLabel(theExtDoc, anExternal);
309   if (theIndex == -1) {
310     anIndex = anExternal ? 2 : 1; // for the external doc don't mind about the main shape
311
312     if (theSubShape.get() || anExternal) { // searching for already selected sub (or whole for ext)
313       // iterate all the already presented shapes to see the same
314       TDF_ChildIterator aSubsIter(aDataLab, Standard_False);
315       for(; aSubsIter.More(); aSubsIter.Next()) {
316         const TDF_Label aLab = aSubsIter.Value();
317         if (aLab.Tag() == 1) // skip the root shape label
318           continue;
319         Handle(TNaming_NamedShape) aNS;
320         if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
321           if (aNS->Get().IsSame(aSubShape)) {
322             return aLab.Tag() - 1; // found exactly the needed shape, nothing else to do
323           }
324         }
325         anIndex = aLab.Tag(); // searching for the latest index
326       }
327       anIndex = (anIndex == 1) ? 2 : (anIndex + 1); // next after 1-root, or next after all
328     }
329   } else {
330     anIndex = theIndex + 1;
331   }
332
333   // set the naming structure at index
334   TDF_Label aLab = aDataLab.FindChild(anIndex, Standard_True);
335
336   // if the subshape is part of a result face, select the whole face (#1997)
337   bool isSelectionMode = false; // and other don't set shapes - all the naming is in face label
338   if (!aSubShape.IsNull() && aSubShape.ShapeType() > TopAbs_FACE) {
339     for(int aFaceIndex = 0; aFaceIndex < facesNum(); aFaceIndex++) {
340       TopExp_Explorer anExp(face(aFaceIndex)->impl<TopoDS_Shape>(), aSubShape.ShapeType());
341       for(; anExp.More(); anExp.Next()) {
342         if (aSubShape.IsSame(anExp.Current())) { // this is the case: select the whole face
343           // here just store the face index (to update face if update of edge is needed)
344           TNaming_Builder aBuilder(aLab);
345           aBuilder.Select(aSubShape, aSubShape);
346           int aFaceSelID = select(face(aFaceIndex), theExtDoc, -1);
347           TDF_Reference::Set(aLab, aLab.Father().FindChild(aFaceSelID));
348           isSelectionMode = true;
349           break;
350         }
351       }
352     }
353   }
354
355   // external full result is not identified by index == 0, so, add here the ID
356   if (!theSubShape.get()) {
357     TDataStd_UAttribute::Set(aLab, kFULL_RESULT_ID);
358     // empty NS
359     TNaming_Builder aBuilder(aLab);
360     // store all sub-faces naming since faces may be used for extrusion, where all edges are needed
361     Handle(TDataStd_IntPackedMap) anIndices = TDataStd_IntPackedMap::Set(aLab);
362     std::list<int> aFacesIndexes;
363     for(int a = 0; a < facesNum(); a++) {
364       anIndices->Add(select(face(a), theExtDoc, -1));
365     }
366     return anIndex - 1;
367   }
368
369   { // this to have erased Builder after the shape was generated (NS on this label may be changed)
370     TNaming_Builder aBuilder(aLab);
371     if (aSubShape.IsNull()) {
372       return anIndex - 1; // just keep empty named shape
373     }
374     // wire never happens as sub, it must be generated to be found
375     // by SelectionNaming TNaming_Tool::NamedShape
376     if (isSelectionMode && aSubShape.ShapeType() != TopAbs_WIRE) {
377       aBuilder.Select(aSubShape, aSubShape);
378     } else {
379       aBuilder.Generated(aSubShape);
380     }
381   }
382
383   if (anIndex == 1 && isInfinite()) { // infinitive results has no sub-selection
384     return anIndex - 1;
385   }
386   ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
387   FeaturePtr aThisFeature = document()->feature(aThisPtr);
388   CompositeFeaturePtr aComposite =
389     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aThisFeature);
390   if (!aComposite || aComposite->numberOfSubs() == 0) {
391     // saving of context is enough: result construction contains exactly the needed shape
392     return anIndex - 1;
393   }
394
395   // identify the results of sub-object of the composite by edges
396   // save type of the selected shape in integer attribute
397   TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
398   TDataStd_Integer::Set(aLab, (int)aShapeType);
399   gp_Pnt aVertexPos;
400   TColStd_MapOfTransient allCurves;
401   if (aShapeType == TopAbs_VERTEX) { // compare positions
402     aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
403   } else {
404     for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
405       TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
406       Standard_Real aFirst, aLast;
407       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
408       allCurves.Add(aCurve);
409     }
410   }
411   std::shared_ptr<Model_Document> aMyDoc =
412     std::dynamic_pointer_cast<Model_Document>(document());
413   // iterate and store the result ids of sub-elements and sub-elements to sub-labels
414   Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
415   const int aSubNum = aComposite->numberOfSubs();
416   for(int a = 0; a < aSubNum; a++) {
417     FeaturePtr aSub = aComposite->subFeature(a);
418     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
419     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
420     // there may be many shapes (circle and center): register if at least one is in selection
421     for(; aRes != aResults.cend(); aRes++) {
422       ResultConstructionPtr aConstr =
423         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
424       if (!aConstr->shape()) {
425         continue;
426       }
427       if (aShapeType != TopAbs_VERTEX) {
428         if (aConstr->shape()->isEdge()) {
429           const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
430           TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
431           if (!anEdge.IsNull()) {
432             Standard_Real aFirst, aLast;
433             Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
434             if (allCurves.Contains(aCurve)) {
435               int anID = aComposite->subFeatureId(a);
436               if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
437                 // add edges to sub-label to support naming for edges selection
438                 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
439                 for(; anEdgeExp.More(); anEdgeExp.Next()) {
440                   TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
441                   Standard_Real aFirst, aLast;
442                   Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
443                   if (aFaceCurve == aCurve) {
444                     TDF_Label aSubLab = aLab.FindChild(anID);
445                     std::string aFullNameSub = fullName(aComposite, anEdge);
446                     saveSubName(aSubLab, isSelectionMode, anEdge, aMyDoc, aFullNameSub);
447
448                     int anOrient = Model_SelectionNaming::edgeOrientation(aSubShape, anEdge);
449                     if (anOrient != 0) {
450                       // store the orientation of edge relatively to face if needed
451                       TDataStd_Integer::Set(aSubLab, anOrient);
452                     }
453                   }
454                 }
455               } else { // put vertices of the selected edge to sub-labels
456                 // add edges to sub-label to support naming for edges selection
457                 int aTagIndex = anID + kSTART_VERTEX_DELTA;
458                 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
459                   anEdgeExp.More();
460                   anEdgeExp.Next(), aTagIndex += kSTART_VERTEX_DELTA) {
461                     TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
462                     TDF_Label aSubLab = aLab.FindChild(aTagIndex);
463                     std::string aFullNameSub = fullName(aComposite, aV);
464                     saveSubName(aSubLab, isSelectionMode, aV, aMyDoc, aFullNameSub);
465                 }
466               }
467             }
468           }
469         }
470       }
471     }
472   }
473   std::string aFullName = fullName(aComposite, aSubShape, aRefs);
474   // store the selected as primitive
475   registerSubShape(aLab, aSubShape, aFullName, 0, aMyDoc, isSelectionMode);
476   return anIndex - 1;
477 }
478
479 std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape(const int theIndex,
480   const std::shared_ptr<ModelAPI_Document> theExtDoc)
481 {
482   std::shared_ptr<GeomAPI_Shape> aResult;
483   if (theIndex == 0)
484     return aResult; // the whole shape, so, NULL
485
486   bool isExt;
487   TDF_Label aLab = startLabel(theExtDoc, isExt).FindChild(theIndex + 1);
488   if (!aLab.IsNull()) { // index is not bad
489     Handle(TNaming_NamedShape) aSelection;
490     if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
491       TopoDS_Shape aSelShape = aSelection->Get();
492       if (aSelShape.IsNull())
493         return aResult; // shape equal to context => null
494       aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
495       aResult->setImpl(new TopoDS_Shape(aSelShape));
496     }
497   }
498
499   return aResult;
500 }
501
502 bool Model_ResultConstruction::update(const int theIndex,
503   const std::shared_ptr<ModelAPI_Document> theExtDoc, bool& theModified)
504 {
505   theModified = false;
506   bool anExt;
507   TDF_Label aLab = startLabel(theExtDoc, anExt).FindChild(theIndex + 1, Standard_True);
508   if (theIndex == 0 || aLab.IsAttribute(kFULL_RESULT_ID)) { // full for external same as index == 0
509     // it is just reference to construction, not sub-shape
510     // if there is a sketch, the sketch-naming must be updated
511     if (!isInfinite()) {
512       // update all faces named by the whole result
513       bool aRes = true;
514       Handle(TDataStd_IntPackedMap) anIndices;
515       if (aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), anIndices)) {
516         NCollection_Map<TopoDS_Shape> aFaces; // collect faces, updated in the tree
517         TColStd_MapIteratorOfPackedMapOfInteger anIndexIter(anIndices->GetMap());
518         Handle(TColStd_HPackedMapOfInteger) aNewPackedMap =
519           new TColStd_HPackedMapOfInteger; // with only faces that are ok
520         // iterate to find existing faces, updated
521         for(; anIndexIter.More(); anIndexIter.Next()) {
522           if (update(anIndexIter.Key(), theExtDoc, theModified)) {
523             GeomShapePtr aFace = shape(anIndexIter.Key(), theExtDoc);
524             if (!aFaces.Contains(aFace->impl<TopoDS_Shape>())) {
525               aNewPackedMap->ChangeMap().Add(anIndexIter.Key());
526               aFaces.Add(aFace->impl<TopoDS_Shape>());
527             }
528           }
529         }
530         // then iterate all existing faces to find new faces
531         int aCurrentFacesNum = facesNum();
532         for(int a = 0; a < aCurrentFacesNum; a++) {
533           GeomShapePtr aFace = face(a);
534           if (!aFaces.Contains(aFace->impl<TopoDS_Shape>())) {
535             // add this one
536             int aNewFaceIndex = select(aFace, theExtDoc, -1);
537             if (aNewFaceIndex > 0) {
538               aNewPackedMap->ChangeMap().Add(aNewFaceIndex);
539             }
540           }
541         }
542         anIndices->ChangeMap(aNewPackedMap);
543       }
544       return aRes;
545     } else {
546       // For correct naming selection, put the shape into the naming structure.
547       // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
548       TNaming_Builder aBuilder(aLab);
549       aBuilder.Generated(shape()->impl<TopoDS_Shape>());
550     }
551     return shape() && !shape()->isNull();
552   }
553   // construction: identification by the results indexes, recompute faces and
554   // take the face that more close by the indexes
555   ResultPtr aThisPtr = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
556   FeaturePtr aContextFeature = document()->feature(aThisPtr);
557
558   // sketch sub-element
559   if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
560   {
561     // update the referenced object if it is sub
562     Handle(TDF_Reference) aRef;
563     if (aLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
564       int aFaceIndex = aRef->Get().Tag();
565       // don't check selection ,since face may disappear, but the shape stays correct
566       Model_ResultConstruction::update(aFaceIndex, theExtDoc, theModified);
567     }
568     // getting a type of selected shape
569     Handle(TDataStd_Integer) aTypeAttr;
570     if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
571       return false;
572     }
573     TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
574     // selected indexes will be needed in each "if"
575     Handle(TDataStd_IntPackedMap) aSubIds;
576     std::shared_ptr<GeomAPI_Shape> aNewSelected;
577     bool aNoIndexes =
578       !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
579     // for now working only with composite features
580     CompositeFeaturePtr aComposite =
581       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
582     if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
583       return false;
584     }
585
586     if (aShapeType == TopAbs_FACE || aShapeType == TopAbs_WIRE) {
587       // compound is for the whole sketch selection
588       // If this is a wire with plane defined then it is a sketch-like object
589       if (!facesNum()) // no faces, update can not work correctly
590         return false;
591       // if there is no edges indexes, any face can be used: take the first
592       std::shared_ptr<GeomAPI_Shape> aNewSelected;
593       if (aNoIndexes) {
594         aNewSelected = face(0);
595       } else { // searching for most looks-like initial face by the indexes
596         // prepare edges of the current result for the fast searching
597         // curves and orientations of edges
598         NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
599         const int aSubNum = aComposite->numberOfSubs();
600         for(int a = 0; a < aSubNum; a++) {
601           int aSubID = aComposite->subFeatureId(a);
602           if (aSubIds->Contains(aSubID)) {
603             FeaturePtr aSub = aComposite->subFeature(a);
604             const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
605             std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
606             for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
607               ResultConstructionPtr aConstr =
608                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
609               if (aConstr->shape() && aConstr->shape()->isEdge()) {
610                 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
611                 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
612                 if (!anEdge.IsNull()) {
613                   Standard_Real aFirst, aLast;
614                   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
615                   // searching for orientation information
616                   int anOrient = 0;
617                   Handle(TDataStd_Integer) anInt;
618                   if (aLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)) {
619                     anOrient = anInt->Get();
620                   }
621                   allCurves.Bind(aCurve, anOrient);
622                 }
623               }
624             }
625           }
626         }
627         aNewSelected = Model_SelectionNaming::findAppropriateFace(
628           aThisPtr, allCurves, aShapeType == TopAbs_WIRE);
629       }
630       if (aNewSelected) { // store this new selection
631         select(aNewSelected, theExtDoc, theIndex);
632         theModified = true;
633         return true;
634       } else {
635         // if the selection is not found, put the empty shape:
636         // it's better to have disappeared shape, than the old, the lost one
637         TNaming_Builder anEmptyBuilder(aLab);
638         return false;
639       }
640     } else if (aShapeType == TopAbs_EDGE) {
641       // just reselect the edge by the id
642       const int aSubNum = aComposite->numberOfSubs();
643       for(int a = 0; a < aSubNum; a++) {
644         // if aSubIds take any, the first appropriate
645         if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
646           // found the appropriate feature
647           FeaturePtr aFeature = aComposite->subFeature(a);
648           std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
649             aFeature->results().cbegin();
650           for(;aResIter != aFeature->results().cend(); aResIter++) {
651             ResultConstructionPtr aRes =
652               std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
653             if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
654               select(aRes->shape(), theExtDoc, theIndex);
655               theModified = true;
656               return true;
657             }
658           }
659         }
660       }
661     } else if (aShapeType == TopAbs_VERTEX) {
662       // just reselect the vertex by the id of edge
663       const int aSubNum = aComposite->numberOfSubs();
664       for(int a = 0; a < aSubNum; a++) {
665         // if aSubIds take any, the first appropriate
666         int aFeatureID = aComposite->subFeatureId(a);
667         if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
668           aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
669           aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
670             // searching for deltas
671             int aVertexNum = 0;
672             if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
673             else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
674             // found the feature with appropriate edge
675             FeaturePtr aFeature = aComposite->subFeature(a);
676             std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
677               aFeature->results().cbegin();
678             for(;aResIter != aFeature->results().cend(); aResIter++) {
679               ResultConstructionPtr aRes =
680                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
681               if (aRes && aRes->shape()) {
682                 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
683                   select(aRes->shape(), theExtDoc, theIndex);
684                   theModified = true;
685                   return true;
686                 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
687                   const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
688                   int aVIndex = 1;
689                   for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
690                     if (aVIndex == aVertexNum) { // found!
691                       std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
692                       aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
693                       select(aVertex, theExtDoc, theIndex);
694                       theModified = true;
695                       return true;
696                     }
697                     aVIndex++;
698                   }
699                 }
700               }
701             }
702         }
703       }
704     }
705   } else { // simple construction element: the selected is that needed
706     select(shape(), theExtDoc, theIndex);
707     theModified = true;
708     return true;
709   }
710   return false; // unknown case
711 }