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);
777 // face may become divided after the model update, so, new labels may be added to the scope
780 if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
781 static Events_Loop* aLoop = Events_Loop::loop();
782 static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
783 aLoop->flush(kDeletedEvent);
788 bool Model_AttributeSelection::selectPart(
789 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
790 const bool theUpdate)
792 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
793 if (!aPart.get() || !aPart->isActivated())
794 return true; // postponed naming
796 Handle(TDataStd_Integer) anIndex;
797 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
798 // by internal selection
799 if (anIndex->Get() > 0) {
800 // update the selection by index
801 return aPart->updateInPart(anIndex->Get());
803 return true; // nothing to do, referencing just by name
806 return true; // nothing to do, referencing just by name
808 // store the shape (in case part is not loaded it should be useful
810 std::string aName = theContext->data()->name();
811 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
812 aShape = theContext->shape()->impl<TopoDS_Shape>();
814 aShape = theSubShape->impl<TopoDS_Shape>();
816 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
817 TDataStd_Integer::Set(selectionLabel(), anIndex);
819 TNaming_Builder aBuilder(selectionLabel());
820 aBuilder.Select(aShape, aShape);
821 // identify by name in the part
822 TDataStd_Name::Set(selectionLabel(), aName.c_str());
823 return !aName.empty();
826 TDF_Label Model_AttributeSelection::selectionLabel()
828 return myRef.myRef->Label().FindChild(1);
831 /// prefixes of the shape names with centers defined
832 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
834 /// returns the map that contains all possible prefixes of the center-names
835 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
837 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
838 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
839 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
840 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
842 return kCENTERS_PREFIX;
845 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
847 std::string aName("");
848 if(!this->isInitialized())
849 return !theDefaultName.empty() ? theDefaultName : aName;
851 CenterType aCenterType = NOT_CENTER;
852 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
853 ResultPtr aCont = context();
856 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
857 // selection of a full feature
858 FeaturePtr aFeatureCont = contextFeature();
859 if (aFeatureCont.get()) {
860 return kWHOLE_FEATURE + aFeatureCont->name();
862 // in case of selection of removed result
866 Model_SelectionNaming aSelNaming(selectionLabel());
867 std::string aResult = aSelNaming.namingName(
868 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
869 if (aCenterType != NOT_CENTER) {
870 aResult += centersMap()[aCenterType];
875 // returns the center type and modifies the shape name if this name is center-name
876 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
878 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
879 centersMap().begin();
880 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
881 std::size_t aFound = theShapeName.find(aPrefixIter->second);
882 if (aFound != std::string::npos &&
883 aFound == theShapeName.size() - aPrefixIter->second.size()) {
884 theShapeName = theShapeName.substr(0, aFound);
885 return aPrefixIter->first;
888 return ModelAPI_AttributeSelection::NOT_CENTER;
891 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
892 void Model_AttributeSelection::selectSubShape(
893 const std::string& theType, const std::string& theSubShapeName)
895 if(theSubShapeName.empty() || theType.empty()) return;
897 std::string aSubShapeName = theSubShapeName;
898 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
899 centerTypeByName(aSubShapeName) : NOT_CENTER;
900 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
902 // first iteration is selection by name without center prefix, second - in case of problem,
903 // try with initial name
904 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
905 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
906 aSubShapeName = theSubShapeName;
907 aCenterType = NOT_CENTER;
909 } else if (aUseCenter != 1) continue;
911 // check this is Part-name: 2 delimiters in the name
912 std::size_t aPartEnd = aSubShapeName.find('/');
913 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
914 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
915 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
916 if (aFound.get()) { // found such part, so asking it for the name
917 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
918 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
920 std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, aType, anIndex);
921 if (aSelected.get()) {
922 if (aCenterType != NOT_CENTER) {
923 if (!aSelected->isEdge())
925 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
926 setValueCenter(aPart, aSelectedEdge, aCenterType);
928 setValue(aPart, aSelected);
929 TDataStd_Integer::Set(selectionLabel(), anIndex);
935 std::shared_ptr<Model_Document> aDoc =
936 std::dynamic_pointer_cast<Model_Document>(owner()->document());
937 // check this is a whole feature context
938 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
939 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
940 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
941 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
943 static const GeomShapePtr anEmptyShape;
944 setValue(anObj, anEmptyShape);
949 Model_SelectionNaming aSelNaming(selectionLabel());
950 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
952 if (aSelNaming.selectSubShape(aType, aSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
953 // try to find the last context to find the up to date shape
954 if (aCont->shape().get() && !aCont->shape()->isNull() &&
955 aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
956 const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
957 if (!aConShape.IsNull()) {
958 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
960 aNS = TNaming_Tool::CurrentNamedShape(aNS);
961 if (!aNS.IsNull() && scope().Contains(aNS->Label())) { // scope check is for 2228
962 TDF_Label aLab = aNS->Label();
963 if (aLab.Depth() % 2 == 0)
964 aLab = aLab.Father();
965 ObjectPtr anObj = aDoc->objects()->object(aLab);
966 while(!anObj.get() && aLab.Depth() > 5) {
967 aLab = aLab.Father().Father();
968 anObj = aDoc->objects()->object(aLab);
972 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
980 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
981 if (aCont.get() && aShapeToBeSelected.get()) {
982 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCont);
983 if (aComp && aComp->numberOfSubs()) {
984 std::list<ResultPtr> allSubs;
985 ModelAPI_Tools::allSubs(aComp, allSubs);
986 std::list<ResultPtr>::iterator aS = allSubs.begin();
987 for(; aS != allSubs.end(); aS++) {
988 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
989 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
990 aSub->shape()->isSubShape(aShapeToBeSelected)) {
997 // try to find the latest active result that must be used instead of the selected
998 // to set the active context (like in GUI selection), not concealed one
999 bool aFindNewContext = true;
1000 while(aFindNewContext && aCont.get()) {
1001 aFindNewContext = false;
1002 // take references to all results: root one, any sub
1003 ResultBodyPtr aCompContext = ModelAPI_Tools::bodyOwner(aCont, true);
1004 std::list<ResultPtr> allRes;
1005 if (aCompContext.get()) {
1006 ModelAPI_Tools::allSubs(aCompContext, allRes);
1007 allRes.push_back(aCompContext);
1009 allRes.push_back(aCont);
1011 for(std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1012 ResultPtr aResCont = *aSub;
1013 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1014 // only lower and higher level subs are counted
1015 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1017 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1018 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1019 for(; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1020 if (!aRef->get() || !(*aRef)->owner().get())
1022 // concealed attribute only
1023 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1024 if (!ModelAPI_Session::get()->validators()->isConcealed(
1025 aRefFeat->getKind(), (*aRef)->id()))
1027 // search the feature result that contains sub-shape selected
1028 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1030 // take all sub-results or one result
1031 std::list<ResultPtr> aRefFeatResults;
1032 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1033 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1034 for(; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1035 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1036 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1037 aResults.push_back(aBody);
1039 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1040 for(; aResIter != aResults.end(); aResIter++) {
1041 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1043 GeomShapePtr aShape = (*aResIter)->shape();
1044 GeomShapePtr aSelectedShape =
1045 aShapeToBeSelected.get() ? aShapeToBeSelected : aCont->shape();
1046 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
1047 aCont = *aResIter; // found new context (produced from this) with same subshape
1048 aFindNewContext = true; // continue searching futher
1056 if (aCenterType != NOT_CENTER) {
1057 if (!aShapeToBeSelected->isEdge())
1059 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
1060 setValueCenter(aCont, aSelectedEdge, aCenterType);
1062 setValue(aCont, aShapeToBeSelected);
1067 TDF_Label aSelLab = selectionLabel();
1068 setInvalidIfFalse(aSelLab, false);
1072 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1073 const GeomPointPtr& thePoint)
1075 if (theType.empty() || !thePoint)
1078 // list of parent features
1079 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
1080 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
1082 int aSelectionIndex = 0;
1083 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
1084 if (aType == GeomAPI_Shape::SHAPE) {
1085 // possibly, the string consists of the type and the index,
1086 // thus, try to separate them
1087 size_t aUndersporePos = theType.find_first_of('_');
1088 if (aUndersporePos != std::string::npos)
1089 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
1091 if (aType != GeomAPI_Shape::SHAPE) {
1092 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
1093 aChar != theType.end(); ++aChar) {
1094 if (std::isdigit(*aChar))
1095 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
1097 aSelectionIndex = 1;
1101 aSelectionIndex -= 1;
1105 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
1107 // collect features from PartSet and the current part
1108 SessionPtr aSession = ModelAPI_Session::get();
1109 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
1110 if (aSession->moduleDocument() != owner()->document()) {
1111 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
1112 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
1114 // Process results of all features from the last to the first
1115 // to find appropriate sub-shape
1116 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
1117 anIt != aFeatures.rend(); ++anIt) {
1118 // selection cannot be linked to the parent features
1119 if (aParents.find(*anIt) != aParents.end())
1121 // check the feature is a part of composite feature (like sketch elements),
1122 // then do not process it, it will be processed in scope of composite feature
1123 bool isSubOfComposite = false;
1124 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
1125 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
1126 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
1127 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
1128 CompositeFeaturePtr aCompFeature =
1129 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
1130 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
1132 if (isSubOfComposite)
1135 // process results of the current feature to find appropriate sub-shape
1136 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
1137 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
1138 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1139 ++anApIt; // skip this shape, because one of the previous is selected
1141 if (anApIt != anAppropriate.end()) {
1142 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1143 setValue(anApIt->myResult, anApIt->mySubshape);
1145 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1146 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1152 TDF_Label aSelLab = selectionLabel();
1153 setInvalidIfFalse(aSelLab, false);
1157 int Model_AttributeSelection::Id()
1160 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1161 ResultPtr aContextRes = context();
1162 // support for compsolids:
1163 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1164 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1166 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1169 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1170 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1171 // searching for the latest main shape
1172 if (aSelection && !aSelection->isNull() && aContext && !aContext->isNull())
1174 std::shared_ptr<Model_Document> aDoc =
1175 std::dynamic_pointer_cast<Model_Document>(context()->document());
1177 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1178 if (!aNS.IsNull()) {
1179 aMainShape = TNaming_Tool::CurrentShape(aNS);
1183 TopTools_IndexedMapOfShape aSubShapesMap;
1184 TopExp::MapShapes(aMainShape, aSubShapesMap);
1185 anID = aSubShapesMap.FindIndex(aSubShape);
1190 void Model_AttributeSelection::setId(int theID)
1192 std::shared_ptr<GeomAPI_Shape> aSelection;
1194 ResultPtr aContextRes = context();
1195 // support for compsolids:
1196 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1197 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1199 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1201 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1202 // searching for the latest main shape
1203 if (theID > 0 && aContext && !aContext->isNull())
1205 std::shared_ptr<Model_Document> aDoc =
1206 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1208 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1209 if (!aNS.IsNull()) {
1210 aMainShape = TNaming_Tool::CurrentShape(aNS);
1214 TopTools_IndexedMapOfShape aSubShapesMap;
1215 TopExp::MapShapes(aMainShape, aSubShapesMap);
1216 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1218 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1219 aResult->setImpl(new TopoDS_Shape(aSelShape));
1221 aSelection = aResult;
1224 setValue(aContextRes, aSelection);
1227 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1229 std::string aResult;
1230 if (owner()->document() != theContext->document()) {
1231 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1232 aResult = theContext->document()->kind() + "/";
1234 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1235 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1236 if (aDocRes.get()) {
1237 aResult = aDocRes->data()->name() + "/";
1241 aResult += theContext->data()->name();
1245 void Model_AttributeSelection::computeValues(
1246 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1247 TopTools_ListOfShape& theShapes)
1249 bool aWasWholeContext = theValShape.IsNull();
1250 if (aWasWholeContext) {
1251 //theShapes.Append(theValShape);
1253 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1255 //TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1256 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1257 //if (anOldContShape.IsSame(theValShape)) { // full context shape substituted by new full context
1258 //theShapes.Append(aNewContShape);
1261 // if a new value is unchanged in the new context, do nothing: value is correct
1262 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1263 for(; aSubExp.More(); aSubExp.Next()) {
1264 if (aSubExp.Current().IsSame(theValShape)) {
1265 theShapes.Append(theValShape);
1269 // if new context becomes compsolid, the resulting sub may be in sub-solids
1270 std::list<ResultPtr> aNewToIterate;
1271 aNewToIterate.push_back(theNewContext);
1272 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1274 std::list<ResultPtr> allNewContextSubs;
1275 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1276 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1277 for(; aSub != allNewContextSubs.end(); aSub++) {
1278 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1279 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1280 aNewToIterate.push_back(aBody);
1284 // first iteration: searching for the whole shape appearance (like face of the box)
1285 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1286 int aToFindPart = 0;
1287 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1288 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1289 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1290 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1291 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1292 std::shared_ptr<Model_Data> aNewData =
1293 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1294 TDF_Label aNewLab = aNewData->label();
1295 // searching for produced sub-shape fully on some label
1296 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID(), Standard_True);
1297 for(; aNSIter.More(); aNSIter.Next()) {
1298 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1299 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1300 if (aToFindPart == 0) { // search shape is fully inside
1301 if (aPairIter.OldShape().IsSame(theValShape)) {
1302 if (aPairIter.NewShape().IsNull()) {// value was removed
1306 theShapes.Append(aPairIter.NewShape());
1308 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1309 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1310 for(; anExp.More(); anExp.Next()) {
1311 if (anExp.Current().IsSame(theValShape)) { // found a new container
1312 if (aPairIter.NewShape().IsNull()) {// value was removed
1316 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1317 anOlds.Add(aPairIter.OldShape());
1326 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1327 // map of sub-shapes -> number of occurences of these shapes in containers
1328 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1329 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1330 for(; aContIter.More(); aContIter.Next()) {
1331 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1332 for(; aSubExp.More(); aSubExp.Next()) {
1333 if (!aSubs.IsBound(aSubExp.Current())) {
1334 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1336 // store old to know how many olds produced this shape
1337 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1340 // if sub is appeared same times in containers as the number of old shapes that contain it
1341 int aCountInOld = anOlds.Size();
1342 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1344 for(; aSubsIter.More(); aSubsIter.Next()) {
1345 if (aSubsIter.Value().Size() == aCountInOld) {
1346 theShapes.Append(aSubsIter.Key());
1350 if (theShapes.IsEmpty()) { // nothing was changed
1351 theShapes.Append(aWasWholeContext ? TopoDS_Shape() : theValShape);
1355 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1356 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1357 TDF_Label theAccessLabel,
1358 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1360 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1361 TopTools_ListOfShape aResContShapes;
1362 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1363 TopTools_ListOfShape aContextList;
1364 aContextList.Append(theContShape);
1365 if (theContext.get()) {
1366 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1367 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1368 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1370 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1371 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1372 for(; aModifIter.More(); aModifIter.Next()) {
1373 TDF_Label anObjLab = aModifIter.Label().Father();
1374 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1375 (theDoc->objects()->object(anObjLab));
1376 if (!aModifierObj.get()) {
1377 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1378 if (!anObjLab.IsRoot())
1379 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1380 (theDoc->objects()->object(anObjLab.Father()));
1381 if (!aModifierObj.get())
1384 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1385 if (!aModifierFeat.get())
1387 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1388 if (aModifierFeat == aThisFeature || theDoc->objects()->isLater(aModifierFeat, aThisFeature))
1389 continue; // the modifier feature is later than this, so, should not be used
1390 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1391 if (aCurrentModifierFeat == aModifierFeat ||
1392 theDoc->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
1393 continue; // the current modifier is later than the found, so, useless
1394 Handle(TNaming_NamedShape) aNewNS;
1395 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1396 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1397 aResults.insert(aModifierObj);
1398 //TNaming_Iterator aPairIter(aNewNS);
1399 //aResContShapes.Append(aPairIter.NewShape());
1400 aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
1401 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1402 aResults.insert(ResultPtr());
1403 } else { // not-precessed modification => don't support it
1408 if (aResults.empty())
1409 return false; // no modifications found, must stay the same
1410 // iterate all results to find futher modifications
1411 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1412 for(; aResIter != aResults.end(); aResIter++) {
1413 if (aResIter->get() != NULL) {
1414 // compute new values by two contextes: the old and the new
1415 TopTools_ListOfShape aValShapes;
1416 computeValues(theContext, *aResIter, theValShape, aValShapes);
1418 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1419 for(; aNewVal.More(); aNewVal.Next()) {
1420 std::list<ResultPtr> aNewRes;
1421 TopTools_ListOfShape aNewUpdatedVal;
1422 TopoDS_Shape aNewValSh = aNewVal.Value();
1423 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1424 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1425 aNewValSh.Nullify();
1426 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1427 theAccessLabel, aNewRes, aNewUpdatedVal))
1429 // appeand new results instead of the current ones
1430 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1431 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1432 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1433 theResults.push_back(*aNewIter);
1434 theValShapes.Append(aNewUpdVal.Value());
1436 } else { // the current result is good
1437 theResults.push_back(*aResIter);
1438 theValShapes.Append(aNewValSh);
1443 return true; // theResults must be empty: everything is deleted
1446 void Model_AttributeSelection::updateInHistory()
1448 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1449 // only bodies and parts may be modified later in the history, don't do anything otherwise
1450 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1451 aContext->groupName() != ModelAPI_ResultPart::group()))
1453 std::shared_ptr<Model_Document> aDoc =
1454 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1455 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1456 if (!aContData.get() || !aContData->isValid())
1458 TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
1459 Handle(TNaming_NamedShape) aContNS;
1460 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1461 bool aFoundNewContext = true;
1462 ResultPtr aNewContext = aContext;
1463 while(aFoundNewContext) {
1464 aFoundNewContext = false;
1465 // parts have no shape in result, so, trace references using the Part info
1466 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1467 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1468 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1469 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1470 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1471 for(; aRef != aRefs.end(); aRef++) {
1472 // to avoid detection of part changes by local selection only
1473 AttributeSelectionPtr aSel =
1474 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1475 if (aSel.get() && !aSel->value()->isSame(aSel->context()->shape()))
1478 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1479 if (aRefFeat.get() && aRefFeat != owner()) {
1480 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1481 if (aDoc->objects()->isLater(aThisFeature, aRefFeat)) { // found better feature
1482 aFoundNewContext = true;
1483 aNewContext = aRefFeat->firstResult();
1490 if (aNewContext != aContext) {
1491 setValue(aNewContext, value());
1495 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1496 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1497 // iterate the context shape modifications in order to find a feature that is upper in history
1498 // that this one and is really modifies the referenced result to refer to it
1499 ResultPtr aModifierResFound;
1500 TNaming_Iterator aPairIter(aContNS);
1501 if (!aPairIter.More())
1503 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1504 bool anIterate = true;
1505 // trying to update also the sub-shape selected
1506 GeomShapePtr aSubShape = value();
1507 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1509 TopoDS_Shape aValShape;
1510 if (aSubShape.get()) {
1511 aValShape = aSubShape->impl<TopoDS_Shape>();
1514 std::list<ResultPtr> aNewContexts;
1515 TopTools_ListOfShape aValShapes;
1516 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1518 // update scope to reset to a new one
1521 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1522 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1523 if (aNewCont == aNewContexts.end()) { // all results were deleted
1524 ResultPtr anEmptyContext;
1525 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1526 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1530 GeomShapePtr aValueShape;
1531 if (!aNewValues.Value().IsNull()) {
1532 aValueShape = std::make_shared<GeomAPI_Shape>();
1533 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1535 setValue(*aNewCont, aValueShape);
1536 // if there are more than one result, put them by "append" into "parent" list
1538 for(aNewCont++, aNewValues.Next(); aNewCont != aNewContexts.end();
1539 aNewCont++, aNewValues.Next()) {
1540 GeomShapePtr aValueShape;
1541 if (!aNewValues.Value().IsNull()) {
1542 aValueShape = std::make_shared<GeomAPI_Shape>();
1543 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1546 // Check that list has the same type of shape selection before adding.
1547 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1548 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1549 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1550 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1552 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1553 if (aValueShape.get()) {
1554 aShapeShapeType = aValueShape->shapeType();
1556 (*aNewCont)->shape()->shapeType();
1559 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1563 myParent->append(*aNewCont, aValueShape);
1569 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1571 myParent = theParent;