1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_AttributeSelection.cpp
5 // Author: Mikhail PONIKAROV
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 <Model_Objects.h>
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_ResultBody.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_ResultPart.h>
18 #include <ModelAPI_CompositeFeature.h>
19 #include <ModelAPI_Tools.h>
20 #include <GeomAPI_Shape.h>
21 #include <ModelAPI_Session.h>
22 #include <GeomAPI_PlanarEdges.h>
23 #include <Events_InfoMessage.h>
25 #include <TNaming_Selector.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TNaming_Tool.hxx>
28 #include <TNaming_Builder.hxx>
29 #include <TNaming_Localizer.hxx>
30 #include <TNaming_SameShapeIterator.hxx>
31 #include <TNaming_Iterator.hxx>
32 #include <TNaming_NewShapeIterator.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <TDataStd_IntPackedMap.hxx>
36 #include <TDataStd_Integer.hxx>
37 #include <TDataStd_UAttribute.hxx>
38 #include <TDataStd_Name.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopTools_MapIteratorOfMapOfShape.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <NCollection_DataMap.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TDF_LabelMap.hxx>
46 #include <BRep_Tool.hxx>
47 #include <BRep_Builder.hxx>
48 #include <TopoDS_Edge.hxx>
51 #include <TColStd_MapOfTransient.hxx>
52 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
56 #include <Precision.hxx>
57 #include <TDF_ChildIterator.hxx>
58 #include <TDF_ChildIDIterator.hxx>
59 #include <TDataStd_Name.hxx>
60 #include <TopAbs_ShapeEnum.hxx>
61 #include <TopoDS_Iterator.hxx>
62 #include <BRep_Builder.hxx>
64 //#define DEB_NAMING 1
66 #include <BRepTools.hxx>
68 /// added to the index in the packed map to signalize that the vertex of edge is selected
69 /// (multiplied by the index of the edge)
70 static const int kSTART_VERTEX_DELTA = 1000000;
71 // identifier that there is simple reference: selection equals to context
72 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
73 // simple reference in the construction
74 Standard_GUID kCONSTUCTION_SIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb28");
75 // reference to Part sub-object
76 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
77 // selection is invalid after recomputation
78 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
80 // on this label is stored:
81 // TNaming_NamedShape - selected shape
82 // TNaming_Naming - topological selection information (for the body)
83 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
84 // TDataStd_Integer - type of the selected shape (for construction)
85 // TDF_Reference - from ReferenceAttribute, the context
86 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
87 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
89 if (theTemporarily) { // just keep the stored without DF update
90 myTmpContext = theContext;
91 myTmpSubShape = theSubShape;
92 owner()->data()->sendAttributeUpdated(this);
96 myTmpSubShape.reset();
99 const std::shared_ptr<GeomAPI_Shape>& anOldShape = value();
100 bool isOldContext = theContext == myRef.value();
101 bool isOldShape = isOldContext &&
102 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
103 if (isOldShape) return; // shape is the same, so context is also unchanged
104 // update the referenced object if needed
106 myRef.setValue(theContext);
109 // do noth use naming if selected shape is result shape itself, but not sub-shape
110 TDF_Label aSelLab = selectionLabel();
111 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
112 aSelLab.ForgetAttribute(kCONSTUCTION_SIMPLE_REF_ID);
113 aSelLab.ForgetAttribute(kINVALID_SELECTION);
115 bool isDegeneratedEdge = false;
116 // do not use the degenerated edge as a shape, a null context and shape is used in the case
117 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
118 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
119 if (aSubShape.ShapeType() == TopAbs_EDGE)
120 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
122 if (!theContext.get() || isDegeneratedEdge) {
123 // to keep the reference attribute label
124 TDF_Label aRefLab = myRef.myRef->Label();
125 aSelLab.ForgetAllAttributes(true);
126 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
129 if (theContext->groupName() == ModelAPI_ResultBody::group()) {
130 // do not select the whole shape for body:it is already must be in the data framework
131 // equal and null selected objects mean the same: object is equal to context,
132 if (theContext->shape().get() &&
133 (theContext->shape()->isEqual(theSubShape) || !theSubShape.get())) {
134 aSelLab.ForgetAllAttributes(true);
135 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
137 selectBody(theContext, theSubShape);
139 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
140 if (!theSubShape.get()) {
141 // to sub, so the whole result is selected
142 aSelLab.ForgetAllAttributes(true);
143 TDataStd_UAttribute::Set(aSelLab, kCONSTUCTION_SIMPLE_REF_ID);
144 ResultConstructionPtr aConstruction =
145 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
146 if (aConstruction->isInfinite()) {
147 // For correct naming selection, put the shape into the naming structure.
148 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
149 TNaming_Builder aBuilder(aSelLab);
150 aBuilder.Generated(theContext->shape()->impl<TopoDS_Shape>());
151 std::shared_ptr<Model_Document> aMyDoc =
152 std::dynamic_pointer_cast<Model_Document>(owner()->document());
153 //std::string aName = contextName(theContext);
154 // for selection in different document, add the document name
155 //aMyDoc->addNamingName(aSelLab, aName);
156 //TDataStd_Name::Set(aSelLab, aName.c_str());
157 } else { // for sketch the naming is needed in DS
158 BRep_Builder aCompoundBuilder;
159 TopoDS_Compound aComp;
160 aCompoundBuilder.MakeCompound(aComp);
161 for(int a = 0; a < aConstruction->facesNum(); a++) {
162 TopoDS_Shape aFace = aConstruction->face(a)->impl<TopoDS_Shape>();
163 aCompoundBuilder.Add(aComp, aFace);
165 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
166 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
167 selectConstruction(theContext, aShape);
170 selectConstruction(theContext, theSubShape);
172 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
173 aSelLab.ForgetAllAttributes(true);
174 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
175 selectPart(theContext, theSubShape);
177 //the attribute initialized state should be changed by sendAttributeUpdated only
178 //myIsInitialized = true;
180 owner()->data()->sendAttributeUpdated(this);
183 void Model_AttributeSelection::removeTemporaryValues()
185 if (myTmpContext.get() || myTmpSubShape.get()) {
186 myTmpContext.reset();
187 myTmpSubShape.reset();
191 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
193 GeomShapePtr aResult;
194 if (myTmpContext.get() || myTmpSubShape.get()) {
195 ResultConstructionPtr aResulConstruction =
196 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
197 if(aResulConstruction.get()) {
198 // it is just reference to construction.
199 return myTmpSubShape;
201 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
204 TDF_Label aSelLab = selectionLabel();
205 if (aSelLab.IsAttribute(kINVALID_SELECTION))
208 if (myRef.isInitialized()) {
209 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
210 ResultPtr aContext = context();
212 return aResult; // empty result
213 return aContext->shape();
215 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) {
216 // it is just reference to construction, nothing is in value
217 return aResult; // empty result
219 if (aSelLab.IsAttribute(kPART_REF_ID)) {
220 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
221 if (!aPart.get() || !aPart->isActivated())
222 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
223 Handle(TDataStd_Integer) anIndex;
224 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
225 if (anIndex->Get()) { // special selection attribute was created, use it
226 return aPart->selectionValue(anIndex->Get());
227 } else { // face with name is already in the data model, so try to take it by name
228 Handle(TDataStd_Name) aName;
229 if (selectionLabel().FindAttribute(TDataStd_Name::GetID(), aName)) {
230 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
231 std::size_t aPartEnd = aSubShapeName.find('/');
232 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
233 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
235 std::string aType; // to reuse already existing selection the type is not needed
236 return aPart->shapeInPart(aNameInPart, aType, anIndex);
243 Handle(TNaming_NamedShape) aSelection;
244 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
245 TopoDS_Shape aSelShape = aSelection->Get();
246 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
247 aResult->setImpl(new TopoDS_Shape(aSelShape));
248 } else { // for simple construction element: just shape of this construction element
249 ResultConstructionPtr aConstr =
250 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
252 return aConstr->shape();
259 bool Model_AttributeSelection::isInvalid()
261 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
264 bool Model_AttributeSelection::isInitialized()
266 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
267 std::shared_ptr<GeomAPI_Shape> aResult;
268 if (myRef.isInitialized()) {
269 TDF_Label aSelLab = selectionLabel();
270 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
271 ResultPtr aContext = context();
272 return aContext.get() != NULL;
274 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) {
275 // it is just reference to construction, nothing is in value
279 Handle(TNaming_NamedShape) aSelection;
280 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
281 return !aSelection->Get().IsNull();
282 } else { // for simple construction element: just shape of this construction element
283 ResultConstructionPtr aConstr =
284 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
286 return aConstr->shape().get() != NULL;
294 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
297 myIsInitialized = myRef.isInitialized();
300 void Model_AttributeSelection::setID(const std::string theID)
303 ModelAPI_AttributeSelection::setID(theID);
306 ResultPtr Model_AttributeSelection::context() {
307 if (myTmpContext.get() || myTmpSubShape.get()) {
311 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
312 // for parts there could be same-data result, so take the last enabled
313 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group()) {
314 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
315 for(int a = aSize - 1; a >= 0; a--) {
316 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
317 if (aPart.get() && aPart->data() == aResult->data()) {
318 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
319 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
320 // check that this result is not this-feature result (it is forbidden t oselect itself)
321 if (anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
331 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
333 ModelAPI_AttributeSelection::setObject(theObject);
334 myRef.setObject(theObject);
337 TDF_LabelMap& Model_AttributeSelection::scope()
339 if (myScope.IsEmpty()) { // create a new scope if not yet done
340 // gets all features with named shapes that are before this feature label (before in history)
341 DocumentPtr aMyDoc = owner()->document();
342 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
343 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
344 bool aMePassed = false;
345 CompositeFeaturePtr aComposite =
346 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(owner());
347 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
348 CompositeFeaturePtr aCompositeOwner, aCompositeOwnerOwner;
349 if (aFeature.get()) {
350 aCompositeOwner = ModelAPI_Tools::compositeOwner(aFeature);
351 if (aCompositeOwner.get()) {
352 aCompositeOwnerOwner = ModelAPI_Tools::compositeOwner(aCompositeOwner);
355 // for group Scope is not limitet: this is always up to date objects
356 bool isGroup = aFeature.get() && aFeature->getKind() == "Group";
357 for(; aFIter != allFeatures.end(); aFIter++) {
358 if (*aFIter == owner()) { // the left features are created later (except subs of composite)
362 if (isGroup) aMePassed = false;
363 bool isInScope = !aMePassed;
364 if (!isInScope && aComposite.get()) {
365 // try to add sub-elements of composite if this is composite
366 if (aComposite->isSub(*aFIter))
369 // remove the composite-owner of this feature (sketch in extrusion-cut)
370 if (isInScope && (aCompositeOwner == *aFIter || aCompositeOwnerOwner == *aFIter))
373 if (isInScope && aFIter->get() && (*aFIter)->data()->isValid()) {
374 TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
375 (*aFIter)->data())->label().Father();
376 TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), 1);
377 for(; aNSIter.More(); aNSIter.Next()) {
378 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
379 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
380 myScope.Add(aNS->Label());
389 /// Sets the invalid flag if flag is false, or removes it if "true"
391 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
393 theLab.ForgetAttribute(kINVALID_SELECTION);
395 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
400 bool Model_AttributeSelection::update()
402 TDF_Label aSelLab = selectionLabel();
403 ResultPtr aContext = context();
405 return setInvalidIfFalse(aSelLab, false);
406 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
407 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
409 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) {
410 // it is just reference to construction, not sub-shape
411 // if there is a sketch, the sketch-naming must be updated
412 ResultConstructionPtr aConstruction =
413 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
414 if (!aConstruction->isInfinite()) {
415 BRep_Builder aCompoundBuilder;
416 TopoDS_Compound aComp;
417 aCompoundBuilder.MakeCompound(aComp);
418 for(int a = 0; a < aConstruction->facesNum(); a++) {
419 TopoDS_Shape aFace = aConstruction->face(a)->impl<TopoDS_Shape>();
420 aCompoundBuilder.Add(aComp, aFace);
422 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
423 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
424 selectConstruction(aContext, aShape);
426 // For correct naming selection, put the shape into the naming structure.
427 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
428 TNaming_Builder aBuilder(aSelLab);
429 aBuilder.Generated(aContext->shape()->impl<TopoDS_Shape>());
430 std::shared_ptr<Model_Document> aMyDoc =
431 std::dynamic_pointer_cast<Model_Document>(owner()->document());
432 //std::string aName = contextName(aContext);
433 //aMyDoc->addNamingName(aSelLab, aName);
434 //TDataStd_Name::Set(aSelLab, aName.c_str());
436 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
439 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
440 std::shared_ptr<GeomAPI_Shape> aNoSelection;
441 bool aResult = selectPart(aContext, aNoSelection, true);
442 aResult = setInvalidIfFalse(aSelLab, aResult);
444 owner()->data()->sendAttributeUpdated(this);
449 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
450 // body: just a named shape, use selection mechanism from OCCT
451 TNaming_Selector aSelector(aSelLab);
452 TopoDS_Shape anOldShape;
453 if (!aSelector.NamedShape().IsNull()) {
454 anOldShape = aSelector.NamedShape()->Get();
456 bool aResult = aSelector.Solve(scope()) == Standard_True;
457 // must be before sending of updated attribute (1556)
458 aResult = setInvalidIfFalse(aSelLab, aResult);
459 TopoDS_Shape aNewShape;
460 if (!aSelector.NamedShape().IsNull()) {
461 aNewShape = aSelector.NamedShape()->Get();
463 if (anOldShape.IsNull() || aNewShape.IsNull() ||
464 !anOldShape.IsEqual(aSelector.NamedShape()->Get())) // send updated if shape is changed
465 owner()->data()->sendAttributeUpdated(this);
467 } else if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
468 // construction: identification by the results indexes, recompute faces and
469 // take the face that more close by the indexes
470 ResultConstructionPtr aConstructionContext =
471 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
472 FeaturePtr aContextFeature = aContext->document()->feature(aContext);
473 // sketch sub-element
474 if (aConstructionContext &&
475 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
477 TDF_Label aLab = myRef.myRef->Label();
478 // getting a type of selected shape
479 Handle(TDataStd_Integer) aTypeAttr;
480 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
481 return setInvalidIfFalse(aSelLab, false);
483 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
484 // selected indexes will be needed in each "if"
485 Handle(TDataStd_IntPackedMap) aSubIds;
486 std::shared_ptr<GeomAPI_Shape> aNewSelected;
488 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
489 // for now working only with composite features
490 CompositeFeaturePtr aComposite =
491 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
492 if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
493 return setInvalidIfFalse(aSelLab, false);
496 if (aShapeType == TopAbs_FACE || aShapeType == TopAbs_WIRE) {
497 // compound is for the whole sketch selection
498 // If this is a wire with plane defined then it is a sketch-like object
499 if (!aConstructionContext->facesNum()) // no faces, update can not work correctly
500 return setInvalidIfFalse(aSelLab, false);
501 // if there is no edges indexes, any face can be used: take the first
502 std::shared_ptr<GeomAPI_Shape> aNewSelected;
504 aNewSelected = aConstructionContext->face(0);
505 } else { // searching for most looks-like initial face by the indexes
506 // prepare edges of the current result for the fast searching
507 // curves and orientations of edges
508 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
509 const int aSubNum = aComposite->numberOfSubs();
510 for(int a = 0; a < aSubNum; a++) {
511 int aSubID = aComposite->subFeatureId(a);
512 if (aSubIds->Contains(aSubID)) {
513 FeaturePtr aSub = aComposite->subFeature(a);
514 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
515 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
516 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
517 ResultConstructionPtr aConstr =
518 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
519 if (aConstr->shape() && aConstr->shape()->isEdge()) {
520 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
521 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
522 if (!anEdge.IsNull()) {
523 Standard_Real aFirst, aLast;
524 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
525 // searching for orientation information
527 Handle(TDataStd_Integer) anInt;
528 if (aSelLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)){
529 anOrient = anInt->Get();
531 allCurves.Bind(aCurve, anOrient);
537 aNewSelected = Model_SelectionNaming::findAppropriateFace(
538 aContext, allCurves, aShapeType == TopAbs_WIRE);
540 if (aNewSelected) { // store this new selection
541 selectConstruction(aContext, aNewSelected);
542 setInvalidIfFalse(aSelLab, true);
543 owner()->data()->sendAttributeUpdated(this);
546 // if the selection is not found, put the empty shape:
547 // it's better to have disappeared shape, than the old, the lost one
548 TNaming_Builder anEmptyBuilder(selectionLabel());
549 return setInvalidIfFalse(aSelLab, false);
551 } else if (aShapeType == TopAbs_EDGE) {
552 // just reselect the edge by the id
553 const int aSubNum = aComposite->numberOfSubs();
554 for(int a = 0; a < aSubNum; a++) {
555 // if aSubIds take any, the first appropriate
556 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
557 // found the appropriate feature
558 FeaturePtr aFeature = aComposite->subFeature(a);
559 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
560 aFeature->results().cbegin();
561 for(;aResIter != aFeature->results().cend(); aResIter++) {
562 ResultConstructionPtr aRes =
563 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
564 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
565 selectConstruction(aContext, aRes->shape());
566 setInvalidIfFalse(aSelLab, true);
567 owner()->data()->sendAttributeUpdated(this);
573 } else if (aShapeType == TopAbs_VERTEX) {
574 // just reselect the vertex by the id of edge
575 const int aSubNum = aComposite->numberOfSubs();
576 for(int a = 0; a < aSubNum; a++) {
577 // if aSubIds take any, the first appropriate
578 int aFeatureID = aComposite->subFeatureId(a);
579 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
580 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
581 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
582 // searching for deltas
584 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
585 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
586 // found the feature with appropriate edge
587 FeaturePtr aFeature = aComposite->subFeature(a);
588 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
589 aFeature->results().cbegin();
590 for(;aResIter != aFeature->results().cend(); aResIter++) {
591 ResultConstructionPtr aRes =
592 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
593 if (aRes && aRes->shape()) {
594 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
595 selectConstruction(aContext, aRes->shape());
596 setInvalidIfFalse(aSelLab, true);
597 owner()->data()->sendAttributeUpdated(this);
599 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
600 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
602 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
603 if (aVIndex == aVertexNum) { // found!
604 std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
605 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
606 selectConstruction(aContext, aVertex);
607 setInvalidIfFalse(aSelLab, true);
608 owner()->data()->sendAttributeUpdated(this);
619 } else { // simple construction element: the selected is that needed
620 selectConstruction(aContext, aContext->shape());
621 setInvalidIfFalse(aSelLab, true);
622 owner()->data()->sendAttributeUpdated(this);
626 return setInvalidIfFalse(aSelLab, false); // unknown case
629 void Model_AttributeSelection::selectBody(
630 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
632 // perform the selection
633 TNaming_Selector aSel(selectionLabel());
634 TopoDS_Shape aContext;
636 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
638 aContext = aBody->shape()->impl<TopoDS_Shape>();
641 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
643 aContext = aResult->shape()->impl<TopoDS_Shape>();
645 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
650 // with "recover" feature the selected context may be not up to date (issue 1710)
651 Handle(TNaming_NamedShape) aResult;
652 TDF_Label aSelLab = selectionLabel();
653 TopoDS_Shape aNewContext = aContext;
654 bool isUpdated = true;
655 while(!aNewContext.IsNull() && isUpdated) {
656 // searching for the very last shape that was produced from this one
658 if (!TNaming_Tool::HasLabel(aSelLab, aNewContext))
659 // to avoid crash of TNaming_SameShapeIterator if pure shape does not exists
661 for(TNaming_SameShapeIterator anIter(aNewContext, aSelLab); anIter.More(); anIter.Next()) {
662 TDF_Label aNSLab = anIter.Label();
663 if (!scope().Contains(aNSLab))
665 Handle(TNaming_NamedShape) aNS;
666 if (aNSLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
667 for(TNaming_Iterator aShapesIter(aNS); aShapesIter.More(); aShapesIter.Next()) {
668 if (aShapesIter.Evolution() == TNaming_SELECTED)
669 continue; // don't use the selection evolution
670 if (!aShapesIter.OldShape().IsNull() && aShapesIter.OldShape().IsSame(aNewContext)) {
671 // found the original shape
672 aNewContext = aShapesIter.NewShape(); // go to the newer shape
680 if (aNewContext.IsNull()) { // a context is already deleted
681 setInvalidIfFalse(aSelLab, false);
682 Events_InfoMessage("Model_AttributeSelection", "Failed to select shape already deleted").send();
686 TopoDS_Shape aNewSub = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
687 if (!aNewSub.IsEqual(aContext)) { // searching for subshape in the new context
688 bool isFound = false;
689 TopExp_Explorer anExp(aNewContext, aNewSub.ShapeType());
690 for(; anExp.More(); anExp.Next()) {
691 if (anExp.Current().IsEqual(aNewSub)) {
696 if (!isFound) { // sub-shape is not found in the up-to-date instance of the context shape
697 setInvalidIfFalse(aSelLab, false);
698 Events_InfoMessage("Model_AttributeSelection",
699 "Failed to select sub-shape already modified").send();
704 /// fix for issue 411: result modified shapes must not participate in this selection mechanism
705 if (!aContext.IsNull()) {
706 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
707 bool aEraseResults = false;
708 if (aFeatureOwner.get()) {
709 aEraseResults = !aFeatureOwner->results().empty();
710 if (aEraseResults) // erase results without flash deleted and redisplay: do it after Select
711 aFeatureOwner->removeResults(0, false);
713 aSel.Select(aNewSub, aNewContext);
715 if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
716 static Events_Loop* aLoop = Events_Loop::loop();
717 static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
718 aLoop->flush(kDeletedEvent);
723 /// registers the name of the shape in the label (theID == 0) of sub label (theID is a tag)
724 /// if theID is zero,
725 /// theOrientation is additional information about the positioning of edge relatively to face
726 /// it is stored in the integer attribute of the edge sub-label:
727 /// -1 is out, 1 is in, 0 is not needed
728 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape,
729 const int theID, const FeaturePtr& theContextFeature, std::shared_ptr<Model_Document> theDoc,
730 std::map<int, int>& theOrientations,
731 // name of sub-elements by ID to be exported instead of indexes
732 std::map<int, std::string>& theSubNames,
733 Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)(),
734 const int theOrientation = 0)
736 TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
737 if (theOrientation != 0) { // store the orientation of edge relatively to face if needed
738 TDataStd_Integer::Set(aLab, theOrientation);
740 TNaming_Builder aBuilder(aLab);
741 aBuilder.Generated(theShape);
742 std::stringstream aName;
743 // #1839 : do not store name of the feature in the tree, since this name could be changed
744 //aName<<theContextFeature->name();
745 if (theShape.ShapeType() != TopAbs_COMPOUND) { // compound means the whole result for construction
747 if (theShape.ShapeType() == TopAbs_FACE) aName<<"Face";
748 else if (theShape.ShapeType() == TopAbs_WIRE) aName<<"Wire";
749 else if (theShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
750 else if (theShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
752 if (theRefs.IsNull()) {
754 if (theOrientation == 1)
756 else if (theOrientation == -1)
758 } else { // make a composite name from all sub-elements indexes: "1_2_3_4"
759 TColStd_MapIteratorOfPackedMapOfInteger aRef(theRefs->GetMap());
760 for(; aRef.More(); aRef.Next()) {
761 aName<<"-"<<theSubNames[aRef.Key()];
762 if (theOrientations.find(aRef.Key()) != theOrientations.end()) {
763 if (theOrientations[aRef.Key()] == 1)
765 else if (theOrientations[aRef.Key()] == -1)
772 theDoc->addNamingName(aLab, aName.str());
773 TDataStd_Name::Set(aLab, aName.str().c_str());
776 void Model_AttributeSelection::selectConstruction(
777 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
779 std::shared_ptr<Model_Document> aMyDoc =
780 std::dynamic_pointer_cast<Model_Document>(owner()->document());
781 FeaturePtr aContextFeature = theContext->document()->feature(theContext);
782 CompositeFeaturePtr aComposite =
783 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
784 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
785 if (!aComposite || aComposite->numberOfSubs() == 0) {
786 // saving of context is enough: result construction contains exactly the needed shape
787 TNaming_Builder aBuilder(selectionLabel());
788 aBuilder.Generated(aSubShape);
789 //std::string aName = contextName(theContext);
790 //aMyDoc->addNamingName(selectionLabel(), aName);
791 //TDataStd_Name::Set(selectionLabel(), aName.c_str());
794 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(owner()->data());
795 TDF_Label aLab = myRef.myRef->Label();
796 // identify the results of sub-object of the composite by edges
797 // save type of the selected shape in integer attribute
798 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
799 TDataStd_Integer::Set(aLab, (int)aShapeType);
801 TColStd_MapOfTransient allCurves;
802 if (aShapeType == TopAbs_VERTEX) { // compare positions
803 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
805 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
806 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
807 Standard_Real aFirst, aLast;
808 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
809 allCurves.Add(aCurve);
812 // iterate and store the result ids of sub-elements and sub-elements to sub-labels
813 Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
814 std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
815 std::map<int, std::string> aSubNames; //map from edges IDs to names of edges
817 const int aSubNum = aComposite->numberOfSubs();
818 for(int a = 0; a < aSubNum; a++) {
819 FeaturePtr aSub = aComposite->subFeature(a);
820 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
821 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
822 // there may be many shapes (circle and center): register if at least one is in selection
823 for(; aRes != aResults.cend(); aRes++) {
824 ResultConstructionPtr aConstr =
825 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
826 if (!aConstr->shape()) {
829 if (aShapeType == TopAbs_VERTEX) {
830 if (aConstr->shape()->isVertex()) { // compare vertices positions
831 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
832 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
833 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
834 aRefs->Add(aComposite->subFeatureId(a));
835 aSubNames[aComposite->subFeatureId(a)] = Model_SelectionNaming::shortName(aConstr);
837 } else { // get first or last vertex of the edge: last is stored with additional delta
838 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
839 int aDelta = kSTART_VERTEX_DELTA;
840 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
841 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
842 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
843 aRefs->Add(aDelta + aComposite->subFeatureId(a));
844 aSubNames[aDelta + aComposite->subFeatureId(a)] =
845 Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
848 aDelta += kSTART_VERTEX_DELTA;
852 if (aConstr->shape()->isEdge()) {
853 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
854 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
855 if (!anEdge.IsNull()) {
856 Standard_Real aFirst, aLast;
857 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
858 if (allCurves.Contains(aCurve)) {
859 int anID = aComposite->subFeatureId(a);
861 aSubNames[anID] = Model_SelectionNaming::shortName(aConstr);
862 if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
863 // add edges to sub-label to support naming for edges selection
864 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
865 for(; anEdgeExp.More(); anEdgeExp.Next()) {
866 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
867 Standard_Real aFirst, aLast;
868 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
869 if (aFaceCurve == aCurve) {
870 int anOrient = Model_SelectionNaming::edgeOrientation(aSubShape, anEdge);
871 anOrientations[anID] = anOrient;
873 selectionLabel(), anEdge, anID, aContextFeature, aMyDoc, anOrientations,
874 aSubNames, Handle(TDataStd_IntPackedMap)(), anOrient);
877 } else { // put vertices of the selected edge to sub-labels
878 // add edges to sub-label to support naming for edges selection
879 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
880 int aTagIndex = anID + kSTART_VERTEX_DELTA;
881 for(; anEdgeExp.More(); anEdgeExp.Next(), aTagIndex += kSTART_VERTEX_DELTA) {
882 TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
884 std::stringstream anAdditionalName;
886 selectionLabel(), aV, aTagIndex, aContextFeature, aMyDoc, anOrientations,
896 // store the selected as primitive
897 TNaming_Builder aBuilder(selectionLabel());
898 aBuilder.Generated(aSubShape);
900 selectionLabel(), aSubShape, 0, aContextFeature, aMyDoc, anOrientations, aSubNames, aRefs);
903 bool Model_AttributeSelection::selectPart(
904 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
905 const bool theUpdate)
907 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
908 if (!aPart.get() || !aPart->isActivated())
909 return true; // postponed naming
911 Handle(TDataStd_Integer) anIndex;
912 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
913 // by internal selection
914 if (anIndex->Get() > 0) {
915 // update the selection by index
916 return aPart->updateInPart(anIndex->Get());
918 return true; // nothing to do, referencing just by name
921 return true; // nothing to do, referencing just by name
923 // store the shape (in case part is not loaded it should be useful
925 std::string aName = theContext->data()->name();
926 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
927 aShape = theContext->shape()->impl<TopoDS_Shape>();
929 aShape = theSubShape->impl<TopoDS_Shape>();
931 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
932 TDataStd_Integer::Set(selectionLabel(), anIndex);
934 TNaming_Builder aBuilder(selectionLabel());
935 aBuilder.Select(aShape, aShape);
936 // identify by name in the part
937 TDataStd_Name::Set(selectionLabel(), aName.c_str());
938 return !aName.empty();
941 TDF_Label Model_AttributeSelection::selectionLabel()
943 return myRef.myRef->Label().FindChild(1);
946 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
948 std::string aName("");
949 if(!this->isInitialized())
950 return !theDefaultName.empty() ? theDefaultName : aName;
952 std::shared_ptr<GeomAPI_Shape> aSubSh = value();
953 ResultPtr aCont = context();
955 Model_SelectionNaming aSelNaming(selectionLabel());
956 return aSelNaming.namingName(
957 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
960 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
961 void Model_AttributeSelection::selectSubShape(
962 const std::string& theType, const std::string& theSubShapeName)
964 if(theSubShapeName.empty() || theType.empty()) return;
966 // check this is Part-name: 2 delimiters in the name
967 std::size_t aPartEnd = theSubShapeName.find('/');
968 if (aPartEnd != std::string::npos && aPartEnd != theSubShapeName.rfind('/')) {
969 std::string aPartName = theSubShapeName.substr(0, aPartEnd);
970 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
971 if (aFound.get()) { // found such part, so asking it for the name
972 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
973 std::string aNameInPart = theSubShapeName.substr(aPartEnd + 1);
975 std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, theType, anIndex);
976 if (aSelected.get()) {
977 setValue(aPart, aSelected);
978 TDataStd_Integer::Set(selectionLabel(), anIndex);
984 Model_SelectionNaming aSelNaming(selectionLabel());
985 std::shared_ptr<Model_Document> aDoc =
986 std::dynamic_pointer_cast<Model_Document>(owner()->document());
987 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
989 if (aSelNaming.selectSubShape(theType, theSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
990 // try to find the last context to find the up to date shape
991 if (aCont->shape().get() && !aCont->shape()->isNull() &&
992 aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
993 const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
994 if (!aConShape.IsNull()) {
995 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
997 aNS = TNaming_Tool::CurrentNamedShape(aNS);
999 TDF_Label aLab = aNS->Label();
1000 while(aLab.Depth() != 7 && aLab.Depth() > 5)
1001 aLab = aLab.Father();
1002 ObjectPtr anObj = aDoc->objects()->object(aLab);
1004 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1012 setValue(aCont, aShapeToBeSelected);
1016 int Model_AttributeSelection::Id()
1019 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1020 std::shared_ptr<GeomAPI_Shape> aContext = context()->shape();
1021 // support for compsolids:
1022 if (context().get() && ModelAPI_Tools::compSolidOwner(context()).get())
1023 aContext = ModelAPI_Tools::compSolidOwner(context())->shape();
1026 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1027 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1028 // searching for the latest main shape
1029 if (aSelection && !aSelection->isNull() &&
1030 aContext && !aContext->isNull())
1032 std::shared_ptr<Model_Document> aDoc =
1033 std::dynamic_pointer_cast<Model_Document>(context()->document());
1035 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1036 if (!aNS.IsNull()) {
1037 aMainShape = TNaming_Tool::CurrentShape(aNS);
1041 TopTools_IndexedMapOfShape aSubShapesMap;
1042 TopExp::MapShapes(aMainShape, aSubShapesMap);
1043 anID = aSubShapesMap.FindIndex(aSubShape);
1048 void Model_AttributeSelection::setId(int theID)
1050 const ResultPtr& aContext = context();
1051 std::shared_ptr<GeomAPI_Shape> aSelection;
1053 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
1054 // support for compsolids:
1055 if (aContext.get() && ModelAPI_Tools::compSolidOwner(aContext).get())
1056 aContextShape = ModelAPI_Tools::compSolidOwner(aContext)->shape();
1058 TopoDS_Shape aMainShape = aContextShape->impl<TopoDS_Shape>();
1059 // searching for the latest main shape
1061 aContextShape && !aContextShape->isNull())
1063 std::shared_ptr<Model_Document> aDoc =
1064 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1066 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1067 if (!aNS.IsNull()) {
1068 aMainShape = TNaming_Tool::CurrentShape(aNS);
1072 TopTools_IndexedMapOfShape aSubShapesMap;
1073 TopExp::MapShapes(aMainShape, aSubShapesMap);
1074 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1076 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1077 aResult->setImpl(new TopoDS_Shape(aSelShape));
1079 aSelection = aResult;
1082 setValue(aContext, aSelection);
1085 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1087 std::string aResult;
1088 if (owner()->document() != theContext->document()) {
1089 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1090 aResult = theContext->document()->kind() + "/";
1092 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1093 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1094 if (aDocRes.get()) {
1095 aResult = aDocRes->data()->name() + "/";
1099 aResult += theContext->data()->name();
1103 void Model_AttributeSelection::updateInHistory()
1105 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1106 // only bodies may be modified later in the history, don't do anything otherwise
1107 if (!aContext.get() || aContext->groupName() != ModelAPI_ResultBody::group())
1109 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1110 if (!aContData.get() || !aContData->isValid())
1112 TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
1113 Handle(TNaming_NamedShape) aContNS;
1114 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS))
1116 std::shared_ptr<Model_Document> aDoc =
1117 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1118 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1119 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1120 // iterate the context shape modifications in order to find a feature that is upper in history
1121 // that this one and is really modifies the referenced result to refer to it
1122 ResultPtr aModifierResFound;
1123 TNaming_Iterator aPairIter(aContNS);
1124 TopoDS_Shape aNewShape = aPairIter.NewShape();
1125 bool anIterate = true;
1126 // trying to update also the sub-shape selected
1127 GeomShapePtr aSubShape = value();
1128 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1133 TNaming_SameShapeIterator aModifIter(aNewShape, aContLab);
1134 for(; aModifIter.More(); aModifIter.Next()) {
1135 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1136 (aDoc->objects()->object(aModifIter.Label().Father()));
1137 if (!aModifierObj.get())
1139 FeaturePtr aModifierFeat = aDoc->feature(aModifierObj);
1140 if (!aModifierFeat.get())
1142 if (aModifierFeat == aThisFeature || aDoc->objects()->isLater(aModifierFeat, aThisFeature))
1143 continue; // the modifier feature is later than this, so, should not be used
1144 if (aCurrentModifierFeat == aModifierFeat ||
1145 aDoc->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
1146 continue; // the current modifier is later than the found, so, useless
1147 Handle(TNaming_NamedShape) aNewNS;
1148 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1149 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1150 aModifierResFound = aModifierObj;
1151 aCurrentModifierFeat = aModifierFeat;
1152 TNaming_Iterator aPairIter(aNewNS);
1153 aNewShape = aPairIter.NewShape();
1155 // searching for sub-shape equivalent on the sub-label of the new context result
1156 TDF_ChildIDIterator aNSIter(aNewNS->Label(), TNaming_NamedShape::GetID());
1157 for(; aNSIter.More(); aNSIter.Next()) {
1158 TNaming_Iterator aPairsIter(aNSIter.Value()->Label());
1159 for(; aPairsIter.More(); aPairsIter.Next()) {
1160 if (aSubShape->impl<TopoDS_Shape>().IsEqual()
1165 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is null
1166 ResultPtr anEmptyContext;
1167 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1168 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1170 } else { // not-precessed modification => don't support it
1176 TNaming_NewShapeIterator aModifIter(aPairIter.NewShape(), aContLab);
1177 if (aModifIter.More()) aModifIter.Next(); // skip this shape result
1178 for(; aModifIter.More(); aModifIter.Next()) {
1179 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1180 (aDoc->objects()->object(aModifIter.Label().Father()));
1181 if (!aModifierObj.get())
1183 FeaturePtr aModifierFeat = aDoc->feature(aModifierObj);
1184 if (!aModifierFeat.get())
1186 if (aModifierFeat == aThisFeature || aDoc->objects()->isLater(aModifierFeat, aThisFeature))
1187 break; // the modifier feature is later than this, so, should not be used
1188 Handle(TNaming_NamedShape) aNewNS = aModifIter.NamedShape();
1189 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1190 aModifierResFound = aModifierObj;
1191 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is null
1192 ResultPtr anEmptyContext;
1193 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1194 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1196 } else { // not-precessed modification => don't support it
1200 // already found what is needed, don't iterate the next pair since normally
1201 if (aModifierResFound.get()) // there must be only one pair in the result-shape
1205 if (aModifierResFound.get()) {
1206 // update scope to reset to a new one
1208 myRef.setValue(aModifierResFound);
1209 update(); // it must recompute a new sub-shape automatically
1212 if (aModifierResFound.get()) {
1213 // update scope to reset to a new one
1215 if (!aSubShape.get() || aSubShape->isNull()) { // no sub-shape, so, just update a context
1216 setValue(aModifierResFound, aSubShape);
1219 // seaching for the same sub-shape: the old topology stays the same
1220 TopoDS_Shape anOldShape = aSubShape->impl<TopoDS_Shape>();
1221 TopAbs_ShapeEnum aSubType = anOldShape.ShapeType();
1222 TopoDS_Shape aNewContext = aModifierResFound->shape()->impl<TopoDS_Shape>();
1223 TopExp_Explorer anExp(aNewContext, aSubType);
1224 for(; anExp.More(); anExp.Next()) {
1225 if (anExp.Current().IsEqual(anOldShape))
1228 if (anExp.More()) { // found
1229 setValue(aModifierResFound, aSubShape);
1232 // seaching for the same sub-shape: equal geometry
1233 for(anExp.Init(aNewContext, aSubType); anExp.More(); anExp.Next()) {
1234 if (aSubType == TopAbs_VERTEX) {
1239 // if sub-shape selection exists, search also sub-shape new instance
1241 GeomShapePtr aSubShape = value();
1242 if (aSubShape.get() && aSubShape != aContext->shape()) {