1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Model_AttributeSelection.h"
22 #include "Model_Application.h"
23 #include "Model_Events.h"
24 #include "Model_Data.h"
25 #include "Model_Document.h"
26 #include <Model_Objects.h>
27 #include <Model_AttributeSelectionList.h>
28 #include <Model_ResultConstruction.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_ResultConstruction.h>
33 #include <ModelAPI_ResultPart.h>
34 #include <ModelAPI_CompositeFeature.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelGeomAlgo_Shape.h>
39 #include <Events_InfoMessage.h>
40 #include <GeomAPI_Edge.h>
41 #include <GeomAPI_Pnt.h>
42 #include <GeomAPI_ShapeIterator.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomAPI_ShapeExplorer.h>
45 #include <GeomAlgoAPI_CompoundBuilder.h>
46 #include <GeomAlgoAPI_NExplode.h>
47 #include <Selector_Selector.h>
49 #include <TNaming_NamedShape.hxx>
50 #include <TNaming_Tool.hxx>
51 #include <TNaming_Builder.hxx>
52 #include <TNaming_SameShapeIterator.hxx>
53 #include <TNaming_NewShapeIterator.hxx>
54 #include <TNaming_Iterator.hxx>
55 #include <TDataStd_Integer.hxx>
56 #include <TDataStd_UAttribute.hxx>
57 #include <TDataStd_Name.hxx>
58 #include <TopTools_ListOfShape.hxx>
59 #include <TopTools_DataMapOfShapeShape.hxx>
60 #include <TopTools_MapOfShape.hxx>
61 #include <TopExp_Explorer.hxx>
62 #include <BRep_Tool.hxx>
64 #include <TopoDS_Edge.hxx>
66 #include <TDF_ChildIterator.hxx>
67 #include <TDF_ChildIDIterator.hxx>
68 #include <TopoDS_Iterator.hxx>
69 #include <TDF_ChildIDIterator.hxx>
70 #include <Geom_Circle.hxx>
71 #include <Geom_Ellipse.hxx>
72 #include <BRep_Builder.hxx>
74 //#define DEB_NAMING 1
76 #include <BRepTools.hxx>
78 /// added to the index in the packed map to signalize that the vertex of edge is selected
79 /// (multiplied by the index of the edge)
80 static const int kSTART_VERTEX_DELTA = 1000000;
81 // identifier that there is simple reference: selection equals to context
82 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
83 // reference to Part sub-object
84 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
85 // selection is invalid after recomputation
86 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
88 // identifier of the selection of the center of circle on edge
89 Standard_GUID kCIRCLE_CENTER("d0d0e0f1-217a-4b95-8fbb-0c4132f23718");
90 // identifier of the selection of the first focus point of ellipse on edge
91 Standard_GUID kELLIPSE_CENTER1("f70df04c-3168-4dc9-87a4-f1f840c1275d");
92 // identifier of the selection of the second focus point of ellipse on edge
93 Standard_GUID kELLIPSE_CENTER2("1395ae73-8e02-4cf8-b204-06ff35873a32");
95 // prefix for the whole feature context identification
96 const static std::string kWHOLE_FEATURE = "all-in-";
98 // on this label is stored:
99 // TNaming_NamedShape - selected shape
100 // TNaming_Naming - topological selection information (for the body)
101 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
102 // TDataStd_Integer - type of the selected shape (for construction)
103 // TDF_Reference - from ReferenceAttribute, the context
104 bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
105 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
107 if (theTemporarily &&
108 (!theContext.get() || theContext->groupName() != ModelAPI_Feature::group())) {
109 // just keep the stored without DF update
110 myTmpContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
111 myTmpSubShape = theSubShape;
112 owner()->data()->sendAttributeUpdated(this);
115 myTmpContext.reset();
116 myTmpSubShape.reset();
117 myTmpCenterType = NOT_CENTER;
121 const std::shared_ptr<GeomAPI_Shape>& anOldShape = internalValue(aType);
122 bool isOldContext = theContext == myRef.value();
123 bool isOldShape = isOldContext &&
124 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
125 if (isOldShape) return false; // shape is the same, so context is also unchanged
126 bool aToUnblock = false;
127 // update the referenced object if needed
129 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
130 myRef.setValue(theContext);
133 // do not use naming if selected shape is result shape itself, but not sub-shape
134 TDF_Label aSelLab = selectionLabel();
135 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
136 aSelLab.ForgetAttribute(kINVALID_SELECTION);
137 aSelLab.ForgetAttribute(kCIRCLE_CENTER);
138 aSelLab.ForgetAttribute(kELLIPSE_CENTER1);
139 aSelLab.ForgetAttribute(kELLIPSE_CENTER2);
141 bool isDegeneratedEdge = false;
142 // do not use the degenerated edge as a shape, a null context and shape is used in the case
143 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
144 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
145 if (aSubShape.ShapeType() == TopAbs_EDGE)
146 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
148 if (!theContext.get() || isDegeneratedEdge) {
149 // to keep the reference attribute label
150 TDF_Label aRefLab = myRef.myRef->Label();
151 aSelLab.ForgetAllAttributes(true);
152 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
154 owner()->data()->blockSendAttributeUpdated(false);
157 bool isSelectBody = theContext->groupName() == ModelAPI_ResultBody::group();
159 ResultConstructionPtr aContextConstruction =
160 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
161 isSelectBody = aContextConstruction.get() && !aContextConstruction->isInfinite();
164 ResultPtr aContextResult = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
165 GeomShapePtr aContextShape = aContextResult->shape();
166 // do not select the whole shape for body:it is already must be in the data framework
167 // equal and null selected objects mean the same: object is equal to context,
168 if (aContextShape.get() && (aContextShape->isEqual(theSubShape) || !theSubShape.get())) {
169 aSelLab.ForgetAllAttributes(true);
170 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
172 selectBody(aContextResult, theSubShape);
174 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
175 aSelLab.ForgetAllAttributes(true); // to remove old selection data
176 std::shared_ptr<Model_ResultConstruction> aConstruction =
177 std::dynamic_pointer_cast<Model_ResultConstruction>(theContext);
178 std::shared_ptr<GeomAPI_Shape> aSubShape;
179 if (theSubShape.get() && !aConstruction->shape()->isEqual(theSubShape))
180 aSubShape = theSubShape; // the whole context
181 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
182 aSelLab.ForgetAllAttributes(true);
183 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
184 selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
185 } else { // check the feature context: parent-Part of this feature should not be used
186 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
187 if (aFeatureContext.get()) {
188 if (owner()->document() != aFeatureContext->document()) {
189 aSelLab.ForgetAllAttributes(true);
190 myRef.setValue(ObjectPtr());
192 owner()->data()->blockSendAttributeUpdated(false);
198 owner()->data()->sendAttributeUpdated(this);
201 owner()->data()->blockSendAttributeUpdated(false);
206 void Model_AttributeSelection::setValueCenter(
207 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
208 const CenterType theCenterType, const bool theTemporarily)
210 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
211 if (theTemporarily) {
212 myTmpCenterType = theCenterType;
213 } else { // store in the data structure
214 TDF_Label aSelLab = selectionLabel();
215 switch(theCenterType) {
218 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
219 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
221 case ELLIPSE_FIRST_FOCUS:
223 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
224 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
226 case ELLIPSE_SECOND_FOCUS:
228 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
229 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
233 owner()->data()->sendAttributeUpdated(this);
237 void Model_AttributeSelection::selectValue(
238 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
241 std::shared_ptr<GeomAPI_Shape> aValue =
242 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
243 if (!aValue.get() || aType == NOT_CENTER) {
244 setValue(theSource->context(), aValue);
246 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
247 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
248 setValueCenter(theSource->context(), anEdge, aType);
252 void Model_AttributeSelection::removeTemporaryValues()
254 if (myTmpContext.get() || myTmpSubShape.get()) {
255 myTmpContext.reset();
256 myTmpSubShape.reset();
260 // returns the center of the edge: circular or elliptical
261 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
263 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
264 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
265 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
266 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
267 double aFirst, aLast;
268 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
269 if (!aCurve.IsNull()) {
270 TopoDS_Vertex aVertex;
271 BRep_Builder aBuilder;
272 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
273 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
274 if (!aCirc.IsNull()) {
275 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
278 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
279 if (!anEll.IsNull()) {
280 aBuilder.MakeVertex(aVertex,
281 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
282 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
285 if (!aVertex.IsNull()) {
286 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
287 aResult->setImpl(new TopoDS_Vertex(aVertex));
293 return theEdge; // no vertex, so, return the initial edge
296 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
298 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
299 return std::shared_ptr<GeomAPI_Shape>();
300 CenterType aType = NOT_CENTER;
301 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
302 return centerByEdge(aResult, aType);
305 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
307 theType = NOT_CENTER;
308 GeomShapePtr aResult;
309 if (myTmpContext.get() || myTmpSubShape.get()) {
310 theType = myTmpCenterType;
311 ResultConstructionPtr aResulConstruction =
312 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
313 if(aResulConstruction.get()) {
314 // it is just reference to construction.
315 return myTmpSubShape;
317 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
320 TDF_Label aSelLab = selectionLabel();
321 if (aSelLab.IsAttribute(kINVALID_SELECTION))
324 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
325 theType = CIRCLE_CENTER;
326 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
327 theType = ELLIPSE_FIRST_FOCUS;
328 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
329 theType = ELLIPSE_SECOND_FOCUS;
332 if (myRef.isInitialized()) {
333 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
334 ResultPtr aContext = context();
335 if (!aContext.get() || aContext->groupName() == ModelAPI_ResultConstruction::group())
336 return aResult; // empty result, for whole construction selection also
337 return aContext->shape();
339 if (aSelLab.IsAttribute(kPART_REF_ID)) {
340 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
341 if (!aPart.get() || !aPart->isActivated())
342 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
343 Handle(TDataStd_Integer) anIndex;
344 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
345 if (anIndex->Get()) { // special selection attribute was created, use it
346 return aPart->selectionValue(anIndex->Get());
347 } else { // face with name is already in the data model, so try to take it by name
348 Handle(TDataStd_Name) aName;
349 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
350 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
351 std::size_t aPartEnd = aSubShapeName.find('/');
352 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
353 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
355 std::string aType; // to reuse already existing selection the type is not needed
356 return aPart->shapeInPart(aNameInPart, aType, anIndex);
363 std::shared_ptr<Model_ResultConstruction> aConstr =
364 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
366 if (aConstr->isInfinite())
367 return aResult; // empty result
369 if (!aConstr.get()) { // for construction context, return empty result as usual even
370 // the whole feature is selected
371 FeaturePtr aFeature = contextFeature();
372 if (aFeature.get()) {
373 std::list<GeomShapePtr> allShapes;
374 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
375 for (; aRes != aFeature->results().cend(); aRes++) {
376 if (aRes->get() && !(*aRes)->isDisabled()) {
377 GeomShapePtr aShape = (*aRes)->shape();
378 if (aShape.get() && !aShape->isNull()) {
379 allShapes.push_back(aShape);
383 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
386 if (contextFeature().get()) {
387 return aResult; // for the whole sketch feature selected return null => all faces
391 Handle(TNaming_NamedShape) aSelection;
392 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
393 TopoDS_Shape aSelShape = aSelection->Get();
394 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
395 aResult->setImpl(new TopoDS_Shape(aSelShape));
396 } else if (aConstr) { // simple construction element: just shape of this construction element
397 aResult = aConstr->shape();
403 bool Model_AttributeSelection::isInvalid()
405 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
408 bool Model_AttributeSelection::isInitialized()
410 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
411 std::shared_ptr<GeomAPI_Shape> aResult;
412 if (myRef.isInitialized()) {
413 TDF_Label aSelLab = selectionLabel();
414 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
415 ResultPtr aContext = context();
416 return aContext.get() != NULL;
418 Handle(TNaming_NamedShape) aSelection;
419 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
420 return !aSelection->Get().IsNull();
421 } else { // for simple construction element: just shape of this construction element
422 std::shared_ptr<Model_ResultConstruction> aConstr =
423 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
427 // for the whole feature, a feature object
428 FeaturePtr aFeat = contextFeature();
437 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
439 myIsGeometricalSelection(false)
441 myIsInitialized = myRef.isInitialized();
445 void Model_AttributeSelection::setID(const std::string theID)
448 ModelAPI_AttributeSelection::setID(theID);
449 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
451 myIsGeometricalSelection = true;//myParent->isGeometricalSelection();
453 myIsGeometricalSelection =
454 ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
458 ResultPtr Model_AttributeSelection::context()
460 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
463 if (myTmpContext.get() || myTmpSubShape.get()) {
467 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
468 // for parts there could be same-data result, so take the last enabled
470 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
471 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
472 for(int a = aSize - 1; a >= 0; a--) {
473 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
474 if(aPart.get() && aPart->data() == aResult->data()) {
475 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
476 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
477 // check that this result is not this-feature result (it is forbidden to select itself)
478 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
484 } else { // if feature - construction is selected, it has only one result, return this result
485 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
486 if (aFeature.get() && aFeature->results().size() == 1 &&
487 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
488 return aFeature->firstResult();
493 FeaturePtr Model_AttributeSelection::contextFeature() {
494 if (myTmpContext.get() || myTmpSubShape.get()) {
495 return FeaturePtr(); // feature can not be selected temporarily
497 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
499 ObjectPtr Model_AttributeSelection::contextObject() {
500 FeaturePtr aRes = contextFeature();
507 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
509 ModelAPI_AttributeSelection::setObject(theObject);
510 myRef.setObject(theObject);
513 /// Sets the invalid flag if flag is false, or removes it if "true"
515 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
517 theLab.ForgetAttribute(kINVALID_SELECTION);
519 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
524 void Model_AttributeSelection::split(
525 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
527 TopTools_ListOfShape aSubs;
528 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
529 if (!anExplorer.Value().IsNull() &&
530 anExplorer.Value().ShapeType() == theType) {
531 aSubs.Append(anExplorer.Value());
532 } else { // invalid case; bad result shape, so, impossible to split easily
537 if (aSubs.Extent() > 1) { // ok to split
538 TopTools_ListIteratorOfListOfShape aSub(aSubs);
539 GeomShapePtr aSubSh(new GeomAPI_Shape);
540 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
541 setValue(theContext, aSubSh);
542 for(aSub.Next(); aSub.More(); aSub.Next()) {
543 GeomShapePtr aSubSh(new GeomAPI_Shape);
544 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
545 myParent->append(theContext, aSubSh);
550 bool Model_AttributeSelection::update()
552 FeaturePtr aContextFeature = contextFeature();
553 if (aContextFeature.get()) {
554 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
557 TDF_Label aSelLab = selectionLabel();
558 ResultPtr aContext = context();
560 return setInvalidIfFalse(aSelLab, false);
561 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
562 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
565 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
566 std::shared_ptr<GeomAPI_Shape> aNoSelection;
567 bool aResult = selectPart(aContext, aNoSelection, true);
568 aResult = setInvalidIfFalse(aSelLab, aResult);
570 owner()->data()->sendAttributeUpdated(this);
575 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
576 // body: just a named shape, use topological selection mechanism
577 bool aResult = false;
578 TopoDS_Shape anOldShape;
579 Handle(TNaming_NamedShape) aNS;
580 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
581 anOldShape = aNS->Get();
583 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
584 Selector_Selector aSelector(aSelLab, baseDocumentLab());
585 aResult = aSelector.restore(aContextShape);
586 setInvalidIfFalse(aSelLab, aResult);
588 TopoDS_Shape aNewShape;
589 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
590 aNewShape = aNS->Get();
592 if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape)) {
593 // shape type should not be changed: if shape becomes compound of such shapes, then split
594 if (!myIsGeometricalSelection && myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
595 anOldShape.ShapeType() != aNewShape.ShapeType() &&
596 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
598 split(aContext, aNewShape, anOldShape.ShapeType());
600 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
605 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
607 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
608 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
609 if (!aConstructionContext->isInfinite()) {
610 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
611 Selector_Selector aSelector(aSelLab, baseDocumentLab());
612 TopoDS_Shape anOldShape = aSelector.value();
613 aResult = aSelector.restore(aContextShape);
614 setInvalidIfFalse(aSelLab, aResult);
615 if (aResult && !anOldShape.IsEqual(aSelector.value()))
616 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
618 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
622 return setInvalidIfFalse(aSelLab, false); // unknown case
625 void Model_AttributeSelection::selectBody(
626 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
628 // perform the selection
629 TopoDS_Shape aContext;
631 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
633 aContext = aBody->shape()->impl<TopoDS_Shape>();
636 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
638 aContext = aResult->shape()->impl<TopoDS_Shape>();
640 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
645 if (!aContext.IsNull()) {
646 TDF_Label aSelLab = selectionLabel();
647 TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
649 bool aSelectorOk = true;
650 Selector_Selector aSelector(aSelLab, baseDocumentLab());
652 aSelectorOk = aSelector.select(aContext, aNewSub, myIsGeometricalSelection);
654 aSelectorOk = aSelector.store(aContext);
660 TopoDS_Shape aShape = aSelector.value();
661 aSelectorOk = !aShape.IsNull() && aShape.ShapeType() == aNewSub.ShapeType();
663 setInvalidIfFalse(aSelLab, aSelectorOk);
667 bool Model_AttributeSelection::selectPart(
668 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
669 const bool theUpdate)
671 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
672 if (!aPart.get() || !aPart->isActivated())
673 return true; // postponed naming
675 Handle(TDataStd_Integer) anIndex;
676 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
677 // by internal selection
678 if (anIndex->Get() > 0) {
679 // update the selection by index
680 return aPart->updateInPart(anIndex->Get());
682 return true; // nothing to do, referencing just by name
685 return true; // nothing to do, referencing just by name
687 // store the shape (in case part is not loaded it should be useful
689 std::string aName = theContext->data()->name();
690 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
691 aShape = theContext->shape()->impl<TopoDS_Shape>();
693 aShape = theSubShape->impl<TopoDS_Shape>();
695 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
696 TDataStd_Integer::Set(selectionLabel(), anIndex);
698 TNaming_Builder aBuilder(selectionLabel());
699 aBuilder.Select(aShape, aShape);
700 // identify by name in the part
701 TDataStd_Name::Set(selectionLabel(), aName.c_str());
702 return !aName.empty();
705 TDF_Label Model_AttributeSelection::selectionLabel()
707 return myRef.myRef->Label().FindChild(1);
710 /// prefixes of the shape names with centers defined
711 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
713 /// returns the map that contains all possible prefixes of the center-names
714 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
716 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
717 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
718 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
719 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
721 return kCENTERS_PREFIX;
724 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
726 std::string aName("");
727 if(!this->isInitialized())
728 return !theDefaultName.empty() ? theDefaultName : aName;
730 CenterType aCenterType = NOT_CENTER;
731 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
732 ResultPtr aCont = context();
735 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
736 // selection of a full feature
737 FeaturePtr aFeatureCont = contextFeature();
738 if (aFeatureCont.get()) {
739 return kWHOLE_FEATURE + aFeatureCont->name();
741 // in case of selection of removed result
745 TDF_Label aSelLab = selectionLabel();
746 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
747 return contextName(aCont);
750 // if it is in result of another part
751 if (aCont->groupName() == ModelAPI_ResultPart::group()) {
752 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCont);
754 GeomShapePtr aValue = value();
756 return aPart->data()->name() + "/" + aPart->nameInPart(aValue, anIndex);
758 return aPart->data()->name();
762 // whole infinitive construction
763 if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
764 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
765 if (aConstr->isInfinite()) {
766 return contextName(aCont);
770 Selector_Selector aSelector(aSelLab, baseDocumentLab());
772 if (aCont->shape().get() && aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
773 aResult = aSelector.name(this);
774 if (aCenterType != NOT_CENTER) {
775 aResult += centersMap()[aCenterType];
780 // returns the center type and modifies the shape name if this name is center-name
781 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
783 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
784 centersMap().begin();
785 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
786 std::size_t aFound = theShapeName.find(aPrefixIter->second);
787 if (aFound != std::string::npos &&
788 aFound == theShapeName.size() - aPrefixIter->second.size()) {
789 theShapeName = theShapeName.substr(0, aFound);
790 return aPrefixIter->first;
793 return ModelAPI_AttributeSelection::NOT_CENTER;
796 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
797 void Model_AttributeSelection::selectSubShape(
798 const std::string& theType, const std::string& theSubShapeName)
800 if(theSubShapeName.empty() || theType.empty()) return;
802 std::string aSubShapeName = theSubShapeName;
803 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
804 centerTypeByName(aSubShapeName) : NOT_CENTER;
805 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
806 static const GeomShapePtr anEmptyShape;
808 // first iteration is selection by name without center prefix, second - in case of problem,
809 // try with initial name
810 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
811 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
812 aSubShapeName = theSubShapeName;
813 aCenterType = NOT_CENTER;
815 } else if (aUseCenter != 1) continue;
816 TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(aType));
818 std::shared_ptr<Model_Document> aDoc =
819 std::dynamic_pointer_cast<Model_Document>(owner()->document());
820 // check this is Part-name: 2 delimiters in the name
821 std::size_t aPartEnd = aSubShapeName.find('/');
822 if (aPartEnd != std::string::npos) {
823 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
824 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
825 if (aPartName == aRootDoc->kind()) {
826 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
827 aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
830 owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
831 if (aFound.get()) { // found such part, so asking it for the name
832 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
833 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
834 if (aNameInPart.empty()) { // whole part
835 setValue(aPart, anEmptyShape);
839 std::shared_ptr<GeomAPI_Shape> aSelected =
840 aPart->shapeInPart(aNameInPart, aType, anIndex);
841 if (aSelected.get()) {
842 if (aCenterType != NOT_CENTER) {
843 if (!aSelected->isEdge())
845 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
846 setValueCenter(aPart, aSelectedEdge, aCenterType);
848 setValue(aPart, aSelected);
849 TDataStd_Integer::Set(selectionLabel(), anIndex);
856 // check this is a whole feature context
857 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
858 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
859 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
860 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
862 setValue(anObj, anEmptyShape);
867 // the whole result selection check
868 if (aSubShapeName.find('/') == std::string::npos) {
869 ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
871 aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
873 setValue(aRes, anEmptyShape);
878 Selector_Selector aSelector(selectionLabel(), baseDocumentLab());
879 myRestoreDocument = aDoc;
880 TDF_Label aContextLabel = aSelector.restoreByName(
881 aSubShapeName, aShapeType, this, myIsGeometricalSelection);
882 myRestoreDocument.reset();
883 if (!aContextLabel.IsNull()) {
884 ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
885 if (aContext.get() && aContext->shape().get()) {
886 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
887 if (aSelector.solve(aContextShape)) {
888 TopoDS_Shape aSelectorShape = aSelector.value();
889 GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
890 aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelectorShape));
891 // make the context result the latest existing
892 aContext = newestContext(aContext, aShapeToBeSelected);
893 if (myIsGeometricalSelection || aCenterType == NOT_CENTER) {
894 // store the currently generated name
895 selectionLabel().ForgetAllAttributes(true);
896 bool aToUnblock = false;
897 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
898 myRef.setValue(aContext);
899 aSelector.store(aContextShape);
900 owner()->data()->sendAttributeUpdated(this);
902 owner()->data()->blockSendAttributeUpdated(false);
904 } else { // re-select center of circle/arc by context and value
905 if (!aShapeToBeSelected->isEdge())
907 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
908 setValueCenter(aContext, aSelectedEdge, aCenterType);
916 TDF_Label aSelLab = selectionLabel();
917 setInvalidIfFalse(aSelLab, false);
921 void Model_AttributeSelection::selectSubShape(const std::string& theType,
922 const GeomPointPtr& thePoint)
924 if (theType.empty() || !thePoint)
927 // list of parent features
928 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
929 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
931 int aSelectionIndex = 0;
932 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
933 if (aType == GeomAPI_Shape::SHAPE) {
934 // possibly, the string consists of the type and the index,
935 // thus, try to separate them
936 size_t aUndersporePos = theType.find_first_of('_');
937 if (aUndersporePos != std::string::npos)
938 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
940 if (aType != GeomAPI_Shape::SHAPE) {
941 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
942 aChar != theType.end(); ++aChar) {
943 if (std::isdigit(*aChar))
944 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
950 aSelectionIndex -= 1;
954 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
956 // collect features from PartSet and the current part
957 SessionPtr aSession = ModelAPI_Session::get();
958 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
959 if (aSession->moduleDocument() != owner()->document()) {
960 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
961 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
963 // Process results of all features from the last to the first
964 // to find appropriate sub-shape
965 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
966 anIt != aFeatures.rend(); ++anIt) {
967 // selection cannot be linked to the parent features
968 if (aParents.find(*anIt) != aParents.end())
970 // check the feature is a part of composite feature (like sketch elements),
971 // then do not process it, it will be processed in scope of composite feature
972 bool isSubOfComposite = false;
973 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
974 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
975 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
976 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
977 CompositeFeaturePtr aCompFeature =
978 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
979 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
981 if (isSubOfComposite)
984 // process results of the current feature to find appropriate sub-shape
985 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
986 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
987 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
988 ++anApIt; // skip this shape, because one of the previous is selected
990 if (anApIt != anAppropriate.end()) {
991 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
992 setValue(anApIt->myResult, anApIt->mySubshape);
994 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
995 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1001 TDF_Label aSelLab = selectionLabel();
1002 setInvalidIfFalse(aSelLab, false);
1006 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1007 const std::string& theContextName, const int theIndex)
1009 // selection of context by name
1010 selectSubShape(theType, theContextName);
1011 ResultPtr aContext = context();
1012 if (aContext.get()) {
1013 GeomShapePtr aContShape = aContext->shape();
1014 if (aContShape.get()) {
1015 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1016 GeomShapePtr aValue = aNExp.shape(theIndex);
1018 setValue(aContext, aValue);
1023 int Model_AttributeSelection::Id()
1026 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1027 ResultPtr aContextRes = context();
1028 // support for compsolids:
1029 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1030 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1032 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1035 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1036 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1037 // searching for the latest main shape
1038 if (aSelection && !aSelection->isNull() && aContext && !aContext->isNull())
1040 std::shared_ptr<Model_Document> aDoc =
1041 std::dynamic_pointer_cast<Model_Document>(context()->document());
1043 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1044 if (!aNS.IsNull()) {
1045 aMainShape = TNaming_Tool::CurrentShape(aNS);
1049 TopTools_IndexedMapOfShape aSubShapesMap;
1050 TopExp::MapShapes(aMainShape, aSubShapesMap);
1051 anID = aSubShapesMap.FindIndex(aSubShape);
1056 void Model_AttributeSelection::setId(int theID)
1058 std::shared_ptr<GeomAPI_Shape> aSelection;
1060 ResultPtr aContextRes = context();
1061 // support for compsolids:
1062 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1063 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1065 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1067 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1068 // searching for the latest main shape
1069 if (theID > 0 && aContext && !aContext->isNull())
1071 std::shared_ptr<Model_Document> aDoc =
1072 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1074 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1075 if (!aNS.IsNull()) {
1076 aMainShape = TNaming_Tool::CurrentShape(aNS);
1080 TopTools_IndexedMapOfShape aSubShapesMap;
1081 TopExp::MapShapes(aMainShape, aSubShapesMap);
1082 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1084 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1085 aResult->setImpl(new TopoDS_Shape(aSelShape));
1087 aSelection = aResult;
1090 setValue(aContextRes, aSelection);
1093 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1095 std::string aResult;
1096 if (owner()->document() != theContext->document()) {
1097 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1098 aResult = theContext->document()->kind() + "/";
1100 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1101 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1102 if (aDocRes.get()) {
1103 aResult = aDocRes->data()->name() + "/";
1107 aResult += theContext->data()->name();
1111 void Model_AttributeSelection::computeValues(
1112 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1113 TopTools_ListOfShape& theShapes)
1115 bool aWasWholeContext = theValShape.IsNull();
1116 if (aWasWholeContext) {
1117 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1119 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1120 // if a new value is unchanged in the new context, do nothing: value is correct
1121 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1122 for(; aSubExp.More(); aSubExp.Next()) {
1123 if (aSubExp.Current().IsSame(theValShape)) {
1124 theShapes.Append(theValShape);
1128 // if new context becomes compsolid, the resulting sub may be in sub-solids
1129 std::list<ResultPtr> aNewToIterate;
1130 aNewToIterate.push_back(theNewContext);
1131 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1133 std::list<ResultPtr> allNewContextSubs;
1134 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1135 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1136 for(; aSub != allNewContextSubs.end(); aSub++) {
1137 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1138 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1139 aNewToIterate.push_back(aBody);
1143 // first iteration: searching for the whole shape appearance (like face of the box)
1144 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1145 int aToFindPart = 0;
1146 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1147 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1148 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1149 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1150 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1151 std::shared_ptr<Model_Data> aNewData =
1152 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1153 TDF_Label aNewLab = aNewData->shapeLab();
1154 // searching for produced sub-shape fully on some label
1155 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID());
1156 for(; aNSIter.More(); aNSIter.Next()) {
1157 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1158 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1159 if (aToFindPart == 0) { // search shape is fully inside
1160 if (aPairIter.OldShape().IsSame(theValShape)) {
1161 if (aPairIter.NewShape().IsNull()) {// value was removed
1165 theShapes.Append(aPairIter.NewShape());
1167 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1168 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1169 for(; anExp.More(); anExp.Next()) {
1170 if (anExp.Current().IsSame(theValShape)) { // found a new container
1171 if (aPairIter.NewShape().IsNull()) {// value was removed
1175 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1176 anOlds.Add(aPairIter.OldShape());
1185 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1186 // map of sub-shapes -> number of occurrences of these shapes in containers
1187 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1188 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1189 for(; aContIter.More(); aContIter.Next()) {
1190 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1191 for(; aSubExp.More(); aSubExp.Next()) {
1192 if (!aSubs.IsBound(aSubExp.Current())) {
1193 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1195 // store old to know how many olds produced this shape
1196 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1199 // if sub is appeared same times in containers as the number of old shapes that contain it
1200 int aCountInOld = anOlds.Size();
1201 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1203 for(; aSubsIter.More(); aSubsIter.Next()) {
1204 if (aSubsIter.Value().Size() == aCountInOld) {
1205 theShapes.Append(aSubsIter.Key());
1209 if (theShapes.IsEmpty()) { // nothing was changed
1210 theShapes.Append(aWasWholeContext ? TopoDS_Shape() : theValShape);
1214 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1215 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1216 TDF_Label theAccessLabel,
1217 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1219 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1220 TopTools_ListOfShape aResContShapes;
1221 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1222 TopTools_ListOfShape aContextList;
1223 aContextList.Append(theContShape);
1224 if (theContext.get()) {
1225 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1226 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1227 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1229 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1230 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1231 for(; aModifIter.More(); aModifIter.Next()) {
1232 TDF_Label anObjLab = aModifIter.Label().Father();
1233 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1234 (theDoc->objects()->object(anObjLab));
1235 if (!aModifierObj.get()) {
1236 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1237 if (!anObjLab.IsRoot())
1238 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1239 (theDoc->objects()->object(anObjLab.Father()));
1240 if (!aModifierObj.get())
1243 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1244 if (!aModifierFeat.get())
1246 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1247 if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
1248 continue; // the modifier feature is later than this, so, should not be used
1249 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1250 if (aCurrentModifierFeat == aModifierFeat ||
1251 !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
1252 continue; // the current modifier is later than the found, so, useless
1253 Handle(TNaming_NamedShape) aNewNS;
1254 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1255 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1256 aResults.insert(aModifierObj);
1257 aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
1258 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1259 aResults.insert(ResultPtr());
1260 } else { // not-processed modification => don't support it
1265 if (aResults.empty())
1266 return false; // no modifications found, must stay the same
1267 // iterate all results to find further modifications
1268 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1269 for(; aResIter != aResults.end(); aResIter++) {
1270 if (aResIter->get() != NULL) {
1271 // compute new values by two contexts: the old and the new
1272 TopTools_ListOfShape aValShapes;
1273 computeValues(theContext, *aResIter, theValShape, aValShapes);
1275 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1276 for(; aNewVal.More(); aNewVal.Next()) {
1277 std::list<ResultPtr> aNewRes;
1278 TopTools_ListOfShape aNewUpdatedVal;
1279 TopoDS_Shape aNewValSh = aNewVal.Value();
1280 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1281 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1282 aNewValSh.Nullify();
1283 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1284 theAccessLabel, aNewRes, aNewUpdatedVal))
1286 // append new results instead of the current ones
1287 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1288 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1289 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1290 theResults.push_back(*aNewIter);
1291 theValShapes.Append(aNewUpdVal.Value());
1293 } else { // the current result is good
1294 theResults.push_back(*aResIter);
1295 theValShapes.Append(aNewValSh);
1300 return true; // theResults must be empty: everything is deleted
1303 void Model_AttributeSelection::updateInHistory()
1305 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1306 // only bodies and parts may be modified later in the history, don't do anything otherwise
1307 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1308 aContext->groupName() != ModelAPI_ResultPart::group()))
1310 std::shared_ptr<Model_Document> aDoc =
1311 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1312 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1313 if (!aContData.get() || !aContData->isValid())
1315 TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
1316 Handle(TNaming_NamedShape) aContNS;
1317 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1318 bool aFoundNewContext = true;
1319 ResultPtr aNewContext = aContext;
1320 while(aFoundNewContext) {
1321 aFoundNewContext = false;
1322 // parts have no shape in result, so, trace references using the Part info
1323 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1324 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1325 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1326 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1327 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1328 for(; aRef != aRefs.end(); aRef++) {
1329 // to avoid detection of part changes by local selection only
1330 AttributeSelectionPtr aSel =
1331 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1332 if (aSel.get() && aSel->value().get() &&
1333 !aSel->value()->isSame(aSel->context()->shape()))
1336 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1337 if (aRefFeat.get() && aRefFeat != owner()) {
1338 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1339 if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
1340 aFoundNewContext = true;
1341 aNewContext = aRefFeat->firstResult();
1348 if (aNewContext != aContext) {
1349 setValue(aNewContext, value());
1353 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1354 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1355 // iterate the context shape modifications in order to find a feature that is upper in history
1356 // that this one and is really modifies the referenced result to refer to it
1357 ResultPtr aModifierResFound;
1358 TNaming_Iterator aPairIter(aContNS);
1359 if (!aPairIter.More())
1361 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1362 bool anIterate = true;
1363 // trying to update also the sub-shape selected
1364 GeomShapePtr aSubShape = value();
1365 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1367 TopoDS_Shape aValShape;
1368 if (aSubShape.get()) {
1369 aValShape = aSubShape->impl<TopoDS_Shape>();
1372 std::list<ResultPtr> aNewContexts;
1373 TopTools_ListOfShape aValShapes;
1374 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1376 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1378 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1379 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1380 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1383 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1384 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1385 bool aFirst = true; // first is set to this, next are appended to parent
1386 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1388 GeomShapePtr aValueShape;
1389 if (!aNewValues.Value().IsNull()) {
1390 aValueShape = std::make_shared<GeomAPI_Shape>();
1391 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1393 // Check that list has the same type of shape selection before adding.
1394 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1395 if (aValueShape.get()) {
1396 aShapeShapeType = aValueShape->shapeType();
1398 aShapeShapeType = (*aNewCont)->shape()->shapeType();
1400 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1404 setValue(*aNewCont, aValueShape);
1406 } else if (myParent) {
1407 myParent->append(*aNewCont, aValueShape);
1410 if (aFirst) { // nothing was added, all results were deleted
1411 ResultPtr anEmptyContext;
1412 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1413 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1419 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1421 myParent = theParent;
1424 std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
1426 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1427 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1428 FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1429 bool aBaseDocumnetUsed = false;
1430 if (!aFeatureOwner.get()) { // use module document
1431 aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
1432 aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1433 aBaseDocumnetUsed = true;
1435 if (aFeatureOwner.get()) {
1436 // if it is sub-element of the sketch, the context name is the name of the sketch
1437 // searching also for result - real context
1439 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
1440 if (aComposite.get() && aComposite->results().size() == 1 &&
1441 aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
1442 aFeatureOwner = aComposite;
1443 aResult = aFeatureOwner->firstResult();
1445 aResult = aDoc->resultByLab(theSelectionLab);
1447 if (aResult.get()) {
1448 // this is to avoid duplicated names of results problem
1449 std::string aContextName = aResult->data()->name();
1450 // myLab corresponds to the current time
1451 TDF_Label aCurrentLab = selectionLabel();
1452 while(aCurrentLab.Depth() > 3)
1453 aCurrentLab = aCurrentLab.Father();
1455 int aNumInHistoryNames =
1456 aDoc->numberOfNameInHistory(aResult, aCurrentLab);
1457 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
1458 aContextName = "_" + aContextName;
1459 aNumInHistoryNames--;
1461 if (aBaseDocumnetUsed)
1462 aContextName = aDoc->kind() + "/" + aContextName;
1463 return aContextName;
1466 return ""; // invalid case
1469 /// This method restores by the context and value name the context label and
1470 /// sub-label where the value is. Returns true if it is valid.
1471 bool Model_AttributeSelection::restoreContext(std::string theName,
1472 TDF_Label& theContext, TDF_Label& theValue)
1474 static const GeomShapePtr anEmptyShape; // to store context only
1475 std::string aName = theName;
1476 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1477 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1479 // remove the sub-value part if exists
1480 std::string aSubShapeName = aName;
1481 std::string::size_type n = aName.find('/');
1482 if (n != std::string::npos) {
1483 aName = aName.substr(0, n);
1486 if (aName.empty()) return false;
1487 bool anUniqueContext = false;
1488 ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1489 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
1491 aDoc = std::dynamic_pointer_cast<Model_Document>(
1492 ModelAPI_Session::get()->moduleDocument());
1493 if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
1494 aSubShapeName = theName.substr(aDoc->kind().size() + 1);
1495 aName = aSubShapeName;
1496 std::string::size_type n = aName.find('/');
1497 if (n != std::string::npos) {
1498 aName = aName.substr(0, n);
1501 aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1502 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
1506 // searching the sub-shape
1507 static const ResultPtr anEmpty;
1508 theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
1510 // sketch sub-component shape and name is located in separated feature label, try the sub-name
1511 if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
1512 std::string::size_type aSlash = aSubShapeName.rfind('/');
1513 if (aSlash != std::string::npos) {
1514 std::string aCompName = aSubShapeName.substr(aSlash + 1);
1515 CompositeFeaturePtr aComposite =
1516 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
1517 if (aComposite.get() && aComposite->numberOfSubs()) {
1518 const int aSubNum = aComposite->numberOfSubs();
1519 for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
1520 FeaturePtr aSub = aComposite->subFeature(a);
1521 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1522 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
1523 for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
1524 if ((*aRes)->data()->name() == aCompName) {
1525 theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1527 } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
1528 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1529 TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
1530 for(; aSubNames.More(); aSubNames.Next()) {
1531 if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
1532 IsEqual(aCompName.c_str())) {
1533 theValue = aSubNames.Value()->Label();
1545 theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
1550 TDF_Label Model_AttributeSelection::newestContext(const TDF_Label theCurrentContext) {
1551 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1552 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1553 ResultPtr aContext = aDoc->resultByLab(theCurrentContext);
1554 if (aContext.get()) {
1555 aContext = newestContext(aContext, GeomShapePtr(), true);
1557 return std::dynamic_pointer_cast<Model_Data>(aContext->data())->label();
1559 return theCurrentContext; // nothing is changed
1562 bool Model_AttributeSelection::isLater(
1563 const TDF_Label theResult1, const TDF_Label theResult2) const
1565 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1566 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1567 FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
1570 FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
1573 return aDoc->isLaterByDep(aFeat1, aFeat2);
1576 ResultPtr Model_AttributeSelection::newestContext(
1577 const ResultPtr theCurrent, const GeomShapePtr theValue, const bool theAnyValue)
1579 ResultPtr aResult = theCurrent;
1580 GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
1581 std::shared_ptr<Model_Document> aDoc =
1582 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1583 bool aFindNewContext = true;
1584 while (aFindNewContext && aResult.get()) {
1585 aFindNewContext = false;
1586 // try to find the last context to find the up to date shape
1587 TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
1588 if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
1589 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1590 if (!aNS.IsNull()) {
1591 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1593 && isLater(selectionLabel(), aNS->Label())
1594 && isLater(aNS->Label(),
1595 std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
1597 TDF_Label aLab = aNS->Label();
1598 ResultPtr aRes = aDoc->resultByLab(aLab);
1600 if (theAnyValue || aRes->shape()->isSubShape(aSelectedShape)) {
1602 aFindNewContext = true;
1609 if (theAnyValue) { // only for neighbors for now
1610 // try to find modification of sub-shapes: the best number of matches
1611 std::map<ResultPtr, int> aMatches; // result -> number of matches of shapes to find the best
1612 TDF_Label aResLab = std::dynamic_pointer_cast<Model_Data>(aResult->data())->shapeLab();
1613 TDF_ChildIDIterator aModifIter(aResLab, TNaming_NamedShape::GetID());
1614 for(; aModifIter.More(); aModifIter.Next()) {
1615 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aModifIter.Value());
1616 if (aNS->Evolution() == TNaming_MODIFY || aNS->Evolution() == TNaming_GENERATED) {
1617 for(TNaming_Iterator aNSIter(aNS); aNSIter.More(); aNSIter.Next()) {
1618 TNaming_NewShapeIterator aNewIter(aNSIter.NewShape(), aNS->Label());
1619 for(; aNewIter.More(); aNewIter.Next()) {
1620 TDF_Label aLab = aNewIter.Label();
1621 if (isLater(aLab, aNS->Label()) && isLater(selectionLabel(), aLab)) {
1622 ResultPtr aRes = aDoc->resultByLab(aLab);
1624 if (aMatches.find(aRes) == aMatches.end())
1626 aMatches[aRes]++; // found result, add matches
1633 // searching for the best result-candidate
1635 ResultPtr aBestResult;
1636 std::map<ResultPtr, int>::iterator aMatchIter = aMatches.begin();
1637 for(; aMatchIter != aMatches.end(); aMatchIter++) {
1638 if (aMatchIter->second > aBest) {
1639 aBest = aMatchIter->second;
1640 aBestResult = aMatchIter->first;
1643 if (aBestResult.get()) {
1644 aResult = aBestResult;
1645 aFindNewContext = true;
1651 // TestFillWireVertex.py - sketch constructions for wire may participate too
1652 //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
1653 // try to search newer context by the concealment references
1654 // take references to all results: root one, any sub
1655 std::list<ResultPtr> allRes;
1656 ResultPtr aCompContext;
1657 ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
1658 if (aCompBody.get()) {
1659 ModelAPI_Tools::allSubs(aCompBody, allRes);
1660 allRes.push_back(aCompBody);
1661 aCompContext = aCompBody;
1664 allRes.push_back(aResult);
1666 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1667 ResultPtr aResCont = *aSub;
1668 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1669 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1670 continue; // only lower and higher level subs are counted
1671 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1672 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1673 for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1674 if (!aRef->get() || !(*aRef)->owner().get())
1676 // concealed attribute only
1677 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1678 if (!ModelAPI_Session::get()->validators()->isConcealed(
1679 aRefFeat->getKind(), (*aRef)->id()))
1681 // search the feature result that contains sub-shape selected
1682 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1684 // take all sub-results or one result
1685 std::list<ResultPtr> aRefFeatResults;
1686 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1687 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1688 for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1689 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1690 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1691 aResults.push_back(aBody);
1693 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1695 if (theAnyValue) { // searching the best sub-result by maximum number of references to orig
1696 int aReferencesCount = 0;
1697 ResultPtr aBestResult;
1698 for (; aResIter != aResults.end(); aResIter++) {
1699 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1701 TDF_Label aCandidateLab =
1702 std::dynamic_pointer_cast<Model_Data>((*aResIter)->data())->shapeLab();
1703 Handle(TDF_Reference) aRef;
1704 if (aCandidateLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
1705 TDF_Label aRefLab = aRef->Get();
1706 ResultPtr aRefRes = aDoc->resultByLab(aRefLab);
1707 if (aRefRes.get() && aRefRes->shape().get() &&
1708 aRefRes->shape()->isEqual(aResult->shape())) {// it directly references to result
1709 aResult = *aResIter; // found new context (produced from this) with same subshape
1710 aFindNewContext = true; // continue searching further
1714 if (!aBestResult.get())
1715 aBestResult = *aResIter;
1718 if (aBestResult.get() && !aFindNewContext) { // the first good result for now
1719 aResult = aBestResult; // found new context
1720 aFindNewContext = true;
1722 } else { // searching by sub-shape
1723 for (; aResIter != aResults.end(); aResIter++) {
1724 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1726 GeomShapePtr aShape = (*aResIter)->shape();
1727 if (aShape.get() && (theAnyValue || aShape->isSubShape(aSelectedShape, false))) {
1728 aResult = *aResIter; // found new context (produced from this) with same subshape
1729 aFindNewContext = true; // continue searching further
1737 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
1738 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1739 if (aComp && aComp->numberOfSubs()) {
1740 std::list<ResultPtr> allSubs;
1741 ModelAPI_Tools::allSubs(aComp, allSubs);
1742 std::list<ResultPtr>::iterator aS = allSubs.begin();
1743 for (; aS != allSubs.end(); aS++) {
1744 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
1745 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
1746 (theAnyValue || aSub->shape()->isSubShape(aSelectedShape))) {
1752 // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
1753 // so, actually, the sketch element was selected (which is never concealed)
1754 if (aResult != theCurrent && aResult->isConcealed())
1755 aResult = theCurrent;
1759 void Model_AttributeSelection::combineGeometrical()
1761 if (myTmpContext.get() || myTmpSubShape.get())
1763 TDF_Label aSelLab = selectionLabel();
1764 if (aSelLab.IsAttribute(kINVALID_SELECTION) || !myRef.isInitialized())
1767 if (aSelLab.IsAttribute(kCIRCLE_CENTER) || aSelLab.IsAttribute(kELLIPSE_CENTER1) ||
1768 aSelLab.IsAttribute(kELLIPSE_CENTER2) || aSelLab.IsAttribute(kSIMPLE_REF_ID))
1771 if (aSelLab.IsAttribute(kPART_REF_ID)) {
1772 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
1773 if (!aPart.get() || !aPart->isActivated())
1774 return; // postponed naming needed
1775 Handle(TDataStd_Integer) anIndex;
1776 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
1777 if (anIndex->Get()) { // special selection attribute was created, use it
1778 std::string aNewName;
1779 aPart->combineGeometrical(anIndex->Get(), aNewName);
1780 TDataStd_Name::Set(aSelLab, aNewName.c_str());
1786 std::shared_ptr<Model_ResultConstruction> aConstr =
1787 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
1790 FeaturePtr aFeature = contextFeature();
1794 Selector_Selector aSelector(aSelLab, baseDocumentLab());
1795 TopoDS_Shape aContextShape = context()->shape()->impl<TopoDS_Shape>();
1796 if (aSelector.restore(aContextShape)) {
1797 aSelector.combineGeometrical(aContextShape);
1801 TDF_Label Model_AttributeSelection::baseDocumentLab()
1803 if (ModelAPI_Session::get()->moduleDocument() != owner()->document())
1804 return std::dynamic_pointer_cast<Model_Document>
1805 (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel();
1806 static TDF_Label anEmpty;