Salome HOME
923b2cd48ee10399871c5022f32fb9f88727f008
[modules/shaper.git] / src / Model / Model_AttributeSelection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_AttributeSelection.cpp
4 // Created:     2 Oct 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeSelection.h"
8 #include "Model_Application.h"
9 #include "Model_Events.h"
10 #include "Model_Data.h"
11 #include "Model_Document.h"
12 #include "Model_SelectionNaming.h"
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_ResultPart.h>
17 #include <ModelAPI_CompositeFeature.h>
18 #include <ModelAPI_Tools.h>
19 #include <GeomAPI_Shape.h>
20 #include <GeomAPI_PlanarEdges.h>
21 #include <Events_InfoMessage.h>
22
23 #include <TNaming_Selector.hxx>
24 #include <TNaming_NamedShape.hxx>
25 #include <TNaming_Tool.hxx>
26 #include <TNaming_Builder.hxx>
27 #include <TNaming_Localizer.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopoDS_Compound.hxx>
30 #include <TDataStd_IntPackedMap.hxx>
31 #include <TDataStd_Integer.hxx>
32 #include <TDataStd_UAttribute.hxx>
33 #include <TDataStd_Name.hxx>
34 #include <TopTools_MapOfShape.hxx>
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopTools_MapIteratorOfMapOfShape.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <NCollection_DataMap.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TDF_LabelMap.hxx>
41 #include <BRep_Tool.hxx>
42 #include <BRep_Builder.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS.hxx>
45 #include <TopExp.hxx>
46 #include <TColStd_MapOfTransient.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
50 #include <gp_Pnt.hxx>
51 #include <Precision.hxx>
52 #include <TDF_ChildIterator.hxx>
53 #include <TDF_ChildIDIterator.hxx>
54 #include <TDataStd_Name.hxx>
55 #include <TopAbs_ShapeEnum.hxx>
56 #include <TopoDS_Iterator.hxx>
57 #include <TNaming_Iterator.hxx>
58 #include <BRep_Builder.hxx>
59 using namespace std;
60 //#define DEB_NAMING 1
61 #ifdef DEB_NAMING
62 #include <BRepTools.hxx>
63 #endif
64 /// added to the index in the packed map to signalize that the vertex of edge is selected
65 /// (multiplied by the index of the edge)
66 static const int kSTART_VERTEX_DELTA = 1000000;
67 // identifier that there is simple reference: selection equals to context
68 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
69 // simple reference in the construction
70 Standard_GUID kCONSTUCTION_SIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb28");
71 // reference to Part sub-object
72 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
73 // selection is invalid after recomputation
74 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
75
76 // on this label is stored:
77 // TNaming_NamedShape - selected shape
78 // TNaming_Naming - topological selection information (for the body)
79 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
80 // TDataStd_Integer - type of the selected shape (for construction)
81 // TDF_Reference - from ReferenceAttribute, the context
82 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
83   const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
84 {
85   if (theTemporarily) { // just keep the stored without DF update
86     myTmpContext = theContext;
87     myTmpSubShape = theSubShape;
88     owner()->data()->sendAttributeUpdated(this);
89     return;
90   } else {
91     myTmpContext.reset();
92     myTmpSubShape.reset();
93   }
94
95   const std::shared_ptr<GeomAPI_Shape>& anOldShape = value();
96   bool isOldContext = theContext == myRef.value();
97   bool isOldShape = isOldContext &&
98     (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
99   if (isOldShape) return; // shape is the same, so context is also unchanged
100   // update the referenced object if needed
101   if (!isOldContext) {
102       myRef.setValue(theContext);
103   }
104
105   // do noth use naming if selected shape is result shape itself, but not sub-shape
106   TDF_Label aSelLab = selectionLabel();
107   aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
108   aSelLab.ForgetAttribute(kCONSTUCTION_SIMPLE_REF_ID);
109   aSelLab.ForgetAttribute(kINVALID_SELECTION);
110
111   bool isDegeneratedEdge = false;
112   // do not use the degenerated edge as a shape, a null context and shape is used in the case
113   if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
114     const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
115     if (aSubShape.ShapeType() == TopAbs_EDGE)
116       isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
117   }
118   if (!theContext.get() || isDegeneratedEdge) {
119     // to keep the reference attribute label
120     TDF_Label aRefLab = myRef.myRef->Label();
121     aSelLab.ForgetAllAttributes(true);
122     myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
123     return;
124   }
125   if (theContext->groupName() == ModelAPI_ResultBody::group()) {
126     // do not select the whole shape for body:it is already must be in the data framework
127     // equal and null selected objects mean the same: object is equal to context,
128     if (theContext->shape().get() && 
129         (theContext->shape()->isEqual(theSubShape) || !theSubShape.get())) {
130       aSelLab.ForgetAllAttributes(true);
131       TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
132     } else {
133       selectBody(theContext, theSubShape);
134     }
135   } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
136     if (!theSubShape.get()) {
137       // to sub, so the whole result is selected
138       aSelLab.ForgetAllAttributes(true);
139       TDataStd_UAttribute::Set(aSelLab, kCONSTUCTION_SIMPLE_REF_ID);
140       ResultConstructionPtr aConstruction = 
141         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
142       if (aConstruction->isInfinite()) {
143         // For correct naming selection, put the shape into the naming structure.
144         // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
145         TNaming_Builder aBuilder(aSelLab);
146         aBuilder.Generated(theContext->shape()->impl<TopoDS_Shape>());
147         std::shared_ptr<Model_Document> aMyDoc = 
148           std::dynamic_pointer_cast<Model_Document>(owner()->document());
149         std::string aName = theContext->data()->name();
150         aMyDoc->addNamingName(aSelLab, aName);
151         TDataStd_Name::Set(aSelLab, aName.c_str());
152       } else {  // for sketch the naming is needed in DS
153         BRep_Builder aCompoundBuilder;
154         TopoDS_Compound aComp;
155         aCompoundBuilder.MakeCompound(aComp);
156         for(int a = 0; a < aConstruction->facesNum(); a++) {
157           TopoDS_Shape aFace = aConstruction->face(a)->impl<TopoDS_Shape>();
158           aCompoundBuilder.Add(aComp, aFace);
159         }
160         std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
161         aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
162         selectConstruction(theContext, aShape);
163       }
164     } else {
165       selectConstruction(theContext, theSubShape);
166     }
167   } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
168     aSelLab.ForgetAllAttributes(true);
169     TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
170     selectPart(theContext, theSubShape);
171   }
172   //the attribute initialized state should be changed by sendAttributeUpdated only
173   //myIsInitialized = true;
174
175   owner()->data()->sendAttributeUpdated(this);
176
177   std::string aSelName = namingName();
178   if(!aSelName.empty())
179     TDataStd_Name::Set(selectionLabel(), aSelName.c_str()); //set name
180 }
181
182 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
183 {
184   GeomShapePtr aResult;
185   if (myTmpContext.get() || myTmpSubShape.get()) {
186     ResultConstructionPtr aResulConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
187     if(aResulConstruction.get()) {
188       // it is just reference to construction.
189       return myTmpSubShape;
190     }
191     return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
192   }
193
194   TDF_Label aSelLab = selectionLabel();
195   if (aSelLab.IsAttribute(kINVALID_SELECTION))
196     return aResult;
197
198   if (myRef.isInitialized()) {
199     if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
200       ResultPtr aContext = context();
201       if (!aContext.get()) 
202         return aResult; // empty result
203       return aContext->shape();
204     }
205     if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) { // it is just reference to construction, nothing is in value
206         return aResult; // empty result
207     }
208     if (aSelLab.IsAttribute(kPART_REF_ID)) {
209       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
210       if (!aPart.get() || !aPart->isActivated())
211         return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
212       Handle(TDataStd_Integer) anIndex;
213       if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
214         if (anIndex->Get()) { // special selection attribute was created, use it
215           return aPart->selectionValue(anIndex->Get());
216         } else { // face with name is already in the data model, so try to take it by name
217           Handle(TDataStd_Name) aName;
218           if (selectionLabel().FindAttribute(TDataStd_Name::GetID(), aName)) {
219             std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
220             std::size_t aPartEnd = aSubShapeName.find('/');
221             if (aPartEnd != string::npos && aPartEnd != aSubShapeName.rfind('/')) {
222               string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
223               int anIndex;
224               std::string aType; // to reuse already existing selection the type is not needed
225               return aPart->shapeInPart(aNameInPart, aType, anIndex);
226             }
227           }
228         }
229       }
230     }
231
232     Handle(TNaming_NamedShape) aSelection;
233     if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
234       TopoDS_Shape aSelShape = aSelection->Get();
235       aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
236       aResult->setImpl(new TopoDS_Shape(aSelShape));
237     } else { // for simple construction element: just shape of this construction element
238       ResultConstructionPtr aConstr = 
239         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
240       if (aConstr) {
241         return aConstr->shape();
242       }
243     }
244   }
245   return aResult;
246 }
247
248 bool Model_AttributeSelection::isInvalid()
249 {
250   return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
251 }
252
253 bool Model_AttributeSelection::isInitialized()
254 {
255   if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
256     std::shared_ptr<GeomAPI_Shape> aResult;
257     if (myRef.isInitialized()) {
258       TDF_Label aSelLab = selectionLabel();
259       if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
260         ResultPtr aContext = context();
261         return aContext.get() != NULL;
262       }
263       if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) { // it is just reference to construction, nothing is in value
264           return true;
265       }
266
267       Handle(TNaming_NamedShape) aSelection;
268       if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
269         return !aSelection->Get().IsNull();
270       } else { // for simple construction element: just shape of this construction element
271         ResultConstructionPtr aConstr = 
272           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
273         if (aConstr.get()) {
274           return aConstr->shape().get() != NULL;
275         }
276       }
277     }
278   }
279   return false;
280 }
281
282 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
283   : myRef(theLabel)
284 {
285   myIsInitialized = myRef.isInitialized();
286 }
287
288 void Model_AttributeSelection::setID(const std::string theID)
289 {
290   myRef.setID(theID);
291   ModelAPI_AttributeSelection::setID(theID);
292 }
293
294 ResultPtr Model_AttributeSelection::context() {
295   if (myTmpContext.get() || myTmpSubShape.get()) {
296     return myTmpContext;
297   }
298
299   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
300   // for parts there could be same-data result, so take the last enabled
301   if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group()) {
302     int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
303     for(int a = aSize - 1; a >= 0; a--) {
304       ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
305       if (aPart.get() && aPart->data() == aResult->data()) {
306         ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
307         FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
308         // check that this result is not this-feature result (it is forbidden t oselect itself)
309         if (anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
310           return aPartResult;
311         }
312       }
313     }
314   }
315   return aResult;
316 }
317
318
319 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
320 {
321   ModelAPI_AttributeSelection::setObject(theObject);
322   myRef.setObject(theObject);
323 }
324
325 TDF_LabelMap& Model_AttributeSelection::scope()
326 {
327   if (myScope.IsEmpty()) { // create a new scope if not yet done
328     // gets all features with named shapes that are before this feature label (before in history)
329     DocumentPtr aMyDoc = owner()->document();
330     std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
331     std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
332     bool aMePassed = false;
333     CompositeFeaturePtr aComposite = 
334       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(owner());
335     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
336     CompositeFeaturePtr aCompositeOwner, aCompositeOwnerOwner;
337     if (aFeature.get()) {
338       aCompositeOwner = ModelAPI_Tools::compositeOwner(aFeature);
339       if (aCompositeOwner.get()) {
340          aCompositeOwnerOwner = ModelAPI_Tools::compositeOwner(aCompositeOwner);
341       }
342     }
343     // for group Scope is not limitet: this is always up to date objects
344     bool isGroup = aFeature.get() && aFeature->getKind() == "Group";
345     for(; aFIter != allFeatures.end(); aFIter++) {
346       if (*aFIter == owner()) {  // the left features are created later (except subs of composite)
347         aMePassed = true;
348         continue;
349       }
350       if (isGroup) aMePassed = false;
351       bool isInScope = !aMePassed;
352       if (!isInScope && aComposite.get()) { // try to add sub-elements of composite if this is composite
353         if (aComposite->isSub(*aFIter))
354           isInScope = true;
355       }
356       // remove the composite-owner of this feature (sketch in extrusion-cut)
357       if (isInScope && (aCompositeOwner == *aFIter || aCompositeOwnerOwner == *aFIter))
358         isInScope = false;
359
360       if (isInScope && aFIter->get() && (*aFIter)->data()->isValid()) {
361         TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
362           (*aFIter)->data())->label().Father();
363         TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), 1);
364         for(; aNSIter.More(); aNSIter.Next()) {
365           Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
366           if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
367             myScope.Add(aNS->Label());
368           }
369         }
370       }
371     }
372   }
373   return myScope;
374 }
375
376 /// Sets the invalid flag if flag is false, or removes it if "true"
377 /// Returns theFlag
378 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
379   if (theFlag) {
380     theLab.ForgetAttribute(kINVALID_SELECTION);
381   } else {
382     TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
383   }
384   return theFlag;
385 }
386
387 bool Model_AttributeSelection::update()
388 {
389   TDF_Label aSelLab = selectionLabel();
390   ResultPtr aContext = context();
391   if (!aContext.get()) 
392     return setInvalidIfFalse(aSelLab, false);
393   if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
394     return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
395   }
396   if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) { // it is just reference to construction, not sub-shape
397     // if there is a sketch, the sketch-naming must be updated
398     ResultConstructionPtr aConstruction = 
399       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
400     if (!aConstruction->isInfinite()) {
401       BRep_Builder aCompoundBuilder;
402       TopoDS_Compound aComp;
403       aCompoundBuilder.MakeCompound(aComp);
404       for(int a = 0; a < aConstruction->facesNum(); a++) {
405         TopoDS_Shape aFace = aConstruction->face(a)->impl<TopoDS_Shape>();
406         aCompoundBuilder.Add(aComp, aFace);
407       }
408       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
409       aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
410       selectConstruction(aContext, aShape);
411     } else {
412       // For correct naming selection, put the shape into the naming structure.
413       // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
414       TNaming_Builder aBuilder(aSelLab);
415       aBuilder.Generated(aContext->shape()->impl<TopoDS_Shape>());
416       std::shared_ptr<Model_Document> aMyDoc = 
417         std::dynamic_pointer_cast<Model_Document>(owner()->document());
418       std::string aName = aContext->data()->name();
419       aMyDoc->addNamingName(aSelLab, aName);
420       TDataStd_Name::Set(aSelLab, aName.c_str());
421     }
422     return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
423   }
424
425   if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
426     std::shared_ptr<GeomAPI_Shape> aNoSelection;
427     bool aResult = selectPart(aContext, aNoSelection, true);
428     aResult = setInvalidIfFalse(aSelLab, aResult);
429     if (aResult) {
430       owner()->data()->sendAttributeUpdated(this);
431     }
432     return aResult;
433   }
434
435   if (aContext->groupName() == ModelAPI_ResultBody::group()) {
436     // body: just a named shape, use selection mechanism from OCCT
437     TNaming_Selector aSelector(aSelLab);
438     bool aResult = aSelector.Solve(scope()) == Standard_True;
439     aResult = setInvalidIfFalse(aSelLab, aResult); // must be before sending of updated attribute (1556)
440     owner()->data()->sendAttributeUpdated(this);
441     return aResult;
442   } else if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
443     // construction: identification by the results indexes, recompute faces and
444     // take the face that more close by the indexes
445     ResultConstructionPtr aConstructionContext = 
446       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
447     FeaturePtr aContextFeature = aContext->document()->feature(aContext);
448     // sketch sub-element
449     if (aConstructionContext && 
450         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
451     {
452       TDF_Label aLab = myRef.myRef->Label();
453       // getting a type of selected shape
454       Handle(TDataStd_Integer) aTypeAttr;
455       if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
456         return setInvalidIfFalse(aSelLab, false);
457       }
458       TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
459       // selected indexes will be needed in each "if"
460       Handle(TDataStd_IntPackedMap) aSubIds;
461       std::shared_ptr<GeomAPI_Shape> aNewSelected;
462       bool aNoIndexes = 
463         !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
464       // for now working only with composite features
465       CompositeFeaturePtr aComposite = 
466         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
467       if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
468         return setInvalidIfFalse(aSelLab, false);
469       }
470
471       if (aShapeType == TopAbs_FACE || aShapeType == TopAbs_WIRE) { // compound is for the whole sketch selection
472         // If this is a wire with plane defined then it is a sketch-like object
473         if (!aConstructionContext->facesNum()) // no faces, update can not work correctly
474           return setInvalidIfFalse(aSelLab, false);
475         // if there is no edges indexes, any face can be used: take the first
476         std::shared_ptr<GeomAPI_Shape> aNewSelected;
477         if (aNoIndexes) {
478           aNewSelected = aConstructionContext->face(0);
479         } else { // searching for most looks-like initial face by the indexes
480           // prepare edges of the current result for the fast searching
481           NCollection_DataMap<Handle(Geom_Curve), int> allCurves; // curves and orientations of edges
482           const int aSubNum = aComposite->numberOfSubs();
483           for(int a = 0; a < aSubNum; a++) {
484             int aSubID = aComposite->subFeatureId(a);
485             if (aSubIds->Contains(aSubID)) {
486               FeaturePtr aSub = aComposite->subFeature(a);
487               const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
488               std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
489               for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
490                 ResultConstructionPtr aConstr = 
491                   std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
492                 if (aConstr->shape() && aConstr->shape()->isEdge()) {
493                   const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
494                   TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
495                   if (!anEdge.IsNull()) {
496                     Standard_Real aFirst, aLast;
497                     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
498                     // searching for orientation information
499                     int anOrient = 0;
500                     Handle(TDataStd_Integer) anInt;
501                     if (aSelLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)){
502                       anOrient = anInt->Get();
503                     }
504                     allCurves.Bind(aCurve, anOrient);
505                   }
506                 }
507               }
508             }
509           }
510           aNewSelected = Model_SelectionNaming::findAppropriateFace(aContext, allCurves);
511         }
512         if (aNewSelected) { // store this new selection
513           if (aShapeType == TopAbs_WIRE) { // just get a wire from face to have wire
514             TopExp_Explorer aWireExp(aNewSelected->impl<TopoDS_Shape>(), TopAbs_WIRE);
515             if (aWireExp.More()) {
516               aNewSelected.reset(new GeomAPI_Shape);
517               aNewSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aWireExp.Current()));
518             }
519           }
520           selectConstruction(aContext, aNewSelected);
521           setInvalidIfFalse(aSelLab, true);
522           owner()->data()->sendAttributeUpdated(this);
523           return true;
524         } else { // if the selection is not found, put the empty shape: it's better to have disappeared shape, than the old, the lost one
525           TNaming_Builder anEmptyBuilder(selectionLabel());
526           return setInvalidIfFalse(aSelLab, false);
527         }
528       } else if (aShapeType == TopAbs_EDGE) {
529         // just reselect the edge by the id
530         const int aSubNum = aComposite->numberOfSubs();
531         for(int a = 0; a < aSubNum; a++) {
532           // if aSubIds take any, the first appropriate
533           if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
534             // found the appropriate feature
535             FeaturePtr aFeature = aComposite->subFeature(a);
536             std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
537               aFeature->results().cbegin();
538             for(;aResIter != aFeature->results().cend(); aResIter++) {
539               ResultConstructionPtr aRes = 
540                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
541               if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
542                 selectConstruction(aContext, aRes->shape());
543                 setInvalidIfFalse(aSelLab, true);
544                 owner()->data()->sendAttributeUpdated(this);
545                 return true;
546               }
547             }
548           }
549         }
550       } else if (aShapeType == TopAbs_VERTEX) {
551         // just reselect the vertex by the id of edge
552         const int aSubNum = aComposite->numberOfSubs();
553         for(int a = 0; a < aSubNum; a++) {
554           // if aSubIds take any, the first appropriate
555           int aFeatureID = aComposite->subFeatureId(a);
556           if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
557             aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
558             aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
559               // searching for deltas
560               int aVertexNum = 0;
561               if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
562               else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
563               // found the feature with appropriate edge
564               FeaturePtr aFeature = aComposite->subFeature(a);
565               std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
566                 aFeature->results().cbegin();
567               for(;aResIter != aFeature->results().cend(); aResIter++) {
568                 ResultConstructionPtr aRes = 
569                   std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
570                 if (aRes && aRes->shape()) {
571                   if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
572                     selectConstruction(aContext, aRes->shape());
573                     setInvalidIfFalse(aSelLab, true);
574                     owner()->data()->sendAttributeUpdated(this);
575                     return true;
576                   } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
577                     const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
578                     int aVIndex = 1;
579                     for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
580                       if (aVIndex == aVertexNum) { // found!
581                         std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
582                         aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
583                         selectConstruction(aContext, aVertex);
584                         setInvalidIfFalse(aSelLab, true);
585                         owner()->data()->sendAttributeUpdated(this);
586                         return true;
587                       }
588                       aVIndex++;
589                     }
590                   }
591                 }
592               }
593           }
594         }
595       }
596     } else { // simple construction element: the selected is that needed
597       selectConstruction(aContext, aContext->shape());
598       setInvalidIfFalse(aSelLab, true);
599       owner()->data()->sendAttributeUpdated(this);
600       return true;
601     }
602   }
603   return setInvalidIfFalse(aSelLab, false); // unknown case
604 }
605
606
607 void Model_AttributeSelection::selectBody(
608   const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
609 {
610   // perform the selection
611   TNaming_Selector aSel(selectionLabel());
612   TopoDS_Shape aContext;
613
614   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
615   if (aBody) {
616     aContext = aBody->shape()->impl<TopoDS_Shape>();
617   } else {
618     ResultPtr aResult = 
619       std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
620     if (aResult) {
621       aContext = aResult->shape()->impl<TopoDS_Shape>();
622     } else {
623       Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
624       return;
625     }
626   }
627   TopoDS_Shape aNewShape = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
628   /// fix for issue 411: result modified shapes must not participate in this selection mechanism
629   FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
630   if (aFeatureOwner.get())
631     aFeatureOwner->eraseResults();
632   if (!aContext.IsNull()) {
633     aSel.Select(aNewShape, aContext); 
634   }
635 }
636
637 /// registers the name of the shape in the label (theID == 0) of sub label (theID is a tag)
638 /// if theID is zero, 
639 /// theOrientation is additional information about the positioning of edge relatively to face
640 ///    it is stored in the integer attribute of the edge sub-label: 
641 ///    -1 is out, 1 is in, 0 is not needed
642 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape,
643   const int theID, const FeaturePtr& theContextFeature, std::shared_ptr<Model_Document> theDoc,
644   std::string theAdditionalName, std::map<int, int>& theOrientations,
645   std::map<int, std::string>& theSubNames, // name of sub-elements by ID to be exported instead of indexes
646   Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)(),
647   const int theOrientation = 0)
648 {
649   TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
650   if (theOrientation != 0) { // store the orientation of edge relatively to face if needed
651     TDataStd_Integer::Set(aLab, theOrientation);
652   }
653   TNaming_Builder aBuilder(aLab);
654   aBuilder.Generated(theShape);
655   std::stringstream aName;
656   aName<<theContextFeature->name();
657   if (theShape.ShapeType() != TopAbs_COMPOUND) { // compound means the whole result for construction
658     aName<<"/";
659     if (!theAdditionalName.empty())
660       aName<<theAdditionalName<<"/";
661     if (theShape.ShapeType() == TopAbs_FACE) aName<<"Face";
662     else if (theShape.ShapeType() == TopAbs_WIRE) aName<<"Wire";
663     else if (theShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
664     else if (theShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
665
666     if (theRefs.IsNull()) {
667       aName<<theID;
668       if (theOrientation == 1)
669         aName<<"f";
670       else if (theOrientation == -1)
671         aName<<"r";
672     } else { // make a composite name from all sub-elements indexes: "1_2_3_4"
673       TColStd_MapIteratorOfPackedMapOfInteger aRef(theRefs->GetMap());
674       for(; aRef.More(); aRef.Next()) {
675         aName<<"-"<<theSubNames[aRef.Key()];
676         if (theOrientations.find(aRef.Key()) != theOrientations.end()) {
677           if (theOrientations[aRef.Key()] == 1)
678             aName<<"f";
679           else if (theOrientations[aRef.Key()] == -1)
680             aName<<"r";
681         }
682       }
683     }
684   }
685
686   theDoc->addNamingName(aLab, aName.str());
687   TDataStd_Name::Set(aLab, aName.str().c_str());
688 }
689
690 void Model_AttributeSelection::selectConstruction(
691   const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
692 {
693   std::shared_ptr<Model_Document> aMyDoc = 
694     std::dynamic_pointer_cast<Model_Document>(owner()->document());
695   FeaturePtr aContextFeature = theContext->document()->feature(theContext);
696   CompositeFeaturePtr aComposite = 
697     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
698   const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
699   if (!aComposite || aComposite->numberOfSubs() == 0) {
700     // saving of context is enough: result construction contains exactly the needed shape
701     TNaming_Builder aBuilder(selectionLabel());
702     aBuilder.Generated(aSubShape);
703     aMyDoc->addNamingName(selectionLabel(), theContext->data()->name());
704     TDataStd_Name::Set(selectionLabel(), theContext->data()->name().c_str());
705     return;
706   }
707   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(owner()->data());
708   TDF_Label aLab = myRef.myRef->Label();
709   // identify the results of sub-object of the composite by edges
710   // save type of the selected shape in integer attribute
711   TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
712   TDataStd_Integer::Set(aLab, (int)aShapeType);
713   gp_Pnt aVertexPos;
714   TColStd_MapOfTransient allCurves;
715   if (aShapeType == TopAbs_VERTEX) { // compare positions
716     aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
717   } else { 
718     for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
719       TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
720       Standard_Real aFirst, aLast;
721       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
722       allCurves.Add(aCurve);
723     }
724   }
725   // iterate and store the result ids of sub-elements and sub-elements to sub-labels
726   Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
727   std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
728   std::map<int, std::string> aSubNames; //map from edges IDs to names of edges
729   aRefs->Clear();
730   const int aSubNum = aComposite->numberOfSubs();
731   for(int a = 0; a < aSubNum; a++) {
732     FeaturePtr aSub = aComposite->subFeature(a);
733     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
734     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
735     // there may be many shapes (circle and center): register if at least one is in selection
736     for(; aRes != aResults.cend(); aRes++) {
737       ResultConstructionPtr aConstr = 
738         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
739       if (!aConstr->shape()) {
740         continue;
741       }
742       if (aShapeType == TopAbs_VERTEX) {
743         if (aConstr->shape()->isVertex()) { // compare vertices positions
744           const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
745           gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
746           if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
747             aRefs->Add(aComposite->subFeatureId(a));
748             aSubNames[aComposite->subFeatureId(a)] = Model_SelectionNaming::shortName(aConstr);
749           }
750         } else { // get first or last vertex of the edge: last is stored with additional delta
751           const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
752           int aDelta = kSTART_VERTEX_DELTA;
753           for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
754             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
755             if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
756               aRefs->Add(aDelta + aComposite->subFeatureId(a));
757               aSubNames[aDelta + aComposite->subFeatureId(a)] =
758                 Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
759               break;
760             }
761             aDelta += kSTART_VERTEX_DELTA;
762           }
763         }
764       } else {
765         if (aConstr->shape()->isEdge()) {
766           const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
767           TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
768           if (!anEdge.IsNull()) {
769             Standard_Real aFirst, aLast;
770             Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
771             if (allCurves.Contains(aCurve)) {
772               int anID = aComposite->subFeatureId(a);
773               aRefs->Add(anID);
774               aSubNames[anID] = Model_SelectionNaming::shortName(aConstr);
775               if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
776                 // add edges to sub-label to support naming for edges selection
777                 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
778                 for(; anEdgeExp.More(); anEdgeExp.Next()) {
779                   TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
780                   Standard_Real aFirst, aLast;
781                   Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
782                   if (aFaceCurve == aCurve) {
783                     int anOrient = Model_SelectionNaming::edgeOrientation(aSubShape, anEdge);
784                     anOrientations[anID] = anOrient;
785                     registerSubShape(
786                       selectionLabel(), anEdge, anID, aContextFeature, aMyDoc, "", anOrientations,
787                       aSubNames, Handle(TDataStd_IntPackedMap)(), anOrient);
788                   }
789                 }
790               } else { // put vertices of the selected edge to sub-labels
791                 // add edges to sub-label to support naming for edges selection
792                 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
793                 int aTagIndex = anID + kSTART_VERTEX_DELTA;
794                 for(; anEdgeExp.More(); anEdgeExp.Next(), aTagIndex += kSTART_VERTEX_DELTA) {
795                   TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
796
797                   std::stringstream anAdditionalName; 
798                   registerSubShape(
799                     selectionLabel(), aV, aTagIndex, aContextFeature, aMyDoc, "", anOrientations,
800                     aSubNames);
801                 }
802               }
803             }
804           }
805         }
806       }
807     }
808   }
809   // store the selected as primitive
810   TNaming_Builder aBuilder(selectionLabel());
811   aBuilder.Generated(aSubShape);
812     registerSubShape(
813       selectionLabel(), aSubShape, 0, aContextFeature, aMyDoc, "", anOrientations, aSubNames, aRefs); 
814 }
815
816 bool Model_AttributeSelection::selectPart(
817   const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
818   const bool theUpdate)
819 {
820   ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
821   if (!aPart.get() || !aPart->isActivated())
822     return true; // postponed naming
823   if (theUpdate) {
824     Handle(TDataStd_Integer) anIndex;
825     if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) { // by internal selection
826       if (anIndex->Get() > 0) {
827         // update the selection by index
828         return aPart->updateInPart(anIndex->Get());
829       } else {
830         return true; // nothing to do, referencing just by name
831       }
832     }
833     return true; // nothing to do, referencing just by name
834   }
835   // store the shape (in case part is not loaded it should be useful
836   TopoDS_Shape aShape;
837   std::string aName = theContext->data()->name();
838   if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
839     aShape = theContext->shape()->impl<TopoDS_Shape>();
840   } else {
841     aShape = theSubShape->impl<TopoDS_Shape>();
842     int anIndex;
843     aName += "/" + aPart->nameInPart(theSubShape, anIndex);
844     TDataStd_Integer::Set(selectionLabel(), anIndex);
845   }
846   TNaming_Builder aBuilder(selectionLabel());
847   aBuilder.Select(aShape, aShape);
848   // identify by name in the part
849   TDataStd_Name::Set(selectionLabel(), aName.c_str());
850   return !aName.empty();
851 }
852
853 TDF_Label Model_AttributeSelection::selectionLabel()
854 {
855   return myRef.myRef->Label().FindChild(1);
856 }
857
858 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
859 {
860   std::string aName("");
861   if(!this->isInitialized())
862     return !theDefaultName.empty() ? theDefaultName : aName;
863   Handle(TDataStd_Name) anAtt;
864   if(selectionLabel().FindAttribute(TDataStd_Name::GetID(), anAtt)) {
865     aName = TCollection_AsciiString(anAtt->Get()).ToCString();
866     return aName;
867   }
868
869   std::shared_ptr<GeomAPI_Shape> aSubSh = value();
870   ResultPtr aCont = context();
871
872   Model_SelectionNaming aSelNaming(selectionLabel());
873   return aSelNaming.namingName(aCont, aSubSh, theDefaultName);
874 }
875
876 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
877 void Model_AttributeSelection::selectSubShape(
878   const std::string& theType, const std::string& theSubShapeName)
879 {
880   if(theSubShapeName.empty() || theType.empty()) return;
881
882   // check this is Part-name: 2 delimiters in the name
883   std::size_t aPartEnd = theSubShapeName.find('/');
884   if (aPartEnd != string::npos && aPartEnd != theSubShapeName.rfind('/')) {
885     std::string aPartName = theSubShapeName.substr(0, aPartEnd);
886     ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
887     if (aFound.get()) { // found such part, so asking it for the name
888       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
889       string aNameInPart = theSubShapeName.substr(aPartEnd + 1);
890       int anIndex;
891       std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, theType, anIndex);
892       if (aSelected.get()) {
893         setValue(aPart, aSelected);
894         TDataStd_Integer::Set(selectionLabel(), anIndex);
895         return;
896       }
897     }
898   }
899
900   Model_SelectionNaming aSelNaming(selectionLabel());
901   std::shared_ptr<Model_Document> aDoc = 
902     std::dynamic_pointer_cast<Model_Document>(owner()->document());
903   std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
904   ResultPtr aCont;
905   if (aSelNaming.selectSubShape(theType, theSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
906     setValue(aCont, aShapeToBeSelected);
907   }
908 }
909
910 int Model_AttributeSelection::Id()
911 {
912   int anID = 0;
913   std::shared_ptr<GeomAPI_Shape> aSelection = value();
914   std::shared_ptr<GeomAPI_Shape> aContext = context()->shape();
915   // support for compsolids:
916   if (context().get() && ModelAPI_Tools::compSolidOwner(context()).get())
917     aContext = ModelAPI_Tools::compSolidOwner(context())->shape();
918
919
920   TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
921   const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
922   // searching for the latest main shape
923   if (aSelection && !aSelection->isNull() &&
924     aContext   && !aContext->isNull())
925   {
926     std::shared_ptr<Model_Document> aDoc =
927       std::dynamic_pointer_cast<Model_Document>(context()->document());
928     if (aDoc.get()) {
929       Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
930       if (!aNS.IsNull()) {
931         aMainShape = TNaming_Tool::CurrentShape(aNS);
932       }
933     }
934
935     TopTools_IndexedMapOfShape aSubShapesMap;
936     TopExp::MapShapes(aMainShape, aSubShapesMap);
937     anID = aSubShapesMap.FindIndex(aSubShape);
938   }
939   return anID;
940 }
941
942 void Model_AttributeSelection::setId(int theID)
943 {
944   const ResultPtr& aContext = context();
945   std::shared_ptr<GeomAPI_Shape> aSelection;
946
947   std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
948   // support for compsolids:
949   if (aContext.get() && ModelAPI_Tools::compSolidOwner(aContext).get())
950     aContextShape = ModelAPI_Tools::compSolidOwner(aContext)->shape();
951
952   TopoDS_Shape aMainShape = aContextShape->impl<TopoDS_Shape>();
953   // searching for the latest main shape
954   if (theID > 0 &&
955       aContextShape && !aContextShape->isNull())
956   {
957     std::shared_ptr<Model_Document> aDoc =
958       std::dynamic_pointer_cast<Model_Document>(aContext->document());
959     if (aDoc.get()) {
960       Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
961       if (!aNS.IsNull()) {
962         aMainShape = TNaming_Tool::CurrentShape(aNS);
963       }
964     }
965
966     TopTools_IndexedMapOfShape aSubShapesMap;
967     TopExp::MapShapes(aMainShape, aSubShapesMap);
968     const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
969
970     std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
971     aResult->setImpl(new TopoDS_Shape(aSelShape));
972
973     aSelection = aResult;
974   }
975
976   setValue(aContext, aSelection);
977 }
978