1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Model_AttributeSelection.h"
22 #include "Model_Application.h"
23 #include "Model_Events.h"
24 #include "Model_Data.h"
25 #include "Model_Document.h"
26 #include "Model_SelectionNaming.h"
27 #include <Model_Objects.h>
28 #include <Model_AttributeSelectionList.h>
29 #include <Model_ResultConstruction.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_ResultBody.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_ResultPart.h>
35 #include <ModelAPI_CompositeFeature.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelGeomAlgo_Shape.h>
40 #include <Events_InfoMessage.h>
41 #include <GeomAPI_Edge.h>
42 #include <GeomAPI_Pnt.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomAlgoAPI_CompoundBuilder.h>
46 #include <TNaming_Selector.hxx>
47 #include <TNaming_NamedShape.hxx>
48 #include <TNaming_Tool.hxx>
49 #include <TNaming_Builder.hxx>
50 #include <TNaming_SameShapeIterator.hxx>
51 #include <TNaming_Iterator.hxx>
52 #include <TDataStd_Integer.hxx>
53 #include <TDataStd_UAttribute.hxx>
54 #include <TDataStd_Name.hxx>
55 #include <TopTools_ListOfShape.hxx>
56 #include <TopTools_DataMapOfShapeShape.hxx>
57 #include <TopTools_MapOfShape.hxx>
58 #include <TopExp_Explorer.hxx>
59 #include <BRep_Tool.hxx>
62 #include <TDF_ChildIterator.hxx>
63 #include <TDF_ChildIDIterator.hxx>
64 #include <TopoDS_Iterator.hxx>
65 #include <TDF_ChildIDIterator.hxx>
66 #include <Geom_Circle.hxx>
67 #include <Geom_Ellipse.hxx>
68 #include <BRep_Builder.hxx>
70 //#define DEB_NAMING 1
72 #include <BRepTools.hxx>
74 /// added to the index in the packed map to signalize that the vertex of edge is selected
75 /// (multiplied by the index of the edge)
76 static const int kSTART_VERTEX_DELTA = 1000000;
77 // identifier that there is simple reference: selection equals to context
78 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
79 // reference to Part sub-object
80 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
81 // selection is invalid after recomputation
82 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
84 // identifier of the selection of the center of circle on edge
85 Standard_GUID kCIRCLE_CENTER("d0d0e0f1-217a-4b95-8fbb-0c4132f23718");
86 // identifier of the selection of the first focus point of ellipse on edge
87 Standard_GUID kELLIPSE_CENTER1("f70df04c-3168-4dc9-87a4-f1f840c1275d");
88 // identifier of the selection of the second focus point of ellipse on edge
89 Standard_GUID kELLIPSE_CENTER2("1395ae73-8e02-4cf8-b204-06ff35873a32");
91 // prefix for the whole feature context identification
92 const static std::string kWHOLE_FEATURE = "all-in-";
94 // on this label is stored:
95 // TNaming_NamedShape - selected shape
96 // TNaming_Naming - topological selection information (for the body)
97 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
98 // TDataStd_Integer - type of the selected shape (for construction)
99 // TDF_Reference - from ReferenceAttribute, the context
100 bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
101 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
103 if (theTemporarily &&
104 (!theContext.get() || theContext->groupName() != ModelAPI_Feature::group())) {
105 // just keep the stored without DF update
106 myTmpContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
107 myTmpSubShape = theSubShape;
108 owner()->data()->sendAttributeUpdated(this);
111 myTmpContext.reset();
112 myTmpSubShape.reset();
113 myTmpCenterType = NOT_CENTER;
117 const std::shared_ptr<GeomAPI_Shape>& anOldShape = internalValue(aType);
118 bool isOldContext = theContext == myRef.value();
119 bool isOldShape = isOldContext &&
120 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
121 if (isOldShape) return false; // shape is the same, so context is also unchanged
122 bool aToUnblock = false;
123 // update the referenced object if needed
125 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
126 myRef.setValue(theContext);
129 // do noth use naming if selected shape is result shape itself, but not sub-shape
130 TDF_Label aSelLab = selectionLabel();
131 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
132 aSelLab.ForgetAttribute(kINVALID_SELECTION);
133 aSelLab.ForgetAttribute(kCIRCLE_CENTER);
134 aSelLab.ForgetAttribute(kELLIPSE_CENTER1);
135 aSelLab.ForgetAttribute(kELLIPSE_CENTER2);
137 bool isDegeneratedEdge = false;
138 // do not use the degenerated edge as a shape, a null context and shape is used in the case
139 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
140 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
141 if (aSubShape.ShapeType() == TopAbs_EDGE)
142 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
144 if (!theContext.get() || isDegeneratedEdge) {
145 // to keep the reference attribute label
146 TDF_Label aRefLab = myRef.myRef->Label();
147 aSelLab.ForgetAllAttributes(true);
148 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
150 owner()->data()->blockSendAttributeUpdated(false);
153 if (theContext->groupName() == ModelAPI_ResultBody::group()) {
154 ResultBodyPtr aContextBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);
155 // do not select the whole shape for body:it is already must be in the data framework
156 // equal and null selected objects mean the same: object is equal to context,
157 if (aContextBody->shape().get() &&
158 (aContextBody->shape()->isEqual(theSubShape) || !theSubShape.get())) {
159 aSelLab.ForgetAllAttributes(true);
160 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
162 selectBody(aContextBody, theSubShape);
164 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
165 ResultConstructionPtr aContextConstruction =
166 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
167 aSelLab.ForgetAllAttributes(true); // to remove old selection data
168 std::shared_ptr<Model_ResultConstruction> aConstruction =
169 std::dynamic_pointer_cast<Model_ResultConstruction>(theContext);
170 std::shared_ptr<GeomAPI_Shape> aSubShape;
171 if (theSubShape.get() && !aContextConstruction->shape()->isEqual(theSubShape))
172 aSubShape = theSubShape; // the whole context
173 if (aConstruction->isInfinite()) {
174 // For correct naming selection, put the shape into the naming structure.
175 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
176 TNaming_Builder aBuilder(aSelLab);
177 aBuilder.Generated(aContextConstruction->shape()->impl<TopoDS_Shape>());
179 int anIndex = aConstruction->select(theSubShape, owner()->document());
180 TDataStd_Integer::Set(aSelLab, anIndex);
181 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
182 aSelLab.ForgetAllAttributes(true);
183 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
184 selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
185 } else { // check the feature context: parent-Part of this feature should not be used
186 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
187 if (aFeatureContext.get()) {
188 if (owner()->document() != aFeatureContext->document()) {
189 aSelLab.ForgetAllAttributes(true);
190 myRef.setValue(ObjectPtr());
192 owner()->data()->blockSendAttributeUpdated(false);
198 owner()->data()->sendAttributeUpdated(this);
201 owner()->data()->blockSendAttributeUpdated(false);
206 void Model_AttributeSelection::setValueCenter(
207 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
208 const CenterType theCenterType, const bool theTemporarily)
210 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
211 if (theTemporarily) {
212 myTmpCenterType = theCenterType;
213 } else { // store in the data structure
214 TDF_Label aSelLab = selectionLabel();
215 switch(theCenterType) {
218 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
219 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
221 case ELLIPSE_FIRST_FOCUS:
223 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
224 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
226 case ELLIPSE_SECOND_FOCUS:
228 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
229 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
233 owner()->data()->sendAttributeUpdated(this);
237 void Model_AttributeSelection::selectValue(
238 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
241 std::shared_ptr<GeomAPI_Shape> aValue =
242 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
243 if (!aValue.get() || aType == NOT_CENTER) {
244 setValue(theSource->context(), aValue);
246 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
247 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
248 setValueCenter(theSource->context(), anEdge, aType);
252 void Model_AttributeSelection::removeTemporaryValues()
254 if (myTmpContext.get() || myTmpSubShape.get()) {
255 myTmpContext.reset();
256 myTmpSubShape.reset();
260 // returns the center of the edge: circular or elliptical
261 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
263 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
264 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
265 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
266 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
267 double aFirst, aLast;
268 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
269 if (!aCurve.IsNull()) {
270 TopoDS_Vertex aVertex;
271 BRep_Builder aBuilder;
272 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
273 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
274 if (!aCirc.IsNull()) {
275 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
278 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
279 if (!anEll.IsNull()) {
280 aBuilder.MakeVertex(aVertex,
281 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
282 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
285 if (!aVertex.IsNull()) {
286 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
287 aResult->setImpl(new TopoDS_Vertex(aVertex));
293 return theEdge; // no vertex, so, return the initial edge
296 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
298 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
299 return std::shared_ptr<GeomAPI_Shape>();
300 CenterType aType = NOT_CENTER;
301 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
302 return centerByEdge(aResult, aType);
305 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
307 theType = NOT_CENTER;
308 GeomShapePtr aResult;
309 if (myTmpContext.get() || myTmpSubShape.get()) {
310 theType = myTmpCenterType;
311 ResultConstructionPtr aResulConstruction =
312 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
313 if(aResulConstruction.get()) {
314 // it is just reference to construction.
315 return myTmpSubShape;
317 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
320 TDF_Label aSelLab = selectionLabel();
321 if (aSelLab.IsAttribute(kINVALID_SELECTION))
324 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
325 theType = CIRCLE_CENTER;
326 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
327 theType = ELLIPSE_FIRST_FOCUS;
328 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
329 theType = ELLIPSE_SECOND_FOCUS;
332 if (myRef.isInitialized()) {
333 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
334 ResultPtr aContext = context();
336 return aResult; // empty result
337 return aContext->shape();
339 if (aSelLab.IsAttribute(kPART_REF_ID)) {
340 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
341 if (!aPart.get() || !aPart->isActivated())
342 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
343 Handle(TDataStd_Integer) anIndex;
344 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
345 if (anIndex->Get()) { // special selection attribute was created, use it
346 return aPart->selectionValue(anIndex->Get());
347 } else { // face with name is already in the data model, so try to take it by name
348 Handle(TDataStd_Name) aName;
349 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
350 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
351 std::size_t aPartEnd = aSubShapeName.find('/');
352 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
353 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
355 std::string aType; // to reuse already existing selection the type is not needed
356 return aPart->shapeInPart(aNameInPart, aType, anIndex);
363 std::shared_ptr<Model_ResultConstruction> aConstr =
364 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
366 if (aConstr->isInfinite())
367 return aResult; // empty result
369 if (!aConstr.get()) { // for construction context, return empty result as usual even
370 // the whole feature is selected
371 FeaturePtr aFeature = contextFeature();
372 if (aFeature.get()) {
373 std::list<GeomShapePtr> allShapes;
374 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
375 for (; aRes != aFeature->results().cend(); aRes++) {
376 if (aRes->get() && !(*aRes)->isDisabled()) {
377 GeomShapePtr aShape = (*aRes)->shape();
378 if (aShape.get() && !aShape->isNull()) {
379 allShapes.push_back(aShape);
383 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
387 Handle(TNaming_NamedShape) aSelection;
388 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
389 TopoDS_Shape aSelShape = aSelection->Get();
390 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
391 aResult->setImpl(new TopoDS_Shape(aSelShape));
392 } else if (aConstr) { // simple construction element: just shape of this construction element
393 Handle(TDataStd_Integer) anIndex;
394 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
395 if (anIndex->Get() == 0) // it is just reference to construction, nothing is in value
397 return aConstr->shape(anIndex->Get(), owner()->document());
404 bool Model_AttributeSelection::isInvalid()
406 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
409 bool Model_AttributeSelection::isInitialized()
411 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
412 std::shared_ptr<GeomAPI_Shape> aResult;
413 if (myRef.isInitialized()) {
414 TDF_Label aSelLab = selectionLabel();
415 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
416 ResultPtr aContext = context();
417 return aContext.get() != NULL;
419 Handle(TNaming_NamedShape) aSelection;
420 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
421 return !aSelection->Get().IsNull();
422 } else { // for simple construction element: just shape of this construction element
423 std::shared_ptr<Model_ResultConstruction> aConstr =
424 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
426 Handle(TDataStd_Integer) anIndex;
427 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
428 // for the whole shape it may return null, so, if index exists, returns true
432 // for the whole feature, a feature object
433 FeaturePtr aFeat = contextFeature();
442 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
445 myIsInitialized = myRef.isInitialized();
449 void Model_AttributeSelection::setID(const std::string theID)
452 ModelAPI_AttributeSelection::setID(theID);
455 ResultPtr Model_AttributeSelection::context()
457 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
460 if (myTmpContext.get() || myTmpSubShape.get()) {
464 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
465 // for parts there could be same-data result, so take the last enabled
467 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
468 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
469 for(int a = aSize - 1; a >= 0; a--) {
470 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
471 if(aPart.get() && aPart->data() == aResult->data()) {
472 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
473 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
474 // check that this result is not this-feature result (it is forbidden t oselect itself)
475 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
481 } else { // if feature - construction is selected, it has only one result, return this result
482 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
483 if (aFeature.get() && aFeature->results().size() == 1 &&
484 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
485 return aFeature->firstResult();
490 FeaturePtr Model_AttributeSelection::contextFeature() {
491 if (myTmpContext.get() || myTmpSubShape.get()) {
492 return FeaturePtr(); // feature can not be selected temporarily
494 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
496 ObjectPtr Model_AttributeSelection::contextObject() {
497 FeaturePtr aRes = contextFeature();
504 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
506 ModelAPI_AttributeSelection::setObject(theObject);
507 myRef.setObject(theObject);
510 TDF_LabelMap& Model_AttributeSelection::scope()
512 if (myScope.IsEmpty()) { // create a new scope if not yet done
513 // gets all features with named shapes that are before this feature label (before in history)
514 DocumentPtr aMyDoc = owner()->document();
515 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
516 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
517 bool aMePassed = false;
518 CompositeFeaturePtr aComposite =
519 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(owner());
520 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
521 CompositeFeaturePtr aCompositeOwner, aCompositeOwnerOwner;
522 if (aFeature.get()) {
523 aCompositeOwner = ModelAPI_Tools::compositeOwner(aFeature);
524 if (aCompositeOwner.get()) {
525 aCompositeOwnerOwner = ModelAPI_Tools::compositeOwner(aCompositeOwner);
528 // for group Scope is not limitet: this is always up to date objects
529 // this causes problem in galeries.py
530 //bool isGroup = aFeature.get() && aFeature->getKind() == "Group";
531 for(; aFIter != allFeatures.end(); aFIter++) {
532 if (*aFIter == owner()) { // the left features are created later (except subs of composite)
536 //if (isGroup) aMePassed = false;
537 bool isInScope = !aMePassed;
538 if (!isInScope && aComposite.get()) {
539 // try to add sub-elements of composite if this is composite
540 if (aComposite->isSub(*aFIter))
543 // remove the composite-owner of this feature (sketch in extrusion-cut)
544 if (isInScope && (aCompositeOwner == *aFIter || aCompositeOwnerOwner == *aFIter))
547 if (isInScope && aFIter->get() && (*aFIter)->data()->isValid()) {
548 TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
549 (*aFIter)->data())->label().Father();
550 TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), true);
551 for(; aNSIter.More(); aNSIter.Next()) {
552 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
553 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
554 myScope.Add(aNS->Label());
559 // also add all naming labels created for external constructions
560 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(aMyDoc);
561 TDF_Label anExtDocLab = aDoc->extConstructionsLabel();
562 TDF_ChildIDIterator aNSIter(anExtDocLab, TNaming_NamedShape::GetID(), true);
563 for(; aNSIter.More(); aNSIter.Next()) {
564 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
565 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
566 myScope.Add(aNS->Label());
573 /// Sets the invalid flag if flag is false, or removes it if "true"
575 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
577 theLab.ForgetAttribute(kINVALID_SELECTION);
579 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
584 void Model_AttributeSelection::split(
585 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
587 TopTools_ListOfShape aSubs;
588 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
589 if (!anExplorer.Value().IsNull() &&
590 anExplorer.Value().ShapeType() == theType) {
591 aSubs.Append(anExplorer.Value());
592 } else { // invalid case; bad result shape, so, impossible to split easily
597 if (aSubs.Extent() > 1) { // ok to split
598 TopTools_ListIteratorOfListOfShape aSub(aSubs);
599 GeomShapePtr aSubSh(new GeomAPI_Shape);
600 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
601 setValue(theContext, aSubSh);
602 for(aSub.Next(); aSub.More(); aSub.Next()) {
603 GeomShapePtr aSubSh(new GeomAPI_Shape);
604 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
605 myParent->append(theContext, aSubSh);
610 bool Model_AttributeSelection::update()
612 FeaturePtr aContextFeature = contextFeature();
613 if (aContextFeature.get()) {
616 TDF_Label aSelLab = selectionLabel();
617 ResultPtr aContext = context();
619 return setInvalidIfFalse(aSelLab, false);
620 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
621 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
624 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
625 std::shared_ptr<GeomAPI_Shape> aNoSelection;
626 bool aResult = selectPart(aContext, aNoSelection, true);
627 aResult = setInvalidIfFalse(aSelLab, aResult);
629 owner()->data()->sendAttributeUpdated(this);
634 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
635 // body: just a named shape, use selection mechanism from OCCT
636 TNaming_Selector aSelector(aSelLab);
637 TopoDS_Shape anOldShape;
638 if (!aSelector.NamedShape().IsNull()) {
639 anOldShape = aSelector.NamedShape()->Get();
641 bool aResult = aSelector.Solve(scope()) == Standard_True;
642 // must be before sending of updated attribute (1556)
643 aResult = setInvalidIfFalse(aSelLab, aResult);
644 TopoDS_Shape aNewShape;
645 if (!aSelector.NamedShape().IsNull()) {
646 aNewShape = aSelector.NamedShape()->Get();
648 if (anOldShape.IsNull() || aNewShape.IsNull() ||
649 !anOldShape.IsEqual(aSelector.NamedShape()->Get())) {
650 // shape type shoud not not changed: if shape becomes compound of such shapes, then split
651 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
652 anOldShape.ShapeType() != aNewShape.ShapeType() &&
653 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
655 split(aContext, aNewShape, anOldShape.ShapeType());
657 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
662 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
663 Handle(TDataStd_Integer) anIndex;
664 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
665 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
666 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
667 bool aModified = true;
668 bool aValid = aConstructionContext->update(anIndex->Get(), owner()->document(), aModified);
669 setInvalidIfFalse(aSelLab, aValid);
670 if (aConstructionContext->isInfinite()) {
671 // Update the selected shape.
672 TNaming_Builder aBuilder(aSelLab);
673 aBuilder.Generated(aConstructionContext->shape()->impl<TopoDS_Shape>());
676 owner()->data()->sendAttributeUpdated(this);
680 return setInvalidIfFalse(aSelLab, false); // unknown case
683 void Model_AttributeSelection::selectBody(
684 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
686 // perform the selection
687 TNaming_Selector aSel(selectionLabel());
688 TopoDS_Shape aContext;
690 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);//myRef.value()
692 aContext = aBody->shape()->impl<TopoDS_Shape>();
695 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
697 aContext = aResult->shape()->impl<TopoDS_Shape>();
699 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
704 // with "recover" feature the selected context may be not up to date (issue 1710)
705 Handle(TNaming_NamedShape) aResult;
706 TDF_Label aSelLab = selectionLabel();
707 TopoDS_Shape aNewContext = aContext;
708 bool isUpdated = true;
709 while(!aNewContext.IsNull() && isUpdated) {
710 // searching for the very last shape that was produced from this one
712 if (!TNaming_Tool::HasLabel(aSelLab, aNewContext))
713 // to avoid crash of TNaming_SameShapeIterator if pure shape does not exists
715 for(TNaming_SameShapeIterator anIter(aNewContext, aSelLab); anIter.More(); anIter.Next()) {
716 TDF_Label aNSLab = anIter.Label();
717 if (!scope().Contains(aNSLab))
719 Handle(TNaming_NamedShape) aNS;
720 if (aNSLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
721 for(TNaming_Iterator aShapesIter(aNS); aShapesIter.More(); aShapesIter.Next()) {
722 if (aShapesIter.Evolution() == TNaming_SELECTED)
723 continue; // don't use the selection evolution
724 if (!aShapesIter.OldShape().IsNull() && aShapesIter.OldShape().IsSame(aNewContext)) {
725 // found the original shape
726 aNewContext = aShapesIter.NewShape(); // go to the newer shape
734 if (aNewContext.IsNull()) { // a context is already deleted
735 setInvalidIfFalse(aSelLab, false);
736 Events_InfoMessage("Model_AttributeSelection", "Failed to select shape already deleted").send();
740 TopoDS_Shape aNewSub = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
741 if (!aNewSub.IsEqual(aContext)) { // searching for subshape in the new context
742 bool isFound = false;
743 TopExp_Explorer anExp(aNewContext, aNewSub.ShapeType());
744 for(; anExp.More(); anExp.Next()) {
745 if (anExp.Current().IsSame(aNewSub)) {
750 if (!isFound) { // sub-shape is not found in the up-to-date instance of the context shape
751 // if context is sub-result of compound/compsolid, selection of sub-shape better propagate to
752 // the main result (which is may be modified); the case is in 1799
753 ResultBodyPtr aMain = ModelAPI_Tools::bodyOwner(theContext);
754 while(ModelAPI_Tools::bodyOwner(aMain).get())
755 aMain = ModelAPI_Tools::bodyOwner(theContext);
757 selectBody(aMain, theSubShape);
760 setInvalidIfFalse(aSelLab, false);
761 Events_InfoMessage("Model_AttributeSelection",
762 "Failed to select sub-shape already modified").send();
767 /// fix for issue 411: result modified shapes must not participate in this selection mechanism
768 if (!aContext.IsNull()) {
769 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
770 bool aEraseResults = false;
771 if (aFeatureOwner.get()) {
772 aEraseResults = !aFeatureOwner->results().empty();
773 if (aEraseResults) // erase results without flash deleted and redisplay: do it after Select
774 aFeatureOwner->removeResults(0, false, false);
776 aSel.Select(aNewSub, aNewContext);
778 if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
779 static Events_Loop* aLoop = Events_Loop::loop();
780 static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
781 aLoop->flush(kDeletedEvent);
786 bool Model_AttributeSelection::selectPart(
787 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
788 const bool theUpdate)
790 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
791 if (!aPart.get() || !aPart->isActivated())
792 return true; // postponed naming
794 Handle(TDataStd_Integer) anIndex;
795 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
796 // by internal selection
797 if (anIndex->Get() > 0) {
798 // update the selection by index
799 return aPart->updateInPart(anIndex->Get());
801 return true; // nothing to do, referencing just by name
804 return true; // nothing to do, referencing just by name
806 // store the shape (in case part is not loaded it should be useful
808 std::string aName = theContext->data()->name();
809 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
810 aShape = theContext->shape()->impl<TopoDS_Shape>();
812 aShape = theSubShape->impl<TopoDS_Shape>();
814 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
815 TDataStd_Integer::Set(selectionLabel(), anIndex);
817 TNaming_Builder aBuilder(selectionLabel());
818 aBuilder.Select(aShape, aShape);
819 // identify by name in the part
820 TDataStd_Name::Set(selectionLabel(), aName.c_str());
821 return !aName.empty();
824 TDF_Label Model_AttributeSelection::selectionLabel()
826 return myRef.myRef->Label().FindChild(1);
829 /// prefixes of the shape names with centers defined
830 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
832 /// returns the map that contains all possible prefixes of the center-names
833 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
835 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
836 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
837 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
838 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
840 return kCENTERS_PREFIX;
843 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
845 std::string aName("");
846 if(!this->isInitialized())
847 return !theDefaultName.empty() ? theDefaultName : aName;
849 CenterType aCenterType = NOT_CENTER;
850 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
851 ResultPtr aCont = context();
854 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
855 // selection of a full feature
856 FeaturePtr aFeatureCont = contextFeature();
857 if (aFeatureCont.get()) {
858 return kWHOLE_FEATURE + aFeatureCont->name();
860 // in case of selection of removed result
864 Model_SelectionNaming aSelNaming(selectionLabel());
865 std::string aResult = aSelNaming.namingName(
866 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
867 if (aCenterType != NOT_CENTER) {
868 aResult += centersMap()[aCenterType];
873 // returns the center type and modifies the shape name if this name is center-name
874 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
876 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
877 centersMap().begin();
878 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
879 std::size_t aFound = theShapeName.find(aPrefixIter->second);
880 if (aFound != std::string::npos &&
881 aFound == theShapeName.size() - aPrefixIter->second.size()) {
882 theShapeName = theShapeName.substr(0, aFound);
883 return aPrefixIter->first;
886 return ModelAPI_AttributeSelection::NOT_CENTER;
889 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
890 void Model_AttributeSelection::selectSubShape(
891 const std::string& theType, const std::string& theSubShapeName)
893 if(theSubShapeName.empty() || theType.empty()) return;
895 std::string aSubShapeName = theSubShapeName;
896 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
897 centerTypeByName(aSubShapeName) : NOT_CENTER;
898 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
900 // first iteration is selection by name without center prefix, second - in case of problem,
901 // try with initial name
902 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
903 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
904 aSubShapeName = theSubShapeName;
905 aCenterType = NOT_CENTER;
907 } else if (aUseCenter != 1) continue;
909 // check this is Part-name: 2 delimiters in the name
910 std::size_t aPartEnd = aSubShapeName.find('/');
911 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
912 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
913 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
914 if (aFound.get()) { // found such part, so asking it for the name
915 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
916 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
918 std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, aType, anIndex);
919 if (aSelected.get()) {
920 if (aCenterType != NOT_CENTER) {
921 if (!aSelected->isEdge())
923 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
924 setValueCenter(aPart, aSelectedEdge, aCenterType);
926 setValue(aPart, aSelected);
927 TDataStd_Integer::Set(selectionLabel(), anIndex);
933 std::shared_ptr<Model_Document> aDoc =
934 std::dynamic_pointer_cast<Model_Document>(owner()->document());
935 // check this is a whole feature context
936 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
937 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
938 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
939 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
941 static const GeomShapePtr anEmptyShape;
942 setValue(anObj, anEmptyShape);
947 Model_SelectionNaming aSelNaming(selectionLabel());
948 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
950 if (aSelNaming.selectSubShape(aType, aSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
951 // try to find the last context to find the up to date shape
952 if (aCont->shape().get() && !aCont->shape()->isNull() &&
953 aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
954 const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
955 if (!aConShape.IsNull()) {
956 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
958 aNS = TNaming_Tool::CurrentNamedShape(aNS);
959 if (!aNS.IsNull() && scope().Contains(aNS->Label())) { // scope check is for 2228
960 TDF_Label aLab = aNS->Label();
961 if (aLab.Depth() % 2 == 0)
962 aLab = aLab.Father();
963 ObjectPtr anObj = aDoc->objects()->object(aLab);
964 while(!anObj.get() && aLab.Depth() > 5) {
965 aLab = aLab.Father().Father();
966 anObj = aDoc->objects()->object(aLab);
970 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
978 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
979 if (aCont.get() && aShapeToBeSelected.get()) {
980 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCont);
981 if (aComp && aComp->numberOfSubs()) {
982 std::list<ResultPtr> allSubs;
983 ModelAPI_Tools::allSubs(aComp, allSubs);
984 std::list<ResultPtr>::iterator aS = allSubs.begin();
985 for(; aS != allSubs.end(); aS++) {
986 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
987 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
988 aSub->shape()->isSubShape(aShapeToBeSelected)) {
995 // try to find the latest active result that must be used instead of the selected
996 // to set the active context (like in GUI selection), not concealed one
997 bool aFindNewContext = true;
998 while(aFindNewContext && aCont.get()) {
999 aFindNewContext = false;
1000 // take references to all results: root one, any sub
1001 ResultBodyPtr aCompContext = ModelAPI_Tools::bodyOwner(aCont, true);
1002 std::list<ResultPtr> allRes;
1003 if (aCompContext.get()) {
1004 ModelAPI_Tools::allSubs(aCompContext, allRes);
1005 allRes.push_back(aCompContext);
1007 allRes.push_back(aCont);
1009 for(std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1010 ResultPtr aResCont = *aSub;
1011 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1012 // only lower and higher level subs are counted
1013 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1015 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1016 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1017 for(; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1018 if (!aRef->get() || !(*aRef)->owner().get())
1020 // concealed attribute only
1021 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1022 if (!ModelAPI_Session::get()->validators()->isConcealed(
1023 aRefFeat->getKind(), (*aRef)->id()))
1025 // search the feature result that contains sub-shape selected
1026 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1028 // take all sub-results or one result
1029 std::list<ResultPtr> aRefFeatResults;
1030 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1031 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1032 for(; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1033 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1034 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1035 aResults.push_back(aBody);
1037 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1038 for(; aResIter != aResults.end(); aResIter++) {
1039 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1041 GeomShapePtr aShape = (*aResIter)->shape();
1042 GeomShapePtr aSelectedShape =
1043 aShapeToBeSelected.get() ? aShapeToBeSelected : aCont->shape();
1044 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
1045 aCont = *aResIter; // found new context (produced from this) with same subshape
1046 aFindNewContext = true; // continue searching futher
1054 if (aCenterType != NOT_CENTER) {
1055 if (!aShapeToBeSelected->isEdge())
1057 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
1058 setValueCenter(aCont, aSelectedEdge, aCenterType);
1060 setValue(aCont, aShapeToBeSelected);
1065 TDF_Label aSelLab = selectionLabel();
1066 setInvalidIfFalse(aSelLab, false);
1070 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1071 const GeomPointPtr& thePoint)
1073 if (theType.empty() || !thePoint)
1076 // list of parent features
1077 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
1078 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
1080 int aSelectionIndex = 0;
1081 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
1082 if (aType == GeomAPI_Shape::SHAPE) {
1083 // possibly, the string consists of the type and the index,
1084 // thus, try to separate them
1085 size_t aUndersporePos = theType.find_first_of('_');
1086 if (aUndersporePos != std::string::npos)
1087 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
1089 if (aType != GeomAPI_Shape::SHAPE) {
1090 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
1091 aChar != theType.end(); ++aChar) {
1092 if (std::isdigit(*aChar))
1093 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
1095 aSelectionIndex = 1;
1099 aSelectionIndex -= 1;
1103 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
1105 // collect features from PartSet and the current part
1106 SessionPtr aSession = ModelAPI_Session::get();
1107 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
1108 if (aSession->moduleDocument() != owner()->document()) {
1109 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
1110 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
1112 // Process results of all features from the last to the first
1113 // to find appropriate sub-shape
1114 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
1115 anIt != aFeatures.rend(); ++anIt) {
1116 // selection cannot be linked to the parent features
1117 if (aParents.find(*anIt) != aParents.end())
1119 // check the feature is a part of composite feature (like sketch elements),
1120 // then do not process it, it will be processed in scope of composite feature
1121 bool isSubOfComposite = false;
1122 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
1123 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
1124 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
1125 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
1126 CompositeFeaturePtr aCompFeature =
1127 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
1128 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
1130 if (isSubOfComposite)
1133 // process results of the current feature to find appropriate sub-shape
1134 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
1135 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
1136 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1137 ++anApIt; // skip this shape, because one of the previous is selected
1139 if (anApIt != anAppropriate.end()) {
1140 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1141 setValue(anApIt->myResult, anApIt->mySubshape);
1143 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1144 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1150 TDF_Label aSelLab = selectionLabel();
1151 setInvalidIfFalse(aSelLab, false);
1155 int Model_AttributeSelection::Id()
1158 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1159 ResultPtr aContextRes = context();
1160 // support for compsolids:
1161 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1162 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1164 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1167 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1168 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1169 // searching for the latest main shape
1170 if (aSelection && !aSelection->isNull() && aContext && !aContext->isNull())
1172 std::shared_ptr<Model_Document> aDoc =
1173 std::dynamic_pointer_cast<Model_Document>(context()->document());
1175 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1176 if (!aNS.IsNull()) {
1177 aMainShape = TNaming_Tool::CurrentShape(aNS);
1181 TopTools_IndexedMapOfShape aSubShapesMap;
1182 TopExp::MapShapes(aMainShape, aSubShapesMap);
1183 anID = aSubShapesMap.FindIndex(aSubShape);
1188 void Model_AttributeSelection::setId(int theID)
1190 std::shared_ptr<GeomAPI_Shape> aSelection;
1192 ResultPtr aContextRes = context();
1193 // support for compsolids:
1194 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1195 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1197 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1199 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1200 // searching for the latest main shape
1201 if (theID > 0 && aContext && !aContext->isNull())
1203 std::shared_ptr<Model_Document> aDoc =
1204 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1206 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1207 if (!aNS.IsNull()) {
1208 aMainShape = TNaming_Tool::CurrentShape(aNS);
1212 TopTools_IndexedMapOfShape aSubShapesMap;
1213 TopExp::MapShapes(aMainShape, aSubShapesMap);
1214 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1216 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1217 aResult->setImpl(new TopoDS_Shape(aSelShape));
1219 aSelection = aResult;
1222 setValue(aContextRes, aSelection);
1225 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1227 std::string aResult;
1228 if (owner()->document() != theContext->document()) {
1229 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1230 aResult = theContext->document()->kind() + "/";
1232 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1233 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1234 if (aDocRes.get()) {
1235 aResult = aDocRes->data()->name() + "/";
1239 aResult += theContext->data()->name();
1243 void Model_AttributeSelection::computeValues(
1244 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1245 TopTools_ListOfShape& theShapes)
1247 bool aWasWholeContext = theValShape.IsNull();
1248 if (aWasWholeContext) {
1249 //theShapes.Append(theValShape);
1251 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1253 //TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1254 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1255 //if (anOldContShape.IsSame(theValShape)) { // full context shape substituted by new full context
1256 //theShapes.Append(aNewContShape);
1259 // if a new value is unchanged in the new context, do nothing: value is correct
1260 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1261 for(; aSubExp.More(); aSubExp.Next()) {
1262 if (aSubExp.Current().IsSame(theValShape)) {
1263 theShapes.Append(theValShape);
1267 // if new context becomes compsolid, the resulting sub may be in sub-solids
1268 std::list<ResultPtr> aNewToIterate;
1269 aNewToIterate.push_back(theNewContext);
1270 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1272 std::list<ResultPtr> allNewContextSubs;
1273 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1274 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1275 for(; aSub != allNewContextSubs.end(); aSub++) {
1276 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1277 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1278 aNewToIterate.push_back(aBody);
1282 // first iteration: searching for the whole shape appearance (like face of the box)
1283 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1284 int aToFindPart = 0;
1285 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1286 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1287 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1288 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1289 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1290 std::shared_ptr<Model_Data> aNewData =
1291 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1292 TDF_Label aNewLab = aNewData->label();
1293 // searching for produced sub-shape fully on some label
1294 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID(), Standard_True);
1295 for(; aNSIter.More(); aNSIter.Next()) {
1296 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1297 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1298 if (aToFindPart == 0) { // search shape is fully inside
1299 if (aPairIter.OldShape().IsSame(theValShape)) {
1300 if (aPairIter.NewShape().IsNull()) {// value was removed
1304 theShapes.Append(aPairIter.NewShape());
1306 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1307 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1308 for(; anExp.More(); anExp.Next()) {
1309 if (anExp.Current().IsSame(theValShape)) { // found a new container
1310 if (aPairIter.NewShape().IsNull()) {// value was removed
1314 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1315 anOlds.Add(aPairIter.OldShape());
1324 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1325 // map of sub-shapes -> number of occurences of these shapes in containers
1326 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1327 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1328 for(; aContIter.More(); aContIter.Next()) {
1329 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1330 for(; aSubExp.More(); aSubExp.Next()) {
1331 if (!aSubs.IsBound(aSubExp.Current())) {
1332 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1334 // store old to know how many olds produced this shape
1335 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1338 // if sub is appeared same times in containers as the number of old shapes that contain it
1339 int aCountInOld = anOlds.Size();
1340 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1342 for(; aSubsIter.More(); aSubsIter.Next()) {
1343 if (aSubsIter.Value().Size() == aCountInOld) {
1344 theShapes.Append(aSubsIter.Key());
1348 if (theShapes.IsEmpty()) { // nothing was changed
1349 theShapes.Append(aWasWholeContext ? TopoDS_Shape() : theValShape);
1353 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1354 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1355 TDF_Label theAccessLabel,
1356 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1358 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1359 TopTools_ListOfShape aResContShapes;
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->objects()->isLater(aModifierFeat, aThisFeature))
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->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
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 //TNaming_Iterator aPairIter(aNewNS);
1397 //aResContShapes.Append(aPairIter.NewShape());
1398 aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
1399 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1400 aResults.insert(ResultPtr());
1401 } else { // not-precessed modification => don't support it
1406 if (aResults.empty())
1407 return false; // no modifications found, must stay the same
1408 // iterate all results to find futher modifications
1409 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1410 for(; aResIter != aResults.end(); aResIter++) {
1411 if (aResIter->get() != NULL) {
1412 // compute new values by two contextes: the old and the new
1413 TopTools_ListOfShape aValShapes;
1414 computeValues(theContext, *aResIter, theValShape, aValShapes);
1416 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1417 for(; aNewVal.More(); aNewVal.Next()) {
1418 std::list<ResultPtr> aNewRes;
1419 TopTools_ListOfShape aNewUpdatedVal;
1420 TopoDS_Shape aNewValSh = aNewVal.Value();
1421 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1422 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1423 aNewValSh.Nullify();
1424 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1425 theAccessLabel, aNewRes, aNewUpdatedVal))
1427 // appeand new results instead of the current ones
1428 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1429 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1430 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1431 theResults.push_back(*aNewIter);
1432 theValShapes.Append(aNewUpdVal.Value());
1434 } else { // the current result is good
1435 theResults.push_back(*aResIter);
1436 theValShapes.Append(aNewValSh);
1441 return true; // theResults must be empty: everything is deleted
1444 void Model_AttributeSelection::updateInHistory()
1446 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1447 // only bodies and parts may be modified later in the history, don't do anything otherwise
1448 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1449 aContext->groupName() != ModelAPI_ResultPart::group()))
1451 std::shared_ptr<Model_Document> aDoc =
1452 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1453 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1454 if (!aContData.get() || !aContData->isValid())
1456 TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
1457 Handle(TNaming_NamedShape) aContNS;
1458 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1459 bool aFoundNewContext = true;
1460 ResultPtr aNewContext = aContext;
1461 while(aFoundNewContext) {
1462 aFoundNewContext = false;
1463 // parts have no shape in result, so, trace references using the Part info
1464 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1465 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1466 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1467 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1468 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1469 for(; aRef != aRefs.end(); aRef++) {
1470 // to avoid detection of part changes by local selection only
1471 AttributeSelectionPtr aSel =
1472 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1473 if (aSel.get() && !aSel->value()->isSame(aSel->context()->shape()))
1476 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1477 if (aRefFeat.get() && aRefFeat != owner()) {
1478 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1479 if (aDoc->objects()->isLater(aThisFeature, aRefFeat)) { // found better feature
1480 aFoundNewContext = true;
1481 aNewContext = aRefFeat->firstResult();
1488 if (aNewContext != aContext) {
1489 setValue(aNewContext, value());
1493 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1494 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1495 // iterate the context shape modifications in order to find a feature that is upper in history
1496 // that this one and is really modifies the referenced result to refer to it
1497 ResultPtr aModifierResFound;
1498 TNaming_Iterator aPairIter(aContNS);
1499 if (!aPairIter.More())
1501 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1502 bool anIterate = true;
1503 // trying to update also the sub-shape selected
1504 GeomShapePtr aSubShape = value();
1505 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1507 TopoDS_Shape aValShape;
1508 if (aSubShape.get()) {
1509 aValShape = aSubShape->impl<TopoDS_Shape>();
1512 std::list<ResultPtr> aNewContexts;
1513 TopTools_ListOfShape aValShapes;
1514 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1516 // update scope to reset to a new one
1519 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1520 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1521 if (aNewCont == aNewContexts.end()) { // all results were deleted
1522 ResultPtr anEmptyContext;
1523 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1524 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1528 GeomShapePtr aValueShape;
1529 if (!aNewValues.Value().IsNull()) {
1530 aValueShape = std::make_shared<GeomAPI_Shape>();
1531 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1533 setValue(*aNewCont, aValueShape);
1534 // if there are more than one result, put them by "append" into "parent" list
1536 for(aNewCont++, aNewValues.Next(); aNewCont != aNewContexts.end();
1537 aNewCont++, aNewValues.Next()) {
1538 GeomShapePtr aValueShape;
1539 if (!aNewValues.Value().IsNull()) {
1540 aValueShape = std::make_shared<GeomAPI_Shape>();
1541 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1544 // Check that list has the same type of shape selection before adding.
1545 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1546 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1547 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1548 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1550 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1551 if (aValueShape.get()) {
1552 aShapeShapeType = aValueShape->shapeType();
1554 (*aNewCont)->shape()->shapeType();
1557 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1561 myParent->append(*aNewCont, aValueShape);
1567 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1569 myParent = theParent;