1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "Model_AttributeSelection.h"
21 #include "Model_Application.h"
22 #include "Model_Events.h"
23 #include "Model_Data.h"
24 #include "Model_Document.h"
25 #include <Model_Objects.h>
26 #include <Model_AttributeSelectionList.h>
27 #include <Model_ResultConstruction.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_ResultGroup.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 <Geom_TrimmedCurve.hxx>
73 #include <BRep_Builder.hxx>
75 //#define DEB_NAMING 1
77 #include <BRepTools.hxx>
79 /// added to the index in the packed map to signalize that the vertex of edge is selected
80 /// (multiplied by the index of the edge)
81 static const int kSTART_VERTEX_DELTA = 1000000;
82 // identifier that there is simple reference: selection equals to context
83 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
84 // reference to Part sub-object
85 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
86 // selection is invalid after recomputation
87 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
89 // identifier of the selection of the center of circle on edge
90 Standard_GUID kCIRCLE_CENTER("d0d0e0f1-217a-4b95-8fbb-0c4132f23718");
91 // identifier of the selection of the first focus point of ellipse on edge
92 Standard_GUID kELLIPSE_CENTER1("f70df04c-3168-4dc9-87a4-f1f840c1275d");
93 // identifier of the selection of the second focus point of ellipse on edge
94 Standard_GUID kELLIPSE_CENTER2("1395ae73-8e02-4cf8-b204-06ff35873a32");
96 // prefix for the whole feature context identification
97 const static std::string kWHOLE_FEATURE = "all-in-";
99 // on this label is stored:
100 // TNaming_NamedShape - selected shape
101 // TNaming_Naming - topological selection information (for the body)
102 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
103 // TDataStd_Integer - type of the selected shape (for construction)
104 // TDF_Reference - from ReferenceAttribute, the context
105 bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
106 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
108 if (theTemporarily) {
109 // just keep the stored without DF update
110 myTmpContext = 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 if (theContext->groupName() == ModelAPI_ResultGroup::group()) {
186 aSelLab.ForgetAllAttributes(true);
187 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
188 } else { // check the feature context: parent-Part of this feature should not be used
189 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
190 if (aFeatureContext.get()) {
191 if (owner()->document() != aFeatureContext->document()) {
192 aSelLab.ForgetAllAttributes(true);
193 myRef.setValue(ObjectPtr());
195 owner()->data()->blockSendAttributeUpdated(false);
201 owner()->data()->sendAttributeUpdated(this);
204 owner()->data()->blockSendAttributeUpdated(false);
209 void Model_AttributeSelection::setValueCenter(
210 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
211 const CenterType theCenterType, const bool theTemporarily)
213 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
214 if (theTemporarily) {
215 myTmpCenterType = theCenterType;
216 } else { // store in the data structure
217 TDF_Label aSelLab = selectionLabel();
218 switch(theCenterType) {
221 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
222 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
224 case ELLIPSE_FIRST_FOCUS:
226 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
227 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
229 case ELLIPSE_SECOND_FOCUS:
231 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
232 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
236 owner()->data()->sendAttributeUpdated(this);
240 void Model_AttributeSelection::selectValue(
241 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
244 std::shared_ptr<GeomAPI_Shape> aValue =
245 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
246 if (!aValue.get() || aType == NOT_CENTER) {
247 setValue(theSource->context(), aValue);
249 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
250 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
251 setValueCenter(theSource->context(), anEdge, aType);
255 void Model_AttributeSelection::removeTemporaryValues()
257 if (myTmpContext.get() || myTmpSubShape.get()) {
258 myTmpContext.reset();
259 myTmpSubShape.reset();
263 // returns the center of the edge: circular or elliptic
264 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
266 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
267 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
268 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
269 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
270 double aFirst, aLast;
271 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
272 if (!aCurve.IsNull()) {
273 TopoDS_Vertex aVertex;
274 BRep_Builder aBuilder;
275 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
276 while(!aCurve.IsNull() && aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
277 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
278 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
279 if (!aCirc.IsNull()) {
280 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
283 while(!aCurve.IsNull() && aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
284 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
285 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
286 if (!anEll.IsNull()) {
287 aBuilder.MakeVertex(aVertex,
288 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
289 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
292 if (!aVertex.IsNull()) {
293 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
294 aResult->setImpl(new TopoDS_Vertex(aVertex));
300 return theEdge; // no vertex, so, return the initial edge
303 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
305 if (!myRef.isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
306 return std::shared_ptr<GeomAPI_Shape>();
307 CenterType aType = NOT_CENTER;
308 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
309 return centerByEdge(aResult, aType);
312 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
314 theType = NOT_CENTER;
315 GeomShapePtr aResult;
316 if (myTmpContext.get() || myTmpSubShape.get()) {
317 theType = myTmpCenterType;
318 ResultConstructionPtr aResulConstruction =
319 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
320 if(aResulConstruction.get()) {
321 // it is just reference to construction.
322 return myTmpSubShape;
324 FeaturePtr aFeature =
325 std::dynamic_pointer_cast<ModelAPI_Feature>(myTmpContext);
326 if (aFeature.get()) {
327 // it is just reference to construction.
328 return myTmpSubShape;
330 return myTmpSubShape.get() ? myTmpSubShape :
331 std::dynamic_pointer_cast<ModelAPI_Result>(myTmpContext)->shape();
334 TDF_Label aSelLab = selectionLabel();
335 if (aSelLab.IsAttribute(kINVALID_SELECTION))
338 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
339 theType = CIRCLE_CENTER;
340 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
341 theType = ELLIPSE_FIRST_FOCUS;
342 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
343 theType = ELLIPSE_SECOND_FOCUS;
346 if (myRef.isInitialized()) {
347 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
348 ResultPtr aContext = context();
349 if (!aContext.get() || aContext->groupName() == ModelAPI_ResultConstruction::group())
350 return aResult; // empty result, for whole construction selection also
351 return aContext->shape();
353 if (aSelLab.IsAttribute(kPART_REF_ID)) {
354 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
355 if (!aPart.get() || !aPart->isActivated())
356 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
357 Handle(TDataStd_Integer) anIndex;
358 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
359 if (anIndex->Get()) { // special selection attribute was created, use it
360 return aPart->selectionValue(anIndex->Get());
361 } else { // face with name is already in the data model, so try to take it by name
362 Handle(TDataStd_Name) aName;
363 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
364 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
365 std::size_t aPartEnd = aSubShapeName.find('/');
366 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
367 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
369 std::string aType; // to reuse already existing selection the type is not needed
370 return aPart->shapeInPart(aNameInPart, aType, anIndex);
377 std::shared_ptr<Model_ResultConstruction> aConstr =
378 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
380 if (aConstr->isInfinite())
381 return aResult; // empty result
383 if (!aConstr.get()) { // for construction context, return empty result as usual even
384 // the whole feature is selected
385 FeaturePtr aFeature = contextFeature();
386 if (aFeature.get()) {
387 std::list<GeomShapePtr> allShapes;
388 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
389 for (; aRes != aFeature->results().cend(); aRes++) {
390 if (aRes->get() && !(*aRes)->isDisabled()) {
391 GeomShapePtr aShape = (*aRes)->shape();
392 if (aShape.get() && !aShape->isNull()) {
393 allShapes.push_back(aShape);
397 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
400 if (contextFeature().get()) {
401 return aResult; // for the whole sketch feature selected return null => all faces
405 Handle(TNaming_NamedShape) aSelection;
406 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
407 TopoDS_Shape aSelShape = aSelection->Get();
408 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
409 aResult->setImpl(new TopoDS_Shape(aSelShape));
410 } else if (aConstr) { // simple construction element: just shape of this construction element
411 aResult = aConstr->shape();
417 bool Model_AttributeSelection::isInvalid()
419 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
422 bool Model_AttributeSelection::isInitialized()
424 if (myRef.isInitialized()) {
425 TDF_Label aSelLab = selectionLabel();
426 // it is just reference to shape, not sub-shape
427 if (aSelLab.IsAttribute(kSIMPLE_REF_ID) || aSelLab.IsAttribute(kPART_REF_ID)) {
428 ResultPtr aContext = context();
429 return aContext.get() != NULL;
431 Handle(TNaming_NamedShape) aSelection;
432 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
433 return !aSelection->Get().IsNull();
434 } else { // for simple construction element: just shape of this construction element
435 if (myRef.value().get())
437 // check that this is on open of document, so, results are not initialized yet
438 TDF_Label aRefLab = myRef.myRef->Get();
439 if (aRefLab.IsNull() || !owner().get())
441 std::shared_ptr<Model_Document> aMyDoc =
442 std::dynamic_pointer_cast<Model_Document>(owner()->document());
445 // check at least the feature exists
446 return aMyDoc->featureByLab(aRefLab).get() != NULL;
452 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
454 myIsGeometricalSelection(false)
456 myIsInitialized = myRef.isInitialized();
460 void Model_AttributeSelection::setID(const std::string theID)
463 ModelAPI_AttributeSelection::setID(theID);
464 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
466 // to be able to select as geometrical selection and then - split
467 myIsGeometricalSelection = true; // myParent->isGeometricalSelection();
469 myIsGeometricalSelection =
470 ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
474 ResultPtr Model_AttributeSelection::context()
476 if (!myRef.isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
479 if (myTmpContext.get() || myTmpSubShape.get()) {
480 return std::dynamic_pointer_cast<ModelAPI_Result>(myTmpContext);
483 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
484 // for parts there could be same-data result, so take the last enabled
486 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
487 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
488 for(int a = aSize - 1; a >= 0; a--) {
489 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
490 if(aPart.get() && aPart->data() == aResult->data()) {
491 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
492 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
493 // check that this result is not this-feature result (it is forbidden to select itself)
494 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
500 } else { // if feature - construction is selected, it has only one result, return this result
501 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
502 if (aFeature.get() && aFeature->results().size() == 1 &&
503 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
504 return aFeature->firstResult();
509 FeaturePtr Model_AttributeSelection::contextFeature() {
510 if (myTmpContext.get()) {
511 return std::dynamic_pointer_cast<ModelAPI_Feature>(myTmpContext);
513 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
515 ObjectPtr Model_AttributeSelection::contextObject() {
516 FeaturePtr aRes = contextFeature();
523 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
525 ModelAPI_AttributeSelection::setObject(theObject);
526 myRef.setObject(theObject);
529 /// Sets the invalid flag if flag is false, or removes it if "true"
531 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
533 theLab.ForgetAttribute(kINVALID_SELECTION);
535 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
540 void Model_AttributeSelection::split(
541 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
543 TopTools_ListOfShape aSubs;
544 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
545 if (!anExplorer.Value().IsNull() &&
546 anExplorer.Value().ShapeType() == theType) {
547 aSubs.Append(anExplorer.Value());
548 } else { // invalid case; bad result shape, so, impossible to split easily
553 if (aSubs.Extent() > 1) { // ok to split
554 TopTools_ListIteratorOfListOfShape aSub(aSubs);
555 GeomShapePtr aSubSh(new GeomAPI_Shape);
556 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
557 setValue(theContext, aSubSh);
558 for(aSub.Next(); aSub.More(); aSub.Next()) {
559 GeomShapePtr aSubSh(new GeomAPI_Shape);
560 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
561 myParent->append(theContext, aSubSh);
566 bool Model_AttributeSelection::update()
568 FeaturePtr aContextFeature = contextFeature();
569 if (aContextFeature.get()) {
570 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
573 TDF_Label aSelLab = selectionLabel();
574 ResultPtr aContext = context();
576 return setInvalidIfFalse(aSelLab, false);
577 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
578 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
581 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
582 std::shared_ptr<GeomAPI_Shape> aNoSelection;
583 bool aResult = selectPart(aContext, aNoSelection, true);
584 aResult = setInvalidIfFalse(aSelLab, aResult);
586 owner()->data()->sendAttributeUpdated(this);
591 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
592 // body: just a named shape, use topological selection mechanism
593 bool aResult = false;
594 TopoDS_Shape anOldShape;
595 Handle(TNaming_NamedShape) aNS;
596 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
597 anOldShape = aNS->Get();
599 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
600 Selector_Selector aSelector(aSelLab, baseDocumentLab());
601 aResult = aSelector.restore(aContextShape);
602 bool aWasInvalid = aSelLab.IsAttribute(kINVALID_SELECTION);
603 setInvalidIfFalse(aSelLab, aResult);
605 TopoDS_Shape aNewShape;
606 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
607 aNewShape = aNS->Get();
609 if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape) || aWasInvalid)
611 // shape type should not be changed: if shape becomes compound of such shapes, then split
612 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
613 anOldShape.ShapeType() != aNewShape.ShapeType() &&
614 aNewShape.ShapeType() == TopAbs_COMPOUND) {
615 split(aContext, aNewShape, anOldShape.ShapeType());
617 // for issue #3076 check that the new value belongs to the new context
618 if (!aNewShape.IsNull() && !aContextShape.IsNull() &&
619 (aNewShape.ShapeType() == TopAbs_VERTEX || aNewShape.ShapeType() == TopAbs_EDGE ||
620 aNewShape.ShapeType() == TopAbs_FACE)) {
621 TopExp_Explorer anExp(aContextShape, aNewShape.ShapeType());
622 for(; anExp.More(); anExp.Next()) {
623 if (anExp.Current().IsSame(aNewShape))
626 aResult = setInvalidIfFalse(aSelLab, anExp.More());
628 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
633 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
635 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
636 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
637 if (!aConstructionContext->isInfinite()) {
638 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
639 Selector_Selector aSelector(aSelLab, baseDocumentLab());
640 TopoDS_Shape anOldShape = aSelector.value();
641 aResult = aSelector.restore(aContextShape);
642 setInvalidIfFalse(aSelLab, aResult);
643 if (aResult && !anOldShape.IsEqual(aSelector.value()))
644 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
646 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
650 return setInvalidIfFalse(aSelLab, false); // unknown case
653 void Model_AttributeSelection::selectBody(
654 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
656 // perform the selection
657 TopoDS_Shape aContext;
659 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
661 aContext = aBody->shape()->impl<TopoDS_Shape>();
664 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
666 aContext = aResult->shape()->impl<TopoDS_Shape>();
668 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
673 if (!aContext.IsNull()) {
674 TDF_Label aSelLab = selectionLabel();
675 TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
677 bool aSelectorOk = true;
678 Selector_Selector aSelector(aSelLab, baseDocumentLab());
680 aSelectorOk = aSelector.select(aContext, aNewSub, myIsGeometricalSelection);
682 aSelectorOk = aSelector.store(aContext);
688 TopoDS_Shape aShape = aSelector.value();
689 aSelectorOk = !aShape.IsNull() && aShape.ShapeType() == aNewSub.ShapeType();
691 setInvalidIfFalse(aSelLab, aSelectorOk);
695 bool Model_AttributeSelection::selectPart(
696 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
697 const bool theUpdate)
699 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
700 if (!aPart.get() || !aPart->isActivated())
701 return true; // postponed naming
703 Handle(TDataStd_Integer) anIndex;
704 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
705 // by internal selection
706 if (anIndex->Get() > 0) {
707 // update the selection by index
708 return aPart->updateInPart(anIndex->Get());
710 return true; // nothing to do, referencing just by name
713 return true; // nothing to do, referencing just by name
715 // store the shape (in case part is not loaded it should be useful
717 std::string aName = theContext->data()->name();
718 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
719 aShape = theContext->shape()->impl<TopoDS_Shape>();
721 aShape = theSubShape->impl<TopoDS_Shape>();
723 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
724 TDataStd_Integer::Set(selectionLabel(), anIndex);
726 TNaming_Builder aBuilder(selectionLabel());
727 aBuilder.Select(aShape, aShape);
728 // identify by name in the part
729 TDataStd_Name::Set(selectionLabel(), aName.c_str());
730 return !aName.empty();
733 TDF_Label Model_AttributeSelection::selectionLabel()
735 return myRef.myRef->Label().FindChild(1);
738 /// prefixes of the shape names with centers defined
739 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
741 /// returns the map that contains all possible prefixes of the center-names
742 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
744 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
745 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
746 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
747 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
749 return kCENTERS_PREFIX;
752 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
754 std::string aName("");
755 if(!this->isInitialized())
756 return !theDefaultName.empty() ? theDefaultName : aName;
758 // not argument has not parametric name (filters)
759 if (!this->isArgument() || (myParent && !myParent->isArgument())) {
760 GeomShapePtr aShape = value();
761 if (!aShape.get() && context().get())
762 aShape = context()->shape();
765 aName = aShape->shapeTypeStr();
767 aName += std::string("_") +
768 TCollection_AsciiString(selectionLabel().Father().Tag()).ToCString();
774 CenterType aCenterType = NOT_CENTER;
775 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
776 ResultPtr aCont = context();
779 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
780 // selection of a full feature
781 FeaturePtr aFeatureCont = contextFeature();
782 if (aFeatureCont.get()) {
783 return kWHOLE_FEATURE + aFeatureCont->name();
785 // in case of selection of removed result
789 TDF_Label aSelLab = selectionLabel();
790 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
791 return contextName(aCont);
794 // if it is in result of another part
795 if (aCont->groupName() == ModelAPI_ResultPart::group()) {
796 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCont);
798 std::string aResult = aSubSh.get() ?
799 aPart->data()->name() + "/" + aPart->nameInPart(aSubSh, anIndex) : aPart->data()->name();
800 if (aCenterType != NOT_CENTER)
801 aResult += centersMap()[aCenterType];
805 // whole infinitive construction
806 if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
807 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
808 if (aConstr->isInfinite()) {
809 return contextName(aCont);
813 Selector_Selector aSelector(aSelLab, baseDocumentLab());
815 if (aCont->shape().get() && aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
816 aResult = aSelector.name(this);
817 if (aCenterType != NOT_CENTER) {
818 aResult += centersMap()[aCenterType];
823 // returns the center type and modifies the shape name if this name is center-name
824 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
826 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
827 centersMap().begin();
828 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
829 std::size_t aFound = theShapeName.find(aPrefixIter->second);
830 if (aFound != std::string::npos &&
831 aFound == theShapeName.size() - aPrefixIter->second.size()) {
832 theShapeName = theShapeName.substr(0, aFound);
833 return aPrefixIter->first;
836 return ModelAPI_AttributeSelection::NOT_CENTER;
839 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
840 void Model_AttributeSelection::selectSubShape(
841 const std::string& theType, const std::string& theSubShapeName)
843 if(theSubShapeName.empty() || theType.empty()) return;
845 std::string aSubShapeName = theSubShapeName;
846 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
847 centerTypeByName(aSubShapeName) : NOT_CENTER;
848 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
849 static const GeomShapePtr anEmptyShape;
851 // first iteration is selection by name without center prefix, second - in case of problem,
852 // try with initial name
853 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
854 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
855 aSubShapeName = theSubShapeName;
856 aCenterType = NOT_CENTER;
858 } else if (aUseCenter != 1) continue;
859 TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(aType));
861 std::shared_ptr<Model_Document> aDoc =
862 std::dynamic_pointer_cast<Model_Document>(owner()->document());
863 // check this is Part-name: 2 delimiters in the name
864 std::size_t aPartEnd = aSubShapeName.find('/');
865 if (aPartEnd != std::string::npos) {
866 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
867 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
868 if (aPartName == aRootDoc->kind()) {
869 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
870 aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
873 owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
874 if (aFound.get()) { // found such part, so asking it for the name
875 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
876 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
877 if (aNameInPart.empty()) { // whole part
878 setValue(aPart, anEmptyShape);
882 std::shared_ptr<GeomAPI_Shape> aSelected =
883 aPart->shapeInPart(aNameInPart, aType, anIndex);
884 if (aSelected.get()) {
885 if (aCenterType != NOT_CENTER) {
886 if (!aSelected->isEdge())
888 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
889 setValueCenter(aPart, aSelectedEdge, aCenterType);
891 setValue(aPart, aSelected);
892 TDataStd_Integer::Set(selectionLabel(), anIndex);
899 // check this is a whole feature context
900 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
901 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
902 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
903 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
905 setValue(anObj, anEmptyShape);
910 // the whole result selection check
911 if (aSubShapeName.find('/') == std::string::npos) {
912 ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
914 aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
916 aRes = aDoc->objectByName(ModelAPI_ResultGroup::group(), aSubShapeName);
919 setValue(aRes, anEmptyShape);
924 Selector_Selector aSelector(selectionLabel(), baseDocumentLab());
925 myRestoreDocument = aDoc;
926 TDF_Label aContextLabel = aSelector.restoreByName(
927 aSubShapeName, aShapeType, this, myIsGeometricalSelection);
928 myRestoreDocument.reset();
929 if (!aContextLabel.IsNull()) {
930 ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
931 if (aContext.get() && aContext->shape().get()) {
932 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
933 if (aSelector.solve(aContextShape)) {
934 TopoDS_Shape aSelectorShape = aSelector.value();
935 GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
936 aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelectorShape));
937 // make the context result the latest existing
938 aContext = newestContext(aContext, aShapeToBeSelected);
939 if (myIsGeometricalSelection || aCenterType == NOT_CENTER) {
940 // store the currently generated name
941 selectionLabel().ForgetAllAttributes(true);
942 bool aToUnblock = false;
943 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
944 myRef.setValue(aContext);
945 aSelector.store(aContextShape);
946 owner()->data()->sendAttributeUpdated(this);
948 owner()->data()->blockSendAttributeUpdated(false);
950 } else { // re-select center of circle/arc by context and value
951 if (!aShapeToBeSelected->isEdge())
953 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
954 setValueCenter(aContext, aSelectedEdge, aCenterType);
962 TDF_Label aSelLab = selectionLabel();
963 setInvalidIfFalse(aSelLab, false);
967 void Model_AttributeSelection::selectSubShape(const std::string& theType,
968 const GeomPointPtr& thePoint)
970 if (theType.empty() || !thePoint)
973 // list of parent features
974 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
975 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
977 int aSelectionIndex = 0;
978 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
979 if (aType == GeomAPI_Shape::SHAPE) {
980 // possibly, the string consists of the type and the index,
981 // thus, try to separate them
982 size_t aUndersporePos = theType.find_first_of('_');
983 if (aUndersporePos != std::string::npos)
984 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
986 if (aType != GeomAPI_Shape::SHAPE) {
987 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
988 aChar != theType.end(); ++aChar) {
989 if (std::isdigit(*aChar))
990 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
996 aSelectionIndex -= 1;
1000 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
1002 // collect features from PartSet and the current part
1003 SessionPtr aSession = ModelAPI_Session::get();
1004 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
1005 if (aSession->moduleDocument() != owner()->document()) {
1006 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
1007 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
1009 // Process results of all features from the last to the first
1010 // to find appropriate sub-shape
1011 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
1012 anIt != aFeatures.rend(); ++anIt) {
1013 // selection cannot be linked to the parent features
1014 if (aParents.find(*anIt) != aParents.end())
1016 // check the feature is a part of composite feature (like sketch elements),
1017 // then do not process it, it will be processed in scope of composite feature
1018 bool isSubOfComposite = false;
1019 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
1020 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
1021 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
1022 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
1023 CompositeFeaturePtr aCompFeature =
1024 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
1025 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
1027 if (isSubOfComposite)
1030 // process results of the current feature to find appropriate sub-shape
1031 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
1032 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
1033 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1034 ++anApIt; // skip this shape, because one of the previous is selected
1036 if (anApIt != anAppropriate.end()) {
1037 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1038 setValue(anApIt->myResult, anApIt->mySubshape);
1040 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1041 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1047 TDF_Label aSelLab = selectionLabel();
1048 setInvalidIfFalse(aSelLab, false);
1052 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1053 const std::string& theContextName, const int theIndex)
1055 // selection of context by name
1056 selectSubShape(theType, theContextName);
1057 ResultPtr aContext = context();
1058 if (aContext.get()) {
1059 GeomShapePtr aContShape = aContext->shape();
1060 if (aContShape.get()) {
1061 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1062 GeomShapePtr aValue = aNExp.shape(theIndex);
1064 setValue(aContext, aValue);
1069 void Model_AttributeSelection::setId(int theID)
1071 std::shared_ptr<GeomAPI_Shape> aSelection;
1073 ResultPtr aContextRes = context();
1074 // support for compsolids:
1075 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1076 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1078 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1080 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1081 // searching for the latest main shape
1082 if (theID > 0 && aContext && !aContext->isNull())
1084 std::shared_ptr<Model_Document> aDoc =
1085 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1087 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1088 if (!aNS.IsNull()) {
1089 aMainShape = TNaming_Tool::CurrentShape(aNS);
1093 TopTools_IndexedMapOfShape aSubShapesMap;
1094 TopExp::MapShapes(aMainShape, aSubShapesMap);
1095 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1097 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1098 aResult->setImpl(new TopoDS_Shape(aSelShape));
1100 aSelection = aResult;
1103 setValue(aContextRes, aSelection);
1106 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1108 std::string aResult;
1109 if (owner()->document() != theContext->document()) {
1110 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1111 aResult = theContext->document()->kind() + "/";
1113 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1114 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1115 if (aDocRes.get()) {
1116 aResult = aDocRes->data()->name() + "/";
1120 aResult += theContext->data()->name();
1124 void Model_AttributeSelection::computeValues(
1125 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1126 TopTools_ListOfShape& theShapes)
1128 bool aWasWholeContext = theValShape.IsNull();
1129 if (aWasWholeContext) {
1130 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1132 TopAbs_ShapeEnum aValType = theValShape.ShapeType();
1133 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1134 // if a new value is unchanged in the new context, do nothing: value is correct
1135 TopExp_Explorer aSubExp(aNewContShape, aValType);
1136 for(; aSubExp.More(); aSubExp.Next()) {
1137 if (aSubExp.Current().IsSame(theValShape)) {
1138 theShapes.Append(theValShape);
1142 // if new context becomes compsolid, the resulting sub may be in sub-solids
1143 std::list<ResultPtr> aNewToIterate;
1144 aNewToIterate.push_back(theNewContext);
1145 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1147 std::list<ResultPtr> allNewContextSubs;
1148 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1149 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1150 for(; aSub != allNewContextSubs.end(); aSub++) {
1151 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1152 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1153 aNewToIterate.push_back(aBody);
1157 // first iteration: searching for the whole shape appearance (like face of the box)
1158 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1159 int aToFindPart = 0;
1160 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1161 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1162 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1163 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1164 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1165 std::shared_ptr<Model_Data> aNewData =
1166 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1167 TDF_Label aNewLab = aNewData->shapeLab();
1168 // searching for produced sub-shape fully on some label
1169 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID());
1170 for(; aNSIter.More(); aNSIter.Next()) {
1171 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1172 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1173 if (aToFindPart == 0) { // search shape is fully inside
1174 if (aPairIter.OldShape().IsSame(theValShape)) {
1175 if (aPairIter.NewShape().IsNull()) {// value was removed
1179 // don't add edges generated from faces
1180 if (aPairIter.NewShape().ShapeType() <= aValType)
1181 theShapes.Append(aPairIter.NewShape());
1183 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1184 TopExp_Explorer anExp(aPairIter.OldShape(), aValType);
1185 for(; anExp.More(); anExp.Next()) {
1186 if (anExp.Current().IsSame(theValShape)) { // found a new container
1187 if (aPairIter.NewShape().IsNull()) // skip removed high-level shape
1189 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1190 anOlds.Add(aPairIter.OldShape());
1199 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1200 // also iterate the whole old shape to find not-modified shapes that contain this old
1201 TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1202 NCollection_Map<TopAbs_ShapeEnum> aNewTypes; // types of shapes to iterate
1203 TopTools_DataMapOfShapeShape::Iterator aNewTypeIter(aNewToOld);
1204 for(; aNewTypeIter.More(); aNewTypeIter.Next()) {
1205 if (aNewTypeIter.Key().ShapeType() != aValType)
1206 aNewTypes.Add(aNewTypeIter.Key().ShapeType());
1208 NCollection_Map<TopAbs_ShapeEnum>::Iterator aTypeIter(aNewTypes);
1209 for(; aTypeIter.More(); aTypeIter.Next()) {
1210 for(TopExp_Explorer anExp(anOldContShape, aTypeIter.Value()); anExp.More(); anExp.Next()) {
1211 TopoDS_Shape anOld = anExp.Current();
1212 if (aNewToOld.IsBound(anOld) || anOlds.Contains(anOld)) // this was modified
1214 TopExp_Explorer aValExp(anOld, aValType);
1215 for(; aValExp.More(); aValExp.Next()) {
1216 const TopoDS_Shape& anUnchanged = aValExp.Current();
1217 if (anUnchanged.IsSame(theValShape)) {
1218 aNewToOld.Bind(anOld, anOld);
1226 // map of sub-shapes -> number of occurrences of these shapes in containers
1227 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1228 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1229 for(; aContIter.More(); aContIter.Next()) {
1230 TopExp_Explorer aSubExp(aContIter.Key(), aValType);
1231 for(; aSubExp.More(); aSubExp.Next()) {
1232 if (!aSubs.IsBound(aSubExp.Current())) {
1233 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1235 // store old to know how many olds produced this shape
1236 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1239 // if sub is appeared same times in containers as the number of old shapes that contain it
1240 int aCountInOld = anOlds.Size();
1241 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1243 for(; aSubsIter.More(); aSubsIter.Next()) {
1244 if (aSubsIter.Value().Size() == aCountInOld) {
1245 TopoDS_Shape anOld = aSubsIter.Key();
1246 // check this exists in the new shape
1247 TopExp_Explorer aNew(aNewContShape, anOld.ShapeType());
1248 for (; aNew.More(); aNew.Next()) {
1249 if (aNew.Current().IsSame(anOld))
1253 theShapes.Append(anOld);
1257 if (theShapes.IsEmpty()) { // nothing was changed
1258 if (aWasWholeContext)
1259 theShapes.Append(TopoDS_Shape());
1260 else { // if theValShape exists in new context, add it without changes, otherwise - nothing
1261 for (TopExp_Explorer aNew(aNewContShape, aValType); aNew.More(); aNew.Next()){
1262 if (aNew.Current().IsSame(theValShape)) {
1263 theShapes.Append(theValShape);
1268 } else if (theShapes.Size() > 1) {
1269 // check it is possible to remove extra sub-shapes:
1270 // keep only shapes with the same number of containers if possible
1271 TopAbs_ShapeEnum anAncType = TopAbs_FACE;
1272 if (aValType == TopAbs_VERTEX)
1273 anAncType = TopAbs_EDGE;
1274 TopoDS_Shape anOldContext = theOldContext->shape()->impl<TopoDS_Shape>();
1275 TopTools_IndexedDataMapOfShapeListOfShape anOldMap;
1276 TopExp::MapShapesAndUniqueAncestors(anOldContext, aValType, anAncType, anOldMap);
1277 if (anOldMap.Contains(theValShape)) {
1278 int aNumInOld = anOldMap.FindFromKey(theValShape).Extent();
1279 TopTools_IndexedDataMapOfShapeListOfShape aNewMap;
1280 TopExp::MapShapesAndUniqueAncestors(aNewContShape, aValType, anAncType, aNewMap);
1281 TopTools_ListOfShape aNewResults;
1282 for(TopTools_ListOfShape::Iterator aNewSubs(theShapes); aNewSubs.More(); aNewSubs.Next()) {
1283 TopoDS_Shape aCand = aNewSubs.Value();
1284 if (aNewMap.Contains(aCand) && aNewMap.FindFromKey(aCand).Extent() == aNumInOld)
1285 aNewResults.Append(aCand);
1287 if (!aNewResults.IsEmpty() && aNewResults.Size() < theShapes.Size())
1288 theShapes = aNewResults;
1294 void Model_AttributeSelection::concealedFeature(
1295 const FeaturePtr theFeature, const FeaturePtr theStop, const bool theCheckCopy,
1296 std::list<FeaturePtr>& theConcealers, const ResultPtr theResultOfFeature)
1298 std::set<FeaturePtr> alreadyProcessed;
1299 alreadyProcessed.insert(theFeature);
1301 alreadyProcessed.insert(theStop);
1302 /// iterate all results to find the concealment-attribute
1303 std::list<ResultPtr> aRootRes;
1304 if (theResultOfFeature.get()) {
1305 ResultPtr aRoot = ModelAPI_Tools::bodyOwner(theResultOfFeature, true);
1306 aRootRes.push_back(aRoot ? aRoot : theResultOfFeature);
1307 } else { // all results of a feature
1308 aRootRes = theFeature->results();
1310 std::list<ResultPtr>::const_iterator aRootIter = aRootRes.cbegin();
1311 for(; aRootIter != aRootRes.cend(); aRootIter++) {
1312 std::list<ResultPtr> allRes;
1313 allRes.push_back(*aRootIter);
1314 ResultBodyPtr aRootBody = ModelAPI_Tools::bodyOwner(*aRootIter, true);
1315 if (!aRootBody.get())
1316 aRootBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRootIter);
1317 if (aRootBody.get()) {
1318 ModelAPI_Tools::allSubs(aRootBody, allRes);
1320 for(std::list<ResultPtr>::iterator aRIter = allRes.begin(); aRIter != allRes.end(); aRIter++) {
1321 const std::set<AttributePtr>& aRefs = (*aRIter)->data()->refsToMe();
1322 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1323 for (; aRef != aRefs.cend(); aRef++) {
1324 if (!aRef->get() || !(*aRef)->owner().get())
1326 // concealed attribute only
1327 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1328 if (alreadyProcessed.find(aRefFeat) != alreadyProcessed.end()) // optimization
1330 alreadyProcessed.insert(aRefFeat);
1331 if (ModelAPI_Session::get()->validators()->isConcealed(aRefFeat->getKind(), (*aRef)->id())
1333 std::dynamic_pointer_cast<ModelAPI_FeatureCopyInterface>(aRefFeat).get()))
1335 // for extrusion cut in python script the nested sketch reference may be concealed before
1336 // it is nested, so, check this composite feature is valid
1337 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
1338 // need to be validated to update the "Apply" state if not previewed
1339 if (aFactory->validate(aRefFeat)) {
1340 if (theStop.get()) {
1341 std::shared_ptr<Model_Document> aDoc =
1342 std::dynamic_pointer_cast<Model_Document>(theStop->document());
1343 if (!aDoc->isLaterByDep(theStop, aRefFeat)) // skip feature later than stop
1346 theConcealers.push_back(aRefFeat);
1354 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1355 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1356 TDF_Label theAccessLabel,
1357 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1359 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1360 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1361 TopTools_ListOfShape aContextList;
1362 aContextList.Append(theContShape);
1363 if (theContext.get()) {
1364 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1365 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1366 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1368 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1369 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1370 for(; aModifIter.More(); aModifIter.Next()) {
1371 TDF_Label anObjLab = aModifIter.Label().Father();
1372 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1373 (theDoc->objects()->object(anObjLab));
1374 if (!aModifierObj.get()) {
1375 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1376 if (!anObjLab.IsRoot())
1377 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1378 (theDoc->objects()->object(anObjLab.Father()));
1379 if (!aModifierObj.get())
1382 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1383 if (!aModifierFeat.get())
1385 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1386 if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
1387 continue; // the modifier feature is later than this, so, should not be used
1388 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1389 if (aCurrentModifierFeat == aModifierFeat ||
1390 !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
1391 continue; // the current modifier is later than the found, so, useless
1392 Handle(TNaming_NamedShape) aNewNS;
1393 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1394 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1395 aResults.insert(aModifierObj);
1396 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1397 aResults.insert(ResultPtr());
1398 } else { // not-processed modification => don't support it
1403 // if there exist context composite and sub-result(s), leave only sub(s)
1404 for(std::set<ResultPtr>::iterator aResIter = aResults.begin(); aResIter != aResults.end();) {
1405 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1406 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1407 if (aResults.count(aParent))
1409 if (aParent.get()) { // erase from set, so, restart iteration
1410 aResults.erase(aParent);
1411 aResIter = aResults.begin();
1415 bool aStaySame = false;
1416 if (aResults.empty()) {
1417 // check the context become concealed by operation which is earlier than this selection
1418 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1419 FeaturePtr aContextOwner = theDoc->feature(theContext);
1420 std::list<FeaturePtr> aConcealers;
1421 concealedFeature(aContextOwner, aThisFeature, false, aConcealers, theContext);
1422 std::list<FeaturePtr>::iterator aConcealer = aConcealers.begin();
1423 for(; aConcealer != aConcealers.end(); aConcealer++) {
1424 std::list<ResultPtr> aRefResults;
1425 ModelAPI_Tools::allResults(*aConcealer, aRefResults);
1426 std::list<ResultPtr>::iterator aRefIter = aRefResults.begin();
1427 for(; aRefIter != aRefResults.end(); aRefIter++) {
1428 ResultBodyPtr aRefBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefIter);
1429 if (!aRefBody.get() || aRefBody->numberOfSubs() != 0) // iterate only leafs
1431 GeomShapePtr aRefShape = aRefBody->shape();
1432 if (!aRefShape.get() || aRefShape->isNull())
1434 if (aRefShape->impl<TopoDS_Shape>().IsSame(theContShape)) {
1435 // add the new context result with the same shape
1436 aResults.insert(aRefBody);
1439 if (aResults.empty())
1440 return true; // feature conceals result, return true, so the context will be removed
1442 aStaySame = aResults.empty();
1444 if (myParent && myParent->isMakeCopy()) {
1445 // check there are copies before the new results, so, make a copy
1446 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1447 std::list<ObjectPtr> aCopyContext;
1448 std::list<GeomShapePtr> aCopyVals;
1449 // features between the new and the old: check the "Move" interface to get a copy
1450 FeaturePtr aRootOwner = theDoc->feature(theContext);
1451 FeaturePtr anOwner = ModelAPI_Tools::compositeOwner(aRootOwner);
1452 for(; anOwner.get(); anOwner = ModelAPI_Tools::compositeOwner(anOwner))
1453 aRootOwner = anOwner;
1454 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1455 // iterate all results to find a "Copy" features between the new and one and to add the
1456 // copy-results also to results if this attribute refers to the copied shape
1457 int anIndex = kUNDEFINED_FEATURE_INDEX;
1458 for(FeaturePtr aFeat = theDoc->objects()->nextFeature(aRootOwner, anIndex); aFeat.get() &&
1459 aFeat != aThisFeature; aFeat = theDoc->objects()->nextFeature(aFeat, anIndex)) {
1460 std::shared_ptr<ModelAPI_FeatureCopyInterface> aCopier =
1461 std::dynamic_pointer_cast<ModelAPI_FeatureCopyInterface>(aFeat);
1462 if (aCopier.get()) {
1463 GeomShapePtr aValShape(new GeomAPI_Shape);
1464 aValShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(
1465 theValShape.IsNull() ? theContShape : theValShape));
1466 aCopier->getCopies(theContext, aValShape, aCopyContext, aCopyVals);
1469 // check for the further modifications of the copy contexts and values
1470 std::list<ObjectPtr>::iterator aCopyContIter = aCopyContext.begin();
1471 std::list<GeomShapePtr>::iterator aCopyValIter = aCopyVals.begin();
1472 for(; aCopyContIter != aCopyContext.end(); aCopyContIter++, aCopyValIter++) {
1473 ResultPtr aNewCont = std::dynamic_pointer_cast<ModelAPI_Result>(*aCopyContIter);
1474 TopoDS_Shape aNewContShape = aNewCont->shape()->impl<TopoDS_Shape>();
1475 GeomShapePtr aNewVal = *aCopyValIter;
1476 TopoDS_Shape aNewValShape;
1477 if (aNewVal.get() && !aNewVal->isNull())
1478 aNewValShape = aNewVal->impl<TopoDS_Shape>();
1479 std::list<ResultPtr> aNewRes;
1480 TopTools_ListOfShape aNewUpdatedVal;
1481 if (searchNewContext(theDoc, aNewContShape, aNewCont, aNewValShape,
1482 theAccessLabel, aNewRes, aNewUpdatedVal)) {
1483 // append new results instead of the current ones
1484 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1485 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1486 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1487 theResults.push_back(*aNewIter);
1488 theValShapes.Append(aNewUpdVal.Value());
1490 } else { // the current result is good
1491 theResults.push_back(aNewCont);
1492 theValShapes.Append(aNewValShape);
1495 if (aStaySame && !theResults.empty()) { // no changes except copy, so, keep the origin as first
1496 theResults.push_front(theContext);
1497 theValShapes.Prepend(theValShape);
1504 // iterate all results to find further modifications
1505 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1506 for(aResIter = aResults.begin(); aResIter != aResults.end(); aResIter++) {
1507 if (aResIter->get() != NULL) {
1508 ResultPtr aNewResObj = *aResIter;
1509 // compute new values by two contexts: the old and the new
1510 TopTools_ListOfShape aValShapes;
1511 computeValues(theContext, aNewResObj, theValShape, aValShapes);
1513 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1514 for(; aNewVal.More(); aNewVal.Next()) {
1515 std::list<ResultPtr> aNewRes;
1516 TopTools_ListOfShape aNewUpdatedVal;
1517 TopoDS_Shape aNewValSh = aNewVal.Value();
1518 TopoDS_Shape aNewContShape = aNewResObj->shape()->impl<TopoDS_Shape>();
1520 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1521 aNewValSh.Nullify();
1522 if (searchNewContext(theDoc, aNewContShape, aNewResObj, aNewValSh,
1523 theAccessLabel, aNewRes, aNewUpdatedVal))
1525 // append new results instead of the current ones
1526 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1527 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1528 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1529 theResults.push_back(*aNewIter);
1530 theValShapes.Append(aNewUpdVal.Value());
1532 } else { // the current result is good
1533 theResults.push_back(aNewResObj);
1534 theValShapes.Append(aNewValSh);
1539 return true; // theResults must be empty: everything is deleted
1542 void Model_AttributeSelection::updateInHistory(bool& theRemove)
1544 static std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1546 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1547 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1548 aContext->groupName() != ModelAPI_ResultPart::group())) {
1549 // but check the case the whole results are allowed: whole features may be selected
1550 if (myParent && myParent->isWholeResultAllowed()) {
1551 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
1552 if (aFeature.get()) {
1553 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1554 std::list<FeaturePtr> aConcealers;
1555 bool aCopyPossible = myParent && myParent->isMakeCopy();
1556 concealedFeature(aFeature, aThisFeature, aCopyPossible, aConcealers, ResultPtr());
1557 if (aConcealers.empty())
1559 // if there are copies, but no direct modification, keep the original
1560 bool aKeepOrigin = false;
1561 if (aCopyPossible) {
1562 std::list<FeaturePtr>::iterator aConcealer = aConcealers.begin();
1563 for(aKeepOrigin = true; aConcealer != aConcealers.end(); aConcealer++)
1564 if (!std::dynamic_pointer_cast<ModelAPI_FeatureCopyInterface>(*aConcealer).get()) {
1565 aKeepOrigin = false;
1569 aConcealers.push_front(aFeature);
1572 bool aChanged = false;
1573 std::list<FeaturePtr>::iterator aConcealer = aConcealers.begin();
1574 for(; aConcealer != aConcealers.end(); aConcealer++)
1576 if (aKeepOrigin || !myParent->isInList(*aConcealer, anEmptyShape))
1577 myParent->append(*aConcealer, anEmptyShape);
1579 if (!myParent->isInList(*aConcealer, anEmptyShape)) {// avoid addition of duplicates
1580 setValue(*aConcealer, anEmptyShape);
1582 } else if (aCopyPossible && *aConcealer == aFeature) { // keep the origin in case of copy
1586 if (!aChanged) // remove this
1588 else if (!aKeepOrigin) // searching further modifications only if current changed
1589 updateInHistory(theRemove);
1592 return;// only bodies and parts may be modified later in the history, skip otherwise
1595 std::shared_ptr<Model_Document> aDoc =
1596 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1597 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1598 if (!aContData.get() || !aContData->isValid())
1600 TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
1602 // checking this may be just a reference to another context (same shape), so use that label
1603 Handle(TNaming_NamedShape) aContNS;
1604 Handle(TDF_Reference) aRefAttr;
1605 while(!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS) &&
1606 aContLab.FindAttribute(TDF_Reference::GetID(), aRefAttr))
1607 aContLab = aRefAttr->Get();
1609 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1610 bool aFoundNewContext = true;
1611 ResultPtr aNewContext = aContext;
1612 while(aFoundNewContext) {
1613 aFoundNewContext = false;
1614 // parts have no shape in result, so, trace references using the Part info
1615 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1616 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1617 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1618 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1619 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1620 for(; aRef != aRefs.end(); aRef++) {
1621 // to avoid detection of part changes by local selection only
1622 AttributeSelectionPtr aSel =
1623 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1624 if (aSel.get() && aSel->value().get() &&
1625 !aSel->value()->isSame(aSel->context()->shape()))
1628 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1630 if (aRefFeat.get() && aRefFeat != owner() && aRefFeat->firstResult().get()) {
1631 // check the reference is concealed: #2900
1632 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1633 if (!aValidators->isConcealed(aRefFeat->getKind(), (*aRef)->id()))
1635 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1636 if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
1637 aFoundNewContext = true;
1638 aNewContext = aRefFeat->firstResult();
1645 if (aNewContext != aContext) {
1646 setValue(aNewContext, value());
1650 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1651 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1652 // iterate the context shape modifications in order to find a feature that is upper in history
1653 // that this one and is really modifies the referenced result to refer to it
1654 ResultPtr aModifierResFound;
1655 TNaming_Iterator aPairIter(aContNS);
1656 if (!aPairIter.More())
1658 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1659 bool anIterate = true;
1660 // trying to update also the sub-shape selected
1661 GeomShapePtr aSubShape = value();
1662 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1664 TopoDS_Shape aValShape;
1665 if (aSubShape.get()) {
1666 aValShape = aSubShape->impl<TopoDS_Shape>();
1669 std::list<ResultPtr> aNewContexts;
1670 TopTools_ListOfShape aValShapes;
1671 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1673 std::set<ResultPtr> allContexts, aSkippedContext;
1674 std::list<ResultPtr>::iterator aNewContext = aNewContexts.begin();
1675 for(; aNewContext != aNewContexts.end(); aNewContext++)
1676 allContexts.insert(*aNewContext);
1678 // if there exist context composite and sub-result(s), leave only sub(s)
1679 std::set<ResultPtr>::iterator aResIter = allContexts.begin();
1680 for(; aResIter != allContexts.end(); aResIter++) {
1681 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1682 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1683 if (allContexts.count(aParent))
1684 aSkippedContext.insert(aParent);
1687 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1689 if (myParent->selectionType() == "VERTEX" || myParent->selectionType() == "Vertices")
1690 aListShapeType = GeomAPI_Shape::VERTEX;
1691 else if (myParent->selectionType() == "EDGE" || myParent->selectionType() == "Edges")
1692 aListShapeType = GeomAPI_Shape::EDGE;
1693 else if (myParent->selectionType() == "FACE" || myParent->selectionType() == "Faces")
1694 aListShapeType = GeomAPI_Shape::FACE;
1697 // issue #3031: skip topology if there is more convenient shape type presents in the
1698 // same context as a result of this
1699 bool isWholeResult = myParent && myParent->isWholeResultAllowed() && !aSubShape.get();
1700 GeomAPI_Shape::ShapeType allowedType = GeomAPI_Shape::SHAPE;
1701 if (isWholeResult) {
1702 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1703 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1704 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1705 if (aNewValues.Value().IsNull()) { // only for the whole context
1706 GeomAPI_Shape::ShapeType aShapeType = (*aNewCont)->shape()->shapeType();
1707 if (allowedType == GeomAPI_Shape::SHAPE) { // just set this one
1708 allowedType = aShapeType;
1710 GeomAPI_Shape::ShapeType anAllowed = allowedType;
1711 if (anAllowed != aShapeType) { // select the best, nearest to the origin
1712 GeomAPI_Shape::ShapeType anOldShapeType = aContext->shape()->shapeType();
1713 GeomAPI_Shape::ShapeType aDeltaAllowed =
1714 (GeomAPI_Shape::ShapeType)(anOldShapeType - anAllowed);
1715 if (aDeltaAllowed < 0)
1716 aDeltaAllowed = (GeomAPI_Shape::ShapeType)(-aDeltaAllowed);
1717 GeomAPI_Shape::ShapeType aDeltaThis =
1718 (GeomAPI_Shape::ShapeType)(anOldShapeType - aShapeType);
1720 aDeltaThis = (GeomAPI_Shape::ShapeType)(-aDeltaThis);
1721 if (aDeltaThis == aDeltaAllowed) { // equal distance to context, select complicated
1722 if (anOldShapeType < anAllowed)
1723 allowedType = aShapeType;
1724 } else if (aDeltaAllowed > aDeltaThis) { // this wins
1725 allowedType = aShapeType;
1733 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1734 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1735 bool aFirst = true; // first is set to this, next are appended to parent
1736 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1737 if (aSkippedContext.count(*aNewCont))
1740 if (isWholeResult && aNewValues.Value().IsNull())
1741 if (allowedType != GeomAPI_Shape::SHAPE &&
1742 (*aNewCont)->shape()->shapeType() != allowedType)
1743 continue; // there is better result exists with the better shape type (issue #3031)
1745 GeomShapePtr aValueShape;
1746 if (!aNewValues.Value().IsNull()) {
1747 aValueShape = std::make_shared<GeomAPI_Shape>();
1748 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1750 // Check that list has the same type of shape selection before adding.
1751 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1752 if (aValueShape.get()) {
1753 aShapeShapeType = aValueShape->shapeType();
1755 aShapeShapeType = (*aNewCont)->shape()->shapeType();
1757 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1758 // exception is for whole results selected
1759 if (!isWholeResult) {
1764 ResultPtr aNewContext = *aNewCont;
1765 if (aValueShape.get()) { // #2892 if context is higher level result, search this sub in lower
1766 ResultBodyPtr aBodyContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aNewContext);
1767 if (aBodyContext.get() && aBodyContext->numberOfSubs() != 0) {
1768 std::list<ResultPtr> aLower;
1769 ModelAPI_Tools::allSubs(aBodyContext, aLower, true);
1770 for(std::list<ResultPtr>::iterator aL = aLower.begin(); aL != aLower.end(); aL++) {
1771 GeomShapePtr aLShape = (*aL)->shape();
1772 if (aLShape.get() && !aLShape->isNull()) {
1773 if (aLShape->isSubShape(aValueShape, false)) {
1783 if (!myParent || !myParent->isInList(aNewContext, aValueShape)) { // avoid duplicates
1784 setValue(aNewContext, aValueShape);
1786 } else if (aNewContext == aContext && myParent && myParent->isMakeCopy()) {
1787 // this may be exactly the old one, not modified in case of copy
1790 } else if (myParent) {
1791 if (!myParent->isInList(aNewContext, aValueShape)) // avoid addition of duplicates
1792 myParent->append(aNewContext, aValueShape);
1795 if (aFirst) { // nothing was added, all results were deleted
1799 static ResultPtr anEmptyContext;
1800 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1807 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1809 myParent = theParent;
1812 std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
1814 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1815 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1816 FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1817 bool aBaseDocumnetUsed = false;
1818 if (!aFeatureOwner.get()) { // use module document
1819 aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
1820 aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1821 aBaseDocumnetUsed = true;
1823 if (aFeatureOwner.get()) {
1824 // if it is sub-element of the sketch, the context name is the name of the sketch
1825 // searching also for result - real context
1827 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
1828 if (aComposite.get() && aComposite->results().size() == 1 &&
1829 aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
1830 aFeatureOwner = aComposite;
1831 aResult = aFeatureOwner->firstResult();
1833 aResult = aDoc->resultByLab(theSelectionLab);
1835 if (aResult.get()) {
1836 // this is to avoid duplicated names of results problem
1837 std::string aContextName = aResult->data()->name();
1838 // myLab corresponds to the current time
1839 TDF_Label aCurrentLab = selectionLabel();
1840 while(aCurrentLab.Depth() > 3)
1841 aCurrentLab = aCurrentLab.Father();
1843 int aNumInHistoryNames =
1844 aDoc->numberOfNameInHistory(aResult, aCurrentLab);
1845 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
1846 aContextName = "_" + aContextName;
1847 aNumInHistoryNames--;
1849 if (aBaseDocumnetUsed)
1850 aContextName = aDoc->kind() + "/" + aContextName;
1851 return aContextName;
1854 return ""; // invalid case
1857 /// This method restores by the context and value name the context label and
1858 /// sub-label where the value is. Returns true if it is valid.
1859 bool Model_AttributeSelection::restoreContext(std::string theName,
1860 TDF_Label& theContext, TDF_Label& theValue)
1862 static const GeomShapePtr anEmptyShape; // to store context only
1863 std::string aName = theName;
1864 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1865 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1867 // remove the sub-value part if exists
1868 std::string aSubShapeName = aName;
1869 std::string::size_type n = aName.find('/');
1870 if (n != std::string::npos) {
1871 aName = aName.substr(0, n);
1874 if (aName.empty()) return false;
1875 bool anUniqueContext = false;
1876 ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1877 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
1879 aDoc = std::dynamic_pointer_cast<Model_Document>(
1880 ModelAPI_Session::get()->moduleDocument());
1881 if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
1882 aSubShapeName = theName.substr(aDoc->kind().size() + 1);
1883 aName = aSubShapeName;
1884 std::string::size_type n = aName.find('/');
1885 if (n != std::string::npos) {
1886 aName = aName.substr(0, n);
1889 aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1890 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
1894 // searching the sub-shape
1895 static const ResultPtr anEmpty;
1896 theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
1898 // sketch sub-component shape and name is located in separated feature label, try the sub-name
1899 if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
1900 std::string::size_type aSlash = aSubShapeName.rfind('/');
1901 if (aSlash != std::string::npos) {
1902 std::string aCompName = aSubShapeName.substr(aSlash + 1);
1903 CompositeFeaturePtr aComposite =
1904 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
1905 if (aComposite.get() && aComposite->numberOfSubs()) {
1906 const int aSubNum = aComposite->numberOfSubs();
1907 for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
1908 FeaturePtr aSub = aComposite->subFeature(a);
1909 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1910 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
1911 for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
1912 if ((*aRes)->data()->name() == aCompName) {
1913 theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1915 } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
1916 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1917 TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
1918 for(; aSubNames.More(); aSubNames.Next()) {
1919 if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
1920 IsEqual(aCompName.c_str())) {
1921 theValue = aSubNames.Value()->Label();
1933 theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
1938 bool Model_AttributeSelection::isLater(
1939 const TDF_Label theResult1, const TDF_Label theResult2) const
1941 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1942 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1943 FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
1946 FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
1949 return aDoc->isLaterByDep(aFeat1, aFeat2);
1952 ResultPtr Model_AttributeSelection::newestContext(
1953 const ResultPtr theCurrent, const GeomShapePtr theValue)
1955 ResultPtr aResult = theCurrent;
1956 GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
1957 std::shared_ptr<Model_Document> aDoc =
1958 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1959 bool aFindNewContext = true;
1960 while (aFindNewContext && aResult.get()) {
1961 aFindNewContext = false;
1962 // try to find the last context to find the up to date shape
1963 TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
1964 if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
1965 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1966 if (!aNS.IsNull()) {
1967 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1969 && isLater(selectionLabel(), aNS->Label())
1970 && isLater(aNS->Label(),
1971 std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
1973 TDF_Label aLab = aNS->Label();
1974 ResultPtr aRes = aDoc->resultByLab(aLab);
1976 if (aRes->shape()->isSubShape(aSelectedShape)) {
1978 aFindNewContext = true;
1986 // TestFillWireVertex.py - sketch constructions for wire may participate too
1987 //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
1988 // try to search newer context by the concealment references
1989 // take references to all results: root one, any sub
1990 std::list<ResultPtr> allRes;
1991 ResultPtr aCompContext;
1992 ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
1993 if (aCompBody.get()) {
1994 ModelAPI_Tools::allSubs(aCompBody, allRes);
1995 allRes.push_back(aCompBody);
1996 aCompContext = aCompBody;
1999 allRes.push_back(aResult);
2001 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
2002 ResultPtr aResCont = *aSub;
2003 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
2004 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
2005 continue; // only lower and higher level subs are counted
2006 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
2007 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
2008 for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
2009 if (!aRef->get() || !(*aRef)->owner().get())
2011 // concealed attribute only
2012 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
2013 if (!ModelAPI_Session::get()->validators()->isConcealed(
2014 aRefFeat->getKind(), (*aRef)->id()))
2016 // search the feature result that contains sub-shape selected
2017 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
2019 // take all sub-results or one result
2020 std::list<ResultPtr> aRefFeatResults;
2021 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
2022 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
2023 for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
2024 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
2025 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
2026 aResults.push_back(aBody);
2028 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
2030 // searching by sub-shape
2031 for (; aResIter != aResults.end(); aResIter++) {
2032 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
2034 GeomShapePtr aShape = (*aResIter)->shape();
2035 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
2036 aResult = *aResIter; // found new context (produced from this) with same subshape
2037 aFindNewContext = true; // continue searching further
2044 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
2045 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
2046 if (aComp && aComp->numberOfSubs()) {
2047 std::list<ResultPtr> allSubs;
2048 ModelAPI_Tools::allSubs(aComp, allSubs);
2049 std::list<ResultPtr>::iterator aS = allSubs.begin();
2050 for (; aS != allSubs.end(); aS++) {
2051 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
2052 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
2053 aSub->shape()->isSubShape(aSelectedShape)) {
2059 // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
2060 // so, actually, the sketch element was selected (which is never concealed)
2061 if (aResult != theCurrent && aResult->isConcealed())
2062 aResult = theCurrent;
2066 void Model_AttributeSelection::combineGeometrical()
2068 if (myTmpContext.get() || myTmpSubShape.get())
2070 TDF_Label aSelLab = selectionLabel();
2071 if (aSelLab.IsAttribute(kINVALID_SELECTION) || !myRef.isInitialized())
2074 if (aSelLab.IsAttribute(kCIRCLE_CENTER) || aSelLab.IsAttribute(kELLIPSE_CENTER1) ||
2075 aSelLab.IsAttribute(kELLIPSE_CENTER2) || aSelLab.IsAttribute(kSIMPLE_REF_ID))
2078 if (aSelLab.IsAttribute(kPART_REF_ID)) {
2079 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
2080 if (!aPart.get() || !aPart->isActivated())
2081 return; // postponed naming needed
2082 Handle(TDataStd_Integer) anIndex;
2083 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
2084 if (anIndex->Get()) { // special selection attribute was created, use it
2085 std::string aNewName;
2086 aPart->combineGeometrical(anIndex->Get(), aNewName);
2087 TDataStd_Name::Set(aSelLab, aNewName.c_str());
2093 std::shared_ptr<Model_ResultConstruction> aConstr =
2094 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
2097 FeaturePtr aFeature = contextFeature();
2101 Selector_Selector aSelector(aSelLab, baseDocumentLab());
2102 TopoDS_Shape aContextShape = context()->shape()->impl<TopoDS_Shape>();
2103 if (aSelector.restore(aContextShape)) {
2104 aSelector.combineGeometrical(aContextShape);
2108 TDF_Label Model_AttributeSelection::baseDocumentLab()
2110 if (ModelAPI_Session::get()->moduleDocument() != owner()->document())
2111 return std::dynamic_pointer_cast<Model_Document>
2112 (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel();
2113 static TDF_Label anEmpty;
2117 void Model_AttributeSelection::reset()
2119 ModelAPI_AttributeSelection::reset();