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 void Model_AttributeSelection::split(
403 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
405 TopTools_ListOfShape aSubs;
406 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
407 if (!anExplorer.Value().IsNull() &&
408 anExplorer.Value().ShapeType() == theType) {
409 aSubs.Append(anExplorer.Value());
410 } else { // invalid case; bad result shape, so, impossible to split easily
415 if (aSubs.Extent() > 1) { // ok to split
416 TopTools_ListIteratorOfListOfShape aSub(aSubs);
417 GeomShapePtr aSubSh(new GeomAPI_Shape);
418 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
419 setValue(theContext, aSubSh);
420 for(aSub.Next(); aSub.More(); aSub.Next()) {
421 GeomShapePtr aSubSh(new GeomAPI_Shape);
422 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
423 myParent->append(theContext, aSubSh);
428 bool Model_AttributeSelection::update()
430 TDF_Label aSelLab = selectionLabel();
431 ResultPtr aContext = context();
433 return setInvalidIfFalse(aSelLab, false);
434 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
435 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
437 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) {
438 // it is just reference to construction, not sub-shape
439 // if there is a sketch, the sketch-naming must be updated
440 ResultConstructionPtr aConstruction =
441 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
442 if (!aConstruction->isInfinite()) {
443 BRep_Builder aCompoundBuilder;
444 TopoDS_Compound aComp;
445 aCompoundBuilder.MakeCompound(aComp);
446 for(int a = 0; a < aConstruction->facesNum(); a++) {
447 TopoDS_Shape aFace = aConstruction->face(a)->impl<TopoDS_Shape>();
448 aCompoundBuilder.Add(aComp, aFace);
450 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
451 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aComp));
452 selectConstruction(aContext, aShape);
454 // For correct naming selection, put the shape into the naming structure.
455 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
456 TNaming_Builder aBuilder(aSelLab);
457 aBuilder.Generated(aContext->shape()->impl<TopoDS_Shape>());
458 std::shared_ptr<Model_Document> aMyDoc =
459 std::dynamic_pointer_cast<Model_Document>(owner()->document());
461 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
464 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
465 std::shared_ptr<GeomAPI_Shape> aNoSelection;
466 bool aResult = selectPart(aContext, aNoSelection, true);
467 aResult = setInvalidIfFalse(aSelLab, aResult);
469 owner()->data()->sendAttributeUpdated(this);
474 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
475 // body: just a named shape, use selection mechanism from OCCT
476 TNaming_Selector aSelector(aSelLab);
477 TopoDS_Shape anOldShape;
478 if (!aSelector.NamedShape().IsNull()) {
479 anOldShape = aSelector.NamedShape()->Get();
481 bool aResult = aSelector.Solve(scope()) == Standard_True;
482 // must be before sending of updated attribute (1556)
483 aResult = setInvalidIfFalse(aSelLab, aResult);
484 TopoDS_Shape aNewShape;
485 if (!aSelector.NamedShape().IsNull()) {
486 aNewShape = aSelector.NamedShape()->Get();
488 if (anOldShape.IsNull() || aNewShape.IsNull() ||
489 !anOldShape.IsEqual(aSelector.NamedShape()->Get())) {
490 // shape type shoud not not changed: if shape becomes compound of such shapes, then split
491 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
492 anOldShape.ShapeType() != aNewShape.ShapeType() &&
493 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
495 split(aContext, aNewShape, anOldShape.ShapeType());
497 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
502 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
503 // construction: identification by the results indexes, recompute faces and
504 // take the face that more close by the indexes
505 ResultConstructionPtr aConstructionContext =
506 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
507 FeaturePtr aContextFeature = aContext->document()->feature(aContext);
508 // sketch sub-element
509 if (aConstructionContext &&
510 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
512 TDF_Label aLab = myRef.myRef->Label();
513 // getting a type of selected shape
514 Handle(TDataStd_Integer) aTypeAttr;
515 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
516 return setInvalidIfFalse(aSelLab, false);
518 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
519 // selected indexes will be needed in each "if"
520 Handle(TDataStd_IntPackedMap) aSubIds;
521 std::shared_ptr<GeomAPI_Shape> aNewSelected;
523 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
524 // for now working only with composite features
525 CompositeFeaturePtr aComposite =
526 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
527 if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
528 return setInvalidIfFalse(aSelLab, false);
531 if (aShapeType == TopAbs_FACE || aShapeType == TopAbs_WIRE) {
532 // compound is for the whole sketch selection
533 // If this is a wire with plane defined then it is a sketch-like object
534 if (!aConstructionContext->facesNum()) // no faces, update can not work correctly
535 return setInvalidIfFalse(aSelLab, false);
536 // if there is no edges indexes, any face can be used: take the first
537 std::shared_ptr<GeomAPI_Shape> aNewSelected;
539 aNewSelected = aConstructionContext->face(0);
540 } else { // searching for most looks-like initial face by the indexes
541 // prepare edges of the current result for the fast searching
542 // curves and orientations of edges
543 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
544 const int aSubNum = aComposite->numberOfSubs();
545 for(int a = 0; a < aSubNum; a++) {
546 int aSubID = aComposite->subFeatureId(a);
547 if (aSubIds->Contains(aSubID)) {
548 FeaturePtr aSub = aComposite->subFeature(a);
549 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
550 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
551 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
552 ResultConstructionPtr aConstr =
553 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
554 if (aConstr->shape() && aConstr->shape()->isEdge()) {
555 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
556 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
557 if (!anEdge.IsNull()) {
558 Standard_Real aFirst, aLast;
559 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
560 // searching for orientation information
562 Handle(TDataStd_Integer) anInt;
563 if (aSelLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)){
564 anOrient = anInt->Get();
566 allCurves.Bind(aCurve, anOrient);
572 aNewSelected = Model_SelectionNaming::findAppropriateFace(
573 aContext, allCurves, aShapeType == TopAbs_WIRE);
575 if (aNewSelected) { // store this new selection
576 selectConstruction(aContext, aNewSelected);
577 setInvalidIfFalse(aSelLab, true);
578 owner()->data()->sendAttributeUpdated(this);
581 // if the selection is not found, put the empty shape:
582 // it's better to have disappeared shape, than the old, the lost one
583 TNaming_Builder anEmptyBuilder(selectionLabel());
584 return setInvalidIfFalse(aSelLab, false);
586 } else if (aShapeType == TopAbs_EDGE) {
587 // just reselect the edge by the id
588 const int aSubNum = aComposite->numberOfSubs();
589 for(int a = 0; a < aSubNum; a++) {
590 // if aSubIds take any, the first appropriate
591 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
592 // found the appropriate feature
593 FeaturePtr aFeature = aComposite->subFeature(a);
594 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
595 aFeature->results().cbegin();
596 for(;aResIter != aFeature->results().cend(); aResIter++) {
597 ResultConstructionPtr aRes =
598 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
599 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
600 selectConstruction(aContext, aRes->shape());
601 setInvalidIfFalse(aSelLab, true);
602 owner()->data()->sendAttributeUpdated(this);
608 } else if (aShapeType == TopAbs_VERTEX) {
609 // just reselect the vertex by the id of edge
610 const int aSubNum = aComposite->numberOfSubs();
611 for(int a = 0; a < aSubNum; a++) {
612 // if aSubIds take any, the first appropriate
613 int aFeatureID = aComposite->subFeatureId(a);
614 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
615 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
616 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
617 // searching for deltas
619 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
620 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
621 // found the feature with appropriate edge
622 FeaturePtr aFeature = aComposite->subFeature(a);
623 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
624 aFeature->results().cbegin();
625 for(;aResIter != aFeature->results().cend(); aResIter++) {
626 ResultConstructionPtr aRes =
627 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
628 if (aRes && aRes->shape()) {
629 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
630 selectConstruction(aContext, aRes->shape());
631 setInvalidIfFalse(aSelLab, true);
632 owner()->data()->sendAttributeUpdated(this);
634 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
635 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
637 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
638 if (aVIndex == aVertexNum) { // found!
639 std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
640 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
641 selectConstruction(aContext, aVertex);
642 setInvalidIfFalse(aSelLab, true);
643 owner()->data()->sendAttributeUpdated(this);
654 } else { // simple construction element: the selected is that needed
655 selectConstruction(aContext, aContext->shape());
656 setInvalidIfFalse(aSelLab, true);
657 owner()->data()->sendAttributeUpdated(this);
661 return setInvalidIfFalse(aSelLab, false); // unknown case
664 void Model_AttributeSelection::selectBody(
665 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
667 // perform the selection
668 TNaming_Selector aSel(selectionLabel());
669 TopoDS_Shape aContext;
671 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);//myRef.value()
673 aContext = aBody->shape()->impl<TopoDS_Shape>();
676 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
678 aContext = aResult->shape()->impl<TopoDS_Shape>();
680 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
685 // with "recover" feature the selected context may be not up to date (issue 1710)
686 Handle(TNaming_NamedShape) aResult;
687 TDF_Label aSelLab = selectionLabel();
688 TopoDS_Shape aNewContext = aContext;
689 bool isUpdated = true;
690 while(!aNewContext.IsNull() && isUpdated) {
691 // searching for the very last shape that was produced from this one
693 if (!TNaming_Tool::HasLabel(aSelLab, aNewContext))
694 // to avoid crash of TNaming_SameShapeIterator if pure shape does not exists
696 for(TNaming_SameShapeIterator anIter(aNewContext, aSelLab); anIter.More(); anIter.Next()) {
697 TDF_Label aNSLab = anIter.Label();
698 if (!scope().Contains(aNSLab))
700 Handle(TNaming_NamedShape) aNS;
701 if (aNSLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
702 for(TNaming_Iterator aShapesIter(aNS); aShapesIter.More(); aShapesIter.Next()) {
703 if (aShapesIter.Evolution() == TNaming_SELECTED)
704 continue; // don't use the selection evolution
705 if (!aShapesIter.OldShape().IsNull() && aShapesIter.OldShape().IsSame(aNewContext)) {
706 // found the original shape
707 aNewContext = aShapesIter.NewShape(); // go to the newer shape
715 if (aNewContext.IsNull()) { // a context is already deleted
716 setInvalidIfFalse(aSelLab, false);
717 Events_InfoMessage("Model_AttributeSelection", "Failed to select shape already deleted").send();
721 TopoDS_Shape aNewSub = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
722 if (!aNewSub.IsEqual(aContext)) { // searching for subshape in the new context
723 bool isFound = false;
724 TopExp_Explorer anExp(aNewContext, aNewSub.ShapeType());
725 for(; anExp.More(); anExp.Next()) {
726 if (anExp.Current().IsEqual(aNewSub)) {
731 if (!isFound) { // sub-shape is not found in the up-to-date instance of the context shape
732 // if context is sub-result of compound/compsolid, selection of sub-shape better propagate to
733 // the main result (which is may be modified), case is in 1799
734 ResultCompSolidPtr aMain = ModelAPI_Tools::compSolidOwner(theContext);
736 selectBody(aMain, theSubShape);
739 setInvalidIfFalse(aSelLab, false);
740 Events_InfoMessage("Model_AttributeSelection",
741 "Failed to select sub-shape already modified").send();
746 /// fix for issue 411: result modified shapes must not participate in this selection mechanism
747 if (!aContext.IsNull()) {
748 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
749 bool aEraseResults = false;
750 if (aFeatureOwner.get()) {
751 aEraseResults = !aFeatureOwner->results().empty();
752 if (aEraseResults) // erase results without flash deleted and redisplay: do it after Select
753 aFeatureOwner->removeResults(0, false);
755 aSel.Select(aNewSub, aNewContext);
757 if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
758 static Events_Loop* aLoop = Events_Loop::loop();
759 static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
760 aLoop->flush(kDeletedEvent);
765 /// registers the name of the shape in the label (theID == 0) of sub label (theID is a tag)
766 /// if theID is zero,
767 /// theOrientation is additional information about the positioning of edge relatively to face
768 /// it is stored in the integer attribute of the edge sub-label:
769 /// -1 is out, 1 is in, 0 is not needed
770 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape,
771 const int theID, const FeaturePtr& theContextFeature, std::shared_ptr<Model_Document> theDoc,
772 std::map<int, int>& theOrientations,
773 // name of sub-elements by ID to be exported instead of indexes
774 std::map<int, std::string>& theSubNames,
775 Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)(),
776 const int theOrientation = 0)
778 TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
779 if (theOrientation != 0) { // store the orientation of edge relatively to face if needed
780 TDataStd_Integer::Set(aLab, theOrientation);
782 TNaming_Builder aBuilder(aLab);
783 aBuilder.Generated(theShape);
784 std::stringstream aName;
785 // #1839 : do not store name of the feature in the tree, since this name could be changed
786 //aName<<theContextFeature->name();
787 if (theShape.ShapeType() != TopAbs_COMPOUND) { // compound means the whole result for construction
789 if (theShape.ShapeType() == TopAbs_FACE) aName<<"Face";
790 else if (theShape.ShapeType() == TopAbs_WIRE) aName<<"Wire";
791 else if (theShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
792 else if (theShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
794 if (theRefs.IsNull()) {
796 if (theOrientation == 1)
798 else if (theOrientation == -1)
800 } else { // make a composite name from all sub-elements indexes: "1_2_3_4"
801 TColStd_MapIteratorOfPackedMapOfInteger aRef(theRefs->GetMap());
802 for(; aRef.More(); aRef.Next()) {
803 aName<<"-"<<theSubNames[aRef.Key()];
804 if (theOrientations.find(aRef.Key()) != theOrientations.end()) {
805 if (theOrientations[aRef.Key()] == 1)
807 else if (theOrientations[aRef.Key()] == -1)
814 theDoc->addNamingName(aLab, aName.str());
815 TDataStd_Name::Set(aLab, aName.str().c_str());
818 void Model_AttributeSelection::selectConstruction(
819 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
821 std::shared_ptr<Model_Document> aMyDoc =
822 std::dynamic_pointer_cast<Model_Document>(owner()->document());
823 FeaturePtr aContextFeature = theContext->document()->feature(theContext);
824 CompositeFeaturePtr aComposite =
825 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
826 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
827 if (!aComposite || aComposite->numberOfSubs() == 0) {
828 // saving of context is enough: result construction contains exactly the needed shape
829 TNaming_Builder aBuilder(selectionLabel());
830 aBuilder.Generated(aSubShape);
831 //std::string aName = contextName(theContext);
832 //aMyDoc->addNamingName(selectionLabel(), aName);
833 //TDataStd_Name::Set(selectionLabel(), aName.c_str());
836 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(owner()->data());
837 TDF_Label aLab = myRef.myRef->Label();
838 // identify the results of sub-object of the composite by edges
839 // save type of the selected shape in integer attribute
840 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
841 TDataStd_Integer::Set(aLab, (int)aShapeType);
843 TColStd_MapOfTransient allCurves;
844 if (aShapeType == TopAbs_VERTEX) { // compare positions
845 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
847 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
848 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
849 Standard_Real aFirst, aLast;
850 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
851 allCurves.Add(aCurve);
854 // iterate and store the result ids of sub-elements and sub-elements to sub-labels
855 Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
856 std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
857 std::map<int, std::string> aSubNames; //map from edges IDs to names of edges
859 const int aSubNum = aComposite->numberOfSubs();
860 for(int a = 0; a < aSubNum; a++) {
861 FeaturePtr aSub = aComposite->subFeature(a);
862 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
863 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
864 // there may be many shapes (circle and center): register if at least one is in selection
865 for(; aRes != aResults.cend(); aRes++) {
866 ResultConstructionPtr aConstr =
867 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
868 if (!aConstr->shape()) {
871 if (aShapeType == TopAbs_VERTEX) {
872 if (aConstr->shape()->isVertex()) { // compare vertices positions
873 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
874 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
875 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
876 aRefs->Add(aComposite->subFeatureId(a));
877 aSubNames[aComposite->subFeatureId(a)] = Model_SelectionNaming::shortName(aConstr);
879 } else { // get first or last vertex of the edge: last is stored with additional delta
880 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
881 int aDelta = kSTART_VERTEX_DELTA;
882 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
883 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
884 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
885 aRefs->Add(aDelta + aComposite->subFeatureId(a));
886 aSubNames[aDelta + aComposite->subFeatureId(a)] =
887 Model_SelectionNaming::shortName(aConstr, aDelta / kSTART_VERTEX_DELTA);
890 aDelta += kSTART_VERTEX_DELTA;
894 if (aConstr->shape()->isEdge()) {
895 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
896 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
897 if (!anEdge.IsNull()) {
898 Standard_Real aFirst, aLast;
899 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
900 if (allCurves.Contains(aCurve)) {
901 int anID = aComposite->subFeatureId(a);
903 aSubNames[anID] = Model_SelectionNaming::shortName(aConstr);
904 if (aShapeType != TopAbs_EDGE) { // face needs the sub-edges on sub-labels
905 // add edges to sub-label to support naming for edges selection
906 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
907 for(; anEdgeExp.More(); anEdgeExp.Next()) {
908 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
909 Standard_Real aFirst, aLast;
910 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
911 if (aFaceCurve == aCurve) {
912 int anOrient = Model_SelectionNaming::edgeOrientation(aSubShape, anEdge);
913 anOrientations[anID] = anOrient;
915 selectionLabel(), anEdge, anID, aContextFeature, aMyDoc, anOrientations,
916 aSubNames, Handle(TDataStd_IntPackedMap)(), anOrient);
919 } else { // put vertices of the selected edge to sub-labels
920 // add edges to sub-label to support naming for edges selection
921 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
922 int aTagIndex = anID + kSTART_VERTEX_DELTA;
923 for(; anEdgeExp.More(); anEdgeExp.Next(), aTagIndex += kSTART_VERTEX_DELTA) {
924 TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
926 std::stringstream anAdditionalName;
928 selectionLabel(), aV, aTagIndex, aContextFeature, aMyDoc, anOrientations,
938 // store the selected as primitive
940 selectionLabel(), aSubShape, 0, aContextFeature, aMyDoc, anOrientations, aSubNames, aRefs);
943 bool Model_AttributeSelection::selectPart(
944 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
945 const bool theUpdate)
947 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
948 if (!aPart.get() || !aPart->isActivated())
949 return true; // postponed naming
951 Handle(TDataStd_Integer) anIndex;
952 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
953 // by internal selection
954 if (anIndex->Get() > 0) {
955 // update the selection by index
956 return aPart->updateInPart(anIndex->Get());
958 return true; // nothing to do, referencing just by name
961 return true; // nothing to do, referencing just by name
963 // store the shape (in case part is not loaded it should be useful
965 std::string aName = theContext->data()->name();
966 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
967 aShape = theContext->shape()->impl<TopoDS_Shape>();
969 aShape = theSubShape->impl<TopoDS_Shape>();
971 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
972 TDataStd_Integer::Set(selectionLabel(), anIndex);
974 TNaming_Builder aBuilder(selectionLabel());
975 aBuilder.Select(aShape, aShape);
976 // identify by name in the part
977 TDataStd_Name::Set(selectionLabel(), aName.c_str());
978 return !aName.empty();
981 TDF_Label Model_AttributeSelection::selectionLabel()
983 return myRef.myRef->Label().FindChild(1);
986 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
988 std::string aName("");
989 if(!this->isInitialized())
990 return !theDefaultName.empty() ? theDefaultName : aName;
992 std::shared_ptr<GeomAPI_Shape> aSubSh = value();
993 ResultPtr aCont = context();
995 Model_SelectionNaming aSelNaming(selectionLabel());
996 return aSelNaming.namingName(
997 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
1000 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
1001 void Model_AttributeSelection::selectSubShape(
1002 const std::string& theType, const std::string& theSubShapeName)
1004 if(theSubShapeName.empty() || theType.empty()) return;
1006 // check this is Part-name: 2 delimiters in the name
1007 std::size_t aPartEnd = theSubShapeName.find('/');
1008 if (aPartEnd != std::string::npos && aPartEnd != theSubShapeName.rfind('/')) {
1009 std::string aPartName = theSubShapeName.substr(0, aPartEnd);
1010 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
1011 if (aFound.get()) { // found such part, so asking it for the name
1012 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
1013 std::string aNameInPart = theSubShapeName.substr(aPartEnd + 1);
1015 std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, theType, anIndex);
1016 if (aSelected.get()) {
1017 setValue(aPart, aSelected);
1018 TDataStd_Integer::Set(selectionLabel(), anIndex);
1024 Model_SelectionNaming aSelNaming(selectionLabel());
1025 std::shared_ptr<Model_Document> aDoc =
1026 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1027 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
1029 if (aSelNaming.selectSubShape(theType, theSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
1030 // try to find the last context to find the up to date shape
1031 if (aCont->shape().get() && !aCont->shape()->isNull() &&
1032 aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
1033 const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
1034 if (!aConShape.IsNull()) {
1035 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1036 if (!aNS.IsNull()) {
1037 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1038 if (!aNS.IsNull()) {
1039 TDF_Label aLab = aNS->Label();
1040 while(aLab.Depth() != 7 && aLab.Depth() > 5)
1041 aLab = aLab.Father();
1042 ObjectPtr anObj = aDoc->objects()->object(aLab);
1044 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1052 setValue(aCont, aShapeToBeSelected);
1056 int Model_AttributeSelection::Id()
1059 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1060 std::shared_ptr<GeomAPI_Shape> aContext = context()->shape();
1061 // support for compsolids:
1062 if (context().get() && ModelAPI_Tools::compSolidOwner(context()).get())
1063 aContext = ModelAPI_Tools::compSolidOwner(context())->shape();
1066 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1067 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1068 // searching for the latest main shape
1069 if (aSelection && !aSelection->isNull() &&
1070 aContext && !aContext->isNull())
1072 std::shared_ptr<Model_Document> aDoc =
1073 std::dynamic_pointer_cast<Model_Document>(context()->document());
1075 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1076 if (!aNS.IsNull()) {
1077 aMainShape = TNaming_Tool::CurrentShape(aNS);
1081 TopTools_IndexedMapOfShape aSubShapesMap;
1082 TopExp::MapShapes(aMainShape, aSubShapesMap);
1083 anID = aSubShapesMap.FindIndex(aSubShape);
1088 void Model_AttributeSelection::setId(int theID)
1090 const ResultPtr& aContext = context();
1091 std::shared_ptr<GeomAPI_Shape> aSelection;
1093 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
1094 // support for compsolids:
1095 if (aContext.get() && ModelAPI_Tools::compSolidOwner(aContext).get())
1096 aContextShape = ModelAPI_Tools::compSolidOwner(aContext)->shape();
1098 TopoDS_Shape aMainShape = aContextShape->impl<TopoDS_Shape>();
1099 // searching for the latest main shape
1101 aContextShape && !aContextShape->isNull())
1103 std::shared_ptr<Model_Document> aDoc =
1104 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1106 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1107 if (!aNS.IsNull()) {
1108 aMainShape = TNaming_Tool::CurrentShape(aNS);
1112 TopTools_IndexedMapOfShape aSubShapesMap;
1113 TopExp::MapShapes(aMainShape, aSubShapesMap);
1114 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1116 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1117 aResult->setImpl(new TopoDS_Shape(aSelShape));
1119 aSelection = aResult;
1122 setValue(aContext, aSelection);
1125 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1127 std::string aResult;
1128 if (owner()->document() != theContext->document()) {
1129 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1130 aResult = theContext->document()->kind() + "/";
1132 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1133 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1134 if (aDocRes.get()) {
1135 aResult = aDocRes->data()->name() + "/";
1139 aResult += theContext->data()->name();
1143 void Model_AttributeSelection::updateInHistory()
1145 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1146 // only bodies may be modified later in the history, don't do anything otherwise
1147 if (!aContext.get() || aContext->groupName() != ModelAPI_ResultBody::group())
1149 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1150 if (!aContData.get() || !aContData->isValid())
1152 TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
1153 Handle(TNaming_NamedShape) aContNS;
1154 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS))
1156 std::shared_ptr<Model_Document> aDoc =
1157 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1158 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1159 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1160 // iterate the context shape modifications in order to find a feature that is upper in history
1161 // that this one and is really modifies the referenced result to refer to it
1162 ResultPtr aModifierResFound;
1163 TNaming_Iterator aPairIter(aContNS);
1164 if (!aPairIter.More())
1166 TopoDS_Shape aNewShape = aPairIter.NewShape();
1167 bool anIterate = true;
1168 // trying to update also the sub-shape selected
1169 GeomShapePtr aSubShape = value();
1170 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1175 TNaming_SameShapeIterator aModifIter(aNewShape, aContLab);
1176 for(; aModifIter.More(); aModifIter.Next()) {
1177 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1178 (aDoc->objects()->object(aModifIter.Label().Father()));
1179 if (!aModifierObj.get())
1181 FeaturePtr aModifierFeat = aDoc->feature(aModifierObj);
1182 if (!aModifierFeat.get())
1184 if (aModifierFeat == aThisFeature || aDoc->objects()->isLater(aModifierFeat, aThisFeature))
1185 continue; // the modifier feature is later than this, so, should not be used
1186 if (aCurrentModifierFeat == aModifierFeat ||
1187 aDoc->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
1188 continue; // the current modifier is later than the found, so, useless
1189 Handle(TNaming_NamedShape) aNewNS;
1190 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1191 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1192 aModifierResFound = aModifierObj;
1193 aCurrentModifierFeat = aModifierFeat;
1194 TNaming_Iterator aPairIter(aNewNS);
1195 aNewShape = aPairIter.NewShape();
1198 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is null
1199 ResultPtr anEmptyContext;
1200 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1201 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1203 } else { // not-precessed modification => don't support it
1208 if (aModifierResFound.get()) {
1209 // update scope to reset to a new one
1211 myRef.setValue(aModifierResFound);
1212 // if context shape type is changed to more complicated and this context is selected, split
1213 if (myParent &&!aSubShape.get() && aModifierResFound->shape().get() && aContext->shape().get())
1215 TopoDS_Shape anOldShape = aContext->shape()->impl<TopoDS_Shape>();
1216 TopoDS_Shape aNewShape = aModifierResFound->shape()->impl<TopoDS_Shape>();
1217 if (!anOldShape.IsNull() && !aNewShape.IsNull() &&
1218 anOldShape.ShapeType() != aNewShape.ShapeType() &&
1219 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID)) {
1220 // prepare for split in "update"
1221 TDF_Label aSelLab = selectionLabel();
1222 split(aContext, aNewShape, anOldShape.ShapeType());
1225 update(); // it must recompute a new sub-shape automatically
1229 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1231 myParent = theParent;