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 if (myRef.value().get())
424 // check that this is on open of document, so, results are not initialized yet
425 TDF_Label aRefLab = myRef.myRef->Get();
426 if (aRefLab.IsNull() || !owner().get())
428 std::shared_ptr<Model_Document> aMyDoc =
429 std::dynamic_pointer_cast<Model_Document>(owner()->document());
432 // check at least the feature exists
433 return aMyDoc->featureByLab(aRefLab).get() != NULL;
440 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
442 myIsGeometricalSelection(false)
444 myIsInitialized = myRef.isInitialized();
448 void Model_AttributeSelection::setID(const std::string theID)
451 ModelAPI_AttributeSelection::setID(theID);
452 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
454 // to be able to select as geometrical selection and then - split
455 myIsGeometricalSelection = true; // myParent->isGeometricalSelection();
457 myIsGeometricalSelection =
458 ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
462 ResultPtr Model_AttributeSelection::context()
464 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
467 if (myTmpContext.get() || myTmpSubShape.get()) {
471 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
472 // for parts there could be same-data result, so take the last enabled
474 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
475 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
476 for(int a = aSize - 1; a >= 0; a--) {
477 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
478 if(aPart.get() && aPart->data() == aResult->data()) {
479 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
480 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
481 // check that this result is not this-feature result (it is forbidden to select itself)
482 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
488 } else { // if feature - construction is selected, it has only one result, return this result
489 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
490 if (aFeature.get() && aFeature->results().size() == 1 &&
491 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
492 return aFeature->firstResult();
497 FeaturePtr Model_AttributeSelection::contextFeature() {
498 if (myTmpContext.get() || myTmpSubShape.get()) {
499 return FeaturePtr(); // feature can not be selected temporarily
501 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
503 ObjectPtr Model_AttributeSelection::contextObject() {
504 FeaturePtr aRes = contextFeature();
511 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
513 ModelAPI_AttributeSelection::setObject(theObject);
514 myRef.setObject(theObject);
517 /// Sets the invalid flag if flag is false, or removes it if "true"
519 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
521 theLab.ForgetAttribute(kINVALID_SELECTION);
523 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
528 void Model_AttributeSelection::split(
529 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
531 TopTools_ListOfShape aSubs;
532 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
533 if (!anExplorer.Value().IsNull() &&
534 anExplorer.Value().ShapeType() == theType) {
535 aSubs.Append(anExplorer.Value());
536 } else { // invalid case; bad result shape, so, impossible to split easily
541 if (aSubs.Extent() > 1) { // ok to split
542 TopTools_ListIteratorOfListOfShape aSub(aSubs);
543 GeomShapePtr aSubSh(new GeomAPI_Shape);
544 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
545 setValue(theContext, aSubSh);
546 for(aSub.Next(); aSub.More(); aSub.Next()) {
547 GeomShapePtr aSubSh(new GeomAPI_Shape);
548 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
549 myParent->append(theContext, aSubSh);
554 bool Model_AttributeSelection::update()
556 FeaturePtr aContextFeature = contextFeature();
557 if (aContextFeature.get()) {
558 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
561 TDF_Label aSelLab = selectionLabel();
562 ResultPtr aContext = context();
564 return setInvalidIfFalse(aSelLab, false);
565 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
566 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
569 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
570 std::shared_ptr<GeomAPI_Shape> aNoSelection;
571 bool aResult = selectPart(aContext, aNoSelection, true);
572 aResult = setInvalidIfFalse(aSelLab, aResult);
574 owner()->data()->sendAttributeUpdated(this);
579 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
580 // body: just a named shape, use topological selection mechanism
581 bool aResult = false;
582 TopoDS_Shape anOldShape;
583 Handle(TNaming_NamedShape) aNS;
584 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
585 anOldShape = aNS->Get();
587 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
588 Selector_Selector aSelector(aSelLab, baseDocumentLab());
589 aResult = aSelector.restore(aContextShape);
590 setInvalidIfFalse(aSelLab, aResult);
592 TopoDS_Shape aNewShape;
593 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
594 aNewShape = aNS->Get();
596 if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape)) {
597 // shape type should not be changed: if shape becomes compound of such shapes, then split
598 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
599 anOldShape.ShapeType() != aNewShape.ShapeType() &&
600 aNewShape.ShapeType() == TopAbs_COMPOUND) {
601 split(aContext, aNewShape, anOldShape.ShapeType());
603 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
608 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
610 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
611 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
612 if (!aConstructionContext->isInfinite()) {
613 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
614 Selector_Selector aSelector(aSelLab, baseDocumentLab());
615 TopoDS_Shape anOldShape = aSelector.value();
616 aResult = aSelector.restore(aContextShape);
617 setInvalidIfFalse(aSelLab, aResult);
618 if (aResult && !anOldShape.IsEqual(aSelector.value()))
619 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
621 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
625 return setInvalidIfFalse(aSelLab, false); // unknown case
628 void Model_AttributeSelection::selectBody(
629 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
631 // perform the selection
632 TopoDS_Shape aContext;
634 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
636 aContext = aBody->shape()->impl<TopoDS_Shape>();
639 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
641 aContext = aResult->shape()->impl<TopoDS_Shape>();
643 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
648 if (!aContext.IsNull()) {
649 TDF_Label aSelLab = selectionLabel();
650 TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
652 bool aSelectorOk = true;
653 Selector_Selector aSelector(aSelLab, baseDocumentLab());
655 aSelectorOk = aSelector.select(aContext, aNewSub, myIsGeometricalSelection);
657 aSelectorOk = aSelector.store(aContext);
663 TopoDS_Shape aShape = aSelector.value();
664 aSelectorOk = !aShape.IsNull() && aShape.ShapeType() == aNewSub.ShapeType();
666 setInvalidIfFalse(aSelLab, aSelectorOk);
670 bool Model_AttributeSelection::selectPart(
671 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
672 const bool theUpdate)
674 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
675 if (!aPart.get() || !aPart->isActivated())
676 return true; // postponed naming
678 Handle(TDataStd_Integer) anIndex;
679 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
680 // by internal selection
681 if (anIndex->Get() > 0) {
682 // update the selection by index
683 return aPart->updateInPart(anIndex->Get());
685 return true; // nothing to do, referencing just by name
688 return true; // nothing to do, referencing just by name
690 // store the shape (in case part is not loaded it should be useful
692 std::string aName = theContext->data()->name();
693 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
694 aShape = theContext->shape()->impl<TopoDS_Shape>();
696 aShape = theSubShape->impl<TopoDS_Shape>();
698 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
699 TDataStd_Integer::Set(selectionLabel(), anIndex);
701 TNaming_Builder aBuilder(selectionLabel());
702 aBuilder.Select(aShape, aShape);
703 // identify by name in the part
704 TDataStd_Name::Set(selectionLabel(), aName.c_str());
705 return !aName.empty();
708 TDF_Label Model_AttributeSelection::selectionLabel()
710 return myRef.myRef->Label().FindChild(1);
713 /// prefixes of the shape names with centers defined
714 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
716 /// returns the map that contains all possible prefixes of the center-names
717 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
719 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
720 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
721 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
722 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
724 return kCENTERS_PREFIX;
727 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
729 std::string aName("");
730 if(!this->isInitialized())
731 return !theDefaultName.empty() ? theDefaultName : aName;
733 CenterType aCenterType = NOT_CENTER;
734 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
735 ResultPtr aCont = context();
738 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
739 // selection of a full feature
740 FeaturePtr aFeatureCont = contextFeature();
741 if (aFeatureCont.get()) {
742 return kWHOLE_FEATURE + aFeatureCont->name();
744 // in case of selection of removed result
748 TDF_Label aSelLab = selectionLabel();
749 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
750 return contextName(aCont);
753 // if it is in result of another part
754 if (aCont->groupName() == ModelAPI_ResultPart::group()) {
755 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCont);
757 GeomShapePtr aValue = value();
759 return aPart->data()->name() + "/" + aPart->nameInPart(aValue, anIndex);
761 return aPart->data()->name();
765 // whole infinitive construction
766 if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
767 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
768 if (aConstr->isInfinite()) {
769 return contextName(aCont);
773 Selector_Selector aSelector(aSelLab, baseDocumentLab());
775 if (aCont->shape().get() && aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
776 aResult = aSelector.name(this);
777 if (aCenterType != NOT_CENTER) {
778 aResult += centersMap()[aCenterType];
783 // returns the center type and modifies the shape name if this name is center-name
784 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
786 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
787 centersMap().begin();
788 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
789 std::size_t aFound = theShapeName.find(aPrefixIter->second);
790 if (aFound != std::string::npos &&
791 aFound == theShapeName.size() - aPrefixIter->second.size()) {
792 theShapeName = theShapeName.substr(0, aFound);
793 return aPrefixIter->first;
796 return ModelAPI_AttributeSelection::NOT_CENTER;
799 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
800 void Model_AttributeSelection::selectSubShape(
801 const std::string& theType, const std::string& theSubShapeName)
803 if(theSubShapeName.empty() || theType.empty()) return;
805 std::string aSubShapeName = theSubShapeName;
806 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
807 centerTypeByName(aSubShapeName) : NOT_CENTER;
808 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
809 static const GeomShapePtr anEmptyShape;
811 // first iteration is selection by name without center prefix, second - in case of problem,
812 // try with initial name
813 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
814 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
815 aSubShapeName = theSubShapeName;
816 aCenterType = NOT_CENTER;
818 } else if (aUseCenter != 1) continue;
819 TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(aType));
821 std::shared_ptr<Model_Document> aDoc =
822 std::dynamic_pointer_cast<Model_Document>(owner()->document());
823 // check this is Part-name: 2 delimiters in the name
824 std::size_t aPartEnd = aSubShapeName.find('/');
825 if (aPartEnd != std::string::npos) {
826 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
827 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
828 if (aPartName == aRootDoc->kind()) {
829 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
830 aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
833 owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
834 if (aFound.get()) { // found such part, so asking it for the name
835 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
836 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
837 if (aNameInPart.empty()) { // whole part
838 setValue(aPart, anEmptyShape);
842 std::shared_ptr<GeomAPI_Shape> aSelected =
843 aPart->shapeInPart(aNameInPart, aType, anIndex);
844 if (aSelected.get()) {
845 if (aCenterType != NOT_CENTER) {
846 if (!aSelected->isEdge())
848 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
849 setValueCenter(aPart, aSelectedEdge, aCenterType);
851 setValue(aPart, aSelected);
852 TDataStd_Integer::Set(selectionLabel(), anIndex);
859 // check this is a whole feature context
860 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
861 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
862 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
863 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
865 setValue(anObj, anEmptyShape);
870 // the whole result selection check
871 if (aSubShapeName.find('/') == std::string::npos) {
872 ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
874 aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
876 setValue(aRes, anEmptyShape);
881 Selector_Selector aSelector(selectionLabel(), baseDocumentLab());
882 myRestoreDocument = aDoc;
883 TDF_Label aContextLabel = aSelector.restoreByName(
884 aSubShapeName, aShapeType, this, myIsGeometricalSelection);
885 myRestoreDocument.reset();
886 if (!aContextLabel.IsNull()) {
887 ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
888 if (aContext.get() && aContext->shape().get()) {
889 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
890 if (aSelector.solve(aContextShape)) {
891 TopoDS_Shape aSelectorShape = aSelector.value();
892 GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
893 aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelectorShape));
894 // make the context result the latest existing
895 aContext = newestContext(aContext, aShapeToBeSelected);
896 if (myIsGeometricalSelection || aCenterType == NOT_CENTER) {
897 // store the currently generated name
898 selectionLabel().ForgetAllAttributes(true);
899 bool aToUnblock = false;
900 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
901 myRef.setValue(aContext);
902 aSelector.store(aContextShape);
903 owner()->data()->sendAttributeUpdated(this);
905 owner()->data()->blockSendAttributeUpdated(false);
907 } else { // re-select center of circle/arc by context and value
908 if (!aShapeToBeSelected->isEdge())
910 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
911 setValueCenter(aContext, aSelectedEdge, aCenterType);
919 TDF_Label aSelLab = selectionLabel();
920 setInvalidIfFalse(aSelLab, false);
924 void Model_AttributeSelection::selectSubShape(const std::string& theType,
925 const GeomPointPtr& thePoint)
927 if (theType.empty() || !thePoint)
930 // list of parent features
931 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
932 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
934 int aSelectionIndex = 0;
935 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
936 if (aType == GeomAPI_Shape::SHAPE) {
937 // possibly, the string consists of the type and the index,
938 // thus, try to separate them
939 size_t aUndersporePos = theType.find_first_of('_');
940 if (aUndersporePos != std::string::npos)
941 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
943 if (aType != GeomAPI_Shape::SHAPE) {
944 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
945 aChar != theType.end(); ++aChar) {
946 if (std::isdigit(*aChar))
947 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
953 aSelectionIndex -= 1;
957 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
959 // collect features from PartSet and the current part
960 SessionPtr aSession = ModelAPI_Session::get();
961 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
962 if (aSession->moduleDocument() != owner()->document()) {
963 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
964 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
966 // Process results of all features from the last to the first
967 // to find appropriate sub-shape
968 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
969 anIt != aFeatures.rend(); ++anIt) {
970 // selection cannot be linked to the parent features
971 if (aParents.find(*anIt) != aParents.end())
973 // check the feature is a part of composite feature (like sketch elements),
974 // then do not process it, it will be processed in scope of composite feature
975 bool isSubOfComposite = false;
976 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
977 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
978 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
979 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
980 CompositeFeaturePtr aCompFeature =
981 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
982 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
984 if (isSubOfComposite)
987 // process results of the current feature to find appropriate sub-shape
988 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
989 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
990 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
991 ++anApIt; // skip this shape, because one of the previous is selected
993 if (anApIt != anAppropriate.end()) {
994 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
995 setValue(anApIt->myResult, anApIt->mySubshape);
997 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
998 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1004 TDF_Label aSelLab = selectionLabel();
1005 setInvalidIfFalse(aSelLab, false);
1009 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1010 const std::string& theContextName, const int theIndex)
1012 // selection of context by name
1013 selectSubShape(theType, theContextName);
1014 ResultPtr aContext = context();
1015 if (aContext.get()) {
1016 GeomShapePtr aContShape = aContext->shape();
1017 if (aContShape.get()) {
1018 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1019 GeomShapePtr aValue = aNExp.shape(theIndex);
1021 setValue(aContext, aValue);
1026 void Model_AttributeSelection::setId(int theID)
1028 std::shared_ptr<GeomAPI_Shape> aSelection;
1030 ResultPtr aContextRes = context();
1031 // support for compsolids:
1032 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1033 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1035 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1037 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1038 // searching for the latest main shape
1039 if (theID > 0 && aContext && !aContext->isNull())
1041 std::shared_ptr<Model_Document> aDoc =
1042 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1044 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1045 if (!aNS.IsNull()) {
1046 aMainShape = TNaming_Tool::CurrentShape(aNS);
1050 TopTools_IndexedMapOfShape aSubShapesMap;
1051 TopExp::MapShapes(aMainShape, aSubShapesMap);
1052 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1054 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1055 aResult->setImpl(new TopoDS_Shape(aSelShape));
1057 aSelection = aResult;
1060 setValue(aContextRes, aSelection);
1063 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1065 std::string aResult;
1066 if (owner()->document() != theContext->document()) {
1067 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1068 aResult = theContext->document()->kind() + "/";
1070 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1071 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1072 if (aDocRes.get()) {
1073 aResult = aDocRes->data()->name() + "/";
1077 aResult += theContext->data()->name();
1081 void Model_AttributeSelection::computeValues(
1082 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1083 TopTools_ListOfShape& theShapes)
1085 bool aWasWholeContext = theValShape.IsNull();
1086 if (aWasWholeContext) {
1087 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1089 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1090 // if a new value is unchanged in the new context, do nothing: value is correct
1091 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1092 for(; aSubExp.More(); aSubExp.Next()) {
1093 if (aSubExp.Current().IsSame(theValShape)) {
1094 theShapes.Append(theValShape);
1098 // if new context becomes compsolid, the resulting sub may be in sub-solids
1099 std::list<ResultPtr> aNewToIterate;
1100 aNewToIterate.push_back(theNewContext);
1101 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1103 std::list<ResultPtr> allNewContextSubs;
1104 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1105 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1106 for(; aSub != allNewContextSubs.end(); aSub++) {
1107 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1108 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1109 aNewToIterate.push_back(aBody);
1113 // first iteration: searching for the whole shape appearance (like face of the box)
1114 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1115 int aToFindPart = 0;
1116 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1117 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1118 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1119 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1120 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1121 std::shared_ptr<Model_Data> aNewData =
1122 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1123 TDF_Label aNewLab = aNewData->shapeLab();
1124 // searching for produced sub-shape fully on some label
1125 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID());
1126 for(; aNSIter.More(); aNSIter.Next()) {
1127 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1128 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1129 if (aToFindPart == 0) { // search shape is fully inside
1130 if (aPairIter.OldShape().IsSame(theValShape)) {
1131 if (aPairIter.NewShape().IsNull()) {// value was removed
1135 // don't add edges generated from faces
1136 if (aPairIter.NewShape().ShapeType() <= theValShape.ShapeType())
1137 theShapes.Append(aPairIter.NewShape());
1139 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1140 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1141 for(; anExp.More(); anExp.Next()) {
1142 if (anExp.Current().IsSame(theValShape)) { // found a new container
1143 if (aPairIter.NewShape().IsNull()) {// value was removed
1147 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1148 anOlds.Add(aPairIter.OldShape());
1157 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1158 // also iterate the whole old shape to find not-modified shapes that contain this old
1159 TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1160 NCollection_Map<TopAbs_ShapeEnum> aNewTypes; // types of shapes to iterate
1161 TopTools_DataMapOfShapeShape::Iterator aNewTypeIter(aNewToOld);
1162 for(; aNewTypeIter.More(); aNewTypeIter.Next()) {
1163 if (aNewTypeIter.Key().ShapeType() != theValShape.ShapeType())
1164 aNewTypes.Add(aNewTypeIter.Key().ShapeType());
1166 NCollection_Map<TopAbs_ShapeEnum>::Iterator aTypeIter(aNewTypes);
1167 for(; aTypeIter.More(); aTypeIter.Next()) {
1168 for(TopExp_Explorer anExp(anOldContShape, aTypeIter.Value()); anExp.More(); anExp.Next()) {
1169 TopoDS_Shape anOld = anExp.Current();
1170 if (aNewToOld.IsBound(anOld) || anOlds.Contains(anOld)) // this was modified
1172 TopExp_Explorer aValExp(anOld, theValShape.ShapeType());
1173 for(; aValExp.More(); aValExp.Next()) {
1174 const TopoDS_Shape& anUnchanged = aValExp.Current();
1175 if (anUnchanged.IsSame(theValShape)) {
1176 aNewToOld.Bind(anOld, anOld);
1184 // map of sub-shapes -> number of occurrences of these shapes in containers
1185 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1186 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1187 for(; aContIter.More(); aContIter.Next()) {
1188 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1189 for(; aSubExp.More(); aSubExp.Next()) {
1190 if (!aSubs.IsBound(aSubExp.Current())) {
1191 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1193 // store old to know how many olds produced this shape
1194 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1197 // if sub is appeared same times in containers as the number of old shapes that contain it
1198 int aCountInOld = anOlds.Size();
1199 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1201 for(; aSubsIter.More(); aSubsIter.Next()) {
1202 if (aSubsIter.Value().Size() == aCountInOld) {
1203 TopoDS_Shape anOld = aSubsIter.Key();
1204 // check this exists in the new shape
1205 TopExp_Explorer aNew(aNewContShape, anOld.ShapeType());
1206 for (; aNew.More(); aNew.Next()) {
1207 if (aNew.Current().IsSame(anOld))
1211 theShapes.Append(anOld);
1215 if (theShapes.IsEmpty()) { // nothing was changed
1216 if (aWasWholeContext)
1217 theShapes.Append(TopoDS_Shape());
1218 else { // if theValShape exists in new context, add it without changes, otherwise - nothing
1219 for (TopExp_Explorer aNew(aNewContShape, theValShape.ShapeType()); aNew.More(); aNew.Next()){
1220 if (aNew.Current().IsSame(theValShape)) {
1221 theShapes.Append(theValShape);
1229 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1230 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1231 TDF_Label theAccessLabel,
1232 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1234 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1235 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1236 TopTools_ListOfShape aContextList;
1237 aContextList.Append(theContShape);
1238 if (theContext.get()) {
1239 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1240 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1241 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1243 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1244 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1245 for(; aModifIter.More(); aModifIter.Next()) {
1246 TDF_Label anObjLab = aModifIter.Label().Father();
1247 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1248 (theDoc->objects()->object(anObjLab));
1249 if (!aModifierObj.get()) {
1250 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1251 if (!anObjLab.IsRoot())
1252 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1253 (theDoc->objects()->object(anObjLab.Father()));
1254 if (!aModifierObj.get())
1257 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1258 if (!aModifierFeat.get())
1260 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1261 if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
1262 continue; // the modifier feature is later than this, so, should not be used
1263 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1264 if (aCurrentModifierFeat == aModifierFeat ||
1265 !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
1266 continue; // the current modifier is later than the found, so, useless
1267 Handle(TNaming_NamedShape) aNewNS;
1268 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1269 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1270 aResults.insert(aModifierObj);
1271 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1272 aResults.insert(ResultPtr());
1273 } else { // not-processed modification => don't support it
1278 // if there exist context composite and sub-result(s), leave only sub(s)
1279 for(std::set<ResultPtr>::iterator aResIter = aResults.begin(); aResIter != aResults.end();) {
1280 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1281 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1282 if (aResults.count(aParent))
1284 if (aParent.get()) { // erase from set, so, restart iteration
1285 aResults.erase(aParent);
1286 aResIter = aResults.begin();
1290 if (aResults.empty()) {
1291 // check the context become concealed by operation which is earlier than this selection
1292 std::list<ResultPtr> allRes;
1293 ResultPtr aRoot = ModelAPI_Tools::bodyOwner(theContext, true);
1296 ResultBodyPtr aRootBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aRoot);
1297 if (aRootBody.get()) {
1298 ModelAPI_Tools::allSubs(aRootBody, allRes);
1299 allRes.push_back(aRootBody);
1301 allRes.push_back(aRoot);
1303 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1304 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1305 ResultPtr aResCont = *aSub;
1306 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1307 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1308 for (; aRef != aRefs.end(); aRef++) {
1309 if (!aRef->get() || !(*aRef)->owner().get())
1311 // concealed attribute only
1312 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1313 if (aRefFeat == aThisFeature)
1315 if (!ModelAPI_Session::get()->validators()->isConcealed(
1316 aRefFeat->getKind(), (*aRef)->id()))
1318 if (theDoc->isLaterByDep(aThisFeature, aRefFeat)) {
1319 // for extrusion cut in python script the nested sketch reference may be concealed before
1320 // it is nested, so, check this composite feature is valid
1321 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
1322 // need to be validated to update the "Apply" state if not previewed
1323 if (aFactory->validate(aRefFeat))
1324 return true; // feature conceals result, return true, so the context will be removed
1328 return false; // no modifications found, must stay the same
1330 // iterate all results to find further modifications
1331 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1332 for(; aResIter != aResults.end(); aResIter++) {
1333 if (aResIter->get() != NULL) {
1334 // compute new values by two contexts: the old and the new
1335 TopTools_ListOfShape aValShapes;
1336 computeValues(theContext, *aResIter, theValShape, aValShapes);
1338 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1339 for(; aNewVal.More(); aNewVal.Next()) {
1340 std::list<ResultPtr> aNewRes;
1341 TopTools_ListOfShape aNewUpdatedVal;
1342 TopoDS_Shape aNewValSh = aNewVal.Value();
1343 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1344 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1345 aNewValSh.Nullify();
1346 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1347 theAccessLabel, aNewRes, aNewUpdatedVal))
1349 // append new results instead of the current ones
1350 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1351 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1352 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1353 theResults.push_back(*aNewIter);
1354 theValShapes.Append(aNewUpdVal.Value());
1356 } else { // the current result is good
1357 theResults.push_back(*aResIter);
1358 theValShapes.Append(aNewValSh);
1363 return true; // theResults must be empty: everything is deleted
1366 void Model_AttributeSelection::updateInHistory(bool& theRemove)
1368 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1369 // only bodies and parts may be modified later in the history, don't do anything otherwise
1370 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1371 aContext->groupName() != ModelAPI_ResultPart::group()))
1373 std::shared_ptr<Model_Document> aDoc =
1374 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1375 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1376 if (!aContData.get() || !aContData->isValid())
1378 TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
1379 Handle(TNaming_NamedShape) aContNS;
1380 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1381 bool aFoundNewContext = true;
1382 ResultPtr aNewContext = aContext;
1383 while(aFoundNewContext) {
1384 aFoundNewContext = false;
1385 // parts have no shape in result, so, trace references using the Part info
1386 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1387 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1388 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1389 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1390 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1391 for(; aRef != aRefs.end(); aRef++) {
1392 // to avoid detection of part changes by local selection only
1393 AttributeSelectionPtr aSel =
1394 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1395 if (aSel.get() && aSel->value().get() &&
1396 !aSel->value()->isSame(aSel->context()->shape()))
1399 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1400 if (aRefFeat.get() && aRefFeat != owner()) {
1401 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1402 if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
1403 aFoundNewContext = true;
1404 aNewContext = aRefFeat->firstResult();
1411 if (aNewContext != aContext) {
1412 setValue(aNewContext, value());
1416 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1417 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1418 // iterate the context shape modifications in order to find a feature that is upper in history
1419 // that this one and is really modifies the referenced result to refer to it
1420 ResultPtr aModifierResFound;
1421 TNaming_Iterator aPairIter(aContNS);
1422 if (!aPairIter.More())
1424 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1425 bool anIterate = true;
1426 // trying to update also the sub-shape selected
1427 GeomShapePtr aSubShape = value();
1428 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1430 TopoDS_Shape aValShape;
1431 if (aSubShape.get()) {
1432 aValShape = aSubShape->impl<TopoDS_Shape>();
1435 std::list<ResultPtr> aNewContexts;
1436 TopTools_ListOfShape aValShapes;
1437 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1439 std::set<ResultPtr> allContexts, aSkippedContext;
1440 std::list<ResultPtr>::iterator aNewContext = aNewContexts.begin();
1441 for(; aNewContext != aNewContexts.end(); aNewContext++)
1442 allContexts.insert(*aNewContext);
1444 // if there exist context composite and sub-result(s), leave only sub(s)
1445 std::set<ResultPtr>::iterator aResIter = allContexts.begin();
1446 for(; aResIter != allContexts.end(); aResIter++) {
1447 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1448 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1449 if (allContexts.count(aParent))
1450 aSkippedContext.insert(aParent);
1453 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1455 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1456 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1457 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1460 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1461 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1462 bool aFirst = true; // first is set to this, next are appended to parent
1463 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1464 if (aSkippedContext.count(*aNewCont))
1467 GeomShapePtr aValueShape;
1468 if (!aNewValues.Value().IsNull()) {
1469 aValueShape = std::make_shared<GeomAPI_Shape>();
1470 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1472 // Check that list has the same type of shape selection before adding.
1473 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1474 if (aValueShape.get()) {
1475 aShapeShapeType = aValueShape->shapeType();
1477 aShapeShapeType = (*aNewCont)->shape()->shapeType();
1479 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1484 setValue(*aNewCont, aValueShape);
1486 } else if (myParent) {
1487 if (!myParent->isInList(*aNewCont, aValueShape)) // avoid addition of duplicates
1488 myParent->append(*aNewCont, aValueShape);
1491 if (aFirst) { // nothing was added, all results were deleted
1495 ResultPtr anEmptyContext;
1496 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1497 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1504 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1506 myParent = theParent;
1509 std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
1511 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1512 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1513 FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1514 bool aBaseDocumnetUsed = false;
1515 if (!aFeatureOwner.get()) { // use module document
1516 aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
1517 aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1518 aBaseDocumnetUsed = true;
1520 if (aFeatureOwner.get()) {
1521 // if it is sub-element of the sketch, the context name is the name of the sketch
1522 // searching also for result - real context
1524 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
1525 if (aComposite.get() && aComposite->results().size() == 1 &&
1526 aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
1527 aFeatureOwner = aComposite;
1528 aResult = aFeatureOwner->firstResult();
1530 aResult = aDoc->resultByLab(theSelectionLab);
1532 if (aResult.get()) {
1533 // this is to avoid duplicated names of results problem
1534 std::string aContextName = aResult->data()->name();
1535 // myLab corresponds to the current time
1536 TDF_Label aCurrentLab = selectionLabel();
1537 while(aCurrentLab.Depth() > 3)
1538 aCurrentLab = aCurrentLab.Father();
1540 int aNumInHistoryNames =
1541 aDoc->numberOfNameInHistory(aResult, aCurrentLab);
1542 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
1543 aContextName = "_" + aContextName;
1544 aNumInHistoryNames--;
1546 if (aBaseDocumnetUsed)
1547 aContextName = aDoc->kind() + "/" + aContextName;
1548 return aContextName;
1551 return ""; // invalid case
1554 /// This method restores by the context and value name the context label and
1555 /// sub-label where the value is. Returns true if it is valid.
1556 bool Model_AttributeSelection::restoreContext(std::string theName,
1557 TDF_Label& theContext, TDF_Label& theValue)
1559 static const GeomShapePtr anEmptyShape; // to store context only
1560 std::string aName = theName;
1561 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1562 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1564 // remove the sub-value part if exists
1565 std::string aSubShapeName = aName;
1566 std::string::size_type n = aName.find('/');
1567 if (n != std::string::npos) {
1568 aName = aName.substr(0, n);
1571 if (aName.empty()) return false;
1572 bool anUniqueContext = false;
1573 ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1574 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
1576 aDoc = std::dynamic_pointer_cast<Model_Document>(
1577 ModelAPI_Session::get()->moduleDocument());
1578 if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
1579 aSubShapeName = theName.substr(aDoc->kind().size() + 1);
1580 aName = aSubShapeName;
1581 std::string::size_type n = aName.find('/');
1582 if (n != std::string::npos) {
1583 aName = aName.substr(0, n);
1586 aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1587 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
1591 // searching the sub-shape
1592 static const ResultPtr anEmpty;
1593 theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
1595 // sketch sub-component shape and name is located in separated feature label, try the sub-name
1596 if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
1597 std::string::size_type aSlash = aSubShapeName.rfind('/');
1598 if (aSlash != std::string::npos) {
1599 std::string aCompName = aSubShapeName.substr(aSlash + 1);
1600 CompositeFeaturePtr aComposite =
1601 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
1602 if (aComposite.get() && aComposite->numberOfSubs()) {
1603 const int aSubNum = aComposite->numberOfSubs();
1604 for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
1605 FeaturePtr aSub = aComposite->subFeature(a);
1606 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1607 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
1608 for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
1609 if ((*aRes)->data()->name() == aCompName) {
1610 theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1612 } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
1613 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1614 TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
1615 for(; aSubNames.More(); aSubNames.Next()) {
1616 if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
1617 IsEqual(aCompName.c_str())) {
1618 theValue = aSubNames.Value()->Label();
1630 theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
1635 bool Model_AttributeSelection::isLater(
1636 const TDF_Label theResult1, const TDF_Label theResult2) const
1638 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1639 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1640 FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
1643 FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
1646 return aDoc->isLaterByDep(aFeat1, aFeat2);
1649 ResultPtr Model_AttributeSelection::newestContext(
1650 const ResultPtr theCurrent, const GeomShapePtr theValue)
1652 ResultPtr aResult = theCurrent;
1653 GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
1654 std::shared_ptr<Model_Document> aDoc =
1655 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1656 bool aFindNewContext = true;
1657 while (aFindNewContext && aResult.get()) {
1658 aFindNewContext = false;
1659 // try to find the last context to find the up to date shape
1660 TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
1661 if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
1662 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1663 if (!aNS.IsNull()) {
1664 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1666 && isLater(selectionLabel(), aNS->Label())
1667 && isLater(aNS->Label(),
1668 std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
1670 TDF_Label aLab = aNS->Label();
1671 ResultPtr aRes = aDoc->resultByLab(aLab);
1673 if (aRes->shape()->isSubShape(aSelectedShape)) {
1675 aFindNewContext = true;
1683 // TestFillWireVertex.py - sketch constructions for wire may participate too
1684 //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
1685 // try to search newer context by the concealment references
1686 // take references to all results: root one, any sub
1687 std::list<ResultPtr> allRes;
1688 ResultPtr aCompContext;
1689 ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
1690 if (aCompBody.get()) {
1691 ModelAPI_Tools::allSubs(aCompBody, allRes);
1692 allRes.push_back(aCompBody);
1693 aCompContext = aCompBody;
1696 allRes.push_back(aResult);
1698 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1699 ResultPtr aResCont = *aSub;
1700 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1701 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1702 continue; // only lower and higher level subs are counted
1703 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1704 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1705 for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1706 if (!aRef->get() || !(*aRef)->owner().get())
1708 // concealed attribute only
1709 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1710 if (!ModelAPI_Session::get()->validators()->isConcealed(
1711 aRefFeat->getKind(), (*aRef)->id()))
1713 // search the feature result that contains sub-shape selected
1714 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1716 // take all sub-results or one result
1717 std::list<ResultPtr> aRefFeatResults;
1718 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1719 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1720 for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1721 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1722 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1723 aResults.push_back(aBody);
1725 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1727 // searching by sub-shape
1728 for (; aResIter != aResults.end(); aResIter++) {
1729 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1731 GeomShapePtr aShape = (*aResIter)->shape();
1732 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
1733 aResult = *aResIter; // found new context (produced from this) with same subshape
1734 aFindNewContext = true; // continue searching further
1741 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
1742 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1743 if (aComp && aComp->numberOfSubs()) {
1744 std::list<ResultPtr> allSubs;
1745 ModelAPI_Tools::allSubs(aComp, allSubs);
1746 std::list<ResultPtr>::iterator aS = allSubs.begin();
1747 for (; aS != allSubs.end(); aS++) {
1748 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
1749 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
1750 aSub->shape()->isSubShape(aSelectedShape)) {
1756 // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
1757 // so, actually, the sketch element was selected (which is never concealed)
1758 if (aResult != theCurrent && aResult->isConcealed())
1759 aResult = theCurrent;
1763 void Model_AttributeSelection::combineGeometrical()
1765 if (myTmpContext.get() || myTmpSubShape.get())
1767 TDF_Label aSelLab = selectionLabel();
1768 if (aSelLab.IsAttribute(kINVALID_SELECTION) || !myRef.isInitialized())
1771 if (aSelLab.IsAttribute(kCIRCLE_CENTER) || aSelLab.IsAttribute(kELLIPSE_CENTER1) ||
1772 aSelLab.IsAttribute(kELLIPSE_CENTER2) || aSelLab.IsAttribute(kSIMPLE_REF_ID))
1775 if (aSelLab.IsAttribute(kPART_REF_ID)) {
1776 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
1777 if (!aPart.get() || !aPart->isActivated())
1778 return; // postponed naming needed
1779 Handle(TDataStd_Integer) anIndex;
1780 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
1781 if (anIndex->Get()) { // special selection attribute was created, use it
1782 std::string aNewName;
1783 aPart->combineGeometrical(anIndex->Get(), aNewName);
1784 TDataStd_Name::Set(aSelLab, aNewName.c_str());
1790 std::shared_ptr<Model_ResultConstruction> aConstr =
1791 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
1794 FeaturePtr aFeature = contextFeature();
1798 Selector_Selector aSelector(aSelLab, baseDocumentLab());
1799 TopoDS_Shape aContextShape = context()->shape()->impl<TopoDS_Shape>();
1800 if (aSelector.restore(aContextShape)) {
1801 aSelector.combineGeometrical(aContextShape);
1805 TDF_Label Model_AttributeSelection::baseDocumentLab()
1807 if (ModelAPI_Session::get()->moduleDocument() != owner()->document())
1808 return std::dynamic_pointer_cast<Model_Document>
1809 (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel();
1810 static TDF_Label anEmpty;