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