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 <Model_AttributeSelectionList.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_ResultBody.h>
17 #include <ModelAPI_ResultConstruction.h>
18 #include <ModelAPI_ResultPart.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Tools.h>
21 #include <GeomAPI_Shape.h>
22 #include <ModelAPI_Session.h>
23 #include <GeomAPI_PlanarEdges.h>
24 #include <Events_InfoMessage.h>
26 #include <TNaming_Selector.hxx>
27 #include <TNaming_NamedShape.hxx>
28 #include <TNaming_Tool.hxx>
29 #include <TNaming_Builder.hxx>
30 #include <TNaming_Localizer.hxx>
31 #include <TNaming_SameShapeIterator.hxx>
32 #include <TNaming_Iterator.hxx>
33 #include <TNaming_NewShapeIterator.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TDataStd_IntPackedMap.hxx>
37 #include <TDataStd_Integer.hxx>
38 #include <TDataStd_UAttribute.hxx>
39 #include <TDataStd_Name.hxx>
40 #include <TopTools_MapOfShape.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <TopTools_MapIteratorOfMapOfShape.hxx>
43 #include <TopTools_ListOfShape.hxx>
44 #include <NCollection_DataMap.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TDF_LabelMap.hxx>
47 #include <BRep_Tool.hxx>
48 #include <BRep_Builder.hxx>
49 #include <TopoDS_Edge.hxx>
52 #include <TColStd_MapOfTransient.hxx>
53 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
57 #include <Precision.hxx>
58 #include <TDF_ChildIterator.hxx>
59 #include <TDF_ChildIDIterator.hxx>
60 #include <TDataStd_Name.hxx>
61 #include <TopAbs_ShapeEnum.hxx>
62 #include <TopoDS_Iterator.hxx>
63 #include <BRep_Builder.hxx>
65 //#define DEB_NAMING 1
67 #include <BRepTools.hxx>
69 /// added to the index in the packed map to signalize that the vertex of edge is selected
70 /// (multiplied by the index of the edge)
71 static const int kSTART_VERTEX_DELTA = 1000000;
72 // identifier that there is simple reference: selection equals to context
73 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
74 // simple reference in the construction
75 Standard_GUID kCONSTUCTION_SIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb28");
76 // reference to Part sub-object
77 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
78 // selection is invalid after recomputation
79 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
81 // on this label is stored:
82 // TNaming_NamedShape - selected shape
83 // TNaming_Naming - topological selection information (for the body)
84 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
85 // TDataStd_Integer - type of the selected shape (for construction)
86 // TDF_Reference - from ReferenceAttribute, the context
87 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
88 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
90 if (theTemporarily) { // just keep the stored without DF update
91 myTmpContext = theContext;
92 myTmpSubShape = theSubShape;
93 owner()->data()->sendAttributeUpdated(this);
97 myTmpSubShape.reset();
100 const std::shared_ptr<GeomAPI_Shape>& anOldShape = value();
101 bool isOldContext = theContext == myRef.value();
102 bool isOldShape = isOldContext &&
103 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
104 if (isOldShape) return; // shape is the same, so context is also unchanged
105 // update the referenced object if needed
107 myRef.setValue(theContext);
110 // do noth use naming if selected shape is result shape itself, but not sub-shape
111 TDF_Label aSelLab = selectionLabel();
112 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
113 aSelLab.ForgetAttribute(kCONSTUCTION_SIMPLE_REF_ID);
114 aSelLab.ForgetAttribute(kINVALID_SELECTION);
116 bool isDegeneratedEdge = false;
117 // do not use the degenerated edge as a shape, a null context and shape is used in the case
118 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
119 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
120 if (aSubShape.ShapeType() == TopAbs_EDGE)
121 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
123 if (!theContext.get() || isDegeneratedEdge) {
124 // to keep the reference attribute label
125 TDF_Label aRefLab = myRef.myRef->Label();
126 aSelLab.ForgetAllAttributes(true);
127 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
130 if (theContext->groupName() == ModelAPI_ResultBody::group()) {
131 // do not select the whole shape for body:it is already must be in the data framework
132 // equal and null selected objects mean the same: object is equal to context,
133 if (theContext->shape().get() &&
134 (theContext->shape()->isEqual(theSubShape) || !theSubShape.get())) {
135 aSelLab.ForgetAllAttributes(true);
136 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
138 selectBody(theContext, theSubShape);
140 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
141 if (!theSubShape.get()) {
142 // to sub, so the whole result is selected
143 aSelLab.ForgetAllAttributes(true);
144 TDataStd_UAttribute::Set(aSelLab, kCONSTUCTION_SIMPLE_REF_ID);
145 ResultConstructionPtr aConstruction =
146 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
147 if (aConstruction->isInfinite()) {
148 // For correct naming selection, put the shape into the naming structure.
149 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
150 TNaming_Builder aBuilder(aSelLab);
151 aBuilder.Generated(theContext->shape()->impl<TopoDS_Shape>());
152 std::shared_ptr<Model_Document> aMyDoc =
153 std::dynamic_pointer_cast<Model_Document>(owner()->document());
154 //std::string aName = contextName(theContext);
155 // for selection in different document, add the document name
156 //aMyDoc->addNamingName(aSelLab, aName);
157 //TDataStd_Name::Set(aSelLab, aName.c_str());
158 } else { // for sketch the naming is needed in DS
159 BRep_Builder aCompoundBuilder;
160 TopoDS_Compound aComp;
161 aCompoundBuilder.MakeCompound(aComp);
162 for(int a = 0; a < aConstruction->facesNum(); a++) {
163 TopoDS_Shape aFace = aConstruction->face(a)->impl<TopoDS_Shape>();
164 aCompoundBuilder.Add(aComp, aFace);
166 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
167 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
168 selectConstruction(theContext, aShape);
171 selectConstruction(theContext, theSubShape);
173 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
174 aSelLab.ForgetAllAttributes(true);
175 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
176 selectPart(theContext, theSubShape);
178 //the attribute initialized state should be changed by sendAttributeUpdated only
179 //myIsInitialized = true;
181 owner()->data()->sendAttributeUpdated(this);
184 void Model_AttributeSelection::removeTemporaryValues()
186 if (myTmpContext.get() || myTmpSubShape.get()) {
187 myTmpContext.reset();
188 myTmpSubShape.reset();
192 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
194 GeomShapePtr aResult;
195 if (myTmpContext.get() || myTmpSubShape.get()) {
196 ResultConstructionPtr aResulConstruction =
197 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
198 if(aResulConstruction.get()) {
199 // it is just reference to construction.
200 return myTmpSubShape;
202 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
205 TDF_Label aSelLab = selectionLabel();
206 if (aSelLab.IsAttribute(kINVALID_SELECTION))
209 if (myRef.isInitialized()) {
210 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
211 ResultPtr aContext = context();
213 return aResult; // empty result
214 return aContext->shape();
216 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) {
217 // it is just reference to construction, nothing is in value
218 return aResult; // empty result
220 if (aSelLab.IsAttribute(kPART_REF_ID)) {
221 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
222 if (!aPart.get() || !aPart->isActivated())
223 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
224 Handle(TDataStd_Integer) anIndex;
225 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
226 if (anIndex->Get()) { // special selection attribute was created, use it
227 return aPart->selectionValue(anIndex->Get());
228 } else { // face with name is already in the data model, so try to take it by name
229 Handle(TDataStd_Name) aName;
230 if (selectionLabel().FindAttribute(TDataStd_Name::GetID(), aName)) {
231 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
232 std::size_t aPartEnd = aSubShapeName.find('/');
233 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
234 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
236 std::string aType; // to reuse already existing selection the type is not needed
237 return aPart->shapeInPart(aNameInPart, aType, anIndex);
244 Handle(TNaming_NamedShape) aSelection;
245 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
246 TopoDS_Shape aSelShape = aSelection->Get();
247 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
248 aResult->setImpl(new TopoDS_Shape(aSelShape));
249 } else { // for simple construction element: just shape of this construction element
250 ResultConstructionPtr aConstr =
251 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
253 return aConstr->shape();
260 bool Model_AttributeSelection::isInvalid()
262 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
265 bool Model_AttributeSelection::isInitialized()
267 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
268 std::shared_ptr<GeomAPI_Shape> aResult;
269 if (myRef.isInitialized()) {
270 TDF_Label aSelLab = selectionLabel();
271 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
272 ResultPtr aContext = context();
273 return aContext.get() != NULL;
275 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) {
276 // it is just reference to construction, nothing is in value
280 Handle(TNaming_NamedShape) aSelection;
281 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
282 return !aSelection->Get().IsNull();
283 } else { // for simple construction element: just shape of this construction element
284 ResultConstructionPtr aConstr =
285 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
287 return aConstr->shape().get() != NULL;
295 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
298 myIsInitialized = myRef.isInitialized();
302 void Model_AttributeSelection::setID(const std::string theID)
305 ModelAPI_AttributeSelection::setID(theID);
308 ResultPtr Model_AttributeSelection::context() {
309 if (myTmpContext.get() || myTmpSubShape.get()) {
313 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
314 // for parts there could be same-data result, so take the last enabled
315 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group()) {
316 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
317 for(int a = aSize - 1; a >= 0; a--) {
318 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
319 if (aPart.get() && aPart->data() == aResult->data()) {
320 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
321 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
322 // check that this result is not this-feature result (it is forbidden t oselect itself)
323 if (anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
333 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
335 ModelAPI_AttributeSelection::setObject(theObject);
336 myRef.setObject(theObject);
339 TDF_LabelMap& Model_AttributeSelection::scope()
341 if (myScope.IsEmpty()) { // create a new scope if not yet done
342 // gets all features with named shapes that are before this feature label (before in history)
343 DocumentPtr aMyDoc = owner()->document();
344 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
345 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
346 bool aMePassed = false;
347 CompositeFeaturePtr aComposite =
348 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(owner());
349 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
350 CompositeFeaturePtr aCompositeOwner, aCompositeOwnerOwner;
351 if (aFeature.get()) {
352 aCompositeOwner = ModelAPI_Tools::compositeOwner(aFeature);
353 if (aCompositeOwner.get()) {
354 aCompositeOwnerOwner = ModelAPI_Tools::compositeOwner(aCompositeOwner);
357 // for group Scope is not limitet: this is always up to date objects
358 bool isGroup = aFeature.get() && aFeature->getKind() == "Group";
359 for(; aFIter != allFeatures.end(); aFIter++) {
360 if (*aFIter == owner()) { // the left features are created later (except subs of composite)
364 if (isGroup) aMePassed = false;
365 bool isInScope = !aMePassed;
366 if (!isInScope && aComposite.get()) {
367 // try to add sub-elements of composite if this is composite
368 if (aComposite->isSub(*aFIter))
371 // remove the composite-owner of this feature (sketch in extrusion-cut)
372 if (isInScope && (aCompositeOwner == *aFIter || aCompositeOwnerOwner == *aFIter))
375 if (isInScope && aFIter->get() && (*aFIter)->data()->isValid()) {
376 TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
377 (*aFIter)->data())->label().Father();
378 TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), 1);
379 for(; aNSIter.More(); aNSIter.Next()) {
380 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
381 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
382 myScope.Add(aNS->Label());
391 /// Sets the invalid flag if flag is false, or removes it if "true"
393 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
395 theLab.ForgetAttribute(kINVALID_SELECTION);
397 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
402 bool Model_AttributeSelection::update()
404 TDF_Label aSelLab = selectionLabel();
405 ResultPtr aContext = context();
407 return setInvalidIfFalse(aSelLab, false);
408 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
409 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
411 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) {
412 // it is just reference to construction, not sub-shape
413 // if there is a sketch, the sketch-naming must be updated
414 ResultConstructionPtr aConstruction =
415 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
416 if (!aConstruction->isInfinite()) {
417 BRep_Builder aCompoundBuilder;
418 TopoDS_Compound aComp;
419 aCompoundBuilder.MakeCompound(aComp);
420 for(int a = 0; a < aConstruction->facesNum(); a++) {
421 TopoDS_Shape aFace = aConstruction->face(a)->impl<TopoDS_Shape>();
422 aCompoundBuilder.Add(aComp, aFace);
424 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
425 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
426 selectConstruction(aContext, aShape);
428 // For correct naming selection, put the shape into the naming structure.
429 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
430 TNaming_Builder aBuilder(aSelLab);
431 aBuilder.Generated(aContext->shape()->impl<TopoDS_Shape>());
432 std::shared_ptr<Model_Document> aMyDoc =
433 std::dynamic_pointer_cast<Model_Document>(owner()->document());
435 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
438 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
439 std::shared_ptr<GeomAPI_Shape> aNoSelection;
440 bool aResult = selectPart(aContext, aNoSelection, true);
441 aResult = setInvalidIfFalse(aSelLab, aResult);
443 owner()->data()->sendAttributeUpdated(this);
448 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
449 // body: just a named shape, use selection mechanism from OCCT
450 TNaming_Selector aSelector(aSelLab);
451 TopoDS_Shape anOldShape;
452 if (!aSelector.NamedShape().IsNull()) {
453 anOldShape = aSelector.NamedShape()->Get();
455 bool aResult = aSelector.Solve(scope()) == Standard_True;
456 // must be before sending of updated attribute (1556)
457 aResult = setInvalidIfFalse(aSelLab, aResult);
458 TopoDS_Shape aNewShape;
459 if (!aSelector.NamedShape().IsNull()) {
460 aNewShape = aSelector.NamedShape()->Get();
462 if (anOldShape.IsNull() || aNewShape.IsNull() ||
463 !anOldShape.IsEqual(aSelector.NamedShape()->Get())) {
464 // shape type shoud not not changed: if shape becomes compound of such shapes, then split
465 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
466 anOldShape.ShapeType() != aNewShape.ShapeType() &&
467 aNewShape.ShapeType() == TopAbs_COMPOUND) {
468 TopTools_ListOfShape aSubs;
469 for(TopoDS_Iterator anExplorer(aNewShape); anExplorer.More(); anExplorer.Next()) {
470 if (!anExplorer.Value().IsNull() &&
471 anExplorer.Value().ShapeType() == anOldShape.ShapeType()) {
472 aSubs.Append(anExplorer.Value());
473 } else { // invalid case; bad result shape, so, impossible to split easily
478 if (aSubs.Extent() > 1) { // ok to split
479 TopTools_ListIteratorOfListOfShape aSub(aSubs);
480 GeomShapePtr aSubSh(new GeomAPI_Shape);
481 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
482 setValue(aContext, aSubSh);
483 for(aSub.Next(); aSub.More(); aSub.Next()) {
484 GeomShapePtr aSubSh(new GeomAPI_Shape);
485 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
486 myParent->append(aContext, aSubSh);
490 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
495 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
496 // construction: identification by the results indexes, recompute faces and
497 // take the face that more close by the indexes
498 ResultConstructionPtr aConstructionContext =
499 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
500 FeaturePtr aContextFeature = aContext->document()->feature(aContext);
501 // sketch sub-element
502 if (aConstructionContext &&
503 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
505 TDF_Label aLab = myRef.myRef->Label();
506 // getting a type of selected shape
507 Handle(TDataStd_Integer) aTypeAttr;
508 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
509 return setInvalidIfFalse(aSelLab, false);
511 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
512 // selected indexes will be needed in each "if"
513 Handle(TDataStd_IntPackedMap) aSubIds;
514 std::shared_ptr<GeomAPI_Shape> aNewSelected;
516 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
517 // for now working only with composite features
518 CompositeFeaturePtr aComposite =
519 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
520 if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
521 return setInvalidIfFalse(aSelLab, false);
524 if (aShapeType == TopAbs_FACE || aShapeType == TopAbs_WIRE) {
525 // compound is for the whole sketch selection
526 // If this is a wire with plane defined then it is a sketch-like object
527 if (!aConstructionContext->facesNum()) // no faces, update can not work correctly
528 return setInvalidIfFalse(aSelLab, false);
529 // if there is no edges indexes, any face can be used: take the first
530 std::shared_ptr<GeomAPI_Shape> aNewSelected;
532 aNewSelected = aConstructionContext->face(0);
533 } else { // searching for most looks-like initial face by the indexes
534 // prepare edges of the current result for the fast searching
535 // curves and orientations of edges
536 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
537 const int aSubNum = aComposite->numberOfSubs();
538 for(int a = 0; a < aSubNum; a++) {
539 int aSubID = aComposite->subFeatureId(a);
540 if (aSubIds->Contains(aSubID)) {
541 FeaturePtr aSub = aComposite->subFeature(a);
542 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
543 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
544 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
545 ResultConstructionPtr aConstr =
546 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
547 if (aConstr->shape() && aConstr->shape()->isEdge()) {
548 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
549 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
550 if (!anEdge.IsNull()) {
551 Standard_Real aFirst, aLast;
552 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
553 // searching for orientation information
555 Handle(TDataStd_Integer) anInt;
556 if (aSelLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)){
557 anOrient = anInt->Get();
559 allCurves.Bind(aCurve, anOrient);
565 aNewSelected = Model_SelectionNaming::findAppropriateFace(
566 aContext, allCurves, aShapeType == TopAbs_WIRE);
568 if (aNewSelected) { // store this new selection
569 selectConstruction(aContext, aNewSelected);
570 setInvalidIfFalse(aSelLab, true);
571 owner()->data()->sendAttributeUpdated(this);
574 // if the selection is not found, put the empty shape:
575 // it's better to have disappeared shape, than the old, the lost one
576 TNaming_Builder anEmptyBuilder(selectionLabel());
577 return setInvalidIfFalse(aSelLab, false);
579 } else if (aShapeType == TopAbs_EDGE) {
580 // just reselect the edge by the id
581 const int aSubNum = aComposite->numberOfSubs();
582 for(int a = 0; a < aSubNum; a++) {
583 // if aSubIds take any, the first appropriate
584 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
585 // found the appropriate feature
586 FeaturePtr aFeature = aComposite->subFeature(a);
587 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
588 aFeature->results().cbegin();
589 for(;aResIter != aFeature->results().cend(); aResIter++) {
590 ResultConstructionPtr aRes =
591 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
592 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
593 selectConstruction(aContext, aRes->shape());
594 setInvalidIfFalse(aSelLab, true);
595 owner()->data()->sendAttributeUpdated(this);
601 } else if (aShapeType == TopAbs_VERTEX) {
602 // just reselect the vertex by the id of edge
603 const int aSubNum = aComposite->numberOfSubs();
604 for(int a = 0; a < aSubNum; a++) {
605 // if aSubIds take any, the first appropriate
606 int aFeatureID = aComposite->subFeatureId(a);
607 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
608 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
609 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
610 // searching for deltas
612 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
613 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
614 // found the feature with appropriate edge
615 FeaturePtr aFeature = aComposite->subFeature(a);
616 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
617 aFeature->results().cbegin();
618 for(;aResIter != aFeature->results().cend(); aResIter++) {
619 ResultConstructionPtr aRes =
620 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
621 if (aRes && aRes->shape()) {
622 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
623 selectConstruction(aContext, aRes->shape());
624 setInvalidIfFalse(aSelLab, true);
625 owner()->data()->sendAttributeUpdated(this);
627 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
628 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
630 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
631 if (aVIndex == aVertexNum) { // found!
632 std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
633 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
634 selectConstruction(aContext, aVertex);
635 setInvalidIfFalse(aSelLab, true);
636 owner()->data()->sendAttributeUpdated(this);
647 } else { // simple construction element: the selected is that needed
648 selectConstruction(aContext, aContext->shape());
649 setInvalidIfFalse(aSelLab, true);
650 owner()->data()->sendAttributeUpdated(this);
654 return setInvalidIfFalse(aSelLab, false); // unknown case
657 void Model_AttributeSelection::selectBody(
658 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
660 // perform the selection
661 TNaming_Selector aSel(selectionLabel());
662 TopoDS_Shape aContext;
664 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);//myRef.value()
666 aContext = aBody->shape()->impl<TopoDS_Shape>();
669 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
671 aContext = aResult->shape()->impl<TopoDS_Shape>();
673 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
678 // with "recover" feature the selected context may be not up to date (issue 1710)
679 Handle(TNaming_NamedShape) aResult;
680 TDF_Label aSelLab = selectionLabel();
681 TopoDS_Shape aNewContext = aContext;
682 bool isUpdated = true;
683 while(!aNewContext.IsNull() && isUpdated) {
684 // searching for the very last shape that was produced from this one
686 if (!TNaming_Tool::HasLabel(aSelLab, aNewContext))
687 // to avoid crash of TNaming_SameShapeIterator if pure shape does not exists
689 for(TNaming_SameShapeIterator anIter(aNewContext, aSelLab); anIter.More(); anIter.Next()) {
690 TDF_Label aNSLab = anIter.Label();
691 if (!scope().Contains(aNSLab))
693 Handle(TNaming_NamedShape) aNS;
694 if (aNSLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
695 for(TNaming_Iterator aShapesIter(aNS); aShapesIter.More(); aShapesIter.Next()) {
696 if (aShapesIter.Evolution() == TNaming_SELECTED)
697 continue; // don't use the selection evolution
698 if (!aShapesIter.OldShape().IsNull() && aShapesIter.OldShape().IsSame(aNewContext)) {
699 // found the original shape
700 aNewContext = aShapesIter.NewShape(); // go to the newer shape
708 if (aNewContext.IsNull()) { // a context is already deleted
709 setInvalidIfFalse(aSelLab, false);
710 Events_InfoMessage("Model_AttributeSelection", "Failed to select shape already deleted").send();
714 TopoDS_Shape aNewSub = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
715 if (!aNewSub.IsEqual(aContext)) { // searching for subshape in the new context
716 bool isFound = false;
717 TopExp_Explorer anExp(aNewContext, aNewSub.ShapeType());
718 for(; anExp.More(); anExp.Next()) {
719 if (anExp.Current().IsEqual(aNewSub)) {
724 if (!isFound) { // sub-shape is not found in the up-to-date instance of the context shape
725 // if context is sub-result of compound/compsolid, selection of sub-shape better propagate to
726 // the main result (which is may be modified), case is in 1799
727 ResultCompSolidPtr aMain = ModelAPI_Tools::compSolidOwner(theContext);
729 selectBody(aMain, theSubShape);
732 setInvalidIfFalse(aSelLab, false);
733 Events_InfoMessage("Model_AttributeSelection",
734 "Failed to select sub-shape already modified").send();
739 /// fix for issue 411: result modified shapes must not participate in this selection mechanism
740 if (!aContext.IsNull()) {
741 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
742 bool aEraseResults = false;
743 if (aFeatureOwner.get()) {
744 aEraseResults = !aFeatureOwner->results().empty();
745 if (aEraseResults) // erase results without flash deleted and redisplay: do it after Select
746 aFeatureOwner->removeResults(0, false);
748 aSel.Select(aNewSub, aNewContext);
750 if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
751 static Events_Loop* aLoop = Events_Loop::loop();
752 static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
753 aLoop->flush(kDeletedEvent);
758 /// registers the name of the shape in the label (theID == 0) of sub label (theID is a tag)
759 /// if theID is zero,
760 /// theOrientation is additional information about the positioning of edge relatively to face
761 /// it is stored in the integer attribute of the edge sub-label:
762 /// -1 is out, 1 is in, 0 is not needed
763 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape,
764 const int theID, const FeaturePtr& theContextFeature, std::shared_ptr<Model_Document> theDoc,
765 std::map<int, int>& theOrientations,
766 // name of sub-elements by ID to be exported instead of indexes
767 std::map<int, std::string>& theSubNames,
768 Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)(),
769 const int theOrientation = 0)
771 TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
772 if (theOrientation != 0) { // store the orientation of edge relatively to face if needed
773 TDataStd_Integer::Set(aLab, theOrientation);
775 TNaming_Builder aBuilder(aLab);
776 aBuilder.Generated(theShape);
777 std::stringstream aName;
778 // #1839 : do not store name of the feature in the tree, since this name could be changed
779 //aName<<theContextFeature->name();
780 if (theShape.ShapeType() != TopAbs_COMPOUND) { // compound means the whole result for construction
782 if (theShape.ShapeType() == TopAbs_FACE) aName<<"Face";
783 else if (theShape.ShapeType() == TopAbs_WIRE) aName<<"Wire";
784 else if (theShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
785 else if (theShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
787 if (theRefs.IsNull()) {
789 if (theOrientation == 1)
791 else if (theOrientation == -1)
793 } else { // make a composite name from all sub-elements indexes: "1_2_3_4"
794 TColStd_MapIteratorOfPackedMapOfInteger aRef(theRefs->GetMap());
795 for(; aRef.More(); aRef.Next()) {
796 aName<<"-"<<theSubNames[aRef.Key()];
797 if (theOrientations.find(aRef.Key()) != theOrientations.end()) {
798 if (theOrientations[aRef.Key()] == 1)
800 else if (theOrientations[aRef.Key()] == -1)
807 theDoc->addNamingName(aLab, aName.str());
808 TDataStd_Name::Set(aLab, aName.str().c_str());
811 void Model_AttributeSelection::selectConstruction(
812 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
814 std::shared_ptr<Model_Document> aMyDoc =
815 std::dynamic_pointer_cast<Model_Document>(owner()->document());
816 FeaturePtr aContextFeature = theContext->document()->feature(theContext);
817 CompositeFeaturePtr aComposite =
818 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
819 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
820 if (!aComposite || aComposite->numberOfSubs() == 0) {
821 // saving of context is enough: result construction contains exactly the needed shape
822 TNaming_Builder aBuilder(selectionLabel());
823 aBuilder.Generated(aSubShape);
824 //std::string aName = contextName(theContext);
825 //aMyDoc->addNamingName(selectionLabel(), aName);
826 //TDataStd_Name::Set(selectionLabel(), aName.c_str());
829 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(owner()->data());
830 TDF_Label aLab = myRef.myRef->Label();
831 // identify the results of sub-object of the composite by edges
832 // save type of the selected shape in integer attribute
833 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
834 TDataStd_Integer::Set(aLab, (int)aShapeType);
836 TColStd_MapOfTransient allCurves;
837 if (aShapeType == TopAbs_VERTEX) { // compare positions
838 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
840 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
841 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
842 Standard_Real aFirst, aLast;
843 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
844 allCurves.Add(aCurve);
847 // iterate and store the result ids of sub-elements and sub-elements to sub-labels
848 Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
849 std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
850 std::map<int, std::string> aSubNames; //map from edges IDs to names of edges
852 const int aSubNum = aComposite->numberOfSubs();
853 for(int a = 0; a < aSubNum; a++) {
854 FeaturePtr aSub = aComposite->subFeature(a);
855 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
856 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
857 // there may be many shapes (circle and center): register if at least one is in selection
858 for(; aRes != aResults.cend(); aRes++) {
859 ResultConstructionPtr aConstr =
860 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
861 if (!aConstr->shape()) {
864 if (aShapeType == TopAbs_VERTEX) {
865 if (aConstr->shape()->isVertex()) { // compare vertices positions
866 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
867 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
868 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
869 aRefs->Add(aComposite->subFeatureId(a));
870 aSubNames[aComposite->subFeatureId(a)] = Model_SelectionNaming::shortName(aConstr);
872 } else { // get first or last vertex of the edge: last is stored with additional delta
873 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
874 int aDelta = kSTART_VERTEX_DELTA;
875 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
876 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
877 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
878 aRefs->Add(aDelta + aComposite->subFeatureId(a));
879 aSubNames[aDelta + aComposite->subFeatureId(a)] =
880 Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
883 aDelta += kSTART_VERTEX_DELTA;
887 if (aConstr->shape()->isEdge()) {
888 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
889 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
890 if (!anEdge.IsNull()) {
891 Standard_Real aFirst, aLast;
892 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
893 if (allCurves.Contains(aCurve)) {
894 int anID = aComposite->subFeatureId(a);
896 aSubNames[anID] = Model_SelectionNaming::shortName(aConstr);
897 if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
898 // add edges to sub-label to support naming for edges selection
899 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
900 for(; anEdgeExp.More(); anEdgeExp.Next()) {
901 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
902 Standard_Real aFirst, aLast;
903 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
904 if (aFaceCurve == aCurve) {
905 int anOrient = Model_SelectionNaming::edgeOrientation(aSubShape, anEdge);
906 anOrientations[anID] = anOrient;
908 selectionLabel(), anEdge, anID, aContextFeature, aMyDoc, anOrientations,
909 aSubNames, Handle(TDataStd_IntPackedMap)(), anOrient);
912 } else { // put vertices of the selected edge to sub-labels
913 // add edges to sub-label to support naming for edges selection
914 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
915 int aTagIndex = anID + kSTART_VERTEX_DELTA;
916 for(; anEdgeExp.More(); anEdgeExp.Next(), aTagIndex += kSTART_VERTEX_DELTA) {
917 TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
919 std::stringstream anAdditionalName;
921 selectionLabel(), aV, aTagIndex, aContextFeature, aMyDoc, anOrientations,
931 // store the selected as primitive
932 TNaming_Builder aBuilder(selectionLabel());
933 aBuilder.Generated(aSubShape);
935 selectionLabel(), aSubShape, 0, aContextFeature, aMyDoc, anOrientations, aSubNames, aRefs);
938 bool Model_AttributeSelection::selectPart(
939 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
940 const bool theUpdate)
942 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
943 if (!aPart.get() || !aPart->isActivated())
944 return true; // postponed naming
946 Handle(TDataStd_Integer) anIndex;
947 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
948 // by internal selection
949 if (anIndex->Get() > 0) {
950 // update the selection by index
951 return aPart->updateInPart(anIndex->Get());
953 return true; // nothing to do, referencing just by name
956 return true; // nothing to do, referencing just by name
958 // store the shape (in case part is not loaded it should be useful
960 std::string aName = theContext->data()->name();
961 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
962 aShape = theContext->shape()->impl<TopoDS_Shape>();
964 aShape = theSubShape->impl<TopoDS_Shape>();
966 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
967 TDataStd_Integer::Set(selectionLabel(), anIndex);
969 TNaming_Builder aBuilder(selectionLabel());
970 aBuilder.Select(aShape, aShape);
971 // identify by name in the part
972 TDataStd_Name::Set(selectionLabel(), aName.c_str());
973 return !aName.empty();
976 TDF_Label Model_AttributeSelection::selectionLabel()
978 return myRef.myRef->Label().FindChild(1);
981 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
983 std::string aName("");
984 if(!this->isInitialized())
985 return !theDefaultName.empty() ? theDefaultName : aName;
987 std::shared_ptr<GeomAPI_Shape> aSubSh = value();
988 ResultPtr aCont = context();
990 Model_SelectionNaming aSelNaming(selectionLabel());
991 return aSelNaming.namingName(
992 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
995 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
996 void Model_AttributeSelection::selectSubShape(
997 const std::string& theType, const std::string& theSubShapeName)
999 if(theSubShapeName.empty() || theType.empty()) return;
1001 // check this is Part-name: 2 delimiters in the name
1002 std::size_t aPartEnd = theSubShapeName.find('/');
1003 if (aPartEnd != std::string::npos && aPartEnd != theSubShapeName.rfind('/')) {
1004 std::string aPartName = theSubShapeName.substr(0, aPartEnd);
1005 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
1006 if (aFound.get()) { // found such part, so asking it for the name
1007 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
1008 std::string aNameInPart = theSubShapeName.substr(aPartEnd + 1);
1010 std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, theType, anIndex);
1011 if (aSelected.get()) {
1012 setValue(aPart, aSelected);
1013 TDataStd_Integer::Set(selectionLabel(), anIndex);
1019 Model_SelectionNaming aSelNaming(selectionLabel());
1020 std::shared_ptr<Model_Document> aDoc =
1021 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1022 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
1024 if (aSelNaming.selectSubShape(theType, theSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
1025 // try to find the last context to find the up to date shape
1026 if (aCont->shape().get() && !aCont->shape()->isNull() &&
1027 aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
1028 const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
1029 if (!aConShape.IsNull()) {
1030 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1031 if (!aNS.IsNull()) {
1032 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1033 if (!aNS.IsNull()) {
1034 TDF_Label aLab = aNS->Label();
1035 while(aLab.Depth() != 7 && aLab.Depth() > 5)
1036 aLab = aLab.Father();
1037 ObjectPtr anObj = aDoc->objects()->object(aLab);
1039 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1047 setValue(aCont, aShapeToBeSelected);
1051 int Model_AttributeSelection::Id()
1054 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1055 std::shared_ptr<GeomAPI_Shape> aContext = context()->shape();
1056 // support for compsolids:
1057 if (context().get() && ModelAPI_Tools::compSolidOwner(context()).get())
1058 aContext = ModelAPI_Tools::compSolidOwner(context())->shape();
1061 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1062 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1063 // searching for the latest main shape
1064 if (aSelection && !aSelection->isNull() &&
1065 aContext && !aContext->isNull())
1067 std::shared_ptr<Model_Document> aDoc =
1068 std::dynamic_pointer_cast<Model_Document>(context()->document());
1070 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1071 if (!aNS.IsNull()) {
1072 aMainShape = TNaming_Tool::CurrentShape(aNS);
1076 TopTools_IndexedMapOfShape aSubShapesMap;
1077 TopExp::MapShapes(aMainShape, aSubShapesMap);
1078 anID = aSubShapesMap.FindIndex(aSubShape);
1083 void Model_AttributeSelection::setId(int theID)
1085 const ResultPtr& aContext = context();
1086 std::shared_ptr<GeomAPI_Shape> aSelection;
1088 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
1089 // support for compsolids:
1090 if (aContext.get() && ModelAPI_Tools::compSolidOwner(aContext).get())
1091 aContextShape = ModelAPI_Tools::compSolidOwner(aContext)->shape();
1093 TopoDS_Shape aMainShape = aContextShape->impl<TopoDS_Shape>();
1094 // searching for the latest main shape
1096 aContextShape && !aContextShape->isNull())
1098 std::shared_ptr<Model_Document> aDoc =
1099 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1101 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1102 if (!aNS.IsNull()) {
1103 aMainShape = TNaming_Tool::CurrentShape(aNS);
1107 TopTools_IndexedMapOfShape aSubShapesMap;
1108 TopExp::MapShapes(aMainShape, aSubShapesMap);
1109 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1111 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1112 aResult->setImpl(new TopoDS_Shape(aSelShape));
1114 aSelection = aResult;
1117 setValue(aContext, aSelection);
1120 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1122 std::string aResult;
1123 if (owner()->document() != theContext->document()) {
1124 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1125 aResult = theContext->document()->kind() + "/";
1127 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1128 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1129 if (aDocRes.get()) {
1130 aResult = aDocRes->data()->name() + "/";
1134 aResult += theContext->data()->name();
1138 void Model_AttributeSelection::updateInHistory()
1140 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1141 // only bodies may be modified later in the history, don't do anything otherwise
1142 if (!aContext.get() || aContext->groupName() != ModelAPI_ResultBody::group())
1144 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1145 if (!aContData.get() || !aContData->isValid())
1147 TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
1148 Handle(TNaming_NamedShape) aContNS;
1149 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS))
1151 std::shared_ptr<Model_Document> aDoc =
1152 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1153 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1154 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1155 // iterate the context shape modifications in order to find a feature that is upper in history
1156 // that this one and is really modifies the referenced result to refer to it
1157 ResultPtr aModifierResFound;
1158 TNaming_Iterator aPairIter(aContNS);
1159 TopoDS_Shape aNewShape = aPairIter.NewShape();
1160 bool anIterate = true;
1161 // trying to update also the sub-shape selected
1162 GeomShapePtr aSubShape = value();
1163 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1168 TNaming_SameShapeIterator aModifIter(aNewShape, aContLab);
1169 for(; aModifIter.More(); aModifIter.Next()) {
1170 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1171 (aDoc->objects()->object(aModifIter.Label().Father()));
1172 if (!aModifierObj.get())
1174 FeaturePtr aModifierFeat = aDoc->feature(aModifierObj);
1175 if (!aModifierFeat.get())
1177 if (aModifierFeat == aThisFeature || aDoc->objects()->isLater(aModifierFeat, aThisFeature))
1178 continue; // the modifier feature is later than this, so, should not be used
1179 if (aCurrentModifierFeat == aModifierFeat ||
1180 aDoc->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
1181 continue; // the current modifier is later than the found, so, useless
1182 Handle(TNaming_NamedShape) aNewNS;
1183 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1184 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1185 aModifierResFound = aModifierObj;
1186 aCurrentModifierFeat = aModifierFeat;
1187 TNaming_Iterator aPairIter(aNewNS);
1188 aNewShape = aPairIter.NewShape();
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
1201 if (aModifierResFound.get()) {
1202 // update scope to reset to a new one
1204 myRef.setValue(aModifierResFound);
1205 update(); // it must recompute a new sub-shape automatically
1209 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1211 myParent = theParent;