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: only construction features of PartSet could be selected
189 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
190 if (aFeatureContext.get() && owner()->document() != aFeatureContext->document()) {
191 if (aFeatureContext->results().empty() ||
192 aFeatureContext->firstResult()->groupName() != ModelAPI_ResultConstruction::group()) {
193 aSelLab.ForgetAllAttributes(true);
194 myRef.setValue(ObjectPtr());
196 owner()->data()->blockSendAttributeUpdated(false);
202 owner()->data()->sendAttributeUpdated(this);
205 owner()->data()->blockSendAttributeUpdated(false);
210 void Model_AttributeSelection::setValueCenter(
211 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
212 const CenterType theCenterType, const bool theTemporarily)
214 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
215 if (theTemporarily) {
216 myTmpCenterType = theCenterType;
217 } else { // store in the data structure
218 TDF_Label aSelLab = selectionLabel();
219 switch(theCenterType) {
222 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
223 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
225 case ELLIPSE_FIRST_FOCUS:
227 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
228 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
230 case ELLIPSE_SECOND_FOCUS:
232 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
233 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
237 owner()->data()->sendAttributeUpdated(this);
241 void Model_AttributeSelection::selectValue(
242 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
245 std::shared_ptr<GeomAPI_Shape> aValue =
246 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
247 if (!aValue.get() || aType == NOT_CENTER) {
248 setValue(theSource->context(), aValue);
250 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
251 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
252 setValueCenter(theSource->context(), anEdge, aType);
256 void Model_AttributeSelection::removeTemporaryValues()
258 if (myTmpContext.get() || myTmpSubShape.get()) {
259 myTmpContext.reset();
260 myTmpSubShape.reset();
264 // returns the center of the edge: circular or elliptic
265 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
267 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
268 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
269 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
270 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
271 double aFirst, aLast;
272 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
273 if (!aCurve.IsNull()) {
274 TopoDS_Vertex aVertex;
275 BRep_Builder aBuilder;
276 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
277 while(!aCurve.IsNull() && aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
278 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
279 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
280 if (!aCirc.IsNull()) {
281 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
284 while(!aCurve.IsNull() && aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
285 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
286 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
287 if (!anEll.IsNull()) {
288 aBuilder.MakeVertex(aVertex,
289 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
290 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
293 if (!aVertex.IsNull()) {
294 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
295 aResult->setImpl(new TopoDS_Vertex(aVertex));
301 return theEdge; // no vertex, so, return the initial edge
304 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
306 if (!myRef.isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
307 return std::shared_ptr<GeomAPI_Shape>();
308 CenterType aType = NOT_CENTER;
309 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
310 return centerByEdge(aResult, aType);
313 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
315 theType = NOT_CENTER;
316 GeomShapePtr aResult;
317 if (myTmpContext.get() || myTmpSubShape.get()) {
318 theType = myTmpCenterType;
319 ResultConstructionPtr aResulConstruction =
320 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
321 if(aResulConstruction.get()) {
322 // it is just reference to construction.
323 return myTmpSubShape;
325 FeaturePtr aFeature =
326 std::dynamic_pointer_cast<ModelAPI_Feature>(myTmpContext);
327 if (aFeature.get()) {
328 // it is just reference to construction.
329 return myTmpSubShape;
331 return myTmpSubShape.get() ? myTmpSubShape :
332 std::dynamic_pointer_cast<ModelAPI_Result>(myTmpContext)->shape();
335 TDF_Label aSelLab = selectionLabel();
336 if (aSelLab.IsAttribute(kINVALID_SELECTION))
339 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
340 theType = CIRCLE_CENTER;
341 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
342 theType = ELLIPSE_FIRST_FOCUS;
343 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
344 theType = ELLIPSE_SECOND_FOCUS;
347 if (myRef.isInitialized()) {
348 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
349 ResultPtr aContext = context();
350 if (!aContext.get() || aContext->groupName() == ModelAPI_ResultConstruction::group())
351 return aResult; // empty result, for whole construction selection also
352 return aContext->shape();
354 if (aSelLab.IsAttribute(kPART_REF_ID)) {
355 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
356 if (!aPart.get() || !aPart->isActivated())
357 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
358 Handle(TDataStd_Integer) anIndex;
359 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
360 if (anIndex->Get()) { // special selection attribute was created, use it
361 return aPart->selectionValue(anIndex->Get());
362 } else { // face with name is already in the data model, so try to take it by name
363 Handle(TDataStd_Name) aName;
364 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
365 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
366 std::size_t aPartEnd = aSubShapeName.find('/');
367 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
368 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
370 std::string aType; // to reuse already existing selection the type is not needed
371 return aPart->shapeInPart(aNameInPart, aType, anIndex);
378 std::shared_ptr<Model_ResultConstruction> aConstr =
379 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
381 if (aConstr->isInfinite())
382 return aResult; // empty result
384 if (!aConstr.get()) { // for construction context, return empty result as usual even
385 // the whole feature is selected
386 FeaturePtr aFeature = contextFeature();
387 if (aFeature.get()) {
388 std::list<GeomShapePtr> allShapes;
389 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
390 for (; aRes != aFeature->results().cend(); aRes++) {
391 if (aRes->get() && !(*aRes)->isDisabled()) {
392 GeomShapePtr aShape = (*aRes)->shape();
393 if (aShape.get() && !aShape->isNull()) {
394 allShapes.push_back(aShape);
398 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
401 if (contextFeature().get()) {
402 return aResult; // for the whole sketch feature selected return null => all faces
406 Handle(TNaming_NamedShape) aSelection;
407 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
408 TopoDS_Shape aSelShape = aSelection->Get();
409 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
410 aResult->setImpl(new TopoDS_Shape(aSelShape));
411 } else if (aConstr) { // simple construction element: just shape of this construction element
412 aResult = aConstr->shape();
418 bool Model_AttributeSelection::isInvalid()
420 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
423 bool Model_AttributeSelection::isInitialized()
425 if (myRef.isInitialized()) {
426 TDF_Label aSelLab = selectionLabel();
427 // it is just reference to shape, not sub-shape
428 if (aSelLab.IsAttribute(kSIMPLE_REF_ID) || aSelLab.IsAttribute(kPART_REF_ID)) {
429 ResultPtr aContext = context();
430 return aContext.get() != NULL;
432 Handle(TNaming_NamedShape) aSelection;
433 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
434 return !aSelection->Get().IsNull();
435 } else { // for simple construction element: just shape of this construction element
436 if (myRef.value().get())
438 // check that this is on open of document, so, results are not initialized yet
439 TDF_Label aRefLab = myRef.myRef->Get();
440 if (aRefLab.IsNull() || !owner().get())
442 std::shared_ptr<Model_Document> aMyDoc =
443 std::dynamic_pointer_cast<Model_Document>(owner()->document());
446 // check at least the feature exists
447 return aMyDoc->featureByLab(aRefLab).get() != NULL;
453 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
455 myIsGeometricalSelection(false)
457 myIsInitialized = myRef.isInitialized();
461 void Model_AttributeSelection::setID(const std::string theID)
464 ModelAPI_AttributeSelection::setID(theID);
465 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
467 // to be able to select as geometrical selection and then - split
468 myIsGeometricalSelection = true; // myParent->isGeometricalSelection();
470 myIsGeometricalSelection =
471 ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
475 ResultPtr Model_AttributeSelection::context()
477 if (!myRef.isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
480 if (myTmpContext.get() || myTmpSubShape.get()) {
481 return std::dynamic_pointer_cast<ModelAPI_Result>(myTmpContext);
484 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
485 // for parts there could be same-data result, so take the last enabled
487 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
488 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
489 for(int a = aSize - 1; a >= 0; a--) {
490 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
491 if(aPart.get() && aPart->data() == aResult->data()) {
492 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
493 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
494 // check that this result is not this-feature result (it is forbidden to select itself)
495 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
501 } else { // if feature - construction is selected, it has only one result, return this result
502 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
503 if (aFeature.get() && aFeature->results().size() == 1 &&
504 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
505 return aFeature->firstResult();
510 FeaturePtr Model_AttributeSelection::contextFeature() {
511 if (myTmpContext.get()) {
512 return std::dynamic_pointer_cast<ModelAPI_Feature>(myTmpContext);
514 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
516 ObjectPtr Model_AttributeSelection::contextObject() {
517 FeaturePtr aRes = contextFeature();
524 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
526 ModelAPI_AttributeSelection::setObject(theObject);
527 myRef.setObject(theObject);
530 /// Sets the invalid flag if flag is false, or removes it if "true"
532 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
534 theLab.ForgetAttribute(kINVALID_SELECTION);
536 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
541 void Model_AttributeSelection::split(
542 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
544 TopTools_ListOfShape aSubs;
545 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
546 if (!anExplorer.Value().IsNull() &&
547 anExplorer.Value().ShapeType() == theType) {
548 aSubs.Append(anExplorer.Value());
549 } else { // invalid case; bad result shape, so, impossible to split easily
554 if (aSubs.Extent() > 1) { // ok to split
555 TopTools_ListIteratorOfListOfShape aSub(aSubs);
556 GeomShapePtr aSubSh(new GeomAPI_Shape);
557 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
558 setValue(theContext, aSubSh);
559 for(aSub.Next(); aSub.More(); aSub.Next()) {
560 GeomShapePtr aSubSh(new GeomAPI_Shape);
561 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
562 myParent->append(theContext, aSubSh);
567 bool Model_AttributeSelection::update()
569 FeaturePtr aContextFeature = contextFeature();
570 if (aContextFeature.get()) {
571 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
574 TDF_Label aSelLab = selectionLabel();
575 ResultPtr aContext = context();
577 return setInvalidIfFalse(aSelLab, false);
578 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
579 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
582 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
583 std::shared_ptr<GeomAPI_Shape> aNoSelection;
584 bool aResult = selectPart(aContext, aNoSelection, true);
585 aResult = setInvalidIfFalse(aSelLab, aResult);
587 owner()->data()->sendAttributeUpdated(this);
592 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
593 // body: just a named shape, use topological selection mechanism
594 bool aResult = false;
595 TopoDS_Shape anOldShape;
596 Handle(TNaming_NamedShape) aNS;
597 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
598 anOldShape = aNS->Get();
600 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
601 Selector_Selector aSelector(aSelLab, baseDocumentLab());
602 aResult = aSelector.restore(aContextShape);
603 bool aWasInvalid = aSelLab.IsAttribute(kINVALID_SELECTION);
604 setInvalidIfFalse(aSelLab, aResult);
606 TopoDS_Shape aNewShape;
607 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
608 aNewShape = aNS->Get();
610 if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape) || aWasInvalid)
612 // shape type should not be changed: if shape becomes compound of such shapes, then split
613 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
614 anOldShape.ShapeType() != aNewShape.ShapeType() &&
615 aNewShape.ShapeType() == TopAbs_COMPOUND) {
616 split(aContext, aNewShape, anOldShape.ShapeType());
618 // for issue #3076 check that the new value belongs to the new context
619 if (!aNewShape.IsNull() && !aContextShape.IsNull() &&
620 (aNewShape.ShapeType() == TopAbs_VERTEX || aNewShape.ShapeType() == TopAbs_EDGE ||
621 aNewShape.ShapeType() == TopAbs_FACE)) {
622 TopExp_Explorer anExp(aContextShape, aNewShape.ShapeType());
623 for(; anExp.More(); anExp.Next()) {
624 if (anExp.Current().IsSame(aNewShape))
627 aResult = setInvalidIfFalse(aSelLab, anExp.More());
629 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
634 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
636 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
637 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
638 if (!aConstructionContext->isInfinite()) {
639 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
640 Selector_Selector aSelector(aSelLab, baseDocumentLab());
641 TopoDS_Shape anOldShape = aSelector.value();
642 aResult = aSelector.restore(aContextShape);
643 setInvalidIfFalse(aSelLab, aResult);
644 if (aResult && !anOldShape.IsEqual(aSelector.value()))
645 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
647 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
651 return setInvalidIfFalse(aSelLab, false); // unknown case
654 void Model_AttributeSelection::selectBody(
655 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
657 // perform the selection
658 TopoDS_Shape aContext;
660 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
662 aContext = aBody->shape()->impl<TopoDS_Shape>();
665 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
667 aContext = aResult->shape()->impl<TopoDS_Shape>();
669 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
674 if (!aContext.IsNull()) {
675 TDF_Label aSelLab = selectionLabel();
676 TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
678 bool aSelectorOk = true;
679 Selector_Selector aSelector(aSelLab, baseDocumentLab());
681 aSelectorOk = aSelector.select(aContext, aNewSub, myIsGeometricalSelection);
683 aSelectorOk = aSelector.store(aContext);
689 TopoDS_Shape aShape = aSelector.value();
690 aSelectorOk = !aShape.IsNull() && aShape.ShapeType() == aNewSub.ShapeType();
692 setInvalidIfFalse(aSelLab, aSelectorOk);
696 bool Model_AttributeSelection::selectPart(
697 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
698 const bool theUpdate)
700 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
701 if (!aPart.get() || !aPart->isActivated())
702 return true; // postponed naming
704 Handle(TDataStd_Integer) anIndex;
705 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
706 // by internal selection
707 if (anIndex->Get() > 0) {
708 // update the selection by index
709 return aPart->updateInPart(anIndex->Get());
711 return true; // nothing to do, referencing just by name
714 return true; // nothing to do, referencing just by name
716 // store the shape (in case part is not loaded it should be useful
718 std::string aName = theContext->data()->name();
719 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
720 aShape = theContext->shape()->impl<TopoDS_Shape>();
722 aShape = theSubShape->impl<TopoDS_Shape>();
724 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
725 TDataStd_Integer::Set(selectionLabel(), anIndex);
727 TNaming_Builder aBuilder(selectionLabel());
728 aBuilder.Select(aShape, aShape);
729 // identify by name in the part
730 TDataStd_Name::Set(selectionLabel(), aName.c_str());
731 return !aName.empty();
734 TDF_Label Model_AttributeSelection::selectionLabel()
736 return myRef.myRef->Label().FindChild(1);
739 /// prefixes of the shape names with centers defined
740 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
742 /// returns the map that contains all possible prefixes of the center-names
743 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
745 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
746 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
747 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
748 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
750 return kCENTERS_PREFIX;
753 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
755 std::string aName("");
756 if(!this->isInitialized())
757 return !theDefaultName.empty() ? theDefaultName : aName;
759 // not argument has not parametric name (filters)
760 if (!this->isArgument() || (myParent && !myParent->isArgument())) {
761 GeomShapePtr aShape = value();
762 if (!aShape.get() && context().get())
763 aShape = context()->shape();
766 aName = aShape->shapeTypeStr();
768 aName += std::string("_") +
769 TCollection_AsciiString(selectionLabel().Father().Tag()).ToCString();
775 CenterType aCenterType = NOT_CENTER;
776 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
778 FeaturePtr aContFeature = contextFeature();
779 if (aContFeature.get()) {
780 std::string aResName;
781 // checking part-owner
782 if (aContFeature->document() != owner()->document())
783 aResName += aContFeature->document()->kind() + "/";
784 // selection of a full feature
785 if (aContFeature.get()) {
786 return aResName + kWHOLE_FEATURE + aContFeature->name();
788 // in case of selection of removed result
792 ResultPtr aCont = context();
793 TDF_Label aSelLab = selectionLabel();
794 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
795 return contextName(aCont);
798 // if it is in result of another part
799 if (aCont->groupName() == ModelAPI_ResultPart::group()) {
800 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCont);
802 std::string aResult = aSubSh.get() ?
803 aPart->data()->name() + "/" + aPart->nameInPart(aSubSh, anIndex) : aPart->data()->name();
804 if (aCenterType != NOT_CENTER)
805 aResult += centersMap()[aCenterType];
809 // whole infinitive construction
810 if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
811 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
812 if (aConstr->isInfinite()) {
813 return contextName(aCont);
817 Selector_Selector aSelector(aSelLab, baseDocumentLab());
819 if (aCont->shape().get() && aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
820 aResult = aSelector.name(this);
821 if (aCenterType != NOT_CENTER) {
822 aResult += centersMap()[aCenterType];
827 // returns the center type and modifies the shape name if this name is center-name
828 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
830 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
831 centersMap().begin();
832 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
833 std::size_t aFound = theShapeName.find(aPrefixIter->second);
834 if (aFound != std::string::npos &&
835 aFound == theShapeName.size() - aPrefixIter->second.size()) {
836 theShapeName = theShapeName.substr(0, aFound);
837 return aPrefixIter->first;
840 return ModelAPI_AttributeSelection::NOT_CENTER;
843 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
844 void Model_AttributeSelection::selectSubShape(
845 const std::string& theType, const std::string& theSubShapeName)
847 if(theSubShapeName.empty() || theType.empty()) return;
849 std::string aSubShapeName = theSubShapeName;
850 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
851 centerTypeByName(aSubShapeName) : NOT_CENTER;
852 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
853 static const GeomShapePtr anEmptyShape;
855 // first iteration is selection by name without center prefix, second - in case of problem,
856 // try with initial name
857 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
858 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
859 aSubShapeName = theSubShapeName;
860 aCenterType = NOT_CENTER;
862 } else if (aUseCenter != 1) continue;
863 TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(aType));
865 std::shared_ptr<Model_Document> aDoc =
866 std::dynamic_pointer_cast<Model_Document>(owner()->document());
867 // check this is Part-name: 2 delimiters in the name
868 std::size_t aPartEnd = aSubShapeName.find('/');
869 if (aPartEnd != std::string::npos) {
870 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
871 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
872 if (aPartName == aRootDoc->kind()) {
873 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
874 aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
877 owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
878 if (aFound.get()) { // found such part, so asking it for the name
879 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
880 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
881 if (aNameInPart.empty()) { // whole part
882 setValue(aPart, anEmptyShape);
886 std::shared_ptr<GeomAPI_Shape> aSelected =
887 aPart->shapeInPart(aNameInPart, aType, anIndex);
888 if (aSelected.get()) {
889 if (aCenterType != NOT_CENTER) {
890 if (!aSelected->isEdge())
892 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
893 setValueCenter(aPart, aSelectedEdge, aCenterType);
895 setValue(aPart, aSelected);
896 TDataStd_Integer::Set(selectionLabel(), anIndex);
903 // check this is a whole feature context
904 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
905 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
906 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
907 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
909 setValue(anObj, anEmptyShape);
914 // the whole result selection check
915 if (aSubShapeName.find('/') == std::string::npos) {
916 ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
918 aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
920 aRes = aDoc->objectByName(ModelAPI_ResultGroup::group(), aSubShapeName);
923 setValue(aRes, anEmptyShape);
928 Selector_Selector aSelector(selectionLabel(), baseDocumentLab());
929 myRestoreDocument = aDoc;
930 TDF_Label aContextLabel = aSelector.restoreByName(
931 aSubShapeName, aShapeType, this, myIsGeometricalSelection);
932 myRestoreDocument.reset();
933 if (!aContextLabel.IsNull()) {
934 ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
935 if (aContext.get() && aContext->shape().get()) {
936 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
937 if (aSelector.solve(aContextShape)) {
938 TopoDS_Shape aSelectorShape = aSelector.value();
939 GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
940 aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelectorShape));
941 // make the context result the latest existing
942 aContext = newestContext(aContext, aShapeToBeSelected);
943 if (myIsGeometricalSelection || aCenterType == NOT_CENTER) {
944 // store the currently generated name
945 selectionLabel().ForgetAllAttributes(true);
946 bool aToUnblock = false;
947 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
948 myRef.setValue(aContext);
949 aSelector.store(aContextShape);
950 owner()->data()->sendAttributeUpdated(this);
952 owner()->data()->blockSendAttributeUpdated(false);
954 } else { // re-select center of circle/arc by context and value
955 if (!aShapeToBeSelected->isEdge())
957 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
958 setValueCenter(aContext, aSelectedEdge, aCenterType);
966 TDF_Label aSelLab = selectionLabel();
967 setInvalidIfFalse(aSelLab, false);
971 void Model_AttributeSelection::selectSubShape(const std::string& theType,
972 const GeomPointPtr& thePoint)
974 if (theType.empty() || !thePoint)
977 // list of parent features
978 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
979 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
981 int aSelectionIndex = 0;
982 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
983 if (aType == GeomAPI_Shape::SHAPE) {
984 // possibly, the string consists of the type and the index,
985 // thus, try to separate them
986 size_t aUndersporePos = theType.find_first_of('_');
987 if (aUndersporePos != std::string::npos)
988 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
990 if (aType != GeomAPI_Shape::SHAPE) {
991 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
992 aChar != theType.end(); ++aChar) {
993 if (std::isdigit(*aChar))
994 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
1000 aSelectionIndex -= 1;
1004 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
1006 // collect features from PartSet and the current part
1007 SessionPtr aSession = ModelAPI_Session::get();
1008 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
1009 if (aSession->moduleDocument() != owner()->document()) {
1010 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
1011 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
1013 // Process results of all features from the last to the first
1014 // to find appropriate sub-shape
1015 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
1016 anIt != aFeatures.rend(); ++anIt) {
1017 // selection cannot be linked to the parent features
1018 if (aParents.find(*anIt) != aParents.end())
1020 // check the feature is a part of composite feature (like sketch elements),
1021 // then do not process it, it will be processed in scope of composite feature
1022 bool isSubOfComposite = false;
1023 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
1024 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
1025 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
1026 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
1027 CompositeFeaturePtr aCompFeature =
1028 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
1029 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
1031 if (isSubOfComposite)
1034 // process results of the current feature to find appropriate sub-shape
1035 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
1036 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
1037 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1038 ++anApIt; // skip this shape, because one of the previous is selected
1040 if (anApIt != anAppropriate.end()) {
1041 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1042 setValue(anApIt->myResult, anApIt->mySubshape);
1044 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1045 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1051 TDF_Label aSelLab = selectionLabel();
1052 setInvalidIfFalse(aSelLab, false);
1056 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1057 const std::string& theContextName, const int theIndex)
1059 // selection of context by name
1060 selectSubShape(theType, theContextName);
1061 ResultPtr aContext = context();
1062 if (aContext.get()) {
1063 GeomShapePtr aContShape = aContext->shape();
1064 if (aContShape.get()) {
1065 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1066 GeomShapePtr aValue = aNExp.shape(theIndex);
1068 setValue(aContext, aValue);
1073 void Model_AttributeSelection::setId(int theID)
1075 std::shared_ptr<GeomAPI_Shape> aSelection;
1077 ResultPtr aContextRes = context();
1078 // support for compsolids:
1079 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1080 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1082 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1084 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1085 // searching for the latest main shape
1086 if (theID > 0 && aContext && !aContext->isNull())
1088 std::shared_ptr<Model_Document> aDoc =
1089 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1091 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1092 if (!aNS.IsNull()) {
1093 aMainShape = TNaming_Tool::CurrentShape(aNS);
1097 TopTools_IndexedMapOfShape aSubShapesMap;
1098 TopExp::MapShapes(aMainShape, aSubShapesMap);
1099 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1101 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1102 aResult->setImpl(new TopoDS_Shape(aSelShape));
1104 aSelection = aResult;
1107 setValue(aContextRes, aSelection);
1110 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1112 std::string aResult;
1113 if (owner()->document() != theContext->document()) {
1114 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1115 aResult = theContext->document()->kind() + "/";
1117 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1118 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1119 if (aDocRes.get()) {
1120 aResult = aDocRes->data()->name() + "/";
1124 aResult += theContext->data()->name();
1128 void Model_AttributeSelection::computeValues(
1129 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1130 TopTools_ListOfShape& theShapes)
1132 bool aWasWholeContext = theValShape.IsNull();
1133 if (aWasWholeContext) {
1134 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1136 TopAbs_ShapeEnum aValType = theValShape.ShapeType();
1137 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1138 // if a new value is unchanged in the new context, do nothing: value is correct
1139 TopExp_Explorer aSubExp(aNewContShape, aValType);
1140 for(; aSubExp.More(); aSubExp.Next()) {
1141 if (aSubExp.Current().IsSame(theValShape)) {
1142 theShapes.Append(theValShape);
1146 // if new context becomes compsolid, the resulting sub may be in sub-solids
1147 std::list<ResultPtr> aNewToIterate;
1148 aNewToIterate.push_back(theNewContext);
1149 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1151 std::list<ResultPtr> allNewContextSubs;
1152 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1153 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1154 for(; aSub != allNewContextSubs.end(); aSub++) {
1155 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1156 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1157 aNewToIterate.push_back(aBody);
1161 // first iteration: searching for the whole shape appearance (like face of the box)
1162 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1163 int aToFindPart = 0;
1164 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1165 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1166 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1167 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1168 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1169 std::shared_ptr<Model_Data> aNewData =
1170 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1171 TDF_Label aNewLab = aNewData->shapeLab();
1172 // searching for produced sub-shape fully on some label
1173 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID());
1174 for(; aNSIter.More(); aNSIter.Next()) {
1175 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1176 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1177 if (aToFindPart == 0) { // search shape is fully inside
1178 if (aPairIter.OldShape().IsSame(theValShape)) {
1179 if (aPairIter.NewShape().IsNull()) {// value was removed
1183 // don't add edges generated from faces
1184 if (aPairIter.NewShape().ShapeType() <= aValType)
1185 theShapes.Append(aPairIter.NewShape());
1187 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1188 TopExp_Explorer anExp(aPairIter.OldShape(), aValType);
1189 for(; anExp.More(); anExp.Next()) {
1190 if (anExp.Current().IsSame(theValShape)) { // found a new container
1191 if (aPairIter.NewShape().IsNull()) // skip removed high-level shape
1193 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1194 anOlds.Add(aPairIter.OldShape());
1203 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1204 // also iterate the whole old shape to find not-modified shapes that contain this old
1205 TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1206 NCollection_Map<TopAbs_ShapeEnum> aNewTypes; // types of shapes to iterate
1207 TopTools_DataMapOfShapeShape::Iterator aNewTypeIter(aNewToOld);
1208 for(; aNewTypeIter.More(); aNewTypeIter.Next()) {
1209 if (aNewTypeIter.Key().ShapeType() != aValType)
1210 aNewTypes.Add(aNewTypeIter.Key().ShapeType());
1212 NCollection_Map<TopAbs_ShapeEnum>::Iterator aTypeIter(aNewTypes);
1213 for(; aTypeIter.More(); aTypeIter.Next()) {
1214 for(TopExp_Explorer anExp(anOldContShape, aTypeIter.Value()); anExp.More(); anExp.Next()) {
1215 TopoDS_Shape anOld = anExp.Current();
1216 if (aNewToOld.IsBound(anOld) || anOlds.Contains(anOld)) // this was modified
1218 TopExp_Explorer aValExp(anOld, aValType);
1219 for(; aValExp.More(); aValExp.Next()) {
1220 const TopoDS_Shape& anUnchanged = aValExp.Current();
1221 if (anUnchanged.IsSame(theValShape)) {
1222 aNewToOld.Bind(anOld, anOld);
1230 // map of sub-shapes -> number of occurrences of these shapes in containers
1231 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1232 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1233 for(; aContIter.More(); aContIter.Next()) {
1234 TopExp_Explorer aSubExp(aContIter.Key(), aValType);
1235 for(; aSubExp.More(); aSubExp.Next()) {
1236 if (!aSubs.IsBound(aSubExp.Current())) {
1237 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1239 // store old to know how many olds produced this shape
1240 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1243 // if sub is appeared same times in containers as the number of old shapes that contain it
1244 int aCountInOld = anOlds.Size();
1245 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1247 for(; aSubsIter.More(); aSubsIter.Next()) {
1248 if (aSubsIter.Value().Size() == aCountInOld) {
1249 TopoDS_Shape anOld = aSubsIter.Key();
1250 // check this exists in the new shape
1251 TopExp_Explorer aNew(aNewContShape, anOld.ShapeType());
1252 for (; aNew.More(); aNew.Next()) {
1253 if (aNew.Current().IsSame(anOld))
1257 theShapes.Append(anOld);
1261 if (theShapes.IsEmpty()) { // nothing was changed
1262 if (aWasWholeContext)
1263 theShapes.Append(TopoDS_Shape());
1264 else { // if theValShape exists in new context, add it without changes, otherwise - nothing
1265 for (TopExp_Explorer aNew(aNewContShape, aValType); aNew.More(); aNew.Next()){
1266 if (aNew.Current().IsSame(theValShape)) {
1267 theShapes.Append(theValShape);
1272 } else if (theShapes.Size() > 1) {
1273 // check it is possible to remove extra sub-shapes:
1274 // keep only shapes with the same number of containers if possible
1275 TopAbs_ShapeEnum anAncType = TopAbs_FACE;
1276 if (aValType == TopAbs_VERTEX)
1277 anAncType = TopAbs_EDGE;
1278 TopoDS_Shape anOldContext = theOldContext->shape()->impl<TopoDS_Shape>();
1279 TopTools_IndexedDataMapOfShapeListOfShape anOldMap;
1280 TopExp::MapShapesAndUniqueAncestors(anOldContext, aValType, anAncType, anOldMap);
1281 if (anOldMap.Contains(theValShape)) {
1282 int aNumInOld = anOldMap.FindFromKey(theValShape).Extent();
1283 TopTools_IndexedDataMapOfShapeListOfShape aNewMap;
1284 TopExp::MapShapesAndUniqueAncestors(aNewContShape, aValType, anAncType, aNewMap);
1285 TopTools_ListOfShape aNewResults;
1286 for(TopTools_ListOfShape::Iterator aNewSubs(theShapes); aNewSubs.More(); aNewSubs.Next()) {
1287 TopoDS_Shape aCand = aNewSubs.Value();
1288 if (aNewMap.Contains(aCand) && aNewMap.FindFromKey(aCand).Extent() == aNumInOld)
1289 aNewResults.Append(aCand);
1291 if (!aNewResults.IsEmpty() && aNewResults.Size() < theShapes.Size())
1292 theShapes = aNewResults;
1298 void Model_AttributeSelection::concealedFeature(
1299 const FeaturePtr theFeature, const FeaturePtr theStop, const bool theCheckCopy,
1300 std::list<FeaturePtr>& theConcealers, const ResultPtr theResultOfFeature)
1302 std::set<FeaturePtr> alreadyProcessed;
1303 alreadyProcessed.insert(theFeature);
1305 alreadyProcessed.insert(theStop);
1306 /// iterate all results to find the concealment-attribute
1307 std::list<ResultPtr> aRootRes;
1308 if (theResultOfFeature.get()) {
1309 ResultPtr aRoot = ModelAPI_Tools::bodyOwner(theResultOfFeature, true);
1310 aRootRes.push_back(aRoot ? aRoot : theResultOfFeature);
1311 } else { // all results of a feature
1312 aRootRes = theFeature->results();
1314 std::list<ResultPtr>::const_iterator aRootIter = aRootRes.cbegin();
1315 for(; aRootIter != aRootRes.cend(); aRootIter++) {
1316 std::list<ResultPtr> allRes;
1317 allRes.push_back(*aRootIter);
1318 ResultBodyPtr aRootBody = ModelAPI_Tools::bodyOwner(*aRootIter, true);
1319 if (!aRootBody.get())
1320 aRootBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRootIter);
1321 if (aRootBody.get()) {
1322 ModelAPI_Tools::allSubs(aRootBody, allRes);
1324 for(std::list<ResultPtr>::iterator aRIter = allRes.begin(); aRIter != allRes.end(); aRIter++) {
1325 const std::set<AttributePtr>& aRefs = (*aRIter)->data()->refsToMe();
1326 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1327 for (; aRef != aRefs.cend(); aRef++) {
1328 if (!aRef->get() || !(*aRef)->owner().get())
1330 // concealed attribute only
1331 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1332 if (alreadyProcessed.find(aRefFeat) != alreadyProcessed.end()) // optimization
1334 alreadyProcessed.insert(aRefFeat);
1335 if (ModelAPI_Session::get()->validators()->isConcealed(aRefFeat->getKind(), (*aRef)->id())
1337 std::dynamic_pointer_cast<ModelAPI_FeatureCopyInterface>(aRefFeat).get()))
1339 // for extrusion cut in python script the nested sketch reference may be concealed before
1340 // it is nested, so, check this composite feature is valid
1341 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
1342 // need to be validated to update the "Apply" state if not previewed
1343 if (aFactory->validate(aRefFeat)) {
1344 if (theStop.get()) {
1345 std::shared_ptr<Model_Document> aDoc =
1346 std::dynamic_pointer_cast<Model_Document>(theStop->document());
1347 if (!aDoc->isLaterByDep(theStop, aRefFeat)) // skip feature later than stop
1350 theConcealers.push_back(aRefFeat);
1358 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1359 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1360 TDF_Label theAccessLabel,
1361 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1363 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1364 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1365 TopTools_ListOfShape aContextList;
1366 aContextList.Append(theContShape);
1367 if (theContext.get()) {
1368 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1369 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1370 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1372 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1373 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1374 for(; aModifIter.More(); aModifIter.Next()) {
1375 TDF_Label anObjLab = aModifIter.Label().Father();
1376 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1377 (theDoc->objects()->object(anObjLab));
1378 if (!aModifierObj.get()) {
1379 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1380 if (!anObjLab.IsRoot())
1381 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1382 (theDoc->objects()->object(anObjLab.Father()));
1383 if (!aModifierObj.get())
1386 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1387 if (!aModifierFeat.get())
1389 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1390 if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
1391 continue; // the modifier feature is later than this, so, should not be used
1392 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1393 if (aCurrentModifierFeat == aModifierFeat ||
1394 !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
1395 continue; // the current modifier is later than the found, so, useless
1396 Handle(TNaming_NamedShape) aNewNS;
1397 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1398 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1399 aResults.insert(aModifierObj);
1400 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1401 aResults.insert(ResultPtr());
1402 } else { // not-processed modification => don't support it
1407 // if there exist context composite and sub-result(s), leave only sub(s)
1408 for(std::set<ResultPtr>::iterator aResIter = aResults.begin(); aResIter != aResults.end();) {
1409 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1410 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1411 if (aResults.count(aParent))
1413 if (aParent.get()) { // erase from set, so, restart iteration
1414 aResults.erase(aParent);
1415 aResIter = aResults.begin();
1419 bool aStaySame = false;
1420 if (aResults.empty()) {
1421 // check the context become concealed by operation which is earlier than this selection
1422 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1423 FeaturePtr aContextOwner = theDoc->feature(theContext);
1424 std::list<FeaturePtr> aConcealers;
1425 concealedFeature(aContextOwner, aThisFeature, false, aConcealers, theContext);
1426 std::list<FeaturePtr>::iterator aConcealer = aConcealers.begin();
1427 for(; aConcealer != aConcealers.end(); aConcealer++) {
1428 std::list<ResultPtr> aRefResults;
1429 ModelAPI_Tools::allResults(*aConcealer, aRefResults);
1430 std::list<ResultPtr>::iterator aRefIter = aRefResults.begin();
1431 for(; aRefIter != aRefResults.end(); aRefIter++) {
1432 ResultBodyPtr aRefBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefIter);
1433 if (!aRefBody.get() || aRefBody->numberOfSubs() != 0) // iterate only leafs
1435 GeomShapePtr aRefShape = aRefBody->shape();
1436 if (!aRefShape.get() || aRefShape->isNull())
1438 if (aRefShape->impl<TopoDS_Shape>().IsSame(theContShape)) {
1439 // add the new context result with the same shape
1440 aResults.insert(aRefBody);
1443 if (aResults.empty())
1444 return true; // feature conceals result, return true, so the context will be removed
1446 aStaySame = aResults.empty();
1448 if (myParent && myParent->isMakeCopy()) {
1449 // check there are copies before the new results, so, make a copy
1450 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1451 std::list<ObjectPtr> aCopyContext;
1452 std::list<GeomShapePtr> aCopyVals;
1453 // features between the new and the old: check the "Move" interface to get a copy
1454 FeaturePtr aRootOwner = theDoc->feature(theContext);
1455 FeaturePtr anOwner = ModelAPI_Tools::compositeOwner(aRootOwner);
1456 for(; anOwner.get(); anOwner = ModelAPI_Tools::compositeOwner(anOwner))
1457 aRootOwner = anOwner;
1458 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1459 // iterate all results to find a "Copy" features between the new and one and to add the
1460 // copy-results also to results if this attribute refers to the copied shape
1461 int anIndex = kUNDEFINED_FEATURE_INDEX;
1462 for(FeaturePtr aFeat = theDoc->objects()->nextFeature(aRootOwner, anIndex); aFeat.get() &&
1463 aFeat != aThisFeature; aFeat = theDoc->objects()->nextFeature(aFeat, anIndex)) {
1464 std::shared_ptr<ModelAPI_FeatureCopyInterface> aCopier =
1465 std::dynamic_pointer_cast<ModelAPI_FeatureCopyInterface>(aFeat);
1466 if (aCopier.get()) {
1467 GeomShapePtr aValShape(new GeomAPI_Shape);
1468 aValShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(
1469 theValShape.IsNull() ? theContShape : theValShape));
1470 aCopier->getCopies(theContext, aValShape, aCopyContext, aCopyVals);
1473 // check for the further modifications of the copy contexts and values
1474 std::list<ObjectPtr>::iterator aCopyContIter = aCopyContext.begin();
1475 std::list<GeomShapePtr>::iterator aCopyValIter = aCopyVals.begin();
1476 for(; aCopyContIter != aCopyContext.end(); aCopyContIter++, aCopyValIter++) {
1477 ResultPtr aNewCont = std::dynamic_pointer_cast<ModelAPI_Result>(*aCopyContIter);
1478 TopoDS_Shape aNewContShape = aNewCont->shape()->impl<TopoDS_Shape>();
1479 GeomShapePtr aNewVal = *aCopyValIter;
1480 TopoDS_Shape aNewValShape;
1481 if (aNewVal.get() && !aNewVal->isNull())
1482 aNewValShape = aNewVal->impl<TopoDS_Shape>();
1483 std::list<ResultPtr> aNewRes;
1484 TopTools_ListOfShape aNewUpdatedVal;
1485 if (searchNewContext(theDoc, aNewContShape, aNewCont, aNewValShape,
1486 theAccessLabel, aNewRes, aNewUpdatedVal)) {
1487 // append new results instead of the current ones
1488 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1489 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1490 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1491 theResults.push_back(*aNewIter);
1492 theValShapes.Append(aNewUpdVal.Value());
1494 } else { // the current result is good
1495 theResults.push_back(aNewCont);
1496 theValShapes.Append(aNewValShape);
1499 if (aStaySame && !theResults.empty()) { // no changes except copy, so, keep the origin as first
1500 theResults.push_front(theContext);
1501 theValShapes.Prepend(theValShape);
1508 // iterate all results to find further modifications
1509 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1510 for(aResIter = aResults.begin(); aResIter != aResults.end(); aResIter++) {
1511 if (aResIter->get() != NULL) {
1512 ResultPtr aNewResObj = *aResIter;
1513 // compute new values by two contexts: the old and the new
1514 TopTools_ListOfShape aValShapes;
1515 computeValues(theContext, aNewResObj, theValShape, aValShapes);
1517 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1518 for(; aNewVal.More(); aNewVal.Next()) {
1519 std::list<ResultPtr> aNewRes;
1520 TopTools_ListOfShape aNewUpdatedVal;
1521 TopoDS_Shape aNewValSh = aNewVal.Value();
1522 TopoDS_Shape aNewContShape = aNewResObj->shape()->impl<TopoDS_Shape>();
1524 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1525 aNewValSh.Nullify();
1526 if (searchNewContext(theDoc, aNewContShape, aNewResObj, aNewValSh,
1527 theAccessLabel, aNewRes, aNewUpdatedVal))
1529 // append new results instead of the current ones
1530 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1531 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1532 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1533 theResults.push_back(*aNewIter);
1534 theValShapes.Append(aNewUpdVal.Value());
1536 } else { // the current result is good
1537 theResults.push_back(aNewResObj);
1538 theValShapes.Append(aNewValSh);
1543 return true; // theResults must be empty: everything is deleted
1546 void Model_AttributeSelection::updateInHistory(bool& theRemove)
1548 static std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1550 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1551 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1552 aContext->groupName() != ModelAPI_ResultPart::group())) {
1553 // but check the case the whole results are allowed: whole features may be selected
1554 if (myParent && myParent->isWholeResultAllowed()) {
1555 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
1556 if (aFeature.get()) {
1557 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1558 std::list<FeaturePtr> aConcealers;
1559 bool aCopyPossible = myParent && myParent->isMakeCopy();
1560 concealedFeature(aFeature, aThisFeature, aCopyPossible, aConcealers, ResultPtr());
1561 if (aConcealers.empty())
1563 // if there are copies, but no direct modification, keep the original
1564 bool aKeepOrigin = false;
1565 if (aCopyPossible) {
1566 std::list<FeaturePtr>::iterator aConcealer = aConcealers.begin();
1567 for(aKeepOrigin = true; aConcealer != aConcealers.end(); aConcealer++)
1568 if (!std::dynamic_pointer_cast<ModelAPI_FeatureCopyInterface>(*aConcealer).get()) {
1569 aKeepOrigin = false;
1573 aConcealers.push_front(aFeature);
1576 bool aChanged = false;
1577 std::list<FeaturePtr>::iterator aConcealer = aConcealers.begin();
1578 for(; aConcealer != aConcealers.end(); aConcealer++)
1580 if (aKeepOrigin || !myParent->isInList(*aConcealer, anEmptyShape))
1581 myParent->append(*aConcealer, anEmptyShape);
1583 if (!myParent->isInList(*aConcealer, anEmptyShape)) {// avoid addition of duplicates
1584 setValue(*aConcealer, anEmptyShape);
1586 } else if (aCopyPossible && *aConcealer == aFeature) {// keep origin in case of copy
1590 if (!aChanged) // remove this
1592 else if (!aKeepOrigin) // searching further modifications only if current changed
1593 updateInHistory(theRemove);
1596 return;// only bodies and parts may be modified later in the history, skip otherwise
1599 std::shared_ptr<Model_Document> aDoc =
1600 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1601 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1602 if (!aContData.get() || !aContData->isValid())
1604 TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
1606 // checking this may be just a reference to another context (same shape), so use that label
1607 Handle(TNaming_NamedShape) aContNS;
1608 Handle(TDF_Reference) aRefAttr;
1609 while(!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS) &&
1610 aContLab.FindAttribute(TDF_Reference::GetID(), aRefAttr))
1611 aContLab = aRefAttr->Get();
1613 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1614 bool aFoundNewContext = true;
1615 ResultPtr aNewContext = aContext;
1616 while(aFoundNewContext) {
1617 aFoundNewContext = false;
1618 // parts have no shape in result, so, trace references using the Part info
1619 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1620 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1621 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1622 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1623 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1624 for(; aRef != aRefs.end(); aRef++) {
1625 // to avoid detection of part changes by local selection only
1626 AttributeSelectionPtr aSel =
1627 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1628 if (aSel.get() && aSel->value().get() &&
1629 !aSel->value()->isSame(aSel->context()->shape()))
1632 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1634 if (aRefFeat.get() && aRefFeat != owner() && aRefFeat->firstResult().get()) {
1635 // check the reference is concealed: #2900
1636 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1637 if (!aValidators->isConcealed(aRefFeat->getKind(), (*aRef)->id()))
1639 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1640 if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
1641 aFoundNewContext = true;
1642 aNewContext = aRefFeat->firstResult();
1649 if (aNewContext != aContext) {
1650 setValue(aNewContext, value());
1654 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1655 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1656 // iterate the context shape modifications in order to find a feature that is upper in history
1657 // that this one and is really modifies the referenced result to refer to it
1658 ResultPtr aModifierResFound;
1659 TNaming_Iterator aPairIter(aContNS);
1660 if (!aPairIter.More())
1662 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1663 bool anIterate = true;
1664 // trying to update also the sub-shape selected
1665 GeomShapePtr aSubShape = value();
1666 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1668 TopoDS_Shape aValShape;
1669 if (aSubShape.get()) {
1670 aValShape = aSubShape->impl<TopoDS_Shape>();
1673 std::list<ResultPtr> aNewContexts;
1674 TopTools_ListOfShape aValShapes;
1675 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1677 std::set<ResultPtr> allContexts, aSkippedContext;
1678 std::list<ResultPtr>::iterator aNewContext = aNewContexts.begin();
1679 for(; aNewContext != aNewContexts.end(); aNewContext++)
1680 allContexts.insert(*aNewContext);
1682 // if there exist context composite and sub-result(s), leave only sub(s)
1683 std::set<ResultPtr>::iterator aResIter = allContexts.begin();
1684 for(; aResIter != allContexts.end(); aResIter++) {
1685 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1686 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1687 if (allContexts.count(aParent))
1688 aSkippedContext.insert(aParent);
1691 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1693 if (myParent->selectionType() == "VERTEX" || myParent->selectionType() == "Vertices")
1694 aListShapeType = GeomAPI_Shape::VERTEX;
1695 else if (myParent->selectionType() == "EDGE" || myParent->selectionType() == "Edges")
1696 aListShapeType = GeomAPI_Shape::EDGE;
1697 else if (myParent->selectionType() == "FACE" || myParent->selectionType() == "Faces")
1698 aListShapeType = GeomAPI_Shape::FACE;
1701 // issue #3031: skip topology if there is more convenient shape type presents in the
1702 // same context as a result of this
1703 bool isWholeResult = myParent && myParent->isWholeResultAllowed() && !aSubShape.get();
1704 GeomAPI_Shape::ShapeType allowedType = GeomAPI_Shape::SHAPE;
1705 if (isWholeResult) {
1706 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1707 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1708 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1709 if (aNewValues.Value().IsNull()) { // only for the whole context
1710 GeomAPI_Shape::ShapeType aShapeType = (*aNewCont)->shape()->shapeType();
1711 if (allowedType == GeomAPI_Shape::SHAPE) { // just set this one
1712 allowedType = aShapeType;
1714 GeomAPI_Shape::ShapeType anAllowed = allowedType;
1715 if (anAllowed != aShapeType) { // select the best, nearest to the origin
1716 GeomAPI_Shape::ShapeType anOldShapeType = aContext->shape()->shapeType();
1717 GeomAPI_Shape::ShapeType aDeltaAllowed =
1718 (GeomAPI_Shape::ShapeType)(anOldShapeType - anAllowed);
1719 if (aDeltaAllowed < 0)
1720 aDeltaAllowed = (GeomAPI_Shape::ShapeType)(-aDeltaAllowed);
1721 GeomAPI_Shape::ShapeType aDeltaThis =
1722 (GeomAPI_Shape::ShapeType)(anOldShapeType - aShapeType);
1724 aDeltaThis = (GeomAPI_Shape::ShapeType)(-aDeltaThis);
1725 if (aDeltaThis == aDeltaAllowed) { // equal distance to context, select complicated
1726 if (anOldShapeType < anAllowed)
1727 allowedType = aShapeType;
1728 } else if (aDeltaAllowed > aDeltaThis) { // this wins
1729 allowedType = aShapeType;
1737 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1738 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1739 bool aFirst = true; // first is set to this, next are appended to parent
1740 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1741 if (aSkippedContext.count(*aNewCont))
1744 if (isWholeResult && aNewValues.Value().IsNull())
1745 if (allowedType != GeomAPI_Shape::SHAPE &&
1746 (*aNewCont)->shape()->shapeType() != allowedType)
1747 continue; // there is better result exists with the better shape type (issue #3031)
1749 GeomShapePtr aValueShape;
1750 if (!aNewValues.Value().IsNull()) {
1751 aValueShape = std::make_shared<GeomAPI_Shape>();
1752 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1754 // Check that list has the same type of shape selection before adding.
1755 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1756 if (aValueShape.get()) {
1757 aShapeShapeType = aValueShape->shapeType();
1759 aShapeShapeType = (*aNewCont)->shape()->shapeType();
1761 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1762 // exception is for whole results selected
1763 if (!isWholeResult) {
1768 ResultPtr aNewContext = *aNewCont;
1769 if (aValueShape.get()) { // #2892 if context is higher level result, search this sub in lower
1770 ResultBodyPtr aBodyContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aNewContext);
1771 if (aBodyContext.get() && aBodyContext->numberOfSubs() != 0) {
1772 std::list<ResultPtr> aLower;
1773 ModelAPI_Tools::allSubs(aBodyContext, aLower, true);
1774 for(std::list<ResultPtr>::iterator aL = aLower.begin(); aL != aLower.end(); aL++) {
1775 GeomShapePtr aLShape = (*aL)->shape();
1776 if (aLShape.get() && !aLShape->isNull()) {
1777 if (aLShape->isSubShape(aValueShape, false)) {
1787 if (!myParent || !myParent->isInList(aNewContext, aValueShape)) { // avoid duplicates
1788 setValue(aNewContext, aValueShape);
1790 } else if (aNewContext == aContext && myParent && myParent->isMakeCopy()) {
1791 // this may be exactly the old one, not modified in case of copy
1794 } else if (myParent) {
1795 if (!myParent->isInList(aNewContext, aValueShape)) // avoid addition of duplicates
1796 myParent->append(aNewContext, aValueShape);
1799 if (aFirst) { // nothing was added, all results were deleted
1803 static ResultPtr anEmptyContext;
1804 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1811 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1813 myParent = theParent;
1816 std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
1818 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1819 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1820 FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1821 bool aBaseDocumnetUsed = false;
1822 if (!aFeatureOwner.get()) { // use module document
1823 aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
1824 aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1825 aBaseDocumnetUsed = true;
1827 if (aFeatureOwner.get()) {
1828 // if it is sub-element of the sketch, the context name is the name of the sketch
1829 // searching also for result - real context
1831 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
1832 if (aComposite.get() && aComposite->results().size() == 1 &&
1833 aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
1834 aFeatureOwner = aComposite;
1835 aResult = aFeatureOwner->firstResult();
1837 aResult = aDoc->resultByLab(theSelectionLab);
1839 if (aResult.get()) {
1840 // this is to avoid duplicated names of results problem
1841 std::string aContextName = aResult->data()->name();
1842 // myLab corresponds to the current time
1843 TDF_Label aCurrentLab = selectionLabel();
1844 while(aCurrentLab.Depth() > 3)
1845 aCurrentLab = aCurrentLab.Father();
1847 int aNumInHistoryNames =
1848 aDoc->numberOfNameInHistory(aResult, aCurrentLab);
1849 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
1850 aContextName = "_" + aContextName;
1851 aNumInHistoryNames--;
1853 if (aBaseDocumnetUsed)
1854 aContextName = aDoc->kind() + "/" + aContextName;
1855 return aContextName;
1858 return ""; // invalid case
1861 /// This method restores by the context and value name the context label and
1862 /// sub-label where the value is. Returns true if it is valid.
1863 bool Model_AttributeSelection::restoreContext(std::string theName,
1864 TDF_Label& theContext, TDF_Label& theValue)
1866 static const GeomShapePtr anEmptyShape; // to store context only
1867 std::string aName = theName;
1868 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1869 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1871 // remove the sub-value part if exists
1872 std::string aSubShapeName = aName;
1873 std::string::size_type n = aName.find('/');
1874 if (n != std::string::npos) {
1875 aName = aName.substr(0, n);
1878 if (aName.empty()) return false;
1879 bool anUniqueContext = false;
1880 ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1881 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
1883 aDoc = std::dynamic_pointer_cast<Model_Document>(
1884 ModelAPI_Session::get()->moduleDocument());
1885 if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
1886 aSubShapeName = theName.substr(aDoc->kind().size() + 1);
1887 aName = aSubShapeName;
1888 std::string::size_type n = aName.find('/');
1889 if (n != std::string::npos) {
1890 aName = aName.substr(0, n);
1893 aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1894 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
1898 // searching the sub-shape
1899 static const ResultPtr anEmpty;
1900 theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
1902 // sketch sub-component shape and name is located in separated feature label, try the sub-name
1903 if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
1904 std::string::size_type aSlash = aSubShapeName.rfind('/');
1905 if (aSlash != std::string::npos) {
1906 std::string aCompName = aSubShapeName.substr(aSlash + 1);
1907 CompositeFeaturePtr aComposite =
1908 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
1909 if (aComposite.get() && aComposite->numberOfSubs()) {
1910 const int aSubNum = aComposite->numberOfSubs();
1911 for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
1912 FeaturePtr aSub = aComposite->subFeature(a);
1913 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1914 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
1915 for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
1916 if ((*aRes)->data()->name() == aCompName) {
1917 theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1919 } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
1920 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1921 TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
1922 for(; aSubNames.More(); aSubNames.Next()) {
1923 if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
1924 IsEqual(aCompName.c_str())) {
1925 theValue = aSubNames.Value()->Label();
1937 theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
1942 bool Model_AttributeSelection::isLater(
1943 const TDF_Label theResult1, const TDF_Label theResult2) const
1945 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1946 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1947 FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
1950 FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
1953 return aDoc->isLaterByDep(aFeat1, aFeat2);
1956 ResultPtr Model_AttributeSelection::newestContext(
1957 const ResultPtr theCurrent, const GeomShapePtr theValue)
1959 ResultPtr aResult = theCurrent;
1960 GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
1961 std::shared_ptr<Model_Document> aDoc =
1962 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1963 bool aFindNewContext = true;
1964 while (aFindNewContext && aResult.get()) {
1965 aFindNewContext = false;
1966 // try to find the last context to find the up to date shape
1967 TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
1968 if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
1969 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1970 if (!aNS.IsNull()) {
1971 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1973 && isLater(selectionLabel(), aNS->Label())
1974 && isLater(aNS->Label(),
1975 std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
1977 TDF_Label aLab = aNS->Label();
1978 ResultPtr aRes = aDoc->resultByLab(aLab);
1980 if (aRes->shape()->isSubShape(aSelectedShape)) {
1982 aFindNewContext = true;
1990 // TestFillWireVertex.py - sketch constructions for wire may participate too
1991 //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
1992 // try to search newer context by the concealment references
1993 // take references to all results: root one, any sub
1994 std::list<ResultPtr> allRes;
1995 ResultPtr aCompContext;
1996 ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
1997 if (aCompBody.get()) {
1998 ModelAPI_Tools::allSubs(aCompBody, allRes);
1999 allRes.push_back(aCompBody);
2000 aCompContext = aCompBody;
2003 allRes.push_back(aResult);
2005 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
2006 ResultPtr aResCont = *aSub;
2007 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
2008 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
2009 continue; // only lower and higher level subs are counted
2010 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
2011 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
2012 for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
2013 if (!aRef->get() || !(*aRef)->owner().get())
2015 // concealed attribute only
2016 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
2017 if (!ModelAPI_Session::get()->validators()->isConcealed(
2018 aRefFeat->getKind(), (*aRef)->id()))
2020 // search the feature result that contains sub-shape selected
2021 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
2023 // take all sub-results or one result
2024 std::list<ResultPtr> aRefFeatResults;
2025 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
2026 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
2027 for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
2028 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
2029 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
2030 aResults.push_back(aBody);
2032 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
2034 // searching by sub-shape
2035 for (; aResIter != aResults.end(); aResIter++) {
2036 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
2038 GeomShapePtr aShape = (*aResIter)->shape();
2039 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
2040 aResult = *aResIter; // found new context (produced from this) with same subshape
2041 aFindNewContext = true; // continue searching further
2048 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
2049 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
2050 if (aComp && aComp->numberOfSubs()) {
2051 std::list<ResultPtr> allSubs;
2052 ModelAPI_Tools::allSubs(aComp, allSubs);
2053 std::list<ResultPtr>::iterator aS = allSubs.begin();
2054 for (; aS != allSubs.end(); aS++) {
2055 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
2056 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
2057 aSub->shape()->isSubShape(aSelectedShape)) {
2063 // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
2064 // so, actually, the sketch element was selected (which is never concealed)
2065 if (aResult != theCurrent && aResult->isConcealed())
2066 aResult = theCurrent;
2070 void Model_AttributeSelection::combineGeometrical()
2072 if (myTmpContext.get() || myTmpSubShape.get())
2074 TDF_Label aSelLab = selectionLabel();
2075 if (aSelLab.IsAttribute(kINVALID_SELECTION) || !myRef.isInitialized())
2078 if (aSelLab.IsAttribute(kCIRCLE_CENTER) || aSelLab.IsAttribute(kELLIPSE_CENTER1) ||
2079 aSelLab.IsAttribute(kELLIPSE_CENTER2) || aSelLab.IsAttribute(kSIMPLE_REF_ID))
2082 if (aSelLab.IsAttribute(kPART_REF_ID)) {
2083 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
2084 if (!aPart.get() || !aPart->isActivated())
2085 return; // postponed naming needed
2086 Handle(TDataStd_Integer) anIndex;
2087 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
2088 if (anIndex->Get()) { // special selection attribute was created, use it
2089 std::string aNewName;
2090 aPart->combineGeometrical(anIndex->Get(), aNewName);
2091 TDataStd_Name::Set(aSelLab, aNewName.c_str());
2097 std::shared_ptr<Model_ResultConstruction> aConstr =
2098 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
2101 FeaturePtr aFeature = contextFeature();
2105 Selector_Selector aSelector(aSelLab, baseDocumentLab());
2106 TopoDS_Shape aContextShape = context()->shape()->impl<TopoDS_Shape>();
2107 if (aSelector.restore(aContextShape)) {
2108 aSelector.combineGeometrical(aContextShape);
2112 TDF_Label Model_AttributeSelection::baseDocumentLab()
2114 if (ModelAPI_Session::get()->moduleDocument() != owner()->document())
2115 return std::dynamic_pointer_cast<Model_Document>
2116 (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel();
2117 static TDF_Label anEmpty;
2121 void Model_AttributeSelection::reset()
2123 ModelAPI_AttributeSelection::reset();