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_Objects.h>
27 #include <Model_AttributeSelectionList.h>
28 #include <Model_ResultConstruction.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_ResultConstruction.h>
33 #include <ModelAPI_ResultPart.h>
34 #include <ModelAPI_CompositeFeature.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelGeomAlgo_Shape.h>
39 #include <Events_InfoMessage.h>
40 #include <GeomAPI_Edge.h>
41 #include <GeomAPI_Pnt.h>
42 #include <GeomAPI_Vertex.h>
43 #include <GeomAPI_ShapeExplorer.h>
44 #include <GeomAlgoAPI_CompoundBuilder.h>
45 #include <GeomAlgoAPI_NExplode.h>
46 #include <Selector_Selector.h>
48 #include <TNaming_NamedShape.hxx>
49 #include <TNaming_Tool.hxx>
50 #include <TNaming_Builder.hxx>
51 #include <TNaming_SameShapeIterator.hxx>
52 #include <TNaming_Iterator.hxx>
53 #include <TDataStd_Integer.hxx>
54 #include <TDataStd_UAttribute.hxx>
55 #include <TDataStd_Name.hxx>
56 #include <TopTools_ListOfShape.hxx>
57 #include <TopTools_DataMapOfShapeShape.hxx>
58 #include <TopTools_MapOfShape.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <BRep_Tool.hxx>
62 #include <TopoDS_Edge.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");
92 // reference to the external sketch face
93 Standard_GUID kEXT_SKETCH_FACE("ba32aa31-bde7-422f-80b4-79c757c77b49");
94 // reference to the external sketch edge
95 Standard_GUID kEXT_SKETCH_EDGE("ba32aa31-bde7-422f-80b4-79c757c77b48");
96 // reference to the external sketch vertex
97 Standard_GUID kEXT_SKETCH_VERT("ba32aa31-bde7-422f-80b4-79c757c77b47");
99 // prefix for the whole feature context identification
100 const static std::string kWHOLE_FEATURE = "all-in-";
102 // on this label is stored:
103 // TNaming_NamedShape - selected shape
104 // TNaming_Naming - topological selection information (for the body)
105 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
106 // TDataStd_Integer - type of the selected shape (for construction)
107 // TDF_Reference - from ReferenceAttribute, the context
108 bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
109 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
111 if (theTemporarily &&
112 (!theContext.get() || theContext->groupName() != ModelAPI_Feature::group())) {
113 // just keep the stored without DF update
114 myTmpContext = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
115 myTmpSubShape = theSubShape;
116 owner()->data()->sendAttributeUpdated(this);
119 myTmpContext.reset();
120 myTmpSubShape.reset();
121 myTmpCenterType = NOT_CENTER;
125 const std::shared_ptr<GeomAPI_Shape>& anOldShape = internalValue(aType);
126 bool isOldContext = theContext == myRef.value();
127 bool isOldShape = isOldContext &&
128 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
129 if (isOldShape) return false; // shape is the same, so context is also unchanged
130 bool aToUnblock = false;
131 // update the referenced object if needed
133 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
134 myRef.setValue(theContext);
137 // do noth use naming if selected shape is result shape itself, but not sub-shape
138 TDF_Label aSelLab = selectionLabel();
139 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
140 aSelLab.ForgetAttribute(kINVALID_SELECTION);
141 aSelLab.ForgetAttribute(kCIRCLE_CENTER);
142 aSelLab.ForgetAttribute(kELLIPSE_CENTER1);
143 aSelLab.ForgetAttribute(kELLIPSE_CENTER2);
144 aSelLab.ForgetAttribute(kEXT_SKETCH_FACE);
145 aSelLab.ForgetAttribute(kEXT_SKETCH_EDGE);
146 aSelLab.ForgetAttribute(kEXT_SKETCH_VERT);
148 bool isDegeneratedEdge = false;
149 // do not use the degenerated edge as a shape, a null context and shape is used in the case
150 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
151 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
152 if (aSubShape.ShapeType() == TopAbs_EDGE)
153 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
155 if (!theContext.get() || isDegeneratedEdge) {
156 // to keep the reference attribute label
157 TDF_Label aRefLab = myRef.myRef->Label();
158 aSelLab.ForgetAllAttributes(true);
159 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
161 owner()->data()->blockSendAttributeUpdated(false);
164 bool isSelectBody = theContext->groupName() == ModelAPI_ResultBody::group();
166 ResultConstructionPtr aContextConstruction =
167 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
168 isSelectBody = aContextConstruction.get() && !aContextConstruction->isInfinite();
171 ResultPtr aContextResult = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
172 GeomShapePtr aContextShape = aContextResult->shape();
173 // do not select the whole shape for body:it is already must be in the data framework
174 // equal and null selected objects mean the same: object is equal to context,
175 if (aContextShape.get() && (aContextShape->isEqual(theSubShape) || !theSubShape.get())) {
176 aSelLab.ForgetAllAttributes(true);
177 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
179 selectBody(aContextResult, theSubShape);
181 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
182 aSelLab.ForgetAllAttributes(true); // to remove old selection data
183 std::shared_ptr<Model_ResultConstruction> aConstruction =
184 std::dynamic_pointer_cast<Model_ResultConstruction>(theContext);
185 std::shared_ptr<GeomAPI_Shape> aSubShape;
186 if (theSubShape.get() && !aConstruction->shape()->isEqual(theSubShape))
187 aSubShape = theSubShape; // the whole context
188 if (aConstruction->isInfinite()) {
189 // For correct naming selection, put the shape into the naming structure.
190 // It seems sub-shapes are not needed: only this shape is (and can be) selected.
191 TNaming_Builder aBuilder(aSelLab);
192 aBuilder.Generated(aConstruction->shape()->impl<TopoDS_Shape>());
194 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
195 aSelLab.ForgetAllAttributes(true);
196 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
197 selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
198 } else { // check the feature context: parent-Part of this feature should not be used
199 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
200 if (aFeatureContext.get()) {
201 if (owner()->document() != aFeatureContext->document()) {
202 aSelLab.ForgetAllAttributes(true);
203 myRef.setValue(ObjectPtr());
205 owner()->data()->blockSendAttributeUpdated(false);
211 owner()->data()->sendAttributeUpdated(this);
214 owner()->data()->blockSendAttributeUpdated(false);
219 void Model_AttributeSelection::setValueCenter(
220 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
221 const CenterType theCenterType, const bool theTemporarily)
223 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
224 if (theTemporarily) {
225 myTmpCenterType = theCenterType;
226 } else { // store in the data structure
227 TDF_Label aSelLab = selectionLabel();
228 switch(theCenterType) {
231 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
232 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
234 case ELLIPSE_FIRST_FOCUS:
236 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
237 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
239 case ELLIPSE_SECOND_FOCUS:
241 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
242 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
246 owner()->data()->sendAttributeUpdated(this);
250 void Model_AttributeSelection::selectValue(
251 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
254 std::shared_ptr<GeomAPI_Shape> aValue =
255 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
256 if (!aValue.get() || aType == NOT_CENTER) {
257 setValue(theSource->context(), aValue);
259 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
260 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
261 setValueCenter(theSource->context(), anEdge, aType);
265 void Model_AttributeSelection::removeTemporaryValues()
267 if (myTmpContext.get() || myTmpSubShape.get()) {
268 myTmpContext.reset();
269 myTmpSubShape.reset();
273 // returns the center of the edge: circular or elliptical
274 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
276 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
277 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
278 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
279 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
280 double aFirst, aLast;
281 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
282 if (!aCurve.IsNull()) {
283 TopoDS_Vertex aVertex;
284 BRep_Builder aBuilder;
285 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
286 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
287 if (!aCirc.IsNull()) {
288 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
291 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
292 if (!anEll.IsNull()) {
293 aBuilder.MakeVertex(aVertex,
294 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
295 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
298 if (!aVertex.IsNull()) {
299 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
300 aResult->setImpl(new TopoDS_Vertex(aVertex));
306 return theEdge; // no vertex, so, return the initial edge
309 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
311 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
312 return std::shared_ptr<GeomAPI_Shape>();
313 CenterType aType = NOT_CENTER;
314 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
315 return centerByEdge(aResult, aType);
318 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
320 theType = NOT_CENTER;
321 GeomShapePtr aResult;
322 if (myTmpContext.get() || myTmpSubShape.get()) {
323 theType = myTmpCenterType;
324 ResultConstructionPtr aResulConstruction =
325 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
326 if(aResulConstruction.get()) {
327 // it is just reference to construction.
328 return myTmpSubShape;
330 return myTmpSubShape.get() ? myTmpSubShape : myTmpContext->shape();
333 TDF_Label aSelLab = selectionLabel();
334 if (aSelLab.IsAttribute(kINVALID_SELECTION))
337 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
338 theType = CIRCLE_CENTER;
339 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
340 theType = ELLIPSE_FIRST_FOCUS;
341 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
342 theType = ELLIPSE_SECOND_FOCUS;
345 if (myRef.isInitialized()) {
346 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
347 ResultPtr aContext = context();
348 if (!aContext.get() || aContext->groupName() == ModelAPI_ResultConstruction::group())
349 return aResult; // empty result, for whole construction selection also
350 return aContext->shape();
352 if (aSelLab.IsAttribute(kPART_REF_ID)) {
353 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
354 if (!aPart.get() || !aPart->isActivated())
355 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
356 Handle(TDataStd_Integer) anIndex;
357 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
358 if (anIndex->Get()) { // special selection attribute was created, use it
359 return aPart->selectionValue(anIndex->Get());
360 } else { // face with name is already in the data model, so try to take it by name
361 Handle(TDataStd_Name) aName;
362 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
363 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
364 std::size_t aPartEnd = aSubShapeName.find('/');
365 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
366 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
368 std::string aType; // to reuse already existing selection the type is not needed
369 return aPart->shapeInPart(aNameInPart, aType, anIndex);
376 std::shared_ptr<Model_ResultConstruction> aConstr =
377 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
379 if (aConstr->isInfinite())
380 return aResult; // empty result
381 // external sketch face
382 Handle(TDataStd_Integer) anIndex;
383 if (aSelLab.FindAttribute(kEXT_SKETCH_FACE, anIndex)) {
384 return aConstr->face(anIndex->Get());
386 if (aSelLab.FindAttribute(kEXT_SKETCH_EDGE, anIndex) ||
387 aSelLab.FindAttribute(kEXT_SKETCH_VERT, anIndex)) {
388 bool isVert = anIndex->ID() == kEXT_SKETCH_VERT; // vertex is selected
389 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
390 aConstr->document()->feature(aConstr));
391 if (aComposite.get()) {
392 int aSubNum = anIndex->Get() % 1000000;
393 int aVertShape = (anIndex->Get() - aSubNum) / 1000000;
394 FeaturePtr aSubFeat = aComposite->subFeature(aSubNum);
395 if (aSubFeat.get()) {
396 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSubFeat->results();
397 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
398 for (; aRes != aResults.cend(); aRes++) {
399 ResultConstructionPtr aConstr =
400 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
401 if (aConstr->shape()) {
402 if (!isVert && aConstr->shape()->isEdge())
403 return aConstr->shape();
404 else if (isVert && aVertShape == 0 && aConstr->shape()->isVertex())
405 return aConstr->shape();
406 else if (isVert && aVertShape > 1 && aConstr->shape()->isEdge()) {
407 GeomAPI_ShapeExplorer anExp(aConstr->shape(), GeomAPI_Shape::VERTEX);
408 for(; anExp.more(); anExp.next()) {
410 return anExp.current();
420 if (!aConstr.get()) { // for construction context, return empty result as usual even
421 // the whole feature is selected
422 FeaturePtr aFeature = contextFeature();
423 if (aFeature.get()) {
424 std::list<GeomShapePtr> allShapes;
425 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
426 for (; aRes != aFeature->results().cend(); aRes++) {
427 if (aRes->get() && !(*aRes)->isDisabled()) {
428 GeomShapePtr aShape = (*aRes)->shape();
429 if (aShape.get() && !aShape->isNull()) {
430 allShapes.push_back(aShape);
434 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
438 Handle(TNaming_NamedShape) aSelection;
439 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
440 TopoDS_Shape aSelShape = aSelection->Get();
441 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
442 aResult->setImpl(new TopoDS_Shape(aSelShape));
443 } else if (aConstr) { // simple construction element: just shape of this construction element
444 aResult = aConstr->shape();
450 bool Model_AttributeSelection::isInvalid()
452 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
455 bool Model_AttributeSelection::isInitialized()
457 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
458 std::shared_ptr<GeomAPI_Shape> aResult;
459 if (myRef.isInitialized()) {
460 TDF_Label aSelLab = selectionLabel();
461 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
462 ResultPtr aContext = context();
463 return aContext.get() != NULL;
465 Handle(TNaming_NamedShape) aSelection;
466 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
467 return !aSelection->Get().IsNull();
468 } else { // for simple construction element: just shape of this construction element
469 std::shared_ptr<Model_ResultConstruction> aConstr =
470 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
474 // for the whole feature, a feature object
475 FeaturePtr aFeat = contextFeature();
484 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
487 myIsInitialized = myRef.isInitialized();
491 void Model_AttributeSelection::setID(const std::string theID)
494 ModelAPI_AttributeSelection::setID(theID);
497 ResultPtr Model_AttributeSelection::context()
499 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
502 if (myTmpContext.get() || myTmpSubShape.get()) {
506 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
507 // for parts there could be same-data result, so take the last enabled
509 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
510 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
511 for(int a = aSize - 1; a >= 0; a--) {
512 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
513 if(aPart.get() && aPart->data() == aResult->data()) {
514 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
515 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
516 // check that this result is not this-feature result (it is forbidden t oselect itself)
517 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
523 } else { // if feature - construction is selected, it has only one result, return this result
524 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
525 if (aFeature.get() && aFeature->results().size() == 1 &&
526 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
527 return aFeature->firstResult();
532 FeaturePtr Model_AttributeSelection::contextFeature() {
533 if (myTmpContext.get() || myTmpSubShape.get()) {
534 return FeaturePtr(); // feature can not be selected temporarily
536 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
538 ObjectPtr Model_AttributeSelection::contextObject() {
539 FeaturePtr aRes = contextFeature();
546 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
548 ModelAPI_AttributeSelection::setObject(theObject);
549 myRef.setObject(theObject);
552 /// Sets the invalid flag if flag is false, or removes it if "true"
554 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
556 theLab.ForgetAttribute(kINVALID_SELECTION);
558 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
563 void Model_AttributeSelection::split(
564 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
566 TopTools_ListOfShape aSubs;
567 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
568 if (!anExplorer.Value().IsNull() &&
569 anExplorer.Value().ShapeType() == theType) {
570 aSubs.Append(anExplorer.Value());
571 } else { // invalid case; bad result shape, so, impossible to split easily
576 if (aSubs.Extent() > 1) { // ok to split
577 TopTools_ListIteratorOfListOfShape aSub(aSubs);
578 GeomShapePtr aSubSh(new GeomAPI_Shape);
579 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
580 setValue(theContext, aSubSh);
581 for(aSub.Next(); aSub.More(); aSub.Next()) {
582 GeomShapePtr aSubSh(new GeomAPI_Shape);
583 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
584 myParent->append(theContext, aSubSh);
589 bool Model_AttributeSelection::update()
591 FeaturePtr aContextFeature = contextFeature();
592 if (aContextFeature.get()) {
593 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
596 TDF_Label aSelLab = selectionLabel();
597 ResultPtr aContext = context();
599 return setInvalidIfFalse(aSelLab, false);
600 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
601 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
604 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
605 std::shared_ptr<GeomAPI_Shape> aNoSelection;
606 bool aResult = selectPart(aContext, aNoSelection, true);
607 aResult = setInvalidIfFalse(aSelLab, aResult);
609 owner()->data()->sendAttributeUpdated(this);
614 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
615 // body: just a named shape, use topological selection mechanism
616 bool aResult = false;
617 TopoDS_Shape anOldShape;
618 Handle(TNaming_NamedShape) aNS;
619 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
620 anOldShape = aNS->Get();
622 Selector_Selector aSelector(aSelLab);
623 if (aSelector.restore()) { // it is stored in old OCCT format, use TNaming_Selector
624 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
625 aResult = aSelector.solve(aContextShape);
627 aResult = setInvalidIfFalse(aSelLab, aResult);
629 TopoDS_Shape aNewShape;
630 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
631 aNewShape = aNS->Get();
633 if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape)) {
634 // shape type shoud not not changed: if shape becomes compound of such shapes, then split
635 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
636 anOldShape.ShapeType() != aNewShape.ShapeType() &&
637 (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
639 split(aContext, aNewShape, anOldShape.ShapeType());
641 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
646 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
648 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
649 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
650 if (!aConstructionContext->isInfinite()) {
651 // external sketch face
652 Handle(TDataStd_Integer) anIndex;
653 if (aSelLab.FindAttribute(kEXT_SKETCH_FACE, anIndex)) {
654 return setInvalidIfFalse(aSelLab, anIndex->Get() < aConstructionContext->facesNum());
656 if (aSelLab.FindAttribute(kEXT_SKETCH_EDGE, anIndex) ||
657 aSelLab.FindAttribute(kEXT_SKETCH_VERT, anIndex)) {
658 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
659 aConstructionContext->document()->feature(aConstructionContext));
660 if (aComposite.get()) {
661 FeaturePtr aSubFeat = aComposite->subFeature(anIndex->Get() % 1000000);
662 return setInvalidIfFalse(aSelLab, aSubFeat.get() != NULL);
664 return setInvalidIfFalse(aSelLab, false); // composite sub-feature is not found
666 Selector_Selector aSelector(aSelLab);
667 aResult = aSelector.restore();
668 TopoDS_Shape anOldShape = aSelector.value();
670 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
671 aResult = aSelector.solve(aContextShape);
673 aResult = setInvalidIfFalse(aSelLab, aResult);
674 if (aResult && !anOldShape.IsEqual(aSelector.value()))
675 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
677 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
681 return setInvalidIfFalse(aSelLab, false); // unknown case
684 void Model_AttributeSelection::selectBody(
685 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
687 // perform the selection
688 TopoDS_Shape aContext;
690 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);//myRef.value()
692 aContext = aBody->shape()->impl<TopoDS_Shape>();
695 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
697 aContext = aResult->shape()->impl<TopoDS_Shape>();
699 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
704 if (!aContext.IsNull()) {
705 TDF_Label aSelLab = selectionLabel();
706 TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
707 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
708 if (aFeatureOwner->document() != theContext->document()) { // reference to the sketch face
709 if (theSubShape->shapeType() == TopAbs_FACE) { // sketch face
710 ResultConstructionPtr aConstr =
711 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
712 int aFaceIndex = -1, aFacesNum = aConstr->facesNum();
713 for(int a = 0; a < aFacesNum; a++) {
714 if (aConstr->face(a)->isEqual(theSubShape)) {
719 if (aFaceIndex >= 0) {
720 TDataStd_Integer::Set(aSelLab, kEXT_SKETCH_FACE, aFaceIndex); // store index of the face
723 } else if (theSubShape->shapeType() == TopAbs_EDGE || // sketch result edge (full one)
724 theSubShape->shapeType() == TopAbs_VERTEX) { // or start/end vertex
725 bool isVertex = theSubShape->shapeType() == TopAbs_VERTEX;
726 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
727 theContext->document()->feature(theContext));
728 if (aComposite.get()) { // iterate edges of composite to find index of matched with value
729 int aSub, anEdgeIndex = -1, aSubNum = aComposite->numberOfSubs();
730 int aVertIndex = -1, aVertShape = -1; // shape: 0 full, 1 start, 2 end
731 for(aSub = 0; aSub < aSubNum && anEdgeIndex == -1; aSub++) {
732 FeaturePtr aSubFeat = aComposite->subFeature(aSub);
733 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSubFeat->results();
734 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
735 for (; aRes != aResults.cend(); aRes++) {
736 ResultConstructionPtr aConstr =
737 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
738 if (aConstr->shape() && aConstr->shape()->isEdge()) {
740 GeomAPI_ShapeExplorer aVertExp(aConstr->shape(), GeomAPI_Shape::VERTEX);
741 for(int aNum = 1; aVertExp.more(); aVertExp.next(), aNum++) {
742 if (aVertExp.current()->isSame(theSubShape) && aVertShape != 0) {
748 if (aConstr->shape()->isSame(theSubShape)) {
753 } else if (isVertex && aConstr->shape() && aConstr->shape()->isVertex()) {
754 if (aConstr->shape()->isSame(theSubShape)) {
761 if (anEdgeIndex >= 0) {
762 TDataStd_Integer::Set(aSelLab, kEXT_SKETCH_EDGE, anEdgeIndex); // store index of edge
764 } else if (aVertIndex >= 0) {
765 aVertIndex += aVertShape * 1000000; // to store both integers: index and shape
766 TDataStd_Integer::Set(aSelLab, kEXT_SKETCH_VERT, aVertIndex); // store index of edge
772 bool aSelectorOk = true;
773 Selector_Selector aSel(aSelLab);
775 aSelectorOk = aSel.select(aContext, aNewSub);
778 aSelectorOk = aSel.solve(aContext);
783 Handle(TNaming_NamedShape) aSelectorShape;
784 if (aSelectorOk && aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelectorShape))
786 TopoDS_Shape aShape = aSelectorShape->Get();
787 if (aShape.IsNull() || aShape.ShapeType() != aNewSub.ShapeType())
791 setInvalidIfFalse(aSelLab, false);
796 bool Model_AttributeSelection::selectPart(
797 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
798 const bool theUpdate)
800 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
801 if (!aPart.get() || !aPart->isActivated())
802 return true; // postponed naming
804 Handle(TDataStd_Integer) anIndex;
805 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
806 // by internal selection
807 if (anIndex->Get() > 0) {
808 // update the selection by index
809 return aPart->updateInPart(anIndex->Get());
811 return true; // nothing to do, referencing just by name
814 return true; // nothing to do, referencing just by name
816 // store the shape (in case part is not loaded it should be useful
818 std::string aName = theContext->data()->name();
819 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
820 aShape = theContext->shape()->impl<TopoDS_Shape>();
822 aShape = theSubShape->impl<TopoDS_Shape>();
824 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
825 TDataStd_Integer::Set(selectionLabel(), anIndex);
827 TNaming_Builder aBuilder(selectionLabel());
828 aBuilder.Select(aShape, aShape);
829 // identify by name in the part
830 TDataStd_Name::Set(selectionLabel(), aName.c_str());
831 return !aName.empty();
834 TDF_Label Model_AttributeSelection::selectionLabel()
836 return myRef.myRef->Label().FindChild(1);
839 /// prefixes of the shape names with centers defined
840 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
842 /// returns the map that contains all possible prefixes of the center-names
843 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
845 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
846 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
847 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
848 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
850 return kCENTERS_PREFIX;
853 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
855 std::string aName("");
856 if(!this->isInitialized())
857 return !theDefaultName.empty() ? theDefaultName : aName;
859 CenterType aCenterType = NOT_CENTER;
860 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
861 ResultPtr aCont = context();
864 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
865 // selection of a full feature
866 FeaturePtr aFeatureCont = contextFeature();
867 if (aFeatureCont.get()) {
868 return kWHOLE_FEATURE + aFeatureCont->name();
870 // in case of selection of removed result
874 TDF_Label aSelLab = selectionLabel();
875 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
876 return contextName(aCont);
879 // whole infinitive construction
880 if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
881 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
882 if (aConstr->isInfinite()) {
883 return contextName(aCont);
885 // external sketch face
886 Handle(TDataStd_Integer) anIndex;
887 if (aSelLab.FindAttribute(kEXT_SKETCH_FACE, anIndex) ||
888 aSelLab.FindAttribute(kEXT_SKETCH_EDGE, anIndex) ||
889 aSelLab.FindAttribute(kEXT_SKETCH_VERT, anIndex)) {
890 std::shared_ptr<Model_Document> anExtDoc =
891 std::dynamic_pointer_cast<Model_Document>(aCont->document());
892 Selector_Selector aSelector(anExtDoc->extConstructionsLabel());
893 TopoDS_Shape aContShape = aConstr->shape()->impl<TopoDS_Shape>();
894 TopoDS_Shape aValShape = value()->impl<TopoDS_Shape>();
895 aSelector.select(aContShape, aValShape);
896 myRestoreDocument = anExtDoc;
897 std::string aName = anExtDoc->kind() + "/" + aSelector.name(this);
898 myRestoreDocument.reset();
904 Selector_Selector aSelector(aSelLab);
906 if (aSelector.restore())
907 aResult = aSelector.name(this);
909 Model_SelectionNaming aSelNaming(aSelLab);
910 std::string aResult = aSelNaming.namingName(
911 aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
913 if (aCenterType != NOT_CENTER) {
914 aResult += centersMap()[aCenterType];
919 // returns the center type and modifies the shape name if this name is center-name
920 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
922 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
923 centersMap().begin();
924 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
925 std::size_t aFound = theShapeName.find(aPrefixIter->second);
926 if (aFound != std::string::npos &&
927 aFound == theShapeName.size() - aPrefixIter->second.size()) {
928 theShapeName = theShapeName.substr(0, aFound);
929 return aPrefixIter->first;
932 return ModelAPI_AttributeSelection::NOT_CENTER;
935 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
936 void Model_AttributeSelection::selectSubShape(
937 const std::string& theType, const std::string& theSubShapeName)
939 if(theSubShapeName.empty() || theType.empty()) return;
941 std::string aSubShapeName = theSubShapeName;
942 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
943 centerTypeByName(aSubShapeName) : NOT_CENTER;
944 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
945 TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(theType));
946 static const GeomShapePtr anEmptyShape;
948 // first iteration is selection by name without center prefix, second - in case of problem,
949 // try with initial name
950 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
951 std::string aSubShapeName = theSubShapeName;
952 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
953 aCenterType = NOT_CENTER;
955 } else if (aUseCenter != 1) continue;
957 std::shared_ptr<Model_Document> aDoc =
958 std::dynamic_pointer_cast<Model_Document>(owner()->document());
959 // check this is Part-name: 2 delimiters in the name
960 std::size_t aPartEnd = aSubShapeName.find('/');
961 if (aPartEnd != std::string::npos) {
962 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
963 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
964 if (aPartName == aRootDoc->kind()) {
965 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
966 aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
968 ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
969 if (aFound.get()) { // found such part, so asking it for the name
970 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
971 aDoc = std::dynamic_pointer_cast<Model_Document>(aPart->partDoc());
972 aSubShapeName = aSubShapeName.substr(aPartEnd +1);
977 // check this is a whole feature context
978 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
979 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
980 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
981 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
983 setValue(anObj, anEmptyShape);
988 // the whole result selection check
989 if (aSubShapeName.find('/') == std::string::npos) {
990 ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
992 aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
994 setValue(aRes, anEmptyShape);
999 Selector_Selector aSelector(aDoc->generalLabel());
1000 myRestoreDocument = aDoc;
1001 TDF_Label aContextLabel = aSelector.restoreByName(aSubShapeName, aShapeType, this);
1002 myRestoreDocument.reset();
1003 if (!aContextLabel.IsNull()) {
1004 ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
1005 if (aContext.get() && aContext->shape().get()) {
1006 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
1007 if (aSelector.solve(aContextShape)) {
1008 GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
1009 aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelector.value()));
1010 if (aCenterType != NOT_CENTER) {
1011 if (!aShapeToBeSelected->isEdge())
1013 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
1014 setValueCenter(aContext, aSelectedEdge, aCenterType);
1017 setValue(aContext, aShapeToBeSelected);
1022 aSubShapeName = theSubShapeName;
1025 TDF_Label aSelLab = selectionLabel();
1026 setInvalidIfFalse(aSelLab, false);
1030 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1031 const GeomPointPtr& thePoint)
1033 if (theType.empty() || !thePoint)
1036 // list of parent features
1037 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
1038 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
1040 int aSelectionIndex = 0;
1041 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
1042 if (aType == GeomAPI_Shape::SHAPE) {
1043 // possibly, the string consists of the type and the index,
1044 // thus, try to separate them
1045 size_t aUndersporePos = theType.find_first_of('_');
1046 if (aUndersporePos != std::string::npos)
1047 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
1049 if (aType != GeomAPI_Shape::SHAPE) {
1050 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
1051 aChar != theType.end(); ++aChar) {
1052 if (std::isdigit(*aChar))
1053 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
1055 aSelectionIndex = 1;
1059 aSelectionIndex -= 1;
1063 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
1065 // collect features from PartSet and the current part
1066 SessionPtr aSession = ModelAPI_Session::get();
1067 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
1068 if (aSession->moduleDocument() != owner()->document()) {
1069 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
1070 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
1072 // Process results of all features from the last to the first
1073 // to find appropriate sub-shape
1074 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
1075 anIt != aFeatures.rend(); ++anIt) {
1076 // selection cannot be linked to the parent features
1077 if (aParents.find(*anIt) != aParents.end())
1079 // check the feature is a part of composite feature (like sketch elements),
1080 // then do not process it, it will be processed in scope of composite feature
1081 bool isSubOfComposite = false;
1082 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
1083 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
1084 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
1085 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
1086 CompositeFeaturePtr aCompFeature =
1087 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
1088 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
1090 if (isSubOfComposite)
1093 // process results of the current feature to find appropriate sub-shape
1094 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
1095 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
1096 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1097 ++anApIt; // skip this shape, because one of the previous is selected
1099 if (anApIt != anAppropriate.end()) {
1100 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1101 setValue(anApIt->myResult, anApIt->mySubshape);
1103 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1104 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1110 TDF_Label aSelLab = selectionLabel();
1111 setInvalidIfFalse(aSelLab, false);
1115 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1116 const std::string& theContextName, const int theIndex)
1118 // selection of context by name
1119 //std::string aNamingContextName = theContextName + "/";
1120 //selectSubShape(theType, aNamingContextName);
1121 std::shared_ptr<Model_Document> aDoc =
1122 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1124 bool aUnique = true;
1125 std::string aContextName = theContextName;
1126 std::string anEmptySub = "";
1127 ResultPtr aContext = aDoc->findByName(aContextName, anEmptySub, aUnique);
1128 //ResultPtr aContext = context();
1129 if (aContext.get()) {
1130 GeomShapePtr aContShape = aContext->shape();
1131 if (aContShape.get()) {
1132 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1133 GeomShapePtr aValue = aNExp.shape(theIndex);
1135 setValue(aContext, aValue);
1141 int Model_AttributeSelection::Id()
1144 std::shared_ptr<GeomAPI_Shape> aSelection = value();
1145 ResultPtr aContextRes = context();
1146 // support for compsolids:
1147 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1148 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1150 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1153 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1154 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
1155 // searching for the latest main shape
1156 if (aSelection && !aSelection->isNull() && aContext && !aContext->isNull())
1158 std::shared_ptr<Model_Document> aDoc =
1159 std::dynamic_pointer_cast<Model_Document>(context()->document());
1161 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1162 if (!aNS.IsNull()) {
1163 aMainShape = TNaming_Tool::CurrentShape(aNS);
1167 TopTools_IndexedMapOfShape aSubShapesMap;
1168 TopExp::MapShapes(aMainShape, aSubShapesMap);
1169 anID = aSubShapesMap.FindIndex(aSubShape);
1174 void Model_AttributeSelection::setId(int theID)
1176 std::shared_ptr<GeomAPI_Shape> aSelection;
1178 ResultPtr aContextRes = context();
1179 // support for compsolids:
1180 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1181 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1183 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1185 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1186 // searching for the latest main shape
1187 if (theID > 0 && aContext && !aContext->isNull())
1189 std::shared_ptr<Model_Document> aDoc =
1190 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1192 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1193 if (!aNS.IsNull()) {
1194 aMainShape = TNaming_Tool::CurrentShape(aNS);
1198 TopTools_IndexedMapOfShape aSubShapesMap;
1199 TopExp::MapShapes(aMainShape, aSubShapesMap);
1200 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1202 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1203 aResult->setImpl(new TopoDS_Shape(aSelShape));
1205 aSelection = aResult;
1208 setValue(aContextRes, aSelection);
1211 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1213 std::string aResult;
1214 if (owner()->document() != theContext->document()) {
1215 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1216 aResult = theContext->document()->kind() + "/";
1218 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1219 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1220 if (aDocRes.get()) {
1221 aResult = aDocRes->data()->name() + "/";
1225 aResult += theContext->data()->name();
1229 void Model_AttributeSelection::computeValues(
1230 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1231 TopTools_ListOfShape& theShapes)
1233 bool aWasWholeContext = theValShape.IsNull();
1234 if (aWasWholeContext) {
1235 //theShapes.Append(theValShape);
1237 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1239 //TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1240 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1241 //if (anOldContShape.IsSame(theValShape)) { // full context shape substituted by new full context
1242 //theShapes.Append(aNewContShape);
1245 // if a new value is unchanged in the new context, do nothing: value is correct
1246 TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
1247 for(; aSubExp.More(); aSubExp.Next()) {
1248 if (aSubExp.Current().IsSame(theValShape)) {
1249 theShapes.Append(theValShape);
1253 // if new context becomes compsolid, the resulting sub may be in sub-solids
1254 std::list<ResultPtr> aNewToIterate;
1255 aNewToIterate.push_back(theNewContext);
1256 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1258 std::list<ResultPtr> allNewContextSubs;
1259 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1260 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1261 for(; aSub != allNewContextSubs.end(); aSub++) {
1262 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1263 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1264 aNewToIterate.push_back(aBody);
1268 // first iteration: searching for the whole shape appearance (like face of the box)
1269 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1270 int aToFindPart = 0;
1271 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1272 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1273 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1274 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1275 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1276 std::shared_ptr<Model_Data> aNewData =
1277 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1278 TDF_Label aNewLab = aNewData->shapeLab();
1279 // searching for produced sub-shape fully on some label
1280 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID(), Standard_True);
1281 for(; aNSIter.More(); aNSIter.Next()) {
1282 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1283 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1284 if (aToFindPart == 0) { // search shape is fully inside
1285 if (aPairIter.OldShape().IsSame(theValShape)) {
1286 if (aPairIter.NewShape().IsNull()) {// value was removed
1290 theShapes.Append(aPairIter.NewShape());
1292 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1293 TopExp_Explorer anExp(aPairIter.OldShape(), theValShape.ShapeType());
1294 for(; anExp.More(); anExp.Next()) {
1295 if (anExp.Current().IsSame(theValShape)) { // found a new container
1296 if (aPairIter.NewShape().IsNull()) {// value was removed
1300 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1301 anOlds.Add(aPairIter.OldShape());
1310 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1311 // map of sub-shapes -> number of occurences of these shapes in containers
1312 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1313 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1314 for(; aContIter.More(); aContIter.Next()) {
1315 TopExp_Explorer aSubExp(aContIter.Key(), theValShape.ShapeType());
1316 for(; aSubExp.More(); aSubExp.Next()) {
1317 if (!aSubs.IsBound(aSubExp.Current())) {
1318 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1320 // store old to know how many olds produced this shape
1321 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1324 // if sub is appeared same times in containers as the number of old shapes that contain it
1325 int aCountInOld = anOlds.Size();
1326 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1328 for(; aSubsIter.More(); aSubsIter.Next()) {
1329 if (aSubsIter.Value().Size() == aCountInOld) {
1330 theShapes.Append(aSubsIter.Key());
1334 if (theShapes.IsEmpty()) { // nothing was changed
1335 theShapes.Append(aWasWholeContext ? TopoDS_Shape() : theValShape);
1339 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1340 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1341 TDF_Label theAccessLabel,
1342 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1344 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1345 TopTools_ListOfShape aResContShapes;
1346 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1347 TopTools_ListOfShape aContextList;
1348 aContextList.Append(theContShape);
1349 if (theContext.get()) {
1350 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1351 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1352 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1354 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1355 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1356 for(; aModifIter.More(); aModifIter.Next()) {
1357 TDF_Label anObjLab = aModifIter.Label().Father();
1358 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1359 (theDoc->objects()->object(anObjLab));
1360 if (!aModifierObj.get()) {
1361 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1362 if (!anObjLab.IsRoot())
1363 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1364 (theDoc->objects()->object(anObjLab.Father()));
1365 if (!aModifierObj.get())
1368 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1369 if (!aModifierFeat.get())
1371 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1372 if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
1373 continue; // the modifier feature is later than this, so, should not be used
1374 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1375 if (aCurrentModifierFeat == aModifierFeat ||
1376 !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
1377 continue; // the current modifier is later than the found, so, useless
1378 Handle(TNaming_NamedShape) aNewNS;
1379 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1380 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1381 aResults.insert(aModifierObj);
1382 //TNaming_Iterator aPairIter(aNewNS);
1383 //aResContShapes.Append(aPairIter.NewShape());
1384 aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
1385 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1386 aResults.insert(ResultPtr());
1387 } else { // not-processed modification => don't support it
1392 if (aResults.empty())
1393 return false; // no modifications found, must stay the same
1394 // iterate all results to find futher modifications
1395 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1396 for(; aResIter != aResults.end(); aResIter++) {
1397 if (aResIter->get() != NULL) {
1398 // compute new values by two contextes: the old and the new
1399 TopTools_ListOfShape aValShapes;
1400 computeValues(theContext, *aResIter, theValShape, aValShapes);
1402 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1403 for(; aNewVal.More(); aNewVal.Next()) {
1404 std::list<ResultPtr> aNewRes;
1405 TopTools_ListOfShape aNewUpdatedVal;
1406 TopoDS_Shape aNewValSh = aNewVal.Value();
1407 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1408 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1409 aNewValSh.Nullify();
1410 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1411 theAccessLabel, aNewRes, aNewUpdatedVal))
1413 // appeand new results instead of the current ones
1414 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1415 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1416 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1417 theResults.push_back(*aNewIter);
1418 theValShapes.Append(aNewUpdVal.Value());
1420 } else { // the current result is good
1421 theResults.push_back(*aResIter);
1422 theValShapes.Append(aNewValSh);
1427 return true; // theResults must be empty: everything is deleted
1430 void Model_AttributeSelection::updateInHistory()
1432 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1433 // only bodies and parts may be modified later in the history, don't do anything otherwise
1434 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1435 aContext->groupName() != ModelAPI_ResultPart::group()))
1437 std::shared_ptr<Model_Document> aDoc =
1438 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1439 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1440 if (!aContData.get() || !aContData->isValid())
1442 TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
1443 Handle(TNaming_NamedShape) aContNS;
1444 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1445 bool aFoundNewContext = true;
1446 ResultPtr aNewContext = aContext;
1447 while(aFoundNewContext) {
1448 aFoundNewContext = false;
1449 // parts have no shape in result, so, trace references using the Part info
1450 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1451 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1452 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1453 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1454 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1455 for(; aRef != aRefs.end(); aRef++) {
1456 // to avoid detection of part changes by local selection only
1457 AttributeSelectionPtr aSel =
1458 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1459 if (aSel.get() && !aSel->value()->isSame(aSel->context()->shape()))
1462 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1463 if (aRefFeat.get() && aRefFeat != owner()) {
1464 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1465 if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
1466 aFoundNewContext = true;
1467 aNewContext = aRefFeat->firstResult();
1474 if (aNewContext != aContext) {
1475 setValue(aNewContext, value());
1479 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1480 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1481 // iterate the context shape modifications in order to find a feature that is upper in history
1482 // that this one and is really modifies the referenced result to refer to it
1483 ResultPtr aModifierResFound;
1484 TNaming_Iterator aPairIter(aContNS);
1485 if (!aPairIter.More())
1487 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1488 bool anIterate = true;
1489 // trying to update also the sub-shape selected
1490 GeomShapePtr aSubShape = value();
1491 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1493 TopoDS_Shape aValShape;
1494 if (aSubShape.get()) {
1495 aValShape = aSubShape->impl<TopoDS_Shape>();
1498 std::list<ResultPtr> aNewContexts;
1499 TopTools_ListOfShape aValShapes;
1500 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1502 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1503 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1504 if (aNewCont == aNewContexts.end()) { // all results were deleted
1505 ResultPtr anEmptyContext;
1506 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1507 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1511 GeomShapePtr aValueShape;
1512 if (!aNewValues.Value().IsNull()) {
1513 aValueShape = std::make_shared<GeomAPI_Shape>();
1514 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1516 setValue(*aNewCont, aValueShape);
1517 // if there are more than one result, put them by "append" into "parent" list
1519 for(aNewCont++, aNewValues.Next(); aNewCont != aNewContexts.end();
1520 aNewCont++, aNewValues.Next()) {
1521 GeomShapePtr aValueShape;
1522 if (!aNewValues.Value().IsNull()) {
1523 aValueShape = std::make_shared<GeomAPI_Shape>();
1524 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1527 // Check that list has the same type of shape selection before adding.
1528 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1529 if (myParent->selectionType() == "VERTEX") aListShapeType = GeomAPI_Shape::VERTEX;
1530 else if (myParent->selectionType() == "EDGE") aListShapeType = GeomAPI_Shape::EDGE;
1531 else if (myParent->selectionType() == "FACE") aListShapeType = GeomAPI_Shape::FACE;
1533 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1534 if (aValueShape.get()) {
1535 aShapeShapeType = aValueShape->shapeType();
1537 (*aNewCont)->shape()->shapeType();
1540 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1544 myParent->append(*aNewCont, aValueShape);
1550 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1552 myParent = theParent;
1555 std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
1557 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1558 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1559 FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1560 if (aFeatureOwner.get()) {
1561 // if it is sub-element of the sketch, the context name is the name of the sketch
1562 // searching also for result - real context
1564 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
1565 if (aComposite.get() && aComposite->results().size() == 1 &&
1566 aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
1567 aFeatureOwner = aComposite;
1568 aResult = aFeatureOwner->firstResult();
1570 aResult = aDoc->resultByLab(theSelectionLab);
1572 if (aResult.get()) {
1573 // this is to avoid duplicated names of results problem
1574 std::string aContextName = aResult->data()->name();
1575 // myLab corresponds to the current time
1576 TDF_Label aCurrentLab = selectionLabel();
1577 while(aCurrentLab.Depth() > 3)
1578 aCurrentLab = aCurrentLab.Father();
1580 int aNumInHistoryNames =
1581 aDoc->numberOfNameInHistory(aResult, aCurrentLab);
1582 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
1583 aContextName = "_" + aContextName;
1584 aNumInHistoryNames--;
1586 return aContextName;
1589 return ""; // invalid case
1592 /// This method restores by the context and value name the context label and
1593 /// sub-label where the value is. Returns true if it is valid.
1594 bool Model_AttributeSelection::restoreContext(std::string theName,
1595 TDF_Label& theContext, TDF_Label& theValue)
1597 static const GeomShapePtr anEmptyShape; // to store context only
1598 std::string aName = theName;
1599 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1600 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1602 // remove the sub-value part if exists
1603 std::string aSubShapeName = aName;
1604 std::string::size_type n = aName.find('/');
1605 if (n != std::string::npos) {
1606 aName = aName.substr(0, n);
1609 if (aName.empty()) return false;
1610 bool anUniqueContext = false;
1611 ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1612 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
1615 // searching the sub-shape
1616 static const ResultPtr anEmpty;
1617 theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
1619 // sketch sub-component shape and name is located in separated feature label, try the sub-name
1620 if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
1621 std::string::size_type aSlash = aSubShapeName.rfind('/');
1622 if (aSlash != std::string::npos) {
1623 std::string aCompName = aSubShapeName.substr(aSlash + 1);
1624 CompositeFeaturePtr aComposite =
1625 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
1626 if (aComposite.get() && aComposite->numberOfSubs()) {
1627 const int aSubNum = aComposite->numberOfSubs();
1628 for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
1629 FeaturePtr aSub = aComposite->subFeature(a);
1630 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1631 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
1632 for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
1633 if ((*aRes)->data()->name() == aCompName) {
1634 theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1636 } else if (aCompName.find((*aRes)->data()->name()) != std::string::npos) {// sub-vertex
1637 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1638 TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
1639 for(; aSubNames.More(); aSubNames.Next()) {
1640 if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
1641 IsEqual(aCompName.c_str())) {
1642 theValue = aSubNames.Value()->Label();
1653 /* to find the latest lower result that keeps given shape
1654 bool aFindNewContext = true;
1655 while(aFindNewContext && aCont.get()) {
1656 aFindNewContext = false;
1657 // try to find the last context to find the up to date shape
1658 TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
1659 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1660 if (!aNS.IsNull()) {
1661 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1662 if (!aNS.IsNull() && isOlderThanMe(aNS->Label())) { // scope check is for 2228
1663 TDF_Label aLab = aNS->Label();
1664 if (aLab.Depth() % 2 == 0)
1665 aLab = aLab.Father();
1666 ObjectPtr anObj = aDoc->objects()->object(aLab);
1667 while (!anObj.get() && aLab.Depth() > 5) {
1668 aLab = aLab.Father().Father();
1669 anObj = aDoc->objects()->object(aLab);
1673 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1676 aFindNewContext = true;
1680 } else if (aCont->groupName() == ModelAPI_ResultBody::group()) {
1681 // try to search newer context by the concealment references
1682 // take references to all results: root one, any sub
1683 std::list<ResultPtr> allRes;
1684 ResultPtr aCompContext;
1685 if (aCont->groupName() == ModelAPI_ResultBody::group()) {
1686 ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aCont, true);
1687 if (aCompBody.get()) {
1688 ModelAPI_Tools::allSubs(aCompBody, allRes);
1689 allRes.push_back(aCompBody);
1690 aCompContext = aCompBody;
1694 allRes.push_back(aCont);
1696 for(std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1697 ResultPtr aResCont = *aSub;
1698 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1699 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1700 continue; // only lower and higher level subs are counted
1701 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1702 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1703 for(; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1704 if (!aRef->get() || !(*aRef)->owner().get())
1706 // concealed attribute only
1707 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1708 if (!ModelAPI_Session::get()->validators()->isConcealed(
1709 aRefFeat->getKind(), (*aRef)->id()))
1711 // search the feature result that contains sub-shape selected
1712 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1714 // take all sub-results or one result
1715 std::list<ResultPtr> aRefFeatResults;
1716 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1717 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1718 for(; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1719 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1720 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1721 aResults.push_back(aBody);
1723 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1724 for(; aResIter != aResults.end(); aResIter++) {
1725 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1727 GeomShapePtr aShape = (*aResIter)->shape();
1728 GeomShapePtr aSelectedShape =
1729 aShapeToBeSelected.get() ? aShapeToBeSelected : aCont->shape();
1730 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
1731 aCont = *aResIter; // found new context (produced from this) with same subshape
1732 aFindNewContext = true; // continue searching futher
1740 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
1741 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCont);
1742 if (aComp && aComp->numberOfSubs()) {
1743 std::list<ResultPtr> allSubs;
1744 ModelAPI_Tools::allSubs(aComp, allSubs);
1745 std::list<ResultPtr>::iterator aS = allSubs.begin();
1746 for (; aS != allSubs.end(); aS++) {
1747 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
1748 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
1749 aSub->shape()->isSubShape(aShapeToBeSelected)) {
1757 theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
1762 bool Model_AttributeSelection::isLater(
1763 const TDF_Label theResult1, const TDF_Label theResult2) const
1765 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1766 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1767 FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
1770 FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
1773 return aDoc->isLaterByDep(aFeat1, aFeat2);