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 if (aConstruction->isInfinite()) {
182 // For correct naming selection, put the shape into the naming structure.
183 // It seems sub-shapes are not needed: only this shape is (and can be) selected.
185 TNaming_Builder aBuilder(aSelLab);
186 aBuilder.Generated(aConstruction->shape()->impl<TopoDS_Shape>());
187 std::string anInfinitiveName = contextName(aConstruction);
188 TDataStd_Name::Set(aSelLab, anInfinitiveName.c_str());
189 std::dynamic_pointer_cast<Model_Document>(owner()->document())
190 ->addNamingName(aSelLab, anInfinitiveName.c_str());
193 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
194 aSelLab.ForgetAllAttributes(true);
195 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
196 selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
197 } else { // check the feature context: parent-Part of this feature should not be used
198 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
199 if (aFeatureContext.get()) {
200 if (owner()->document() != aFeatureContext->document()) {
201 aSelLab.ForgetAllAttributes(true);
202 myRef.setValue(ObjectPtr());
204 owner()->data()->blockSendAttributeUpdated(false);
210 owner()->data()->sendAttributeUpdated(this);
213 owner()->data()->blockSendAttributeUpdated(false);
218 void Model_AttributeSelection::setValueCenter(
219 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
220 const CenterType theCenterType, const bool theTemporarily)
222 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
223 if (theTemporarily) {
224 myTmpCenterType = theCenterType;
225 } else { // store in the data structure
226 TDF_Label aSelLab = selectionLabel();
227 switch(theCenterType) {
230 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
231 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
233 case ELLIPSE_FIRST_FOCUS:
235 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
236 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
238 case ELLIPSE_SECOND_FOCUS:
240 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
241 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
245 owner()->data()->sendAttributeUpdated(this);
249 void Model_AttributeSelection::selectValue(
250 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
253 std::shared_ptr<GeomAPI_Shape> aValue =
254 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
255 if (!aValue.get() || aType == NOT_CENTER) {
256 setValue(theSource->context(), aValue);
258 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
259 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
260 setValueCenter(theSource->context(), anEdge, aType);
264 void Model_AttributeSelection::removeTemporaryValues()
266 if (myTmpContext.get() || myTmpSubShape.get()) {
267 myTmpContext.reset();
268 myTmpSubShape.reset();
272 // returns the center of the edge: circular or elliptical
273 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
275 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
276 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
277 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
278 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
279 double aFirst, aLast;
280 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
281 if (!aCurve.IsNull()) {
282 TopoDS_Vertex aVertex;
283 BRep_Builder aBuilder;
284 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
285 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
286 if (!aCirc.IsNull()) {
287 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
290 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
291 if (!anEll.IsNull()) {
292 aBuilder.MakeVertex(aVertex,
293 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
294 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
297 if (!aVertex.IsNull()) {
298 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
299 aResult->setImpl(new TopoDS_Vertex(aVertex));
305 return theEdge; // no vertex, so, return the initial edge
308 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
310 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
311 return std::shared_ptr<GeomAPI_Shape>();
312 CenterType aType = NOT_CENTER;
313 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
314 return centerByEdge(aResult, aType);
317 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
319 theType = NOT_CENTER;
320 GeomShapePtr aResult;
321 if (myTmpContext.get() || myTmpSubShape.get()) {
322 theType = myTmpCenterType;
323 ResultConstructionPtr aResulConstruction =
324 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
325 if(aResulConstruction.get()) {
326 // it is just reference to construction.
327 return myTmpSubShape;
329 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
332 TDF_Label aSelLab = selectionLabel();
333 if (aSelLab.IsAttribute(kINVALID_SELECTION))
336 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
337 theType = CIRCLE_CENTER;
338 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
339 theType = ELLIPSE_FIRST_FOCUS;
340 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
341 theType = ELLIPSE_SECOND_FOCUS;
344 if (myRef.isInitialized()) {
345 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
346 ResultPtr aContext = context();
347 if (!aContext.get() || aContext->groupName() == ModelAPI_ResultConstruction::group())
348 return aResult; // empty result, for whole construction selection also
349 return aContext->shape();
351 if (aSelLab.IsAttribute(kPART_REF_ID)) {
352 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
353 if (!aPart.get() || !aPart->isActivated())
354 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
355 Handle(TDataStd_Integer) anIndex;
356 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
357 if (anIndex->Get()) { // special selection attribute was created, use it
358 return aPart->selectionValue(anIndex->Get());
359 } else { // face with name is already in the data model, so try to take it by name
360 Handle(TDataStd_Name) aName;
361 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
362 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
363 std::size_t aPartEnd = aSubShapeName.find('/');
364 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
365 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
367 std::string aType; // to reuse already existing selection the type is not needed
368 return aPart->shapeInPart(aNameInPart, aType, anIndex);
375 std::shared_ptr<Model_ResultConstruction> aConstr =
376 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
378 if (aConstr->isInfinite())
379 return aResult; // empty result
381 if (!aConstr.get()) { // for construction context, return empty result as usual even
382 // the whole feature is selected
383 FeaturePtr aFeature = contextFeature();
384 if (aFeature.get()) {
385 std::list<GeomShapePtr> allShapes;
386 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
387 for (; aRes != aFeature->results().cend(); aRes++) {
388 if (aRes->get() && !(*aRes)->isDisabled()) {
389 GeomShapePtr aShape = (*aRes)->shape();
390 if (aShape.get() && !aShape->isNull()) {
391 allShapes.push_back(aShape);
395 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
398 if (contextFeature().get()) {
399 return aResult; // for the whole sketch feature selected return null => all faces
403 Handle(TNaming_NamedShape) aSelection;
404 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
405 TopoDS_Shape aSelShape = aSelection->Get();
406 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
407 aResult->setImpl(new TopoDS_Shape(aSelShape));
408 } else if (aConstr) { // simple construction element: just shape of this construction element
409 aResult = aConstr->shape();
415 bool Model_AttributeSelection::isInvalid()
417 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
420 bool Model_AttributeSelection::isInitialized()
422 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
423 std::shared_ptr<GeomAPI_Shape> aResult;
424 if (myRef.isInitialized()) {
425 TDF_Label aSelLab = selectionLabel();
426 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
427 ResultPtr aContext = context();
428 return aContext.get() != NULL;
430 Handle(TNaming_NamedShape) aSelection;
431 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
432 return !aSelection->Get().IsNull();
433 } else { // for simple construction element: just shape of this construction element
434 std::shared_ptr<Model_ResultConstruction> aConstr =
435 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
439 // for the whole feature, a feature object
440 FeaturePtr aFeat = contextFeature();
449 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
451 myIsGeometricalSelection(false)
453 myIsInitialized = myRef.isInitialized();
457 void Model_AttributeSelection::setID(const std::string theID)
460 ModelAPI_AttributeSelection::setID(theID);
461 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
463 myIsGeometricalSelection = myParent->isGeometricalSelection();
465 myIsGeometricalSelection =
466 ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
470 ResultPtr Model_AttributeSelection::context()
472 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
475 if (myTmpContext.get() || myTmpSubShape.get()) {
479 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
480 // for parts there could be same-data result, so take the last enabled
482 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
483 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
484 for(int a = aSize - 1; a >= 0; a--) {
485 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
486 if(aPart.get() && aPart->data() == aResult->data()) {
487 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
488 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
489 // check that this result is not this-feature result (it is forbidden to select itself)
490 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
496 } else { // if feature - construction is selected, it has only one result, return this result
497 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
498 if (aFeature.get() && aFeature->results().size() == 1 &&
499 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
500 return aFeature->firstResult();
505 FeaturePtr Model_AttributeSelection::contextFeature() {
506 if (myTmpContext.get() || myTmpSubShape.get()) {
507 return FeaturePtr(); // feature can not be selected temporarily
509 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
511 ObjectPtr Model_AttributeSelection::contextObject() {
512 FeaturePtr aRes = contextFeature();
519 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
521 ModelAPI_AttributeSelection::setObject(theObject);
522 myRef.setObject(theObject);
525 /// Sets the invalid flag if flag is false, or removes it if "true"
527 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
529 theLab.ForgetAttribute(kINVALID_SELECTION);
531 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
536 void Model_AttributeSelection::split(
537 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
539 TopTools_ListOfShape aSubs;
540 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
541 if (!anExplorer.Value().IsNull() &&
542 anExplorer.Value().ShapeType() == theType) {
543 aSubs.Append(anExplorer.Value());
544 } else { // invalid case; bad result shape, so, impossible to split easily
549 if (aSubs.Extent() > 1) { // ok to split
550 TopTools_ListIteratorOfListOfShape aSub(aSubs);
551 GeomShapePtr aSubSh(new GeomAPI_Shape);
552 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
553 setValue(theContext, aSubSh);
554 for(aSub.Next(); aSub.More(); aSub.Next()) {
555 GeomShapePtr aSubSh(new GeomAPI_Shape);
556 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
557 myParent->append(theContext, aSubSh);
562 bool Model_AttributeSelection::update()
564 FeaturePtr aContextFeature = contextFeature();
565 if (aContextFeature.get()) {
566 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
569 TDF_Label aSelLab = selectionLabel();
570 ResultPtr aContext = context();
572 return setInvalidIfFalse(aSelLab, false);
573 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
574 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
577 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
578 std::shared_ptr<GeomAPI_Shape> aNoSelection;
579 bool aResult = selectPart(aContext, aNoSelection, true);
580 aResult = setInvalidIfFalse(aSelLab, aResult);
582 owner()->data()->sendAttributeUpdated(this);
587 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
588 // body: just a named shape, use topological selection mechanism
589 bool aResult = false;
590 TopoDS_Shape anOldShape;
591 Handle(TNaming_NamedShape) aNS;
592 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
593 anOldShape = aNS->Get();
595 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
596 Selector_Selector aSelector(aSelLab, baseDocumentLab());
597 aResult = aSelector.restore(aContextShape);
598 setInvalidIfFalse(aSelLab, aResult);
600 TopoDS_Shape aNewShape;
601 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
602 aNewShape = aNS->Get();
604 if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape)) {
605 // shape type should not be changed: if shape becomes compound of such shapes, then split
606 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
607 anOldShape.ShapeType() != aNewShape.ShapeType() &&
608 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
610 split(aContext, aNewShape, anOldShape.ShapeType());
612 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
617 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
619 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
620 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
621 if (!aConstructionContext->isInfinite()) {
622 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
623 Selector_Selector aSelector(aSelLab, baseDocumentLab());
624 TopoDS_Shape anOldShape = aSelector.value();
625 aResult = aSelector.restore(aContextShape);
626 setInvalidIfFalse(aSelLab, aResult);
627 if (aResult && !anOldShape.IsEqual(aSelector.value()))
628 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
630 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
634 return setInvalidIfFalse(aSelLab, false); // unknown case
637 void Model_AttributeSelection::selectBody(
638 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
640 // perform the selection
641 TopoDS_Shape aContext;
643 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
645 aContext = aBody->shape()->impl<TopoDS_Shape>();
648 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
650 aContext = aResult->shape()->impl<TopoDS_Shape>();
652 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
657 if (!aContext.IsNull()) {
658 TDF_Label aSelLab = selectionLabel();
659 TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
661 bool aSelectorOk = true;
662 Selector_Selector aSelector(aSelLab, baseDocumentLab());
664 aSelectorOk = aSelector.select(aContext, aNewSub, myIsGeometricalSelection);
666 aSelectorOk = aSelector.store(aContext);
672 TopoDS_Shape aShape = aSelector.value();
673 aSelectorOk = !aShape.IsNull() && aShape.ShapeType() == aNewSub.ShapeType();
675 setInvalidIfFalse(aSelLab, aSelectorOk);
679 bool Model_AttributeSelection::selectPart(
680 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
681 const bool theUpdate)
683 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
684 if (!aPart.get() || !aPart->isActivated())
685 return true; // postponed naming
687 Handle(TDataStd_Integer) anIndex;
688 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
689 // by internal selection
690 if (anIndex->Get() > 0) {
691 // update the selection by index
692 return aPart->updateInPart(anIndex->Get());
694 return true; // nothing to do, referencing just by name
697 return true; // nothing to do, referencing just by name
699 // store the shape (in case part is not loaded it should be useful
701 std::string aName = theContext->data()->name();
702 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
703 aShape = theContext->shape()->impl<TopoDS_Shape>();
705 aShape = theSubShape->impl<TopoDS_Shape>();
707 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
708 TDataStd_Integer::Set(selectionLabel(), anIndex);
710 TNaming_Builder aBuilder(selectionLabel());
711 aBuilder.Select(aShape, aShape);
712 // identify by name in the part
713 TDataStd_Name::Set(selectionLabel(), aName.c_str());
714 return !aName.empty();
717 TDF_Label Model_AttributeSelection::selectionLabel()
719 return myRef.myRef->Label().FindChild(1);
722 /// prefixes of the shape names with centers defined
723 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
725 /// returns the map that contains all possible prefixes of the center-names
726 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
728 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
729 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
730 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
731 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
733 return kCENTERS_PREFIX;
736 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
738 std::string aName("");
739 if(!this->isInitialized())
740 return !theDefaultName.empty() ? theDefaultName : aName;
742 CenterType aCenterType = NOT_CENTER;
743 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
744 ResultPtr aCont = context();
747 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
748 // selection of a full feature
749 FeaturePtr aFeatureCont = contextFeature();
750 if (aFeatureCont.get()) {
751 return kWHOLE_FEATURE + aFeatureCont->name();
753 // in case of selection of removed result
757 TDF_Label aSelLab = selectionLabel();
758 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
759 return contextName(aCont);
762 // if it is in result of another part
763 if (aCont->groupName() == ModelAPI_ResultPart::group()) {
764 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCont);
766 GeomShapePtr aValue = value();
768 return aPart->data()->name() + "/" + aPart->nameInPart(aValue, anIndex);
770 return aPart->data()->name();
774 // whole infinitive construction
775 if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
776 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
777 if (aConstr->isInfinite()) {
778 return contextName(aCont);
782 Selector_Selector aSelector(aSelLab, baseDocumentLab());
784 if (aCont->shape().get() && aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
785 aResult = aSelector.name(this);
786 if (aCenterType != NOT_CENTER) {
787 aResult += centersMap()[aCenterType];
792 // returns the center type and modifies the shape name if this name is center-name
793 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
795 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
796 centersMap().begin();
797 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
798 std::size_t aFound = theShapeName.find(aPrefixIter->second);
799 if (aFound != std::string::npos &&
800 aFound == theShapeName.size() - aPrefixIter->second.size()) {
801 theShapeName = theShapeName.substr(0, aFound);
802 return aPrefixIter->first;
805 return ModelAPI_AttributeSelection::NOT_CENTER;
808 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
809 void Model_AttributeSelection::selectSubShape(
810 const std::string& theType, const std::string& theSubShapeName)
812 if(theSubShapeName.empty() || theType.empty()) return;
814 std::string aSubShapeName = theSubShapeName;
815 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
816 centerTypeByName(aSubShapeName) : NOT_CENTER;
817 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
818 static const GeomShapePtr anEmptyShape;
820 // first iteration is selection by name without center prefix, second - in case of problem,
821 // try with initial name
822 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
823 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
824 aSubShapeName = theSubShapeName;
825 aCenterType = NOT_CENTER;
827 } else if (aUseCenter != 1) continue;
828 TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(aType));
830 std::shared_ptr<Model_Document> aDoc =
831 std::dynamic_pointer_cast<Model_Document>(owner()->document());
832 // check this is Part-name: 2 delimiters in the name
833 std::size_t aPartEnd = aSubShapeName.find('/');
834 if (aPartEnd != std::string::npos) {
835 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
836 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
837 if (aPartName == aRootDoc->kind()) {
838 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
839 aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
842 owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
843 if (aFound.get()) { // found such part, so asking it for the name
844 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
845 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
846 if (aNameInPart.empty()) { // whole part
847 setValue(aPart, anEmptyShape);
851 std::shared_ptr<GeomAPI_Shape> aSelected =
852 aPart->shapeInPart(aNameInPart, aType, anIndex);
853 if (aSelected.get()) {
854 if (aCenterType != NOT_CENTER) {
855 if (!aSelected->isEdge())
857 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
858 setValueCenter(aPart, aSelectedEdge, aCenterType);
860 setValue(aPart, aSelected);
861 TDataStd_Integer::Set(selectionLabel(), anIndex);
868 // check this is a whole feature context
869 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
870 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
871 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
872 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
874 setValue(anObj, anEmptyShape);
879 // the whole result selection check
880 if (aSubShapeName.find('/') == std::string::npos) {
881 ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
883 aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
885 setValue(aRes, anEmptyShape);
890 Selector_Selector aSelector(selectionLabel(), baseDocumentLab());
891 myRestoreDocument = aDoc;
892 TDF_Label aContextLabel = aSelector.restoreByName(
893 aSubShapeName, aShapeType, this, myIsGeometricalSelection);
894 myRestoreDocument.reset();
895 if (!aContextLabel.IsNull()) {
896 ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
897 if (aContext.get() && aContext->shape().get()) {
898 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
899 if (aSelector.solve(aContextShape)) {
900 TopoDS_Shape aSelectorShape = aSelector.value();
901 GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
902 aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelectorShape));
903 // make the context result the latest existing
904 aContext = newestContext(aContext, aShapeToBeSelected);
905 if (myIsGeometricalSelection || aCenterType == NOT_CENTER) {
906 // store the currently generated name
907 selectionLabel().ForgetAllAttributes(true);
908 bool aToUnblock = false;
909 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
910 myRef.setValue(aContext);
911 aSelector.store(aContextShape);
912 owner()->data()->sendAttributeUpdated(this);
914 owner()->data()->blockSendAttributeUpdated(false);
916 } else { // re-select center of circle/arc by context and value
917 if (!aShapeToBeSelected->isEdge())
919 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
920 setValueCenter(aContext, aSelectedEdge, aCenterType);
928 TDF_Label aSelLab = selectionLabel();
929 setInvalidIfFalse(aSelLab, false);
933 void Model_AttributeSelection::selectSubShape(const std::string& theType,
934 const GeomPointPtr& thePoint)
936 if (theType.empty() || !thePoint)
939 // list of parent features
940 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
941 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
943 int aSelectionIndex = 0;
944 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
945 if (aType == GeomAPI_Shape::SHAPE) {
946 // possibly, the string consists of the type and the index,
947 // thus, try to separate them
948 size_t aUndersporePos = theType.find_first_of('_');
949 if (aUndersporePos != std::string::npos)
950 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
952 if (aType != GeomAPI_Shape::SHAPE) {
953 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
954 aChar != theType.end(); ++aChar) {
955 if (std::isdigit(*aChar))
956 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
962 aSelectionIndex -= 1;
966 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
968 // collect features from PartSet and the current part
969 SessionPtr aSession = ModelAPI_Session::get();
970 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
971 if (aSession->moduleDocument() != owner()->document()) {
972 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
973 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
975 // Process results of all features from the last to the first
976 // to find appropriate sub-shape
977 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
978 anIt != aFeatures.rend(); ++anIt) {
979 // selection cannot be linked to the parent features
980 if (aParents.find(*anIt) != aParents.end())
982 // check the feature is a part of composite feature (like sketch elements),
983 // then do not process it, it will be processed in scope of composite feature
984 bool isSubOfComposite = false;
985 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
986 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
987 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
988 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
989 CompositeFeaturePtr aCompFeature =
990 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
991 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
993 if (isSubOfComposite)
996 // process results of the current feature to find appropriate sub-shape
997 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
998 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
999 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1000 ++anApIt; // skip this shape, because one of the previous is selected
1002 if (anApIt != anAppropriate.end()) {
1003 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1004 setValue(anApIt->myResult, anApIt->mySubshape);
1006 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1007 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1013 TDF_Label aSelLab = selectionLabel();
1014 setInvalidIfFalse(aSelLab, false);
1018 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1019 const std::string& theContextName, const int theIndex)
1021 // selection of context by name
1022 selectSubShape(theType, theContextName);
1023 ResultPtr aContext = context();
1024 if (aContext.get()) {
1025 GeomShapePtr aContShape = aContext->shape();
1026 if (aContShape.get()) {
1027 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1028 GeomShapePtr aValue = aNExp.shape(theIndex);
1030 setValue(aContext, aValue);
1035 int Model_AttributeSelection::Id()
1038 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1039 ResultPtr aContextRes = context();
1040 // support for compsolids:
1041 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1042 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1044 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1047 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1048 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1049 // searching for the latest main shape
1050 if (aSelection && !aSelection->isNull() && aContext && !aContext->isNull())
1052 std::shared_ptr<Model_Document> aDoc =
1053 std::dynamic_pointer_cast<Model_Document>(context()->document());
1055 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1056 if (!aNS.IsNull()) {
1057 aMainShape = TNaming_Tool::CurrentShape(aNS);
1061 TopTools_IndexedMapOfShape aSubShapesMap;
1062 TopExp::MapShapes(aMainShape, aSubShapesMap);
1063 anID = aSubShapesMap.FindIndex(aSubShape);
1068 void Model_AttributeSelection::setId(int theID)
1070 std::shared_ptr<GeomAPI_Shape> aSelection;
1072 ResultPtr aContextRes = context();
1073 // support for compsolids:
1074 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1075 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1077 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1079 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1080 // searching for the latest main shape
1081 if (theID > 0 && aContext && !aContext->isNull())
1083 std::shared_ptr<Model_Document> aDoc =
1084 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1086 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1087 if (!aNS.IsNull()) {
1088 aMainShape = TNaming_Tool::CurrentShape(aNS);
1092 TopTools_IndexedMapOfShape aSubShapesMap;
1093 TopExp::MapShapes(aMainShape, aSubShapesMap);
1094 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1096 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1097 aResult->setImpl(new TopoDS_Shape(aSelShape));
1099 aSelection = aResult;
1102 setValue(aContextRes, aSelection);
1105 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1107 std::string aResult;
1108 if (owner()->document() != theContext->document()) {
1109 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1110 aResult = theContext->document()->kind() + "/";
1112 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1113 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1114 if (aDocRes.get()) {
1115 aResult = aDocRes->data()->name() + "/";
1119 aResult += theContext->data()->name();
1123 void Model_AttributeSelection::computeValues(
1124 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1125 TopTools_ListOfShape& theShapes)
1127 bool aWasWholeContext = theValShape.IsNull();
1128 if (aWasWholeContext) {
1129 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1131 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1132 // if a new value is unchanged in the new context, do nothing: value is correct
1133 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1134 for(; aSubExp.More(); aSubExp.Next()) {
1135 if (aSubExp.Current().IsSame(theValShape)) {
1136 theShapes.Append(theValShape);
1140 // if new context becomes compsolid, the resulting sub may be in sub-solids
1141 std::list<ResultPtr> aNewToIterate;
1142 aNewToIterate.push_back(theNewContext);
1143 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1145 std::list<ResultPtr> allNewContextSubs;
1146 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1147 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1148 for(; aSub != allNewContextSubs.end(); aSub++) {
1149 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1150 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1151 aNewToIterate.push_back(aBody);
1155 // first iteration: searching for the whole shape appearance (like face of the box)
1156 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1157 int aToFindPart = 0;
1158 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1159 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1160 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1161 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1162 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1163 std::shared_ptr<Model_Data> aNewData =
1164 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1165 TDF_Label aNewLab = aNewData->shapeLab();
1166 // searching for produced sub-shape fully on some label
1167 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID());
1168 for(; aNSIter.More(); aNSIter.Next()) {
1169 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1170 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1171 if (aToFindPart == 0) { // search shape is fully inside
1172 if (aPairIter.OldShape().IsSame(theValShape)) {
1173 if (aPairIter.NewShape().IsNull()) {// value was removed
1177 theShapes.Append(aPairIter.NewShape());
1179 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1180 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1181 for(; anExp.More(); anExp.Next()) {
1182 if (anExp.Current().IsSame(theValShape)) { // found a new container
1183 if (aPairIter.NewShape().IsNull()) {// value was removed
1187 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1188 anOlds.Add(aPairIter.OldShape());
1197 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1198 // map of sub-shapes -> number of occurrences of these shapes in containers
1199 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1200 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1201 for(; aContIter.More(); aContIter.Next()) {
1202 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1203 for(; aSubExp.More(); aSubExp.Next()) {
1204 if (!aSubs.IsBound(aSubExp.Current())) {
1205 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1207 // store old to know how many olds produced this shape
1208 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1211 // if sub is appeared same times in containers as the number of old shapes that contain it
1212 int aCountInOld = anOlds.Size();
1213 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1215 for(; aSubsIter.More(); aSubsIter.Next()) {
1216 if (aSubsIter.Value().Size() == aCountInOld) {
1217 theShapes.Append(aSubsIter.Key());
1221 if (theShapes.IsEmpty()) { // nothing was changed
1222 theShapes.Append(aWasWholeContext ? TopoDS_Shape() : theValShape);
1226 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1227 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1228 TDF_Label theAccessLabel,
1229 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1231 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1232 TopTools_ListOfShape aResContShapes;
1233 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1234 TopTools_ListOfShape aContextList;
1235 aContextList.Append(theContShape);
1236 if (theContext.get()) {
1237 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1238 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1239 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1241 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1242 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1243 for(; aModifIter.More(); aModifIter.Next()) {
1244 TDF_Label anObjLab = aModifIter.Label().Father();
1245 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1246 (theDoc->objects()->object(anObjLab));
1247 if (!aModifierObj.get()) {
1248 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1249 if (!anObjLab.IsRoot())
1250 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1251 (theDoc->objects()->object(anObjLab.Father()));
1252 if (!aModifierObj.get())
1255 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1256 if (!aModifierFeat.get())
1258 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1259 if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
1260 continue; // the modifier feature is later than this, so, should not be used
1261 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1262 if (aCurrentModifierFeat == aModifierFeat ||
1263 !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
1264 continue; // the current modifier is later than the found, so, useless
1265 Handle(TNaming_NamedShape) aNewNS;
1266 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1267 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1268 aResults.insert(aModifierObj);
1269 aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
1270 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1271 aResults.insert(ResultPtr());
1272 } else { // not-processed modification => don't support it
1277 if (aResults.empty())
1278 return false; // no modifications found, must stay the same
1279 // iterate all results to find further modifications
1280 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1281 for(; aResIter != aResults.end(); aResIter++) {
1282 if (aResIter->get() != NULL) {
1283 // compute new values by two contexts: the old and the new
1284 TopTools_ListOfShape aValShapes;
1285 computeValues(theContext, *aResIter, theValShape, aValShapes);
1287 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1288 for(; aNewVal.More(); aNewVal.Next()) {
1289 std::list<ResultPtr> aNewRes;
1290 TopTools_ListOfShape aNewUpdatedVal;
1291 TopoDS_Shape aNewValSh = aNewVal.Value();
1292 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1293 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1294 aNewValSh.Nullify();
1295 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1296 theAccessLabel, aNewRes, aNewUpdatedVal))
1298 // append new results instead of the current ones
1299 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1300 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1301 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1302 theResults.push_back(*aNewIter);
1303 theValShapes.Append(aNewUpdVal.Value());
1305 } else { // the current result is good
1306 theResults.push_back(*aResIter);
1307 theValShapes.Append(aNewValSh);
1312 return true; // theResults must be empty: everything is deleted
1315 void Model_AttributeSelection::updateInHistory()
1317 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1318 // only bodies and parts may be modified later in the history, don't do anything otherwise
1319 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1320 aContext->groupName() != ModelAPI_ResultPart::group()))
1322 std::shared_ptr<Model_Document> aDoc =
1323 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1324 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1325 if (!aContData.get() || !aContData->isValid())
1327 TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
1328 Handle(TNaming_NamedShape) aContNS;
1329 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1330 bool aFoundNewContext = true;
1331 ResultPtr aNewContext = aContext;
1332 while(aFoundNewContext) {
1333 aFoundNewContext = false;
1334 // parts have no shape in result, so, trace references using the Part info
1335 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1336 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1337 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1338 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1339 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1340 for(; aRef != aRefs.end(); aRef++) {
1341 // to avoid detection of part changes by local selection only
1342 AttributeSelectionPtr aSel =
1343 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1344 if (aSel.get() && aSel->value().get() &&
1345 !aSel->value()->isSame(aSel->context()->shape()))
1348 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1349 if (aRefFeat.get() && aRefFeat != owner()) {
1350 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1351 if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
1352 aFoundNewContext = true;
1353 aNewContext = aRefFeat->firstResult();
1360 if (aNewContext != aContext) {
1361 setValue(aNewContext, value());
1365 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1366 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1367 // iterate the context shape modifications in order to find a feature that is upper in history
1368 // that this one and is really modifies the referenced result to refer to it
1369 ResultPtr aModifierResFound;
1370 TNaming_Iterator aPairIter(aContNS);
1371 if (!aPairIter.More())
1373 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1374 bool anIterate = true;
1375 // trying to update also the sub-shape selected
1376 GeomShapePtr aSubShape = value();
1377 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1379 TopoDS_Shape aValShape;
1380 if (aSubShape.get()) {
1381 aValShape = aSubShape->impl<TopoDS_Shape>();
1384 std::list<ResultPtr> aNewContexts;
1385 TopTools_ListOfShape aValShapes;
1386 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1388 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1390 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1391 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1392 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1395 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1396 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1397 bool aFirst = true; // first is set to this, next are appended to parent
1398 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1400 GeomShapePtr aValueShape;
1401 if (!aNewValues.Value().IsNull()) {
1402 aValueShape = std::make_shared<GeomAPI_Shape>();
1403 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1405 // Check that list has the same type of shape selection before adding.
1406 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1407 if (aValueShape.get()) {
1408 aShapeShapeType = aValueShape->shapeType();
1410 aShapeShapeType = (*aNewCont)->shape()->shapeType();
1412 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1416 setValue(*aNewCont, aValueShape);
1418 } else if (myParent) {
1419 myParent->append(*aNewCont, aValueShape);
1422 if (aFirst) { // nothing was added, all results were deleted
1423 ResultPtr anEmptyContext;
1424 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1425 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1431 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1433 myParent = theParent;
1436 std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
1438 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1439 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1440 FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1441 bool aBaseDocumnetUsed = false;
1442 if (!aFeatureOwner.get()) { // use module document
1443 aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
1444 aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1445 aBaseDocumnetUsed = true;
1447 if (aFeatureOwner.get()) {
1448 // if it is sub-element of the sketch, the context name is the name of the sketch
1449 // searching also for result - real context
1451 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
1452 if (aComposite.get() && aComposite->results().size() == 1 &&
1453 aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
1454 aFeatureOwner = aComposite;
1455 aResult = aFeatureOwner->firstResult();
1457 aResult = aDoc->resultByLab(theSelectionLab);
1459 if (aResult.get()) {
1460 // this is to avoid duplicated names of results problem
1461 std::string aContextName = aResult->data()->name();
1462 // myLab corresponds to the current time
1463 TDF_Label aCurrentLab = selectionLabel();
1464 while(aCurrentLab.Depth() > 3)
1465 aCurrentLab = aCurrentLab.Father();
1467 int aNumInHistoryNames =
1468 aDoc->numberOfNameInHistory(aResult, aCurrentLab);
1469 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
1470 aContextName = "_" + aContextName;
1471 aNumInHistoryNames--;
1473 if (aBaseDocumnetUsed)
1474 aContextName = aDoc->kind() + "/" + aContextName;
1475 return aContextName;
1478 return ""; // invalid case
1481 /// This method restores by the context and value name the context label and
1482 /// sub-label where the value is. Returns true if it is valid.
1483 bool Model_AttributeSelection::restoreContext(std::string theName,
1484 TDF_Label& theContext, TDF_Label& theValue)
1486 static const GeomShapePtr anEmptyShape; // to store context only
1487 std::string aName = theName;
1488 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1489 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1491 // remove the sub-value part if exists
1492 std::string aSubShapeName = aName;
1493 std::string::size_type n = aName.find('/');
1494 if (n != std::string::npos) {
1495 aName = aName.substr(0, n);
1498 if (aName.empty()) return false;
1499 bool anUniqueContext = false;
1500 ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1501 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
1503 aDoc = std::dynamic_pointer_cast<Model_Document>(
1504 ModelAPI_Session::get()->moduleDocument());
1505 if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
1506 aSubShapeName = theName.substr(aDoc->kind().size() + 1);
1507 aName = aSubShapeName;
1508 std::string::size_type n = aName.find('/');
1509 if (n != std::string::npos) {
1510 aName = aName.substr(0, n);
1513 aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1514 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
1518 // searching the sub-shape
1519 static const ResultPtr anEmpty;
1520 theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
1522 // sketch sub-component shape and name is located in separated feature label, try the sub-name
1523 if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
1524 std::string::size_type aSlash = aSubShapeName.rfind('/');
1525 if (aSlash != std::string::npos) {
1526 std::string aCompName = aSubShapeName.substr(aSlash + 1);
1527 CompositeFeaturePtr aComposite =
1528 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
1529 if (aComposite.get() && aComposite->numberOfSubs()) {
1530 const int aSubNum = aComposite->numberOfSubs();
1531 for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
1532 FeaturePtr aSub = aComposite->subFeature(a);
1533 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1534 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
1535 for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
1536 if ((*aRes)->data()->name() == aCompName) {
1537 theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1539 } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
1540 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1541 TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
1542 for(; aSubNames.More(); aSubNames.Next()) {
1543 if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
1544 IsEqual(aCompName.c_str())) {
1545 theValue = aSubNames.Value()->Label();
1557 theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
1562 TDF_Label Model_AttributeSelection::newestContext(const TDF_Label theCurrentContext) {
1563 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1564 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1565 ResultPtr aContext = aDoc->resultByLab(theCurrentContext);
1566 if (aContext.get()) {
1567 aContext = newestContext(aContext, GeomShapePtr(), true);
1569 return std::dynamic_pointer_cast<Model_Data>(aContext->data())->label();
1571 return theCurrentContext; // nothing is changed
1574 bool Model_AttributeSelection::isLater(
1575 const TDF_Label theResult1, const TDF_Label theResult2) const
1577 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1578 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1579 FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
1582 FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
1585 return aDoc->isLaterByDep(aFeat1, aFeat2);
1588 ResultPtr Model_AttributeSelection::newestContext(
1589 const ResultPtr theCurrent, const GeomShapePtr theValue, const bool theAnyValue)
1591 ResultPtr aResult = theCurrent;
1592 GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
1593 std::shared_ptr<Model_Document> aDoc =
1594 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1595 bool aFindNewContext = true;
1596 while (aFindNewContext && aResult.get()) {
1597 aFindNewContext = false;
1598 // try to find the last context to find the up to date shape
1599 TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
1600 if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
1601 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1602 if (!aNS.IsNull()) {
1603 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1605 && isLater(selectionLabel(), aNS->Label())
1606 && isLater(aNS->Label(),
1607 std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
1609 TDF_Label aLab = aNS->Label();
1610 ResultPtr aRes = aDoc->resultByLab(aLab);
1612 if (theAnyValue || aRes->shape()->isSubShape(aSelectedShape)) {
1614 aFindNewContext = true;
1621 if (theAnyValue) { // only for neighbors for now
1622 // try to find modification of sub-shapes: the best number of matches
1623 std::map<ResultPtr, int> aMatches; // result -> number of matches of shapes to find the best
1624 TDF_Label aResLab = std::dynamic_pointer_cast<Model_Data>(aResult->data())->shapeLab();
1625 TDF_ChildIDIterator aModifIter(aResLab, TNaming_NamedShape::GetID());
1626 for(; aModifIter.More(); aModifIter.Next()) {
1627 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aModifIter.Value());
1628 if (aNS->Evolution() == TNaming_MODIFY || aNS->Evolution() == TNaming_GENERATED) {
1629 for(TNaming_Iterator aNSIter(aNS); aNSIter.More(); aNSIter.Next()) {
1630 TNaming_NewShapeIterator aNewIter(aNSIter.NewShape(), aNS->Label());
1631 for(; aNewIter.More(); aNewIter.Next()) {
1632 TDF_Label aLab = aNewIter.Label();
1633 if (isLater(aLab, aNS->Label()) && isLater(selectionLabel(), aLab)) {
1634 ResultPtr aRes = aDoc->resultByLab(aLab);
1636 if (aMatches.find(aRes) == aMatches.end())
1638 aMatches[aRes]++; // found result, add matches
1645 // searching for the best result-candidate
1647 ResultPtr aBestResult;
1648 std::map<ResultPtr, int>::iterator aMatchIter = aMatches.begin();
1649 for(; aMatchIter != aMatches.end(); aMatchIter++) {
1650 if (aMatchIter->second > aBest) {
1651 aBest = aMatchIter->second;
1652 aBestResult = aMatchIter->first;
1655 if (aBestResult.get()) {
1656 aResult = aBestResult;
1657 aFindNewContext = true;
1663 // TestFillWireVertex.py - sketch constructions for wire may participate too
1664 //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
1665 // try to search newer context by the concealment references
1666 // take references to all results: root one, any sub
1667 std::list<ResultPtr> allRes;
1668 ResultPtr aCompContext;
1669 ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
1670 if (aCompBody.get()) {
1671 ModelAPI_Tools::allSubs(aCompBody, allRes);
1672 allRes.push_back(aCompBody);
1673 aCompContext = aCompBody;
1676 allRes.push_back(aResult);
1678 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1679 ResultPtr aResCont = *aSub;
1680 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1681 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1682 continue; // only lower and higher level subs are counted
1683 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1684 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1685 for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1686 if (!aRef->get() || !(*aRef)->owner().get())
1688 // concealed attribute only
1689 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1690 if (!ModelAPI_Session::get()->validators()->isConcealed(
1691 aRefFeat->getKind(), (*aRef)->id()))
1693 // search the feature result that contains sub-shape selected
1694 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1696 // take all sub-results or one result
1697 std::list<ResultPtr> aRefFeatResults;
1698 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1699 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1700 for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1701 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1702 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1703 aResults.push_back(aBody);
1705 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1707 if (theAnyValue) { // searching the best sub-result by maximum number of references to orig
1708 int aReferencesCount = 0;
1709 ResultPtr aBestResult;
1710 for (; aResIter != aResults.end(); aResIter++) {
1711 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1713 TDF_Label aCandidateLab =
1714 std::dynamic_pointer_cast<Model_Data>((*aResIter)->data())->shapeLab();
1715 Handle(TDF_Reference) aRef;
1716 if (aCandidateLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
1717 TDF_Label aRefLab = aRef->Get();
1718 ResultPtr aRefRes = aDoc->resultByLab(aRefLab);
1719 if (aRefRes.get() && aRefRes->shape().get() &&
1720 aRefRes->shape()->isEqual(aResult->shape())) {// it directly references to result
1721 aResult = *aResIter; // found new context (produced from this) with same subshape
1722 aFindNewContext = true; // continue searching further
1726 if (!aBestResult.get())
1727 aBestResult = *aResIter;
1730 if (aBestResult.get() && !aFindNewContext) { // the first good result for now
1731 aResult = aBestResult; // found new context
1732 aFindNewContext = true;
1734 } else { // searching by sub-shape
1735 for (; aResIter != aResults.end(); aResIter++) {
1736 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1738 GeomShapePtr aShape = (*aResIter)->shape();
1739 if (aShape.get() && (theAnyValue || aShape->isSubShape(aSelectedShape, false))) {
1740 aResult = *aResIter; // found new context (produced from this) with same subshape
1741 aFindNewContext = true; // continue searching further
1749 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
1750 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1751 if (aComp && aComp->numberOfSubs()) {
1752 std::list<ResultPtr> allSubs;
1753 ModelAPI_Tools::allSubs(aComp, allSubs);
1754 std::list<ResultPtr>::iterator aS = allSubs.begin();
1755 for (; aS != allSubs.end(); aS++) {
1756 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
1757 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
1758 (theAnyValue || aSub->shape()->isSubShape(aSelectedShape))) {
1764 // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
1765 // so, actually, the sketch element was selected (which is never concealed)
1766 if (aResult != theCurrent && aResult->isConcealed())
1767 aResult = theCurrent;
1771 void Model_AttributeSelection::combineGeometrical()
1773 if (myTmpContext.get() || myTmpSubShape.get())
1775 TDF_Label aSelLab = selectionLabel();
1776 if (aSelLab.IsAttribute(kINVALID_SELECTION) || !myRef.isInitialized())
1779 if (aSelLab.IsAttribute(kCIRCLE_CENTER) || aSelLab.IsAttribute(kELLIPSE_CENTER1) ||
1780 aSelLab.IsAttribute(kELLIPSE_CENTER2) || aSelLab.IsAttribute(kSIMPLE_REF_ID))
1783 if (aSelLab.IsAttribute(kPART_REF_ID)) {
1784 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
1785 if (!aPart.get() || !aPart->isActivated())
1786 return; // postponed naming needed
1787 Handle(TDataStd_Integer) anIndex;
1788 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
1789 if (anIndex->Get()) { // special selection attribute was created, use it
1790 std::string aNewName;
1791 aPart->combineGeometrical(anIndex->Get(), aNewName);
1792 TDataStd_Name::Set(aSelLab, aNewName.c_str());
1798 std::shared_ptr<Model_ResultConstruction> aConstr =
1799 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
1802 FeaturePtr aFeature = contextFeature();
1806 Selector_Selector aSelector(aSelLab, baseDocumentLab());
1807 TopoDS_Shape aContextShape = context()->shape()->impl<TopoDS_Shape>();
1808 if (aSelector.restore(aContextShape)) {
1809 aSelector.combineGeometrical(aContextShape);
1813 TDF_Label Model_AttributeSelection::baseDocumentLab()
1815 if (ModelAPI_Session::get()->moduleDocument() != owner()->document())
1816 return std::dynamic_pointer_cast<Model_Document>
1817 (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel();
1818 static TDF_Label anEmpty;