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_ShapeIterator.h>
44 #include <GeomAPI_Vertex.h>
45 #include <GeomAlgoAPI_CompoundBuilder.h>
46 #include <GeomAlgoAPI_NExplode.h>
48 #include <TNaming_Selector.hxx>
49 #include <TNaming_NamedShape.hxx>
50 #include <TNaming_Tool.hxx>
51 #include <TNaming_Builder.hxx>
52 #include <TNaming_SameShapeIterator.hxx>
53 #include <TNaming_Iterator.hxx>
54 #include <TDataStd_Integer.hxx>
55 #include <TDataStd_UAttribute.hxx>
56 #include <TDataStd_Name.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <TopTools_DataMapOfShapeShape.hxx>
59 #include <TopTools_MapOfShape.hxx>
60 #include <TopExp_Explorer.hxx>
61 #include <BRep_Tool.hxx>
64 #include <TDF_ChildIterator.hxx>
65 #include <TDF_ChildIDIterator.hxx>
66 #include <TopoDS_Iterator.hxx>
67 #include <TDF_ChildIDIterator.hxx>
68 #include <Geom_Circle.hxx>
69 #include <Geom_Ellipse.hxx>
70 #include <BRep_Builder.hxx>
72 //#define DEB_NAMING 1
74 #include <BRepTools.hxx>
76 /// added to the index in the packed map to signalize that the vertex of edge is selected
77 /// (multiplied by the index of the edge)
78 static const int kSTART_VERTEX_DELTA = 1000000;
79 // identifier that there is simple reference: selection equals to context
80 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
81 // reference to Part sub-object
82 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
83 // selection is invalid after recomputation
84 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
86 // identifier of the selection of the center of circle on edge
87 Standard_GUID kCIRCLE_CENTER("d0d0e0f1-217a-4b95-8fbb-0c4132f23718");
88 // identifier of the selection of the first focus point of ellipse on edge
89 Standard_GUID kELLIPSE_CENTER1("f70df04c-3168-4dc9-87a4-f1f840c1275d");
90 // identifier of the selection of the second focus point of ellipse on edge
91 Standard_GUID kELLIPSE_CENTER2("1395ae73-8e02-4cf8-b204-06ff35873a32");
93 // prefix for the whole feature context identification
94 const static std::string kWHOLE_FEATURE = "all-in-";
96 // on this label is stored:
97 // TNaming_NamedShape - selected shape
98 // TNaming_Naming - topological selection information (for the body)
99 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
100 // TDataStd_Integer - type of the selected shape (for construction)
101 // TDF_Reference - from ReferenceAttribute, the context
102 bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
103 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
105 if (theTemporarily &&
106 (!theContext.get() || theContext->groupName() != ModelAPI_Feature::group())) {
107 // just keep the stored without DF update
108 myTmpContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
109 myTmpSubShape = theSubShape;
110 owner()->data()->sendAttributeUpdated(this);
113 myTmpContext.reset();
114 myTmpSubShape.reset();
115 myTmpCenterType = NOT_CENTER;
119 const std::shared_ptr<GeomAPI_Shape>& anOldShape = internalValue(aType);
120 bool isOldContext = theContext == myRef.value();
121 bool isOldShape = isOldContext &&
122 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
123 if (isOldShape) return false; // shape is the same, so context is also unchanged
124 bool aToUnblock = false;
125 // update the referenced object if needed
127 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
128 myRef.setValue(theContext);
131 // do noth use naming if selected shape is result shape itself, but not sub-shape
132 TDF_Label aSelLab = selectionLabel();
133 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
134 aSelLab.ForgetAttribute(kINVALID_SELECTION);
135 aSelLab.ForgetAttribute(kCIRCLE_CENTER);
136 aSelLab.ForgetAttribute(kELLIPSE_CENTER1);
137 aSelLab.ForgetAttribute(kELLIPSE_CENTER2);
139 bool isDegeneratedEdge = false;
140 // do not use the degenerated edge as a shape, a null context and shape is used in the case
141 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
142 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
143 if (aSubShape.ShapeType() == TopAbs_EDGE)
144 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
146 if (!theContext.get() || isDegeneratedEdge) {
147 // to keep the reference attribute label
148 TDF_Label aRefLab = myRef.myRef->Label();
149 aSelLab.ForgetAllAttributes(true);
150 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
152 owner()->data()->blockSendAttributeUpdated(false);
155 if (theContext->groupName() == ModelAPI_ResultBody::group()) {
156 ResultBodyPtr aContextBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);
157 // do not select the whole shape for body:it is already must be in the data framework
158 // equal and null selected objects mean the same: object is equal to context,
159 if (aContextBody->shape().get() &&
160 (aContextBody->shape()->isEqual(theSubShape) || !theSubShape.get())) {
161 aSelLab.ForgetAllAttributes(true);
162 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
164 selectBody(aContextBody, theSubShape);
166 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
167 ResultConstructionPtr aContextConstruction =
168 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
169 aSelLab.ForgetAllAttributes(true); // to remove old selection data
170 std::shared_ptr<Model_ResultConstruction> aConstruction =
171 std::dynamic_pointer_cast<Model_ResultConstruction>(theContext);
172 std::shared_ptr<GeomAPI_Shape> aSubShape;
173 if (theSubShape.get() && !aContextConstruction->shape()->isEqual(theSubShape))
174 aSubShape = theSubShape; // the whole context
175 if (aConstruction->isInfinite()) {
176 // For correct naming selection, put the shape into the naming structure.
177 // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
178 TNaming_Builder aBuilder(aSelLab);
179 aBuilder.Generated(aContextConstruction->shape()->impl<TopoDS_Shape>());
181 int anIndex = aConstruction->select(theSubShape, owner()->document());
182 TDataStd_Integer::Set(aSelLab, anIndex);
183 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
184 aSelLab.ForgetAllAttributes(true);
185 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
186 selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
187 } else { // check the feature context: parent-Part of this feature should not be used
188 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
189 if (aFeatureContext.get()) {
190 if (owner()->document() != aFeatureContext->document()) {
191 aSelLab.ForgetAllAttributes(true);
192 myRef.setValue(ObjectPtr());
194 owner()->data()->blockSendAttributeUpdated(false);
200 owner()->data()->sendAttributeUpdated(this);
203 owner()->data()->blockSendAttributeUpdated(false);
208 void Model_AttributeSelection::setValueCenter(
209 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
210 const CenterType theCenterType, const bool theTemporarily)
212 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
213 if (theTemporarily) {
214 myTmpCenterType = theCenterType;
215 } else { // store in the data structure
216 TDF_Label aSelLab = selectionLabel();
217 switch(theCenterType) {
220 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
221 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
223 case ELLIPSE_FIRST_FOCUS:
225 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
226 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
228 case ELLIPSE_SECOND_FOCUS:
230 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
231 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
235 owner()->data()->sendAttributeUpdated(this);
239 void Model_AttributeSelection::selectValue(
240 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
243 std::shared_ptr<GeomAPI_Shape> aValue =
244 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
245 if (!aValue.get() || aType == NOT_CENTER) {
246 setValue(theSource->context(), aValue);
248 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
249 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
250 setValueCenter(theSource->context(), anEdge, aType);
254 void Model_AttributeSelection::removeTemporaryValues()
256 if (myTmpContext.get() || myTmpSubShape.get()) {
257 myTmpContext.reset();
258 myTmpSubShape.reset();
262 // returns the center of the edge: circular or elliptical
263 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
265 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
266 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
267 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
268 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
269 double aFirst, aLast;
270 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
271 if (!aCurve.IsNull()) {
272 TopoDS_Vertex aVertex;
273 BRep_Builder aBuilder;
274 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
275 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
276 if (!aCirc.IsNull()) {
277 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
280 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
281 if (!anEll.IsNull()) {
282 aBuilder.MakeVertex(aVertex,
283 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
284 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
287 if (!aVertex.IsNull()) {
288 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
289 aResult->setImpl(new TopoDS_Vertex(aVertex));
295 return theEdge; // no vertex, so, return the initial edge
298 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
300 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
301 return std::shared_ptr<GeomAPI_Shape>();
302 CenterType aType = NOT_CENTER;
303 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
304 return centerByEdge(aResult, aType);
307 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
309 theType = NOT_CENTER;
310 GeomShapePtr aResult;
311 if (myTmpContext.get() || myTmpSubShape.get()) {
312 theType = myTmpCenterType;
313 ResultConstructionPtr aResulConstruction =
314 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
315 if(aResulConstruction.get()) {
316 // it is just reference to construction.
317 return myTmpSubShape;
319 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
322 TDF_Label aSelLab = selectionLabel();
323 if (aSelLab.IsAttribute(kINVALID_SELECTION))
326 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
327 theType = CIRCLE_CENTER;
328 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
329 theType = ELLIPSE_FIRST_FOCUS;
330 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
331 theType = ELLIPSE_SECOND_FOCUS;
334 if (myRef.isInitialized()) {
335 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
336 ResultPtr aContext = context();
338 return aResult; // empty result
339 return aContext->shape();
341 if (aSelLab.IsAttribute(kPART_REF_ID)) {
342 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
343 if (!aPart.get() || !aPart->isActivated())
344 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
345 Handle(TDataStd_Integer) anIndex;
346 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
347 if (anIndex->Get()) { // special selection attribute was created, use it
348 return aPart->selectionValue(anIndex->Get());
349 } else { // face with name is already in the data model, so try to take it by name
350 Handle(TDataStd_Name) aName;
351 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
352 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
353 std::size_t aPartEnd = aSubShapeName.find('/');
354 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
355 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
357 std::string aType; // to reuse already existing selection the type is not needed
358 return aPart->shapeInPart(aNameInPart, aType, anIndex);
365 std::shared_ptr<Model_ResultConstruction> aConstr =
366 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
368 if (aConstr->isInfinite())
369 return aResult; // empty result
371 if (!aConstr.get()) { // for construction context, return empty result as usual even
372 // the whole feature is selected
373 FeaturePtr aFeature = contextFeature();
374 if (aFeature.get()) {
375 std::list<GeomShapePtr> allShapes;
376 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
377 for (; aRes != aFeature->results().cend(); aRes++) {
378 if (aRes->get() && !(*aRes)->isDisabled()) {
379 GeomShapePtr aShape = (*aRes)->shape();
380 if (aShape.get() && !aShape->isNull()) {
381 allShapes.push_back(aShape);
385 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
389 Handle(TNaming_NamedShape) aSelection;
390 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
391 TopoDS_Shape aSelShape = aSelection->Get();
392 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
393 aResult->setImpl(new TopoDS_Shape(aSelShape));
394 } else if (aConstr) { // simple construction element: just shape of this construction element
395 Handle(TDataStd_Integer) anIndex;
396 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
397 if (anIndex->Get() == 0) // it is just reference to construction, nothing is in value
399 return aConstr->shape(anIndex->Get(), owner()->document());
406 bool Model_AttributeSelection::isInvalid()
408 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
411 bool Model_AttributeSelection::isInitialized()
413 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
414 std::shared_ptr<GeomAPI_Shape> aResult;
415 if (myRef.isInitialized()) {
416 TDF_Label aSelLab = selectionLabel();
417 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
418 ResultPtr aContext = context();
419 return aContext.get() != NULL;
421 Handle(TNaming_NamedShape) aSelection;
422 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
423 return !aSelection->Get().IsNull();
424 } else { // for simple construction element: just shape of this construction element
425 std::shared_ptr<Model_ResultConstruction> aConstr =
426 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
428 Handle(TDataStd_Integer) anIndex;
429 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
430 // for the whole shape it may return null, so, if index exists, returns true
434 // for the whole feature, a feature object
435 FeaturePtr aFeat = contextFeature();
444 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
447 myIsInitialized = myRef.isInitialized();
451 void Model_AttributeSelection::setID(const std::string theID)
454 ModelAPI_AttributeSelection::setID(theID);
455 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
456 // TODO: check if parent list have geometrical selection flag.
457 myIsGeometricalSelection =
458 ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
461 ResultPtr Model_AttributeSelection::context()
463 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
466 if (myTmpContext.get() || myTmpSubShape.get()) {
470 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
471 // for parts there could be same-data result, so take the last enabled
473 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
474 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
475 for(int a = aSize - 1; a >= 0; a--) {
476 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
477 if(aPart.get() && aPart->data() == aResult->data()) {
478 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
479 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
480 // check that this result is not this-feature result (it is forbidden t oselect itself)
481 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
487 } else { // if feature - construction is selected, it has only one result, return this result
488 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
489 if (aFeature.get() && aFeature->results().size() == 1 &&
490 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
491 return aFeature->firstResult();
496 FeaturePtr Model_AttributeSelection::contextFeature() {
497 if (myTmpContext.get() || myTmpSubShape.get()) {
498 return FeaturePtr(); // feature can not be selected temporarily
500 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
502 ObjectPtr Model_AttributeSelection::contextObject() {
503 FeaturePtr aRes = contextFeature();
510 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
512 ModelAPI_AttributeSelection::setObject(theObject);
513 myRef.setObject(theObject);
516 TDF_LabelMap& Model_AttributeSelection::scope()
518 if (myScope.IsEmpty()) { // create a new scope if not yet done
519 // gets all features with named shapes that are before this feature label (before in history)
520 DocumentPtr aMyDoc = owner()->document();
521 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
522 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
523 bool aMePassed = false;
524 CompositeFeaturePtr aComposite =
525 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(owner());
526 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
527 CompositeFeaturePtr aCompositeOwner, aCompositeOwnerOwner;
528 if (aFeature.get()) {
529 aCompositeOwner = ModelAPI_Tools::compositeOwner(aFeature);
530 if (aCompositeOwner.get()) {
531 aCompositeOwnerOwner = ModelAPI_Tools::compositeOwner(aCompositeOwner);
534 // for group Scope is not limitet: this is always up to date objects
535 // this causes problem in galeries.py
536 //bool isGroup = aFeature.get() && aFeature->getKind() == "Group";
537 for(; aFIter != allFeatures.end(); aFIter++) {
538 if (*aFIter == owner()) { // the left features are created later (except subs of composite)
542 //if (isGroup) aMePassed = false;
543 bool isInScope = !aMePassed;
544 if (!isInScope && aComposite.get()) {
545 // try to add sub-elements of composite if this is composite
546 if (aComposite->isSub(*aFIter))
549 // remove the composite-owner of this feature (sketch in extrusion-cut)
550 if (isInScope && (aCompositeOwner == *aFIter || aCompositeOwnerOwner == *aFIter))
553 if (isInScope && aFIter->get() && (*aFIter)->data()->isValid()) {
554 TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
555 (*aFIter)->data())->label().Father();
556 TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), true);
557 for(; aNSIter.More(); aNSIter.Next()) {
558 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
559 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
560 myScope.Add(aNS->Label());
565 // also add all naming labels created for external constructions
566 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(aMyDoc);
567 TDF_Label anExtDocLab = aDoc->extConstructionsLabel();
568 TDF_ChildIDIterator aNSIter(anExtDocLab, TNaming_NamedShape::GetID(), true);
569 for(; aNSIter.More(); aNSIter.Next()) {
570 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
571 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
572 myScope.Add(aNS->Label());
579 /// Sets the invalid flag if flag is false, or removes it if "true"
581 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
583 theLab.ForgetAttribute(kINVALID_SELECTION);
585 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
590 void Model_AttributeSelection::split(
591 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
593 TopTools_ListOfShape aSubs;
594 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
595 if (!anExplorer.Value().IsNull() &&
596 anExplorer.Value().ShapeType() == theType) {
597 aSubs.Append(anExplorer.Value());
598 } else { // invalid case; bad result shape, so, impossible to split easily
603 if (aSubs.Extent() > 1) { // ok to split
604 TopTools_ListIteratorOfListOfShape aSub(aSubs);
605 GeomShapePtr aSubSh(new GeomAPI_Shape);
606 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
607 setValue(theContext, aSubSh);
608 for(aSub.Next(); aSub.More(); aSub.Next()) {
609 GeomShapePtr aSubSh(new GeomAPI_Shape);
610 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
611 myParent->append(theContext, aSubSh);
616 bool Model_AttributeSelection::update()
618 FeaturePtr aContextFeature = contextFeature();
619 if (aContextFeature.get()) {
622 TDF_Label aSelLab = selectionLabel();
623 ResultPtr aContext = context();
625 return setInvalidIfFalse(aSelLab, false);
626 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
627 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
630 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
631 std::shared_ptr<GeomAPI_Shape> aNoSelection;
632 bool aResult = selectPart(aContext, aNoSelection, true);
633 aResult = setInvalidIfFalse(aSelLab, aResult);
635 owner()->data()->sendAttributeUpdated(this);
640 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
641 // body: just a named shape, use selection mechanism from OCCT
642 TNaming_Selector aSelector(aSelLab);
643 TopoDS_Shape anOldShape;
644 if (!aSelector.NamedShape().IsNull()) {
645 anOldShape = aSelector.NamedShape()->Get();
647 bool aResult = aSelector.Solve(scope()) == Standard_True;
648 // must be before sending of updated attribute (1556)
649 aResult = setInvalidIfFalse(aSelLab, aResult);
650 TopoDS_Shape aNewShape;
651 if (!aSelector.NamedShape().IsNull()) {
652 aNewShape = aSelector.NamedShape()->Get();
654 if (anOldShape.IsNull() || aNewShape.IsNull() ||
655 !anOldShape.IsEqual(aSelector.NamedShape()->Get())) {
656 // shape type shoud not not changed: if shape becomes compound of such shapes, then split
657 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
658 anOldShape.ShapeType() != aNewShape.ShapeType() &&
659 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
661 split(aContext, aNewShape, anOldShape.ShapeType());
663 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
668 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
669 Handle(TDataStd_Integer) anIndex;
670 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
671 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
672 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
673 bool aModified = true;
674 bool aValid = aConstructionContext->update(anIndex->Get(), owner()->document(), aModified);
675 setInvalidIfFalse(aSelLab, aValid);
676 if (aConstructionContext->isInfinite()) {
677 // Update the selected shape.
678 TNaming_Builder aBuilder(aSelLab);
679 aBuilder.Generated(aConstructionContext->shape()->impl<TopoDS_Shape>());
682 owner()->data()->sendAttributeUpdated(this);
686 return setInvalidIfFalse(aSelLab, false); // unknown case
689 void Model_AttributeSelection::selectBody(
690 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
692 // perform the selection
693 TNaming_Selector aSel(selectionLabel());
694 TopoDS_Shape aContext;
696 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);//myRef.value()
698 aContext = aBody->shape()->impl<TopoDS_Shape>();
701 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
703 aContext = aResult->shape()->impl<TopoDS_Shape>();
705 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
710 // with "recover" feature the selected context may be not up to date (issue 1710)
711 Handle(TNaming_NamedShape) aResult;
712 TDF_Label aSelLab = selectionLabel();
713 TopoDS_Shape aNewContext = aContext;
714 bool isUpdated = true;
715 while(!aNewContext.IsNull() && isUpdated) {
716 // searching for the very last shape that was produced from this one
718 if (!TNaming_Tool::HasLabel(aSelLab, aNewContext))
719 // to avoid crash of TNaming_SameShapeIterator if pure shape does not exists
721 for(TNaming_SameShapeIterator anIter(aNewContext, aSelLab); anIter.More(); anIter.Next()) {
722 TDF_Label aNSLab = anIter.Label();
723 if (!scope().Contains(aNSLab))
725 Handle(TNaming_NamedShape) aNS;
726 if (aNSLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
727 for(TNaming_Iterator aShapesIter(aNS); aShapesIter.More(); aShapesIter.Next()) {
728 if (aShapesIter.Evolution() == TNaming_SELECTED)
729 continue; // don't use the selection evolution
730 if (!aShapesIter.OldShape().IsNull() && aShapesIter.OldShape().IsSame(aNewContext)) {
731 // found the original shape
732 aNewContext = aShapesIter.NewShape(); // go to the newer shape
740 if (aNewContext.IsNull()) { // a context is already deleted
741 setInvalidIfFalse(aSelLab, false);
742 Events_InfoMessage("Model_AttributeSelection", "Failed to select shape already deleted").send();
746 TopoDS_Shape aNewSub = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
747 if (!aNewSub.IsEqual(aContext)) { // searching for subshape in the new context
748 bool isFound = false;
749 TopExp_Explorer anExp(aNewContext, aNewSub.ShapeType());
750 for(; anExp.More(); anExp.Next()) {
751 if (anExp.Current().IsSame(aNewSub)) {
756 if (!isFound) { // sub-shape is not found in the up-to-date instance of the context shape
757 // if context is sub-result of compound/compsolid, selection of sub-shape better propagate to
758 // the main result (which is may be modified); the case is in 1799
759 ResultBodyPtr aMain = ModelAPI_Tools::bodyOwner(theContext);
760 while(ModelAPI_Tools::bodyOwner(aMain).get())
761 aMain = ModelAPI_Tools::bodyOwner(theContext);
763 selectBody(aMain, theSubShape);
766 setInvalidIfFalse(aSelLab, false);
767 Events_InfoMessage("Model_AttributeSelection",
768 "Failed to select sub-shape already modified").send();
773 /// fix for issue 411: result modified shapes must not participate in this selection mechanism
774 if (!aContext.IsNull()) {
775 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
776 bool aEraseResults = false;
777 if (aFeatureOwner.get()) {
778 aEraseResults = !aFeatureOwner->results().empty();
779 if (aEraseResults) // erase results without flash deleted and redisplay: do it after Select
780 aFeatureOwner->removeResults(0, false, false);
782 aSel.Select(aNewSub, aNewContext, myIsGeometricalSelection);
783 // face may become divided after the model update, so, new labels may be added to the scope
786 if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
787 static Events_Loop* aLoop = Events_Loop::loop();
788 static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
789 aLoop->flush(kDeletedEvent);
794 bool Model_AttributeSelection::selectPart(
795 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
796 const bool theUpdate)
798 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
799 if (!aPart.get() || !aPart->isActivated())
800 return true; // postponed naming
802 Handle(TDataStd_Integer) anIndex;
803 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
804 // by internal selection
805 if (anIndex->Get() > 0) {
806 // update the selection by index
807 return aPart->updateInPart(anIndex->Get());
809 return true; // nothing to do, referencing just by name
812 return true; // nothing to do, referencing just by name
814 // store the shape (in case part is not loaded it should be useful
816 std::string aName = theContext->data()->name();
817 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
818 aShape = theContext->shape()->impl<TopoDS_Shape>();
820 aShape = theSubShape->impl<TopoDS_Shape>();
822 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
823 TDataStd_Integer::Set(selectionLabel(), anIndex);
825 TNaming_Builder aBuilder(selectionLabel());
826 aBuilder.Select(aShape, aShape);
827 // identify by name in the part
828 TDataStd_Name::Set(selectionLabel(), aName.c_str());
829 return !aName.empty();
832 TDF_Label Model_AttributeSelection::selectionLabel()
834 return myRef.myRef->Label().FindChild(1);
837 /// prefixes of the shape names with centers defined
838 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
840 /// returns the map that contains all possible prefixes of the center-names
841 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
843 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
844 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
845 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
846 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
848 return kCENTERS_PREFIX;
851 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
853 std::string aName("");
854 if(!this->isInitialized())
855 return !theDefaultName.empty() ? theDefaultName : aName;
857 CenterType aCenterType = NOT_CENTER;
858 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
859 ResultPtr aCont = context();
862 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
863 // selection of a full feature
864 FeaturePtr aFeatureCont = contextFeature();
865 if (aFeatureCont.get()) {
866 return kWHOLE_FEATURE + aFeatureCont->name();
868 // in case of selection of removed result
872 if (myIsGeometricalSelection
874 && aSubSh->isCompound())
876 GeomAPI_ShapeIterator anIt(aSubSh);
877 aSubSh = anIt.current();
880 Model_SelectionNaming aSelNaming(selectionLabel());
881 std::string aResult = aSelNaming.namingName(
882 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
883 if (aCenterType != NOT_CENTER) {
884 aResult += centersMap()[aCenterType];
889 // returns the center type and modifies the shape name if this name is center-name
890 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
892 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
893 centersMap().begin();
894 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
895 std::size_t aFound = theShapeName.find(aPrefixIter->second);
896 if (aFound != std::string::npos &&
897 aFound == theShapeName.size() - aPrefixIter->second.size()) {
898 theShapeName = theShapeName.substr(0, aFound);
899 return aPrefixIter->first;
902 return ModelAPI_AttributeSelection::NOT_CENTER;
905 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
906 void Model_AttributeSelection::selectSubShape(
907 const std::string& theType, const std::string& theSubShapeName)
909 if(theSubShapeName.empty() || theType.empty()) return;
911 std::string aSubShapeName = theSubShapeName;
912 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
913 centerTypeByName(aSubShapeName) : NOT_CENTER;
914 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
916 // first iteration is selection by name without center prefix, second - in case of problem,
917 // try with initial name
918 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
919 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
920 aSubShapeName = theSubShapeName;
921 aCenterType = NOT_CENTER;
923 } else if (aUseCenter != 1) continue;
925 // check this is Part-name: 2 delimiters in the name
926 std::size_t aPartEnd = aSubShapeName.find('/');
927 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
928 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
929 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
930 if (aFound.get()) { // found such part, so asking it for the name
931 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
932 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
934 std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, aType, anIndex);
935 if (aSelected.get()) {
936 if (aCenterType != NOT_CENTER) {
937 if (!aSelected->isEdge())
939 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
940 setValueCenter(aPart, aSelectedEdge, aCenterType);
942 setValue(aPart, aSelected);
943 TDataStd_Integer::Set(selectionLabel(), anIndex);
949 std::shared_ptr<Model_Document> aDoc =
950 std::dynamic_pointer_cast<Model_Document>(owner()->document());
951 // check this is a whole feature context
952 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
953 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
954 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
955 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
957 static const GeomShapePtr anEmptyShape;
958 setValue(anObj, anEmptyShape);
963 Model_SelectionNaming aSelNaming(selectionLabel());
964 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
966 if (aSelNaming.selectSubShape(aType, aSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
967 // try to find the last context to find the up to date shape
968 if (aCont->shape().get() && !aCont->shape()->isNull() &&
969 aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
970 const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
971 if (!aConShape.IsNull()) {
972 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
974 aNS = TNaming_Tool::CurrentNamedShape(aNS);
975 if (!aNS.IsNull() && scope().Contains(aNS->Label())) { // scope check is for 2228
976 TDF_Label aLab = aNS->Label();
977 if (aLab.Depth() % 2 == 0)
978 aLab = aLab.Father();
979 ObjectPtr anObj = aDoc->objects()->object(aLab);
980 while(!anObj.get() && aLab.Depth() > 5) {
981 aLab = aLab.Father().Father();
982 anObj = aDoc->objects()->object(aLab);
986 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
994 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
995 if (aCont.get() && aShapeToBeSelected.get()) {
996 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCont);
997 if (aComp && aComp->numberOfSubs()) {
998 std::list<ResultPtr> allSubs;
999 ModelAPI_Tools::allSubs(aComp, allSubs);
1000 std::list<ResultPtr>::iterator aS = allSubs.begin();
1001 for(; aS != allSubs.end(); aS++) {
1002 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
1003 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
1004 aSub->shape()->isSubShape(aShapeToBeSelected)) {
1011 // try to find the latest active result that must be used instead of the selected
1012 // to set the active context (like in GUI selection), not concealed one
1013 bool aFindNewContext = true;
1014 while(aFindNewContext && aCont.get()) {
1015 aFindNewContext = false;
1016 // take references to all results: root one, any sub
1017 ResultBodyPtr aCompContext = ModelAPI_Tools::bodyOwner(aCont, true);
1018 std::list<ResultPtr> allRes;
1019 if (aCompContext.get()) {
1020 ModelAPI_Tools::allSubs(aCompContext, allRes);
1021 allRes.push_back(aCompContext);
1023 allRes.push_back(aCont);
1025 for(std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1026 ResultPtr aResCont = *aSub;
1027 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1028 // only lower and higher level subs are counted
1029 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1031 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1032 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1033 for(; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1034 if (!aRef->get() || !(*aRef)->owner().get())
1036 // concealed attribute only
1037 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1038 if (!ModelAPI_Session::get()->validators()->isConcealed(
1039 aRefFeat->getKind(), (*aRef)->id()))
1041 // search the feature result that contains sub-shape selected
1042 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1044 // take all sub-results or one result
1045 std::list<ResultPtr> aRefFeatResults;
1046 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1047 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1048 for(; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1049 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1050 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1051 aResults.push_back(aBody);
1053 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1054 for(; aResIter != aResults.end(); aResIter++) {
1055 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1057 GeomShapePtr aShape = (*aResIter)->shape();
1058 GeomShapePtr aSelectedShape =
1059 aShapeToBeSelected.get() ? aShapeToBeSelected : aCont->shape();
1060 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
1061 aCont = *aResIter; // found new context (produced from this) with same subshape
1062 aFindNewContext = true; // continue searching futher
1070 if (aCenterType != NOT_CENTER) {
1071 if (!aShapeToBeSelected->isEdge())
1073 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
1074 setValueCenter(aCont, aSelectedEdge, aCenterType);
1076 setValue(aCont, aShapeToBeSelected);
1081 TDF_Label aSelLab = selectionLabel();
1082 setInvalidIfFalse(aSelLab, false);
1086 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1087 const GeomPointPtr& thePoint)
1089 if (theType.empty() || !thePoint)
1092 // list of parent features
1093 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
1094 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
1096 int aSelectionIndex = 0;
1097 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
1098 if (aType == GeomAPI_Shape::SHAPE) {
1099 // possibly, the string consists of the type and the index,
1100 // thus, try to separate them
1101 size_t aUndersporePos = theType.find_first_of('_');
1102 if (aUndersporePos != std::string::npos)
1103 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
1105 if (aType != GeomAPI_Shape::SHAPE) {
1106 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
1107 aChar != theType.end(); ++aChar) {
1108 if (std::isdigit(*aChar))
1109 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
1111 aSelectionIndex = 1;
1115 aSelectionIndex -= 1;
1119 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
1121 // collect features from PartSet and the current part
1122 SessionPtr aSession = ModelAPI_Session::get();
1123 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
1124 if (aSession->moduleDocument() != owner()->document()) {
1125 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
1126 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
1128 // Process results of all features from the last to the first
1129 // to find appropriate sub-shape
1130 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
1131 anIt != aFeatures.rend(); ++anIt) {
1132 // selection cannot be linked to the parent features
1133 if (aParents.find(*anIt) != aParents.end())
1135 // check the feature is a part of composite feature (like sketch elements),
1136 // then do not process it, it will be processed in scope of composite feature
1137 bool isSubOfComposite = false;
1138 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
1139 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
1140 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
1141 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
1142 CompositeFeaturePtr aCompFeature =
1143 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
1144 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
1146 if (isSubOfComposite)
1149 // process results of the current feature to find appropriate sub-shape
1150 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
1151 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
1152 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1153 ++anApIt; // skip this shape, because one of the previous is selected
1155 if (anApIt != anAppropriate.end()) {
1156 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1157 setValue(anApIt->myResult, anApIt->mySubshape);
1159 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1160 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1166 TDF_Label aSelLab = selectionLabel();
1167 setInvalidIfFalse(aSelLab, false);
1171 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1172 const std::string& theContextName, const int theIndex)
1174 // selection of context by name
1175 //std::string aNamingContextName = theContextName + "/";
1176 //selectSubShape(theType, aNamingContextName);
1177 std::shared_ptr<Model_Document> aDoc =
1178 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1180 bool aUnique = true;
1181 std::string aContextName = theContextName;
1182 std::string anEmptySub = "";
1183 ResultPtr aContext = aDoc->findByName(aContextName, anEmptySub, aUnique);
1184 //ResultPtr aContext = context();
1185 if (aContext.get()) {
1186 GeomShapePtr aContShape = aContext->shape();
1187 if (aContShape.get()) {
1188 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1189 GeomShapePtr aValue = aNExp.shape(theIndex);
1191 setValue(aContext, aValue);
1197 int Model_AttributeSelection::Id()
1200 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1201 ResultPtr aContextRes = context();
1202 // support for compsolids:
1203 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1204 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1206 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1209 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1210 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1211 // searching for the latest main shape
1212 if (aSelection && !aSelection->isNull() && aContext && !aContext->isNull())
1214 std::shared_ptr<Model_Document> aDoc =
1215 std::dynamic_pointer_cast<Model_Document>(context()->document());
1217 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1218 if (!aNS.IsNull()) {
1219 aMainShape = TNaming_Tool::CurrentShape(aNS);
1223 TopTools_IndexedMapOfShape aSubShapesMap;
1224 TopExp::MapShapes(aMainShape, aSubShapesMap);
1225 anID = aSubShapesMap.FindIndex(aSubShape);
1230 void Model_AttributeSelection::setId(int theID)
1232 std::shared_ptr<GeomAPI_Shape> aSelection;
1234 ResultPtr aContextRes = context();
1235 // support for compsolids:
1236 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1237 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1239 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1241 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1242 // searching for the latest main shape
1243 if (theID > 0 && aContext && !aContext->isNull())
1245 std::shared_ptr<Model_Document> aDoc =
1246 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1248 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1249 if (!aNS.IsNull()) {
1250 aMainShape = TNaming_Tool::CurrentShape(aNS);
1254 TopTools_IndexedMapOfShape aSubShapesMap;
1255 TopExp::MapShapes(aMainShape, aSubShapesMap);
1256 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1258 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1259 aResult->setImpl(new TopoDS_Shape(aSelShape));
1261 aSelection = aResult;
1264 setValue(aContextRes, aSelection);
1267 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1269 std::string aResult;
1270 if (owner()->document() != theContext->document()) {
1271 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1272 aResult = theContext->document()->kind() + "/";
1274 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1275 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1276 if (aDocRes.get()) {
1277 aResult = aDocRes->data()->name() + "/";
1281 aResult += theContext->data()->name();
1285 void Model_AttributeSelection::computeValues(
1286 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1287 TopTools_ListOfShape& theShapes)
1289 bool aWasWholeContext = theValShape.IsNull();
1290 if (aWasWholeContext) {
1291 //theShapes.Append(theValShape);
1293 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1295 //TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1296 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1297 //if (anOldContShape.IsSame(theValShape)) { // full context shape substituted by new full context
1298 //theShapes.Append(aNewContShape);
1301 // if a new value is unchanged in the new context, do nothing: value is correct
1302 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1303 for(; aSubExp.More(); aSubExp.Next()) {
1304 if (aSubExp.Current().IsSame(theValShape)) {
1305 theShapes.Append(theValShape);
1309 // if new context becomes compsolid, the resulting sub may be in sub-solids
1310 std::list<ResultPtr> aNewToIterate;
1311 aNewToIterate.push_back(theNewContext);
1312 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1314 std::list<ResultPtr> allNewContextSubs;
1315 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1316 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1317 for(; aSub != allNewContextSubs.end(); aSub++) {
1318 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1319 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1320 aNewToIterate.push_back(aBody);
1324 // first iteration: searching for the whole shape appearance (like face of the box)
1325 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1326 int aToFindPart = 0;
1327 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1328 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1329 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1330 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1331 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1332 std::shared_ptr<Model_Data> aNewData =
1333 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1334 TDF_Label aNewLab = aNewData->label();
1335 // searching for produced sub-shape fully on some label
1336 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID(), Standard_True);
1337 for(; aNSIter.More(); aNSIter.Next()) {
1338 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1339 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1340 if (aToFindPart == 0) { // search shape is fully inside
1341 if (aPairIter.OldShape().IsSame(theValShape)) {
1342 if (aPairIter.NewShape().IsNull()) {// value was removed
1346 theShapes.Append(aPairIter.NewShape());
1348 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1349 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1350 for(; anExp.More(); anExp.Next()) {
1351 if (anExp.Current().IsSame(theValShape)) { // found a new container
1352 if (aPairIter.NewShape().IsNull()) {// value was removed
1356 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1357 anOlds.Add(aPairIter.OldShape());
1366 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1367 // map of sub-shapes -> number of occurences of these shapes in containers
1368 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1369 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1370 for(; aContIter.More(); aContIter.Next()) {
1371 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1372 for(; aSubExp.More(); aSubExp.Next()) {
1373 if (!aSubs.IsBound(aSubExp.Current())) {
1374 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1376 // store old to know how many olds produced this shape
1377 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1380 // if sub is appeared same times in containers as the number of old shapes that contain it
1381 int aCountInOld = anOlds.Size();
1382 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1384 for(; aSubsIter.More(); aSubsIter.Next()) {
1385 if (aSubsIter.Value().Size() == aCountInOld) {
1386 theShapes.Append(aSubsIter.Key());
1390 if (theShapes.IsEmpty()) { // nothing was changed
1391 theShapes.Append(aWasWholeContext ? TopoDS_Shape() : theValShape);
1395 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1396 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1397 TDF_Label theAccessLabel,
1398 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1400 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1401 TopTools_ListOfShape aResContShapes;
1402 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1403 TopTools_ListOfShape aContextList;
1404 aContextList.Append(theContShape);
1405 if (theContext.get()) {
1406 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1407 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1408 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1410 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1411 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1412 for(; aModifIter.More(); aModifIter.Next()) {
1413 TDF_Label anObjLab = aModifIter.Label().Father();
1414 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1415 (theDoc->objects()->object(anObjLab));
1416 if (!aModifierObj.get()) {
1417 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1418 if (!anObjLab.IsRoot())
1419 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1420 (theDoc->objects()->object(anObjLab.Father()));
1421 if (!aModifierObj.get())
1424 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1425 if (!aModifierFeat.get())
1427 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1428 if (aModifierFeat == aThisFeature || theDoc->objects()->isLater(aModifierFeat, aThisFeature))
1429 continue; // the modifier feature is later than this, so, should not be used
1430 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1431 if (aCurrentModifierFeat == aModifierFeat ||
1432 theDoc->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
1433 continue; // the current modifier is later than the found, so, useless
1434 Handle(TNaming_NamedShape) aNewNS;
1435 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1436 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1437 aResults.insert(aModifierObj);
1438 //TNaming_Iterator aPairIter(aNewNS);
1439 //aResContShapes.Append(aPairIter.NewShape());
1440 aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
1441 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1442 aResults.insert(ResultPtr());
1443 } else { // not-precessed modification => don't support it
1448 if (aResults.empty())
1449 return false; // no modifications found, must stay the same
1450 // iterate all results to find futher modifications
1451 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1452 for(; aResIter != aResults.end(); aResIter++) {
1453 if (aResIter->get() != NULL) {
1454 // compute new values by two contextes: the old and the new
1455 TopTools_ListOfShape aValShapes;
1456 computeValues(theContext, *aResIter, theValShape, aValShapes);
1458 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1459 for(; aNewVal.More(); aNewVal.Next()) {
1460 std::list<ResultPtr> aNewRes;
1461 TopTools_ListOfShape aNewUpdatedVal;
1462 TopoDS_Shape aNewValSh = aNewVal.Value();
1463 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1464 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1465 aNewValSh.Nullify();
1466 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1467 theAccessLabel, aNewRes, aNewUpdatedVal))
1469 // appeand new results instead of the current ones
1470 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1471 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1472 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1473 theResults.push_back(*aNewIter);
1474 theValShapes.Append(aNewUpdVal.Value());
1476 } else { // the current result is good
1477 theResults.push_back(*aResIter);
1478 theValShapes.Append(aNewValSh);
1483 return true; // theResults must be empty: everything is deleted
1486 void Model_AttributeSelection::updateInHistory()
1488 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1489 // only bodies and parts may be modified later in the history, don't do anything otherwise
1490 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1491 aContext->groupName() != ModelAPI_ResultPart::group()))
1493 std::shared_ptr<Model_Document> aDoc =
1494 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1495 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1496 if (!aContData.get() || !aContData->isValid())
1498 TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
1499 Handle(TNaming_NamedShape) aContNS;
1500 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1501 bool aFoundNewContext = true;
1502 ResultPtr aNewContext = aContext;
1503 while(aFoundNewContext) {
1504 aFoundNewContext = false;
1505 // parts have no shape in result, so, trace references using the Part info
1506 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1507 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1508 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1509 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1510 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1511 for(; aRef != aRefs.end(); aRef++) {
1512 // to avoid detection of part changes by local selection only
1513 AttributeSelectionPtr aSel =
1514 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1515 if (aSel.get() && !aSel->value()->isSame(aSel->context()->shape()))
1518 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1519 if (aRefFeat.get() && aRefFeat != owner()) {
1520 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1521 if (aDoc->objects()->isLater(aThisFeature, aRefFeat)) { // found better feature
1522 aFoundNewContext = true;
1523 aNewContext = aRefFeat->firstResult();
1530 if (aNewContext != aContext) {
1531 setValue(aNewContext, value());
1535 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1536 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1537 // iterate the context shape modifications in order to find a feature that is upper in history
1538 // that this one and is really modifies the referenced result to refer to it
1539 ResultPtr aModifierResFound;
1540 TNaming_Iterator aPairIter(aContNS);
1541 if (!aPairIter.More())
1543 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1544 bool anIterate = true;
1545 // trying to update also the sub-shape selected
1546 GeomShapePtr aSubShape = value();
1547 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1549 TopoDS_Shape aValShape;
1550 if (aSubShape.get()) {
1551 aValShape = aSubShape->impl<TopoDS_Shape>();
1554 std::list<ResultPtr> aNewContexts;
1555 TopTools_ListOfShape aValShapes;
1556 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1558 // update scope to reset to a new one
1561 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1562 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1563 if (aNewCont == aNewContexts.end()) { // all results were deleted
1564 ResultPtr anEmptyContext;
1565 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1566 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1570 GeomShapePtr aValueShape;
1571 if (!aNewValues.Value().IsNull()) {
1572 aValueShape = std::make_shared<GeomAPI_Shape>();
1573 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1575 setValue(*aNewCont, aValueShape);
1576 // if there are more than one result, put them by "append" into "parent" list
1578 for(aNewCont++, aNewValues.Next(); aNewCont != aNewContexts.end();
1579 aNewCont++, aNewValues.Next()) {
1580 GeomShapePtr aValueShape;
1581 if (!aNewValues.Value().IsNull()) {
1582 aValueShape = std::make_shared<GeomAPI_Shape>();
1583 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1586 // Check that list has the same type of shape selection before adding.
1587 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1588 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1589 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1590 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1592 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1593 if (aValueShape.get()) {
1594 aShapeShapeType = aValueShape->shapeType();
1596 (*aNewCont)->shape()->shapeType();
1599 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1603 myParent->append(*aNewCont, aValueShape);
1609 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1611 myParent = theParent;