Salome HOME
Make the correct order in the searching of shape by name algorithm
[modules/shaper.git] / src / Model / Model_AttributeSelection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_AttributeSelection.h
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 <ModelAPI_Feature.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_CompositeFeature.h>
16 #include <GeomAPI_Shape.h>
17 #include <GeomAPI_PlanarEdges.h>
18 #include <GeomAlgoAPI_SketchBuilder.h>
19 #include <Events_Error.h>
20
21 #include <TNaming_Selector.hxx>
22 #include <TNaming_NamedShape.hxx>
23 #include <TNaming_Tool.hxx>
24 #include <TNaming_Builder.hxx>
25 #include <TNaming_Localizer.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Compound.hxx>
28 #include <TDataStd_IntPackedMap.hxx>
29 #include <TDataStd_Integer.hxx>
30 #include <TDataStd_UAttribute.hxx>
31 #include <TDataStd_Name.hxx>
32 #include <TopTools_MapOfShape.hxx>
33 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <TopTools_MapIteratorOfMapOfShape.hxx>
35 #include <TopTools_ListOfShape.hxx>
36 #include <TopExp_Explorer.hxx>
37 #include <TDF_LabelMap.hxx>
38 #include <BRep_Tool.hxx>
39 #include <BRep_Builder.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS.hxx>
42 #include <TopExp.hxx>
43 #include <TColStd_MapOfTransient.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
47 #include <gp_Pnt.hxx>
48 #include <Precision.hxx>
49 #include <TDF_ChildIterator.hxx>
50 #include <TDF_ChildIDIterator.hxx>
51 #include <TDataStd_Name.hxx>
52 #include <TopAbs_ShapeEnum.hxx>
53 #include <TopoDS_Iterator.hxx>
54 #include <TNaming_Iterator.hxx>
55 using namespace std;
56 //#define DEB_NAMING 1
57 #ifdef DEB_NAMING
58 #include <BRepTools.hxx>
59 #endif
60 /// adeed to the index in the packed map to signalize that the vertex of edge is seleted
61 /// (multiplied by the index of the edge)
62 static const int kSTART_VERTEX_DELTA = 1000000;
63 // identifier that there is simple reference: selection equals to context
64 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
65
66 // on this label is stored:
67 // TNaming_NamedShape - selected shape
68 // TNaming_Naming - topological selection information (for the body)
69 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
70 // TDataStd_Integer - type of the selected shape (for construction)
71 // TDF_Reference - from ReferenceAttribute, the context
72 #define DDDD 1
73 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
74   const std::shared_ptr<GeomAPI_Shape>& theSubShape)
75 {
76   const std::shared_ptr<GeomAPI_Shape>& anOldShape = value();
77   bool isOldShape = 
78     (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
79   if (isOldShape) return; // shape is the same, so context is also unchanged
80   // update the referenced object if needed
81   bool isOldContext = theContext == myRef.value();
82
83
84   if (!isOldContext)
85     myRef.setValue(theContext);
86
87   // do noth use naming if selected shape is result shape itself, but not sub-shape
88   TDF_Label aSelLab = selectionLabel();
89   aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
90   if (theContext->groupName() == ModelAPI_ResultBody::group()) {
91     // do not select the whole shape for body:it is already must be in the data framework
92     if (theContext->shape().get() && theContext->shape()->isEqual(theSubShape)) {
93       aSelLab.ForgetAllAttributes(true);
94       TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
95     } else {
96       selectBody(theContext, theSubShape);
97     }
98   } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
99     selectConstruction(theContext, theSubShape);
100   }
101   myIsInitialized = true;
102
103   std::string aSelName = namingName();
104   if(!aSelName.empty())
105     TDataStd_Name::Set(selectionLabel(), aSelName.c_str()); //set name
106 #ifdef DDDD
107   //####
108   //selectSubShape("FACE",  "Extrusion_1/LateralFace_3");
109   //selectSubShape("FACE",  "Extrusion_1/TopFace");
110   //selectSubShape("EDGE", "Extrusion_1/TopFace|Extrusion_1/LateralFace_1");
111   //selectSubShape("EDGE", "Sketch_1/Edge_6");
112 #endif
113   owner()->data()->sendAttributeUpdated(this);
114 }
115
116 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
117 {
118   std::shared_ptr<GeomAPI_Shape> aResult;
119   if (myIsInitialized) {
120     TDF_Label aSelLab = selectionLabel();
121     if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
122       ResultPtr aContext = context();
123       if (!aContext.get()) 
124         return aResult; // empty result
125       return aContext->shape();
126     }
127
128     Handle(TNaming_NamedShape) aSelection;
129     if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
130       TopoDS_Shape aSelShape = aSelection->Get();
131       aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
132       aResult->setImpl(new TopoDS_Shape(aSelShape));
133     } else { // for simple construction element: just shape of this construction element
134       ResultConstructionPtr aConstr = 
135         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
136       if (aConstr) {
137         return aConstr->shape();
138       }
139     }
140   }
141   return aResult;
142 }
143
144 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
145   : myRef(theLabel)
146 {
147   myIsInitialized = myRef.isInitialized();
148 }
149
150 ResultPtr Model_AttributeSelection::context() {
151   return std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
152 }
153
154
155 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
156 {
157   ModelAPI_AttributeSelection::setObject(theObject);
158   myRef.setObject(theObject);
159 }
160
161 TDF_LabelMap& Model_AttributeSelection::scope()
162 {
163   if (myScope.IsEmpty()) { // create a new scope if not yet done
164     // gets all labels with named shapes that are bofore this feature label (before in history)
165     TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
166       owner()->data())->label().Father();
167     int aFeatureID = aFeatureLab.Tag();
168     TDF_ChildIterator aFeaturesIter(aFeatureLab.Father());
169     for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
170       if (aFeaturesIter.Value().Tag() >= aFeatureID) // the left labels are created later
171         break;
172       TDF_ChildIDIterator aNSIter(aFeaturesIter.Value(), TNaming_NamedShape::GetID(), 1);
173       for(; aNSIter.More(); aNSIter.Next()) {
174         Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
175         if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
176           myScope.Add(aNS->Label());
177         }
178       }
179     }
180   }
181   return myScope;
182 }
183
184 bool Model_AttributeSelection::update()
185 {
186   ResultPtr aContext = context();
187   if (!aContext.get()) return false;
188   TDF_Label aSelLab = selectionLabel();
189   if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
190     return aContext->shape() && !aContext->shape()->isNull();
191   }
192
193   if (aContext->groupName() == ModelAPI_ResultBody::group()) {
194     // body: just a named shape, use selection mechanism from OCCT
195     TNaming_Selector aSelector(aSelLab);
196     bool aResult = aSelector.Solve(scope()) == Standard_True;
197     owner()->data()->sendAttributeUpdated(this);
198     return aResult;
199   } else if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
200     // construction: identification by the results indexes, recompute faces and
201     // take the face that more close by the indexes
202     std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr = 
203       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(
204       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext)->shape());
205     if (aWirePtr && aWirePtr->hasPlane()) { // sketch sub-element
206       TDF_Label aLab = myRef.myRef->Label();
207       // getting a type of selected shape
208       Handle(TDataStd_Integer) aTypeAttr;
209       if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
210         return false;
211       }
212       TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
213       // selected indexes will be needed in each "if"
214       Handle(TDataStd_IntPackedMap) aSubIds;
215       std::shared_ptr<GeomAPI_Shape> aNewSelected;
216       bool aNoIndexes = 
217         !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
218       // for now working only with composite features
219       FeaturePtr aContextFeature = aContext->document()->feature(aContext);
220       CompositeFeaturePtr aComposite = 
221         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
222       if (!aComposite || aComposite->numberOfSubs() == 0) {
223         return false;
224       }
225
226       if (aShapeType == TopAbs_FACE) {
227         // If this is a wire with plane defined thin it is a sketch-like object
228         std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
229         GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
230           aWirePtr->dirY(), aWirePtr->norm(), aWirePtr, aFaces);
231         if (aFaces.empty()) // no faces, update can not work correctly
232           return false;
233         // if there is no edges indexes, any face can be used: take the first
234         std::shared_ptr<GeomAPI_Shape> aNewSelected;
235         if (aNoIndexes) {
236           aNewSelected = *(aFaces.begin());
237         } else { // searching for most looks-like initial face by the indexes
238           // prepare edges of the current resut for the fast searching
239           TColStd_MapOfTransient allCurves;
240           const int aSubNum = aComposite->numberOfSubs();
241           for(int a = 0; a < aSubNum; a++) {
242             if (aSubIds->Contains(aComposite->subFeatureId(a))) {
243               FeaturePtr aSub = aComposite->subFeature(a);
244               const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
245               std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
246               for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
247                 ResultConstructionPtr aConstr = 
248                   std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
249                 if (aConstr->shape() && aConstr->shape()->isEdge()) {
250                   const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
251                   TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
252                   if (!anEdge.IsNull()) {
253                     Standard_Real aFirst, aLast;
254                     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
255                     allCurves.Add(aCurve);
256                   }
257                 }
258               }
259             }
260           }
261           // iterate new result faces and searching for these edges
262           std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFacesIter = aFaces.begin();
263           double aBestFound = 0; // best percentage of found edges
264           for(; aFacesIter != aFaces.end(); aFacesIter++) {
265             int aFound = 0, aNotFound = 0;
266             TopExp_Explorer anEdgesExp((*aFacesIter)->impl<TopoDS_Shape>(), TopAbs_EDGE);
267             for(; anEdgesExp.More(); anEdgesExp.Next()) {
268               TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
269               if (!anEdge.IsNull()) {
270                 Standard_Real aFirst, aLast;
271                 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
272                 if (allCurves.Contains(aCurve)) {
273                   aFound++;
274                 } else {
275                   aNotFound++;
276                 }
277               }
278             }
279             if (aFound + aNotFound != 0) {
280               double aPercentage = double(aFound) / double(aFound + aNotFound);
281               if (aPercentage > aBestFound) {
282                 aBestFound = aPercentage;
283                 aNewSelected = *aFacesIter;
284               }
285             }
286           }
287         }
288         if (aNewSelected) { // store this new selection
289           selectConstruction(aContext, aNewSelected);
290           owner()->data()->sendAttributeUpdated(this);
291           return true;
292         }
293       } else if (aShapeType == TopAbs_EDGE) {
294         // just reselect the edge by the id
295         const int aSubNum = aComposite->numberOfSubs();
296         for(int a = 0; a < aSubNum; a++) {
297           // if aSubIds take any, the first appropriate
298           if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
299             // found the appropriate feature
300             FeaturePtr aFeature = aComposite->subFeature(a);
301             std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
302               aFeature->results().cbegin();
303             for(;aResIter != aFeature->results().cend(); aResIter++) {
304               ResultConstructionPtr aRes = 
305                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
306               if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
307                 selectConstruction(aContext, aRes->shape());
308                 owner()->data()->sendAttributeUpdated(this);
309                 return true;
310               }
311             }
312           }
313         }
314       } else if (aShapeType == TopAbs_VERTEX) {
315         // just reselect the vertex by the id of edge
316         const int aSubNum = aComposite->numberOfSubs();
317         for(int a = 0; a < aSubNum; a++) {
318           // if aSubIds take any, the first appropriate
319           int aFeatureID = aComposite->subFeatureId(a);
320           if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
321             aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
322             aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
323               // searching for deltas
324               int aVertexNum = 0;
325               if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
326               else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
327               // found the feature with appropriate edge
328               FeaturePtr aFeature = aComposite->subFeature(a);
329               std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
330                 aFeature->results().cbegin();
331               for(;aResIter != aFeature->results().cend(); aResIter++) {
332                 ResultConstructionPtr aRes = 
333                   std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
334                 if (aRes && aRes->shape()) {
335                   if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
336                     selectConstruction(aContext, aRes->shape());
337                     owner()->data()->sendAttributeUpdated(this);
338                     return true;
339                   } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
340                     const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
341                     int aVIndex = 1;
342                     for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
343                       if (aVIndex == aVertexNum) { // found!
344                         std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
345                         aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
346                         selectConstruction(aContext, aVertex);
347                         owner()->data()->sendAttributeUpdated(this);
348                         return true;
349                       }
350                       aVIndex++;
351                     }
352                   }
353                 }
354               }
355           }
356         }
357       }
358     } else { // simple construction element: the selected is that needed
359       selectConstruction(aContext, aContext->shape());
360       owner()->data()->sendAttributeUpdated(this);
361       return true;
362     }
363   }
364   return false; // unknown case
365 }
366
367
368 void Model_AttributeSelection::selectBody(
369   const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
370 {
371   // perform the selection
372   TNaming_Selector aSel(selectionLabel());
373   TopoDS_Shape aNewShape = theSubShape ? theSubShape->impl<TopoDS_Shape>() : TopoDS_Shape();
374   TopoDS_Shape aContext;
375
376   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
377   if (aBody) {
378     aContext = aBody->shape()->impl<TopoDS_Shape>();
379   } else {
380     ResultConstructionPtr aConstr = 
381       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myRef.value());
382     if (aConstr) {
383       aContext = aConstr->shape()->impl<TopoDS_Shape>();
384     } else {
385       Events_Error::send("A result with shape is expected");
386       return;
387     }
388   }
389   /// fix for issue 411: result modified shapes must not participate in this selection mechanism
390   FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
391   if (aFeatureOwner.get())
392     aFeatureOwner->eraseResults();
393
394   aSel.Select(aNewShape, aContext); 
395 }
396
397 /// registers the name of the shape in the label (theID == 0) of sub label (theID is a tag)
398 /// if theID is zero, 
399 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape,
400   const int theID, const FeaturePtr& theContextFeature, std::shared_ptr<Model_Document> theDoc,
401   std::string theAdditionalName,
402   Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)())
403 {
404   TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
405   TNaming_Builder aBuilder(aLab);
406   aBuilder.Generated(theShape);
407   std::stringstream aName;
408   aName<<theContextFeature->name()<<"/";
409   if (!theAdditionalName.empty())
410     aName<<theAdditionalName<<"/";
411   if (theShape.ShapeType() == TopAbs_FACE) aName<<"Face";
412   else if (theShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
413   else if (theShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
414
415   if (theRefs.IsNull()) {
416     aName<<theID;
417   } else { // make a compisite name from all sub-elements indexes: "1_2_3_4"
418     TColStd_MapIteratorOfPackedMapOfInteger aRef(theRefs->GetMap());
419     for(; aRef.More(); aRef.Next()) {
420       aName<<"-"<<aRef.Key();
421     }
422   }
423
424   theDoc->addNamingName(aLab, aName.str());
425   TDataStd_Name::Set(aLab, aName.str().c_str());
426 }
427
428 void Model_AttributeSelection::selectConstruction(
429   const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
430 {
431   std::shared_ptr<Model_Document> aMyDoc = 
432     std::dynamic_pointer_cast<Model_Document>(owner()->document());
433   FeaturePtr aContextFeature = theContext->document()->feature(theContext);
434   CompositeFeaturePtr aComposite = 
435     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
436   const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
437   if (!aComposite || aComposite->numberOfSubs() == 0) {
438     // saving of context is enough: result construction contains exactly the needed shape
439     TNaming_Builder aBuilder(selectionLabel());
440     aBuilder.Generated(aSubShape);
441     aMyDoc->addNamingName(selectionLabel(), theContext->data()->name());
442     TDataStd_Name::Set(selectionLabel(), theContext->data()->name().c_str());
443     return;
444   }
445   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(owner()->data());
446   TDF_Label aLab = myRef.myRef->Label();
447   // identify the reuslts of sub-object of the composite by edges
448   // save type of the selected shape in integer attribute
449   TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
450   TDataStd_Integer::Set(aLab, (int)aShapeType);
451   gp_Pnt aVertexPos;
452   TColStd_MapOfTransient allCurves;
453   if (aShapeType == TopAbs_VERTEX) { // compare positions
454     aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
455   } else { 
456     for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
457       TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
458       Standard_Real aFirst, aLast;
459       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
460       allCurves.Add(aCurve);
461     }
462   }
463   // iterate and store the result ids of sub-elements and sub-elements to sub-labels
464   Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
465   aRefs->Clear();
466   const int aSubNum = aComposite->numberOfSubs();
467   for(int a = 0; a < aSubNum; a++) {
468     FeaturePtr aSub = aComposite->subFeature(a);
469     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
470     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
471     // there may be many shapes (circle and center): register if at least one is in selection
472     for(; aRes != aResults.cend(); aRes++) {
473       ResultConstructionPtr aConstr = 
474         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
475       if (!aConstr->shape()) {
476         continue;
477       }
478       if (aShapeType == TopAbs_VERTEX) {
479         if (aConstr->shape()->isVertex()) { // compare vertices positions
480           const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
481           gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
482           if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
483             aRefs->Add(aComposite->subFeatureId(a));
484           }
485         } else { // get first or last vertex of the edge: last is stored with negative sign
486           const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
487           int aDelta = kSTART_VERTEX_DELTA;
488           for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
489             gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
490             if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
491               aRefs->Add(aDelta + aComposite->subFeatureId(a));
492               break;
493             }
494             aDelta += kSTART_VERTEX_DELTA;
495           }
496         }
497       } else {
498         if (aConstr->shape()->isEdge()) {
499           const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
500           TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
501           if (!anEdge.IsNull()) {
502             Standard_Real aFirst, aLast;
503             Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
504             if (allCurves.Contains(aCurve)) {
505               int anID = aComposite->subFeatureId(a);
506               aRefs->Add(anID);
507               if (aShapeType != TopAbs_EDGE) { // edge do not need the sub-edges on sub-labels
508                 // add edges to sub-label to support naming for edges selection
509                 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
510                 for(; anEdgeExp.More(); anEdgeExp.Next()) {
511                   TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
512                   Standard_Real aFirst, aLast;
513                   Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
514                   if (aFaceCurve == aCurve) {
515                     registerSubShape(selectionLabel(), anEdge, anID, aContextFeature, aMyDoc, "");
516                   }
517                 }
518               } else { // put vertices of the selected edge to sub-labels
519                 // add edges to sub-label to support naming for edges selection
520                 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
521                 int aTagIndex = anID + kSTART_VERTEX_DELTA;
522                 for(; anEdgeExp.More(); anEdgeExp.Next(), aTagIndex += kSTART_VERTEX_DELTA) {
523                   TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
524
525                   std::stringstream anAdditionalName; 
526                   registerSubShape(selectionLabel(), aV, aTagIndex, aContextFeature, aMyDoc,
527                     "");
528                 }
529               }
530             }
531           }
532         }
533       }
534     }
535   }
536   // store the selected as primitive
537   TNaming_Builder aBuilder(selectionLabel());
538   aBuilder.Generated(aSubShape);
539   registerSubShape(selectionLabel(), aSubShape, 0, aContextFeature, aMyDoc, "", aRefs);
540 }
541
542 TDF_Label Model_AttributeSelection::selectionLabel()
543 {
544   return myRef.myRef->Label().FindChild(1);
545 }
546
547 #define FIX_BUG1 1
548 std::string GetShapeName(std::shared_ptr<Model_Document> theDoc, const TopoDS_Shape& theShape, 
549   const TDF_Label& theLabel)
550 {
551   std::string aName;
552   // check if the subShape is already in DF
553   Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(theShape, theLabel);
554   Handle(TDataStd_Name) anAttr;
555   if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document    
556     if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
557       aName = TCollection_AsciiString(anAttr->Get()).ToCString();
558       if(!aName.empty()) {          
559         const TDF_Label& aLabel = theDoc->findNamingName(aName);
560         /* MPV: the same shape with the same name may be duplicated on different labels (selection of the same construction object)
561         if(!aLabel.IsEqual(aNS->Label())) {
562         //aName.erase(); //something is wrong, to be checked!!!
563         aName += "_SomethingWrong";
564         return aName;
565         }*/
566
567         static const std::string aPostFix("_");
568         TNaming_Iterator anItL(aNS);
569         for(int i = 1; anItL.More(); anItL.Next(), i++) {
570           if(anItL.NewShape() == theShape) {
571             aName += aPostFix;
572             aName += TCollection_AsciiString (i).ToCString();
573             break;
574           }
575         }
576         /* MPV: the code below does not work because of TNaming_Tool.cxx line 145: aNS->Get() uses maps
577         const TopoDS_Shape& aShape = aNS->Get();
578         if(aShape.ShapeType() == TopAbs_COMPOUND) {
579         std::string aPostFix("_");
580         TopoDS_Iterator it(aShape);                     
581         for (int i = 1;it.More();it.Next(), i++) {
582         if(it.Value() == theShape) {
583         aPostFix += TCollection_AsciiString (i).ToCString();
584         aName    += aPostFix;
585         break;
586         }
587         else continue;                                          
588         }
589         }
590         */
591       } 
592     }
593   }
594   return aName;
595 }
596
597 bool isTrivial (const TopTools_ListOfShape& theAncestors, TopTools_IndexedMapOfShape& theSMap)
598 {
599   // a trivial case: F1 & F2,  aNumber = 1, i.e. intersection gives 1 edge.
600   TopoDS_Compound aCmp;
601   BRep_Builder BB;
602   BB.MakeCompound(aCmp);
603   TopTools_ListIteratorOfListOfShape it(theAncestors);
604   for(;it.More();it.Next()) {
605     BB.Add(aCmp, it.Value());
606     theSMap.Add(it.Value());
607   }
608   int aNumber(0);
609   TopTools_IndexedDataMapOfShapeListOfShape aMap2;
610   TopExp::MapShapesAndAncestors(aCmp, TopAbs_EDGE, TopAbs_FACE, aMap2);
611   for (int i = 1; i <= aMap2.Extent(); i++) {
612     const TopoDS_Shape& aKey = aMap2.FindKey(i);
613     const TopTools_ListOfShape& anAncestors = aMap2.FindFromIndex(i);
614     if(anAncestors.Extent() > 1) aNumber++;
615   }
616   if(aNumber > 1) return false;
617   return true;
618 }
619 std::string Model_AttributeSelection::namingName()
620 {
621   std::string aName("");
622   if(!this->isInitialized()) return aName;
623   Handle(TDataStd_Name) anAtt;
624   if(selectionLabel().FindAttribute(TDataStd_Name::GetID(), anAtt)) {
625     aName = TCollection_AsciiString(anAtt->Get()).ToCString();
626     return aName;
627   }
628
629   std::shared_ptr<GeomAPI_Shape> aSubSh = value();
630   ResultPtr aCont = context();
631   aName = "Undefined name";
632   if(!aSubSh.get() || aSubSh->isNull() || !aCont.get() || aCont->shape()->isNull()) 
633     return aName;
634   TopoDS_Shape aSubShape = aSubSh->impl<TopoDS_Shape>();
635   TopoDS_Shape aContext  = aCont->shape()->impl<TopoDS_Shape>();
636 #ifdef DEB_NAMING
637   BRepTools::Write(aSubShape, "Selection.brep");
638   BRepTools::Write(aContext, "Context.brep");
639 #endif
640   std::shared_ptr<Model_Document> aDoc = 
641     std::dynamic_pointer_cast<Model_Document>(aCont->document());
642
643   // check if the subShape is already in DF
644   aName = GetShapeName(aDoc, aSubShape, selectionLabel());
645   if(aName.empty() ) { // not in the document!
646     TopAbs_ShapeEnum aType = aSubShape.ShapeType();
647     switch (aType) {
648     case TopAbs_FACE:
649       // the Face should be in DF. If it is not the case, it is an error ==> to be debugged             
650       break;
651     case TopAbs_EDGE:
652       {
653         // name structure: F1 | F2 [| F3 | F4], where F1 & F2 the faces which gives the Edge in trivial case
654         // if it is not atrivial case we use localization by neighbours. F3 & F4 - neighbour faces               
655         TopTools_IndexedDataMapOfShapeListOfShape aMap;
656         TopExp::MapShapesAndAncestors(aContext, TopAbs_EDGE, TopAbs_FACE, aMap);
657         TopTools_IndexedMapOfShape aSMap; // map for ancestors of the sub-shape
658         bool isTrivialCase(true);
659         /*                for (int i = 1; i <= aMap.Extent(); i++) {
660         const TopoDS_Shape& aKey = aMap.FindKey(i);
661         //if (aKey.IsNotEqual(aSubShape)) continue; // find exactly the selected key
662         if (aKey.IsSame(aSubShape)) continue;
663         const TopTools_ListOfShape& anAncestors = aMap.FindFromIndex(i);
664         // check that it is not a trivial case (F1 & F2: aNumber = 1)
665         isTrivialCase = isTrivial(anAncestors, aSMap);                  
666         break;
667         }*/
668         if(aMap.Contains(aSubShape)) {
669           const TopTools_ListOfShape& anAncestors = aMap.FindFromKey(aSubShape);
670           // check that it is not a trivial case (F1 & F2: aNumber = 1)
671           isTrivialCase = isTrivial(anAncestors, aSMap);                
672         } else 
673           break;
674         TopTools_ListOfShape aListOfNbs;
675         if(!isTrivialCase) { // find Neighbors
676           TNaming_Localizer aLocalizer;
677           TopTools_MapOfShape aMap3;
678           aLocalizer.FindNeighbourg(aContext, aSubShape, aMap3);
679           //int n = aMap3.Extent();
680           TopTools_MapIteratorOfMapOfShape it(aMap3);
681           for(;it.More();it.Next()) {
682             const TopoDS_Shape& aNbShape = it.Key(); // neighbor edge
683             //TopAbs_ShapeEnum aType = aNbShape.ShapeType();
684             const TopTools_ListOfShape& aList  = aMap.FindFromKey(aNbShape);
685             TopTools_ListIteratorOfListOfShape it2(aList);
686             for(;it2.More();it2.Next()) {
687               if(aSMap.Contains(it2.Value())) continue; // skip this Face
688               aListOfNbs.Append(it2.Value());
689             }
690           }
691         }  // else a trivial case
692
693         // build name of the sub-shape Edge
694         for(int i=1; i <= aSMap.Extent(); i++) {
695           const TopoDS_Shape& aFace = aSMap.FindKey(i);
696           std::string aFaceName = GetShapeName(aDoc, aFace, selectionLabel());
697           if(i == 1)
698             aName = aFaceName;
699           else 
700             aName += "|" + aFaceName;
701         }
702         TopTools_ListIteratorOfListOfShape itl(aListOfNbs);
703         for (;itl.More();itl.Next()) {
704           std::string aFaceName = GetShapeName(aDoc, itl.Value(), selectionLabel());
705           aName += "|" + aFaceName;
706         }                 
707       }
708       break;
709
710     case TopAbs_VERTEX:
711       // name structure (Monifold Topology): 
712       // 1) F1 | F2 | F3 - intersection of 3 faces defines a vertex - trivial case.
713       // 2) F1 | F2 | F3 [|F4 [|Fn]] - redundant definition, but it should be kept as is to obtain safe recomputation
714       // 2) F1 | F2      - intersection of 2 faces definses a vertex - applicable for case
715       //                   when 1 faces is cylindrical, conical, spherical or revolution and etc.
716       // 3) E1 | E2 | E3 - intersection of 3 edges defines a vertex - when we have case of a shell
717       //                   or compound of 2 open faces.
718       // 4) E1 | E2      - intesection of 2 edges defines a vertex - when we have a case of 
719       //                   two independent edges (wire or compound)
720       // implemented 2 first cases
721       {
722         TopTools_IndexedDataMapOfShapeListOfShape aMap;
723         TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_FACE, aMap);
724         const TopTools_ListOfShape& aList2  = aMap.FindFromKey(aSubShape);
725         TopTools_ListOfShape aList;
726         TopTools_MapOfShape aFMap;
727 #ifdef FIX_BUG1
728         //int n = aList2.Extent(); //bug! duplication
729         // fix is below
730         TopTools_ListIteratorOfListOfShape itl2(aList2);
731         for (int i = 1;itl2.More();itl2.Next(),i++) {
732           if(aFMap.Add(itl2.Value()))
733             aList.Append(itl2.Value());
734         }
735         //n = aList.Extent();
736 #endif
737         int n = aList.Extent();
738         if(n < 3) { // open topology case => via edges
739           TopTools_IndexedDataMapOfShapeListOfShape aMap;
740           TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_EDGE, aMap);
741           const TopTools_ListOfShape& aList2  = aMap.FindFromKey(aSubShape);
742           if(aList2.Extent() >= 2)  { // regular solution
743             TopTools_ListIteratorOfListOfShape itl(aList2);
744             for (int i = 1;itl.More();itl.Next(),i++) {
745               const TopoDS_Shape& anEdge = itl.Value();
746               std::string anEdgeName = GetShapeName(aDoc, anEdge, selectionLabel());
747               if(i == 1)
748                 aName = anEdgeName;
749               else 
750                 aName += "|" + anEdgeName;
751             }
752           }
753         } 
754         else {
755           TopTools_ListIteratorOfListOfShape itl(aList);
756           for (int i = 1;itl.More();itl.Next(),i++) {
757             const TopoDS_Shape& aFace = itl.Value();
758             std::string aFaceName = GetShapeName(aDoc, aFace, selectionLabel());
759             if(i == 1)
760               aName = aFaceName;
761             else 
762               aName += "|" + aFaceName;
763           }
764         }
765       }
766       break;
767     }
768     // register name                    
769     // aDoc->addNamingName(selectionLabel(), aName);
770     // the selected sub-shape will not be shared and as result it will not require registration
771   }
772   return aName;
773 }
774
775 TopAbs_ShapeEnum translateType (const std::string& theType)
776 {
777   TCollection_AsciiString aStr(theType.c_str());
778   aStr.UpperCase();
779   if(aStr.IsEqual("COMP"))
780     return TopAbs_COMPOUND;
781   else if(aStr.IsEqual("COMS"))
782     return TopAbs_COMPSOLID;
783   else if(aStr.IsEqual("SOLD"))
784     return TopAbs_SOLID;
785   else if(aStr.IsEqual("SHEL"))
786     return TopAbs_SHELL;
787   else if(aStr.IsEqual("FACE"))
788     return TopAbs_FACE;
789   else if(aStr.IsEqual("WIRE"))
790     return TopAbs_WIRE;
791   else if(aStr.IsEqual("EDGE"))
792     return TopAbs_EDGE;
793   else if(aStr.IsEqual("VERT"))
794     return TopAbs_VERTEX;  
795
796   return TopAbs_SHAPE;
797 }
798
799 const TopoDS_Shape getShapeFromNS(
800   const std::string& theSubShapeName, Handle(TNaming_NamedShape) theNS)
801 {
802   TopoDS_Shape aSelection;
803   std::string::size_type n = theSubShapeName.rfind('/');                        
804   if (n == std::string::npos) n = 0;
805   std::string aSubString = theSubShapeName.substr(n + 1);
806   n = aSubString.rfind('_');
807   if (n == std::string::npos) return aSelection;
808   aSubString = aSubString.substr(n+1);
809   int indx = atoi(aSubString.c_str());
810
811   TNaming_Iterator anItL(theNS);
812   for(int i = 1; anItL.More(); anItL.Next(), i++) {
813     if (i == indx) {
814       return anItL.NewShape();
815     }
816   }
817   return aSelection;    
818 }
819
820 const TopoDS_Shape findFaceByName(
821   const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc)
822 {
823   TopoDS_Shape aFace;
824   std::string::size_type n, nb = theSubShapeName.rfind('/');                    
825   if (nb == std::string::npos) nb = 0;
826   std::string aSubString = theSubShapeName.substr(nb + 1);
827   n = aSubString.rfind('_');
828   if (n != std::string::npos) {
829     std::string aSubStr2 = aSubString.substr(0, n);
830     aSubString  = theSubShapeName.substr(0, nb + 1);
831     aSubString = aSubString + aSubStr2; 
832   } else
833     aSubString = theSubShapeName;
834
835   const TDF_Label& aLabel = theDoc->findNamingName(aSubString);
836   if(aLabel.IsNull()) return aFace;
837   Handle(TNaming_NamedShape) aNS;
838   if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
839     aFace = getShapeFromNS(theSubShapeName, aNS);
840   }
841   return aFace;
842 }
843
844 int ParseName(const std::string& theSubShapeName,   std::list<std::string>& theList)
845 {
846   std::string aName = theSubShapeName;
847   std::string aLastName;
848   int n1(0), n2(0); // n1 - start position, n2 - position of the delimiter
849   while ((n2 = aName.find('|', n1)) != std::string::npos) {
850     const std::string aName1 = aName.substr(n1, n2 - n1); //name of face
851     theList.push_back(aName1);  
852     n1 = n2 + 1;
853     aLastName = aName.substr(n1);
854   }
855   if(!aLastName.empty())
856     theList.push_back(aLastName);
857   return theList.size();
858 }
859
860 const TopoDS_Shape findCommonShape(
861   const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
862 {
863   TopoDS_Shape aShape;
864   std::vector<TopTools_MapOfShape> aVec;
865   TopTools_MapOfShape aMap1, aMap2, aMap3, aMap4;
866   if(theList.Extent() > 1) {
867     aVec.push_back(aMap1);
868     aVec.push_back(aMap2);
869   }
870   if(theList.Extent() > 2)
871     aVec.push_back(aMap3);
872   if(theList.Extent() == 4)
873     aVec.push_back(aMap4);
874
875   //fill maps
876   TopTools_ListIteratorOfListOfShape it(theList);
877   for(int i = 0;it.More();it.Next(),i++) {
878     const TopoDS_Shape& aFace = it.Value();             
879     if(i < 2) {
880       TopExp_Explorer anExp (aFace, theType);
881       for(;anExp.More();anExp.Next()) {
882         const TopoDS_Shape& anEdge = anExp.Current();
883         if (!anEdge.IsNull())
884           aVec[i].Add(anExp.Current());
885       }
886     } else {
887       TopExp_Explorer anExp (aFace, TopAbs_VERTEX);
888       for(;anExp.More();anExp.Next()) {
889         const TopoDS_Shape& aVertex = anExp.Current();
890         if (!aVertex.IsNull())
891           aVec[i].Add(anExp.Current());
892       }
893     }
894   }
895   //trivial case: 2 faces
896   TopTools_ListOfShape aList;
897   TopTools_MapIteratorOfMapOfShape it2(aVec[0]);
898   for(;it2.More();it2.Next()) {
899     if(aVec[1].Contains(it2.Key())) {
900       aShape = it2.Key();
901       if(theList.Extent() == 2)
902         break;
903       else 
904         aList.Append(it2.Key());
905     }
906   }
907   if(aList.Extent() && aVec.size() > 3) {// list of common edges ==> search ny neighbors 
908     if(aVec[2].Extent() && aVec[3].Extent()) {
909       TopTools_ListIteratorOfListOfShape it(aList);
910       for(;it.More();it.Next()) {
911         const TopoDS_Shape& aCand = it.Value();
912         // not yet completelly implemented, to be rechecked
913         TopoDS_Vertex aV1, aV2;
914         TopExp::Vertices(TopoDS::Edge(aCand), aV1, aV2);
915         int aNum(0);
916         if(aVec[2].Contains(aV1)) aNum++;
917         else if(aVec[2].Contains(aV2)) aNum++;
918         if(aVec[3].Contains(aV1)) aNum++;
919         else if(aVec[3].Contains(aV2)) aNum++;
920         if(aNum == 2) {
921           aShape = aCand;
922           break;
923         }
924       }
925     }
926   }
927
928   if(aList.Extent() && aVec.size() == 3) {
929
930     TopTools_ListIteratorOfListOfShape it(aList);
931     for(;it.More();it.Next()) {
932       const TopoDS_Shape& aCand = it.Value();
933       if(aVec[2].Contains(aCand)) {
934         aShape = aCand;
935         break;
936       }
937     }
938   }
939   return aShape;
940 }
941
942 std::string getContextName(const std::string& theSubShapeName)
943 {
944   std::string aName;
945   std::string::size_type n = theSubShapeName.find('/');                 
946   if (n == std::string::npos) return aName;
947   aName = theSubShapeName.substr(0, n);
948   return aName;
949 }
950 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
951 void Model_AttributeSelection::selectSubShape(
952   const std::string& theType, const std::string& theSubShapeName)
953 {
954   if(theSubShapeName.empty() || theType.empty()) return;
955   TopAbs_ShapeEnum aType = translateType(theType);
956   std::shared_ptr<Model_Document> aDoc = 
957     std::dynamic_pointer_cast<Model_Document>(owner()->document());
958   std::string aContName = getContextName(theSubShapeName);
959   if(aContName.empty()) return;
960   //ResultPtr aCont = context();
961   ResultPtr aCont = aDoc->findByName(aContName);
962   if(!aCont.get() || aCont->shape()->isNull()) return;
963   TopoDS_Shape aContext  = aCont->shape()->impl<TopoDS_Shape>();
964   TopAbs_ShapeEnum aContType = aContext.ShapeType();
965   if(aType <= aContType) return; // not applicable
966
967
968   TopoDS_Shape aSelection;
969   switch (aType) 
970   {
971   case TopAbs_COMPOUND:
972     break;
973   case TopAbs_COMPSOLID:
974     break;
975   case TopAbs_SOLID:
976     break;
977   case TopAbs_SHELL:
978     break;
979   case TopAbs_FACE:
980     {
981       const TopoDS_Shape aSelection = findFaceByName(theSubShapeName, aDoc);
982       if(!aSelection.IsNull()) {// Select it
983         std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
984         aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
985         setValue(aCont, aShapeToBeSelected);
986       }
987     }
988     break;
989   case TopAbs_WIRE:
990     break;
991   case TopAbs_EDGE:
992     {  
993       TopoDS_Shape aSelection;// = findFaceByName(theSubShapeName, aDoc);
994       const TDF_Label& aLabel = aDoc->findNamingName(theSubShapeName);
995       if(!aLabel.IsNull()) {
996         Handle(TNaming_NamedShape) aNS;
997         if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
998           aSelection = getShapeFromNS(theSubShapeName, aNS);
999         }
1000       }
1001       if(aSelection.IsNull()) {
1002         std::list<std::string> aListofNames;
1003         int n = ParseName(theSubShapeName, aListofNames);
1004         if(n > 1 && n < 5) {
1005           TopTools_ListOfShape aList;
1006           std::list<std::string>::iterator it =aListofNames.begin();
1007           for(;it != aListofNames.end();it++){
1008             const TopoDS_Shape aFace = findFaceByName(*it, aDoc);
1009             aList.Append(aFace);                
1010           }
1011           aSelection = findCommonShape(TopAbs_EDGE, aList);
1012         }
1013       }
1014       if(!aSelection.IsNull()) {// Select it
1015         std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
1016         aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
1017         setValue(aCont, aShapeToBeSelected);
1018       }
1019     }
1020     break;
1021   case TopAbs_VERTEX:
1022     {
1023       TopoDS_Shape aSelection;
1024       const TDF_Label& aLabel = aDoc->findNamingName(theSubShapeName);
1025       if(!aLabel.IsNull()) {
1026         Handle(TNaming_NamedShape) aNS;
1027         if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
1028           aSelection = getShapeFromNS(theSubShapeName, aNS);
1029         }
1030       }
1031       if(aSelection.IsNull()) {
1032         std::list<std::string> aListofNames;
1033         int n = ParseName(theSubShapeName, aListofNames);
1034         if(n > 1 && n < 4) { // 2 || 3
1035           TopTools_ListOfShape aList;
1036           std::list<std::string>::iterator it = aListofNames.begin();
1037           for(; it != aListofNames.end(); it++){
1038             const TopoDS_Shape aFace = findFaceByName(*it, aDoc);
1039             if(!aFace.IsNull())
1040               aList.Append(aFace);              
1041           }
1042           aSelection = findCommonShape(TopAbs_VERTEX, aList);
1043         }
1044       }
1045       if(!aSelection.IsNull()) {// Select it
1046         std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
1047         aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
1048         setValue(aCont, aShapeToBeSelected);
1049       }
1050     }
1051     break;
1052   default: //TopAbs_SHAPE
1053     return;
1054   }
1055
1056 }
1057
1058 int Model_AttributeSelection::Id()
1059 {
1060   std::shared_ptr<GeomAPI_Shape> aSelection = value();
1061   std::shared_ptr<GeomAPI_Shape> aContext = context()->shape();
1062   const TopoDS_Shape& aMainShape = aContext->impl<TopoDS_Shape>();
1063   const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1064   int anID = 0;
1065   if (aSelection && !aSelection->isNull() &&
1066     aContext   && !aContext->isNull())
1067   {
1068     TopTools_IndexedMapOfShape aSubShapesMap;
1069     TopExp::MapShapes(aMainShape, aSubShapesMap);
1070     anID = aSubShapesMap.FindIndex(aSubShape);
1071   }
1072   return anID;
1073 }