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 <Events_InfoMessage.h>
40 #include <GeomAPI_Edge.h>
41 #include <GeomAPI_Vertex.h>
42 #include <GeomAlgoAPI_CompoundBuilder.h>
44 #include <TNaming_Selector.hxx>
45 #include <TNaming_NamedShape.hxx>
46 #include <TNaming_Tool.hxx>
47 #include <TNaming_Builder.hxx>
48 #include <TNaming_SameShapeIterator.hxx>
49 #include <TNaming_Iterator.hxx>
50 #include <TDataStd_Integer.hxx>
51 #include <TDataStd_UAttribute.hxx>
52 #include <TDataStd_Name.hxx>
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopTools_DataMapOfShapeShape.hxx>
55 #include <TopTools_MapOfShape.hxx>
56 #include <TopExp_Explorer.hxx>
57 #include <BRep_Tool.hxx>
60 #include <TDF_ChildIterator.hxx>
61 #include <TDF_ChildIDIterator.hxx>
62 #include <TopoDS_Iterator.hxx>
63 #include <TDF_ChildIDIterator.hxx>
64 #include <Geom_Circle.hxx>
65 #include <Geom_Ellipse.hxx>
66 #include <BRep_Builder.hxx>
68 //#define DEB_NAMING 1
70 #include <BRepTools.hxx>
72 /// added to the index in the packed map to signalize that the vertex of edge is selected
73 /// (multiplied by the index of the edge)
74 static const int kSTART_VERTEX_DELTA = 1000000;
75 // identifier that there is simple reference: selection equals to context
76 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
77 // reference to Part sub-object
78 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
79 // selection is invalid after recomputation
80 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
82 // identifier of the selection of the center of circle on edge
83 Standard_GUID kCIRCLE_CENTER("d0d0e0f1-217a-4b95-8fbb-0c4132f23718");
84 // identifier of the selection of the first focus point of ellipse on edge
85 Standard_GUID kELLIPSE_CENTER1("f70df04c-3168-4dc9-87a4-f1f840c1275d");
86 // identifier of the selection of the second focus point of ellipse on edge
87 Standard_GUID kELLIPSE_CENTER2("1395ae73-8e02-4cf8-b204-06ff35873a32");
89 // prefix for the whole feature context identification
90 const static std::string kWHOLE_FEATURE = "all-in-";
92 // on this label is stored:
93 // TNaming_NamedShape - selected shape
94 // TNaming_Naming - topological selection information (for the body)
95 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
96 // TDataStd_Integer - type of the selected shape (for construction)
97 // TDF_Reference - from ReferenceAttribute, the context
98 bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
99 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
101 if (theTemporarily &&
102 (!theContext.get() || theContext->groupName() != ModelAPI_Feature::group())) {
103 // just keep the stored without DF update
104 myTmpContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
105 myTmpSubShape = theSubShape;
106 owner()->data()->sendAttributeUpdated(this);
109 myTmpContext.reset();
110 myTmpSubShape.reset();
111 myTmpCenterType = NOT_CENTER;
115 const std::shared_ptr<GeomAPI_Shape>& anOldShape = internalValue(aType);
116 bool isOldContext = theContext == myRef.value();
117 bool isOldShape = isOldContext &&
118 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
119 if (isOldShape) return false; // shape is the same, so context is also unchanged
120 bool aToUnblock = false;
121 // update the referenced object if needed
123 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
124 myRef.setValue(theContext);
127 // do noth use naming if selected shape is result shape itself, but not sub-shape
128 TDF_Label aSelLab = selectionLabel();
129 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
130 aSelLab.ForgetAttribute(kINVALID_SELECTION);
131 aSelLab.ForgetAttribute(kCIRCLE_CENTER);
132 aSelLab.ForgetAttribute(kELLIPSE_CENTER1);
133 aSelLab.ForgetAttribute(kELLIPSE_CENTER2);
135 bool isDegeneratedEdge = false;
136 // do not use the degenerated edge as a shape, a null context and shape is used in the case
137 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
138 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
139 if (aSubShape.ShapeType() == TopAbs_EDGE)
140 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
142 if (!theContext.get() || isDegeneratedEdge) {
143 // to keep the reference attribute label
144 TDF_Label aRefLab = myRef.myRef->Label();
145 aSelLab.ForgetAllAttributes(true);
146 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
148 owner()->data()->blockSendAttributeUpdated(false);
151 if (theContext->groupName() == ModelAPI_ResultBody::group()) {
152 ResultBodyPtr aContextBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);
153 // do not select the whole shape for body:it is already must be in the data framework
154 // equal and null selected objects mean the same: object is equal to context,
155 if (aContextBody->shape().get() &&
156 (aContextBody->shape()->isEqual(theSubShape) || !theSubShape.get())) {
157 aSelLab.ForgetAllAttributes(true);
158 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
160 selectBody(aContextBody, theSubShape);
162 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
163 ResultConstructionPtr aContextConstruction =
164 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
165 aSelLab.ForgetAllAttributes(true); // to remove old selection data
166 std::shared_ptr<Model_ResultConstruction> aConstruction =
167 std::dynamic_pointer_cast<Model_ResultConstruction>(theContext);
168 std::shared_ptr<GeomAPI_Shape> aSubShape;
169 if (theSubShape.get() && !aContextConstruction->shape()->isEqual(theSubShape))
170 aSubShape = theSubShape; // the whole context
171 if (aConstruction->isInfinite()) {
172 // For correct naming selection, put the shape into the naming structure.
173 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
174 TNaming_Builder aBuilder(aSelLab);
175 aBuilder.Generated(aContextConstruction->shape()->impl<TopoDS_Shape>());
177 int anIndex = aConstruction->select(theSubShape, owner()->document());
178 TDataStd_Integer::Set(aSelLab, anIndex);
179 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
180 aSelLab.ForgetAllAttributes(true);
181 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
182 selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
185 owner()->data()->sendAttributeUpdated(this);
188 owner()->data()->blockSendAttributeUpdated(false);
193 void Model_AttributeSelection::setValueCenter(
194 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
195 const CenterType theCenterType, const bool theTemporarily)
197 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
198 if (theTemporarily) {
199 myTmpCenterType = theCenterType;
200 } else { // store in the data structure
201 TDF_Label aSelLab = selectionLabel();
202 switch(theCenterType) {
205 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
206 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
208 case ELLIPSE_FIRST_FOCUS:
210 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
211 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
213 case ELLIPSE_SECOND_FOCUS:
215 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
216 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
220 owner()->data()->sendAttributeUpdated(this);
224 void Model_AttributeSelection::selectValue(
225 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
228 std::shared_ptr<GeomAPI_Shape> aValue =
229 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
230 if (!aValue.get() || aType == NOT_CENTER) {
231 setValue(theSource->context(), aValue);
233 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
234 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
235 setValueCenter(theSource->context(), anEdge, aType);
239 void Model_AttributeSelection::removeTemporaryValues()
241 if (myTmpContext.get() || myTmpSubShape.get()) {
242 myTmpContext.reset();
243 myTmpSubShape.reset();
247 // returns the center of the edge: circular or elliptical
248 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
250 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
251 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
252 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
253 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
254 double aFirst, aLast;
255 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
256 if (!aCurve.IsNull()) {
257 TopoDS_Vertex aVertex;
258 BRep_Builder aBuilder;
259 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
260 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
261 if (!aCirc.IsNull()) {
262 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
265 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
266 if (!anEll.IsNull()) {
267 aBuilder.MakeVertex(aVertex,
268 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
269 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
272 if (!aVertex.IsNull()) {
273 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
274 aResult->setImpl(new TopoDS_Vertex(aVertex));
280 return theEdge; // no vertex, so, return the initial edge
283 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
285 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
286 return std::shared_ptr<GeomAPI_Shape>();
287 CenterType aType = NOT_CENTER;
288 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
289 return centerByEdge(aResult, aType);
292 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
294 theType = NOT_CENTER;
295 GeomShapePtr aResult;
296 if (myTmpContext.get() || myTmpSubShape.get()) {
297 theType = myTmpCenterType;
298 ResultConstructionPtr aResulConstruction =
299 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
300 if(aResulConstruction.get()) {
301 // it is just reference to construction.
302 return myTmpSubShape;
304 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
307 TDF_Label aSelLab = selectionLabel();
308 if (aSelLab.IsAttribute(kINVALID_SELECTION))
311 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
312 theType = CIRCLE_CENTER;
313 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
314 theType = ELLIPSE_FIRST_FOCUS;
315 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
316 theType = ELLIPSE_SECOND_FOCUS;
319 if (myRef.isInitialized()) {
320 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
321 ResultPtr aContext = context();
323 return aResult; // empty result
324 return aContext->shape();
326 if (aSelLab.IsAttribute(kPART_REF_ID)) {
327 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
328 if (!aPart.get() || !aPart->isActivated())
329 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
330 Handle(TDataStd_Integer) anIndex;
331 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
332 if (anIndex->Get()) { // special selection attribute was created, use it
333 return aPart->selectionValue(anIndex->Get());
334 } else { // face with name is already in the data model, so try to take it by name
335 Handle(TDataStd_Name) aName;
336 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
337 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
338 std::size_t aPartEnd = aSubShapeName.find('/');
339 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
340 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
342 std::string aType; // to reuse already existing selection the type is not needed
343 return aPart->shapeInPart(aNameInPart, aType, anIndex);
350 std::shared_ptr<Model_ResultConstruction> aConstr =
351 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
353 if (aConstr->isInfinite())
354 return aResult; // empty result
356 if (!aConstr.get()) { // for construction context, return empty result as usual even
357 // the whole feature is selected
358 FeaturePtr aFeature = contextFeature();
359 if (aFeature.get()) {
360 std::list<GeomShapePtr> allShapes;
361 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
362 for (; aRes != aFeature->results().cend(); aRes++) {
363 if (aRes->get() && !(*aRes)->isDisabled()) {
364 GeomShapePtr aShape = (*aRes)->shape();
365 if (aShape.get() && !aShape->isNull()) {
366 allShapes.push_back(aShape);
370 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
374 Handle(TNaming_NamedShape) aSelection;
375 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
376 TopoDS_Shape aSelShape = aSelection->Get();
377 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
378 aResult->setImpl(new TopoDS_Shape(aSelShape));
379 } else if (aConstr) { // simple construction element: just shape of this construction element
380 Handle(TDataStd_Integer) anIndex;
381 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
382 if (anIndex->Get() == 0) // it is just reference to construction, nothing is in value
384 return aConstr->shape(anIndex->Get(), owner()->document());
391 bool Model_AttributeSelection::isInvalid()
393 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
396 bool Model_AttributeSelection::isInitialized()
398 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
399 std::shared_ptr<GeomAPI_Shape> aResult;
400 if (myRef.isInitialized()) {
401 TDF_Label aSelLab = selectionLabel();
402 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
403 ResultPtr aContext = context();
404 return aContext.get() != NULL;
406 Handle(TNaming_NamedShape) aSelection;
407 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
408 return !aSelection->Get().IsNull();
409 } else { // for simple construction element: just shape of this construction element
410 std::shared_ptr<Model_ResultConstruction> aConstr =
411 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
413 Handle(TDataStd_Integer) anIndex;
414 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
415 // for the whole shape it may return null, so, if index exists, returns true
419 // for the whole feature, a feature object
420 FeaturePtr aFeat = contextFeature();
429 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
432 myIsInitialized = myRef.isInitialized();
436 void Model_AttributeSelection::setID(const std::string theID)
439 ModelAPI_AttributeSelection::setID(theID);
442 ResultPtr Model_AttributeSelection::context()
444 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
447 if (myTmpContext.get() || myTmpSubShape.get()) {
451 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
452 // for parts there could be same-data result, so take the last enabled
454 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
455 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
456 for(int a = aSize - 1; a >= 0; a--) {
457 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
458 if(aPart.get() && aPart->data() == aResult->data()) {
459 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
460 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
461 // check that this result is not this-feature result (it is forbidden t oselect itself)
462 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
468 } else { // if feature - construction is selected, it has only one result, return this result
469 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
470 if (aFeature.get() && aFeature->results().size() == 1 &&
471 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
472 return aFeature->firstResult();
477 FeaturePtr Model_AttributeSelection::contextFeature() {
478 if (myTmpContext.get() || myTmpSubShape.get()) {
479 return FeaturePtr(); // feature can not be selected temporarily
481 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
483 ObjectPtr Model_AttributeSelection::contextObject() {
484 ResultPtr aRes = context();
487 return contextFeature();
491 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
493 ModelAPI_AttributeSelection::setObject(theObject);
494 myRef.setObject(theObject);
497 TDF_LabelMap& Model_AttributeSelection::scope()
499 if (myScope.IsEmpty()) { // create a new scope if not yet done
500 // gets all features with named shapes that are before this feature label (before in history)
501 DocumentPtr aMyDoc = owner()->document();
502 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
503 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
504 bool aMePassed = false;
505 CompositeFeaturePtr aComposite =
506 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(owner());
507 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
508 CompositeFeaturePtr aCompositeOwner, aCompositeOwnerOwner;
509 if (aFeature.get()) {
510 aCompositeOwner = ModelAPI_Tools::compositeOwner(aFeature);
511 if (aCompositeOwner.get()) {
512 aCompositeOwnerOwner = ModelAPI_Tools::compositeOwner(aCompositeOwner);
515 // for group Scope is not limitet: this is always up to date objects
516 // this causes problem in galeries.py
517 //bool isGroup = aFeature.get() && aFeature->getKind() == "Group";
518 for(; aFIter != allFeatures.end(); aFIter++) {
519 if (*aFIter == owner()) { // the left features are created later (except subs of composite)
523 //if (isGroup) aMePassed = false;
524 bool isInScope = !aMePassed;
525 if (!isInScope && aComposite.get()) {
526 // try to add sub-elements of composite if this is composite
527 if (aComposite->isSub(*aFIter))
530 // remove the composite-owner of this feature (sketch in extrusion-cut)
531 if (isInScope && (aCompositeOwner == *aFIter || aCompositeOwnerOwner == *aFIter))
534 if (isInScope && aFIter->get() && (*aFIter)->data()->isValid()) {
535 TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
536 (*aFIter)->data())->label().Father();
537 TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), true);
538 for(; aNSIter.More(); aNSIter.Next()) {
539 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
540 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
541 myScope.Add(aNS->Label());
546 // also add all naming labels created for external constructions
547 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(aMyDoc);
548 TDF_Label anExtDocLab = aDoc->extConstructionsLabel();
549 TDF_ChildIDIterator aNSIter(anExtDocLab, TNaming_NamedShape::GetID(), true);
550 for(; aNSIter.More(); aNSIter.Next()) {
551 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
552 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
553 myScope.Add(aNS->Label());
560 /// Sets the invalid flag if flag is false, or removes it if "true"
562 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
564 theLab.ForgetAttribute(kINVALID_SELECTION);
566 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
571 void Model_AttributeSelection::split(
572 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
574 TopTools_ListOfShape aSubs;
575 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
576 if (!anExplorer.Value().IsNull() &&
577 anExplorer.Value().ShapeType() == theType) {
578 aSubs.Append(anExplorer.Value());
579 } else { // invalid case; bad result shape, so, impossible to split easily
584 if (aSubs.Extent() > 1) { // ok to split
585 TopTools_ListIteratorOfListOfShape aSub(aSubs);
586 GeomShapePtr aSubSh(new GeomAPI_Shape);
587 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
588 setValue(theContext, aSubSh);
589 for(aSub.Next(); aSub.More(); aSub.Next()) {
590 GeomShapePtr aSubSh(new GeomAPI_Shape);
591 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
592 myParent->append(theContext, aSubSh);
597 bool Model_AttributeSelection::update()
599 FeaturePtr aContextFeature = contextFeature();
600 if (aContextFeature.get()) {
603 TDF_Label aSelLab = selectionLabel();
604 ResultPtr aContext = context();
606 return setInvalidIfFalse(aSelLab, false);
607 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
608 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
611 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
612 std::shared_ptr<GeomAPI_Shape> aNoSelection;
613 bool aResult = selectPart(aContext, aNoSelection, true);
614 aResult = setInvalidIfFalse(aSelLab, aResult);
616 owner()->data()->sendAttributeUpdated(this);
621 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
622 // body: just a named shape, use selection mechanism from OCCT
623 TNaming_Selector aSelector(aSelLab);
624 TopoDS_Shape anOldShape;
625 if (!aSelector.NamedShape().IsNull()) {
626 anOldShape = aSelector.NamedShape()->Get();
628 bool aResult = aSelector.Solve(scope()) == Standard_True;
629 // must be before sending of updated attribute (1556)
630 aResult = setInvalidIfFalse(aSelLab, aResult);
631 TopoDS_Shape aNewShape;
632 if (!aSelector.NamedShape().IsNull()) {
633 aNewShape = aSelector.NamedShape()->Get();
635 if (anOldShape.IsNull() || aNewShape.IsNull() ||
636 !anOldShape.IsEqual(aSelector.NamedShape()->Get())) {
637 // shape type shoud not not changed: if shape becomes compound of such shapes, then split
638 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
639 anOldShape.ShapeType() != aNewShape.ShapeType() &&
640 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
642 split(aContext, aNewShape, anOldShape.ShapeType());
644 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
649 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
650 Handle(TDataStd_Integer) anIndex;
651 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
652 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
653 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
654 bool aModified = true;
655 bool aValid = aConstructionContext->update(anIndex->Get(), owner()->document(), aModified);
656 setInvalidIfFalse(aSelLab, aValid);
657 if (aConstructionContext->isInfinite()) {
658 // Update the selected shape.
659 TNaming_Builder aBuilder(aSelLab);
660 aBuilder.Generated(aConstructionContext->shape()->impl<TopoDS_Shape>());
663 owner()->data()->sendAttributeUpdated(this);
667 return setInvalidIfFalse(aSelLab, false); // unknown case
670 void Model_AttributeSelection::selectBody(
671 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
673 // perform the selection
674 TNaming_Selector aSel(selectionLabel());
675 TopoDS_Shape aContext;
677 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);//myRef.value()
679 aContext = aBody->shape()->impl<TopoDS_Shape>();
682 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
684 aContext = aResult->shape()->impl<TopoDS_Shape>();
686 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
691 // with "recover" feature the selected context may be not up to date (issue 1710)
692 Handle(TNaming_NamedShape) aResult;
693 TDF_Label aSelLab = selectionLabel();
694 TopoDS_Shape aNewContext = aContext;
695 bool isUpdated = true;
696 while(!aNewContext.IsNull() && isUpdated) {
697 // searching for the very last shape that was produced from this one
699 if (!TNaming_Tool::HasLabel(aSelLab, aNewContext))
700 // to avoid crash of TNaming_SameShapeIterator if pure shape does not exists
702 for(TNaming_SameShapeIterator anIter(aNewContext, aSelLab); anIter.More(); anIter.Next()) {
703 TDF_Label aNSLab = anIter.Label();
704 if (!scope().Contains(aNSLab))
706 Handle(TNaming_NamedShape) aNS;
707 if (aNSLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
708 for(TNaming_Iterator aShapesIter(aNS); aShapesIter.More(); aShapesIter.Next()) {
709 if (aShapesIter.Evolution() == TNaming_SELECTED)
710 continue; // don't use the selection evolution
711 if (!aShapesIter.OldShape().IsNull() && aShapesIter.OldShape().IsSame(aNewContext)) {
712 // found the original shape
713 aNewContext = aShapesIter.NewShape(); // go to the newer shape
721 if (aNewContext.IsNull()) { // a context is already deleted
722 setInvalidIfFalse(aSelLab, false);
723 Events_InfoMessage("Model_AttributeSelection", "Failed to select shape already deleted").send();
727 TopoDS_Shape aNewSub = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
728 if (!aNewSub.IsEqual(aContext)) { // searching for subshape in the new context
729 bool isFound = false;
730 TopExp_Explorer anExp(aNewContext, aNewSub.ShapeType());
731 for(; anExp.More(); anExp.Next()) {
732 if (anExp.Current().IsSame(aNewSub)) {
737 if (!isFound) { // sub-shape is not found in the up-to-date instance of the context shape
738 // if context is sub-result of compound/compsolid, selection of sub-shape better propagate to
739 // the main result (which is may be modified); the case is in 1799
740 ResultBodyPtr aMain = ModelAPI_Tools::bodyOwner(theContext);
741 while(ModelAPI_Tools::bodyOwner(aMain).get())
742 aMain = ModelAPI_Tools::bodyOwner(theContext);
744 selectBody(aMain, theSubShape);
747 setInvalidIfFalse(aSelLab, false);
748 Events_InfoMessage("Model_AttributeSelection",
749 "Failed to select sub-shape already modified").send();
754 /// fix for issue 411: result modified shapes must not participate in this selection mechanism
755 if (!aContext.IsNull()) {
756 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
757 bool aEraseResults = false;
758 if (aFeatureOwner.get()) {
759 aEraseResults = !aFeatureOwner->results().empty();
760 if (aEraseResults) // erase results without flash deleted and redisplay: do it after Select
761 aFeatureOwner->removeResults(0, false, false);
763 aSel.Select(aNewSub, aNewContext);
765 if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
766 static Events_Loop* aLoop = Events_Loop::loop();
767 static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
768 aLoop->flush(kDeletedEvent);
773 bool Model_AttributeSelection::selectPart(
774 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
775 const bool theUpdate)
777 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
778 if (!aPart.get() || !aPart->isActivated())
779 return true; // postponed naming
781 Handle(TDataStd_Integer) anIndex;
782 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
783 // by internal selection
784 if (anIndex->Get() > 0) {
785 // update the selection by index
786 return aPart->updateInPart(anIndex->Get());
788 return true; // nothing to do, referencing just by name
791 return true; // nothing to do, referencing just by name
793 // store the shape (in case part is not loaded it should be useful
795 std::string aName = theContext->data()->name();
796 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
797 aShape = theContext->shape()->impl<TopoDS_Shape>();
799 aShape = theSubShape->impl<TopoDS_Shape>();
801 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
802 TDataStd_Integer::Set(selectionLabel(), anIndex);
804 TNaming_Builder aBuilder(selectionLabel());
805 aBuilder.Select(aShape, aShape);
806 // identify by name in the part
807 TDataStd_Name::Set(selectionLabel(), aName.c_str());
808 return !aName.empty();
811 TDF_Label Model_AttributeSelection::selectionLabel()
813 return myRef.myRef->Label().FindChild(1);
816 /// prefixes of the shape names with centers defined
817 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
819 /// returns the map that contains all possible prefixes of the center-names
820 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
822 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
823 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
824 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
825 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
827 return kCENTERS_PREFIX;
830 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
832 std::string aName("");
833 if(!this->isInitialized())
834 return !theDefaultName.empty() ? theDefaultName : aName;
836 CenterType aCenterType = NOT_CENTER;
837 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
838 ResultPtr aCont = context();
841 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
842 // selection of a full feature
843 FeaturePtr aFeatureCont = contextFeature();
844 if (aFeatureCont.get()) {
845 return kWHOLE_FEATURE + aFeatureCont->name();
847 // in case of selection of removed result
851 Model_SelectionNaming aSelNaming(selectionLabel());
852 std::string aResult = aSelNaming.namingName(
853 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
854 if (aCenterType != NOT_CENTER) {
855 aResult += centersMap()[aCenterType];
860 // returns the center type and modifies the shape name if this name is center-name
861 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
863 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
864 centersMap().begin();
865 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
866 std::size_t aFound = theShapeName.find(aPrefixIter->second);
867 if (aFound != std::string::npos &&
868 aFound == theShapeName.size() - aPrefixIter->second.size()) {
869 theShapeName = theShapeName.substr(0, aFound);
870 return aPrefixIter->first;
873 return ModelAPI_AttributeSelection::NOT_CENTER;
876 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
877 void Model_AttributeSelection::selectSubShape(
878 const std::string& theType, const std::string& theSubShapeName)
880 if(theSubShapeName.empty() || theType.empty()) return;
882 std::string aSubShapeName = theSubShapeName;
883 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
884 centerTypeByName(aSubShapeName) : NOT_CENTER;
885 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
887 // first iteration is selection by name without center prefix, second - in case of problem,
888 // try with initial name
889 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
890 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
891 aSubShapeName = theSubShapeName;
892 aCenterType = NOT_CENTER;
894 } else if (aUseCenter != 1) continue;
896 // check this is Part-name: 2 delimiters in the name
897 std::size_t aPartEnd = aSubShapeName.find('/');
898 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
899 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
900 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
901 if (aFound.get()) { // found such part, so asking it for the name
902 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
903 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
905 std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, aType, anIndex);
906 if (aSelected.get()) {
907 if (aCenterType != NOT_CENTER) {
908 if (!aSelected->isEdge())
910 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
911 setValueCenter(aPart, aSelectedEdge, aCenterType);
913 setValue(aPart, aSelected);
914 TDataStd_Integer::Set(selectionLabel(), anIndex);
920 std::shared_ptr<Model_Document> aDoc =
921 std::dynamic_pointer_cast<Model_Document>(owner()->document());
922 // check this is a whole feature context
923 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
924 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
925 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
926 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
928 static const GeomShapePtr anEmptyShape;
929 setValue(anObj, anEmptyShape);
934 Model_SelectionNaming aSelNaming(selectionLabel());
935 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
937 if (aSelNaming.selectSubShape(aType, aSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
938 // try to find the last context to find the up to date shape
939 if (aCont->shape().get() && !aCont->shape()->isNull() &&
940 aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
941 const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
942 if (!aConShape.IsNull()) {
943 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
945 aNS = TNaming_Tool::CurrentNamedShape(aNS);
946 if (!aNS.IsNull() && scope().Contains(aNS->Label())) { // scope check is for 2228
947 TDF_Label aLab = aNS->Label();
948 if (aLab.Depth() % 2 == 0)
949 aLab = aLab.Father();
950 ObjectPtr anObj = aDoc->objects()->object(aLab);
951 while(!anObj.get() && aLab.Depth() > 5) {
952 aLab = aLab.Father().Father();
953 anObj = aDoc->objects()->object(aLab);
957 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
965 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
966 if (aCont.get() && aShapeToBeSelected.get()) {
967 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCont);
968 if (aComp && aComp->numberOfSubs()) {
969 std::list<ResultPtr> allSubs;
970 ModelAPI_Tools::allSubs(aComp, allSubs);
971 std::list<ResultPtr>::iterator aS = allSubs.begin();
972 for(; aS != allSubs.end(); aS++) {
973 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
974 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
975 aSub->shape()->isSubShape(aShapeToBeSelected)) {
982 // try to find the latest active result that must be used instead of the selected
983 // to set the active context (like in GUI selection), not concealed one
984 bool aFindNewContext = true;
985 while(aFindNewContext && aCont.get()) {
986 aFindNewContext = false;
987 // take references to all results: root one, any sub
988 ResultBodyPtr aCompContext = ModelAPI_Tools::bodyOwner(aCont, true);
989 std::list<ResultPtr> allRes;
990 if (aCompContext.get()) {
991 ModelAPI_Tools::allSubs(aCompContext, allRes);
992 allRes.push_back(aCompContext);
994 allRes.push_back(aCont);
996 for(std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
997 ResultPtr aResCont = *aSub;
998 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
999 // only lower and higher level subs are counted
1000 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1002 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1003 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1004 for(; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1005 if (!aRef->get() || !(*aRef)->owner().get())
1007 // concealed attribute only
1008 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1009 if (!ModelAPI_Session::get()->validators()->isConcealed(
1010 aRefFeat->getKind(), (*aRef)->id()))
1012 // search the feature result that contains sub-shape selected
1013 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1015 // take all sub-results or one result
1016 std::list<ResultPtr> aRefFeatResults;
1017 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1018 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1019 for(; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1020 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1021 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1022 aResults.push_back(aBody);
1024 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1025 for(; aResIter != aResults.end(); aResIter++) {
1026 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1028 GeomShapePtr aShape = (*aResIter)->shape();
1029 GeomShapePtr aSelectedShape =
1030 aShapeToBeSelected.get() ? aShapeToBeSelected : aCont->shape();
1031 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
1032 aCont = *aResIter; // found new context (produced from this) with same subshape
1033 aFindNewContext = true; // continue searching futher
1041 if (aCenterType != NOT_CENTER) {
1042 if (!aShapeToBeSelected->isEdge())
1044 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
1045 setValueCenter(aCont, aSelectedEdge, aCenterType);
1047 setValue(aCont, aShapeToBeSelected);
1052 TDF_Label aSelLab = selectionLabel();
1053 setInvalidIfFalse(aSelLab, false);
1057 int Model_AttributeSelection::Id()
1060 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1061 ResultPtr aContextRes = context();
1062 // support for compsolids:
1063 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1064 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1066 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1069 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1070 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1071 // searching for the latest main shape
1072 if (aSelection && !aSelection->isNull() && aContext && !aContext->isNull())
1074 std::shared_ptr<Model_Document> aDoc =
1075 std::dynamic_pointer_cast<Model_Document>(context()->document());
1077 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1078 if (!aNS.IsNull()) {
1079 aMainShape = TNaming_Tool::CurrentShape(aNS);
1083 TopTools_IndexedMapOfShape aSubShapesMap;
1084 TopExp::MapShapes(aMainShape, aSubShapesMap);
1085 anID = aSubShapesMap.FindIndex(aSubShape);
1090 void Model_AttributeSelection::setId(int theID)
1092 std::shared_ptr<GeomAPI_Shape> aSelection;
1094 ResultPtr aContextRes = context();
1095 // support for compsolids:
1096 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1097 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1099 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1101 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1102 // searching for the latest main shape
1103 if (theID > 0 && aContext && !aContext->isNull())
1105 std::shared_ptr<Model_Document> aDoc =
1106 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1108 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1109 if (!aNS.IsNull()) {
1110 aMainShape = TNaming_Tool::CurrentShape(aNS);
1114 TopTools_IndexedMapOfShape aSubShapesMap;
1115 TopExp::MapShapes(aMainShape, aSubShapesMap);
1116 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1118 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1119 aResult->setImpl(new TopoDS_Shape(aSelShape));
1121 aSelection = aResult;
1124 setValue(aContextRes, aSelection);
1127 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1129 std::string aResult;
1130 if (owner()->document() != theContext->document()) {
1131 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1132 aResult = theContext->document()->kind() + "/";
1134 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1135 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1136 if (aDocRes.get()) {
1137 aResult = aDocRes->data()->name() + "/";
1141 aResult += theContext->data()->name();
1145 void Model_AttributeSelection::computeValues(
1146 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1147 TopTools_ListOfShape& theShapes)
1149 bool aWasWholeContext = theValShape.IsNull();
1150 if (aWasWholeContext) {
1151 //theShapes.Append(theValShape);
1153 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1155 //TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1156 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1157 //if (anOldContShape.IsSame(theValShape)) { // full context shape substituted by new full context
1158 //theShapes.Append(aNewContShape);
1161 // if a new value is unchanged in the new context, do nothing: value is correct
1162 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1163 for(; aSubExp.More(); aSubExp.Next()) {
1164 if (aSubExp.Current().IsSame(theValShape)) {
1165 theShapes.Append(theValShape);
1169 // if new context becomes compsolid, the resulting sub may be in sub-solids
1170 std::list<ResultPtr> aNewToIterate;
1171 aNewToIterate.push_back(theNewContext);
1172 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1174 std::list<ResultPtr> allNewContextSubs;
1175 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1176 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1177 for(; aSub != allNewContextSubs.end(); aSub++) {
1178 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1179 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1180 aNewToIterate.push_back(aBody);
1184 // first iteration: searching for the whole shape appearance (like face of the box)
1185 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1186 int aToFindPart = 0;
1187 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1188 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1189 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1190 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1191 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1192 std::shared_ptr<Model_Data> aNewData =
1193 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1194 TDF_Label aNewLab = aNewData->label();
1195 // searching for produced sub-shape fully on some label
1196 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID(), Standard_True);
1197 for(; aNSIter.More(); aNSIter.Next()) {
1198 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1199 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1200 if (aToFindPart == 0) { // search shape is fully inside
1201 if (aPairIter.OldShape().IsSame(theValShape)) {
1202 if (aPairIter.NewShape().IsNull()) {// value was removed
1206 theShapes.Append(aPairIter.NewShape());
1208 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1209 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1210 for(; anExp.More(); anExp.Next()) {
1211 if (anExp.Current().IsSame(theValShape)) { // found a new container
1212 if (aPairIter.NewShape().IsNull()) {// value was removed
1216 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1217 anOlds.Add(aPairIter.OldShape());
1226 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1227 // map of sub-shapes -> number of occurences of these shapes in containers
1228 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1229 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1230 for(; aContIter.More(); aContIter.Next()) {
1231 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1232 for(; aSubExp.More(); aSubExp.Next()) {
1233 if (!aSubs.IsBound(aSubExp.Current())) {
1234 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1236 // store old to know how many olds produced this shape
1237 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1240 // if sub is appeared same times in containers as the number of old shapes that contain it
1241 int aCountInOld = anOlds.Size();
1242 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1244 for(; aSubsIter.More(); aSubsIter.Next()) {
1245 if (aSubsIter.Value().Size() == aCountInOld) {
1246 theShapes.Append(aSubsIter.Key());
1250 if (theShapes.IsEmpty()) { // nothing was changed
1251 theShapes.Append(aWasWholeContext ? TopoDS_Shape() : theValShape);
1255 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1256 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1257 TDF_Label theAccessLabel,
1258 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1260 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1261 TopTools_ListOfShape aResContShapes;
1262 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1263 TopTools_ListOfShape aContextList;
1264 aContextList.Append(theContShape);
1265 if (theContext.get()) {
1266 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1267 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1268 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1270 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1271 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1272 for(; aModifIter.More(); aModifIter.Next()) {
1273 TDF_Label anObjLab = aModifIter.Label().Father();
1274 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1275 (theDoc->objects()->object(anObjLab));
1276 if (!aModifierObj.get()) {
1277 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1278 if (!anObjLab.IsRoot())
1279 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1280 (theDoc->objects()->object(anObjLab.Father()));
1281 if (!aModifierObj.get())
1284 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1285 if (!aModifierFeat.get())
1287 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1288 if (aModifierFeat == aThisFeature || theDoc->objects()->isLater(aModifierFeat, aThisFeature))
1289 continue; // the modifier feature is later than this, so, should not be used
1290 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1291 if (aCurrentModifierFeat == aModifierFeat ||
1292 theDoc->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
1293 continue; // the current modifier is later than the found, so, useless
1294 Handle(TNaming_NamedShape) aNewNS;
1295 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1296 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1297 aResults.insert(aModifierObj);
1298 //TNaming_Iterator aPairIter(aNewNS);
1299 //aResContShapes.Append(aPairIter.NewShape());
1300 aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
1301 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1302 aResults.insert(ResultPtr());
1303 } else { // not-precessed modification => don't support it
1308 if (aResults.empty())
1309 return false; // no modifications found, must stay the same
1310 // iterate all results to find futher modifications
1311 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1312 for(; aResIter != aResults.end(); aResIter++) {
1313 if (aResIter->get() != NULL) {
1314 // compute new values by two contextes: the old and the new
1315 TopTools_ListOfShape aValShapes;
1316 computeValues(theContext, *aResIter, theValShape, aValShapes);
1318 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1319 for(; aNewVal.More(); aNewVal.Next()) {
1320 std::list<ResultPtr> aNewRes;
1321 TopTools_ListOfShape aNewUpdatedVal;
1322 TopoDS_Shape aNewValSh = aNewVal.Value();
1323 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1324 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1325 aNewValSh.Nullify();
1326 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1327 theAccessLabel, aNewRes, aNewUpdatedVal))
1329 // appeand new results instead of the current ones
1330 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1331 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1332 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1333 theResults.push_back(*aNewIter);
1334 theValShapes.Append(aNewUpdVal.Value());
1336 } else { // the current result is good
1337 theResults.push_back(*aResIter);
1338 theValShapes.Append(aNewValSh);
1343 return true; // theResults must be empty: everything is deleted
1346 void Model_AttributeSelection::updateInHistory()
1348 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1349 // only bodies and parts may be modified later in the history, don't do anything otherwise
1350 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1351 aContext->groupName() != ModelAPI_ResultPart::group()))
1353 std::shared_ptr<Model_Document> aDoc =
1354 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1355 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1356 if (!aContData.get() || !aContData->isValid())
1358 TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
1359 Handle(TNaming_NamedShape) aContNS;
1360 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1361 bool aFoundNewContext = true;
1362 ResultPtr aNewContext = aContext;
1363 while(aFoundNewContext) {
1364 aFoundNewContext = false;
1365 // parts have no shape in result, so, trace references using the Part info
1366 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1367 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1368 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1369 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1370 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1371 for(; aRef != aRefs.end(); aRef++) {
1372 // to avoid detection of part changes by local selection only
1373 AttributeSelectionPtr aSel =
1374 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1375 if (aSel.get() && !aSel->value()->isSame(aSel->context()->shape()))
1378 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1379 if (aRefFeat.get() && aRefFeat != owner()) {
1380 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1381 if (aDoc->objects()->isLater(aThisFeature, aRefFeat)) { // found better feature
1382 aFoundNewContext = true;
1383 aNewContext = aRefFeat->firstResult();
1390 if (aNewContext != aContext) {
1391 setValue(aNewContext, value());
1395 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1396 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1397 // iterate the context shape modifications in order to find a feature that is upper in history
1398 // that this one and is really modifies the referenced result to refer to it
1399 ResultPtr aModifierResFound;
1400 TNaming_Iterator aPairIter(aContNS);
1401 if (!aPairIter.More())
1403 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1404 bool anIterate = true;
1405 // trying to update also the sub-shape selected
1406 GeomShapePtr aSubShape = value();
1407 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1409 TopoDS_Shape aValShape;
1410 if (aSubShape.get()) {
1411 aValShape = aSubShape->impl<TopoDS_Shape>();
1414 std::list<ResultPtr> aNewContexts;
1415 TopTools_ListOfShape aValShapes;
1416 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1418 // update scope to reset to a new one
1421 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1422 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1423 if (aNewCont == aNewContexts.end()) { // all results were deleted
1424 ResultPtr anEmptyContext;
1425 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1426 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1430 GeomShapePtr aValueShape;
1431 if (!aNewValues.Value().IsNull()) {
1432 aValueShape = std::make_shared<GeomAPI_Shape>();
1433 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1435 setValue(*aNewCont, aValueShape);
1436 // if there are more than one result, put them by "append" into "parent" list
1438 for(aNewCont++, aNewValues.Next(); aNewCont != aNewContexts.end();
1439 aNewCont++, aNewValues.Next()) {
1440 GeomShapePtr aValueShape;
1441 if (!aNewValues.Value().IsNull()) {
1442 aValueShape = std::make_shared<GeomAPI_Shape>();
1443 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1446 // Check that list has the same type of shape selection before adding.
1447 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1448 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1449 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1450 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1452 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1453 if (aValueShape.get()) {
1454 aShapeShapeType = aValueShape->shapeType();
1456 (*aNewCont)->shape()->shapeType();
1459 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1463 myParent->append(*aNewCont, aValueShape);
1469 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1471 myParent = theParent;