1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "Model_AttributeSelection.h"
21 #include "Model_Application.h"
22 #include "Model_Events.h"
23 #include "Model_Data.h"
24 #include "Model_Document.h"
25 #include <Model_Objects.h>
26 #include <Model_AttributeSelectionList.h>
27 #include <Model_ResultConstruction.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_ResultGroup.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_ShapeIterator.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomAPI_ShapeExplorer.h>
45 #include <GeomAlgoAPI_CompoundBuilder.h>
46 #include <GeomAlgoAPI_NExplode.h>
47 #include <Selector_Selector.h>
49 #include <TNaming_NamedShape.hxx>
50 #include <TNaming_Tool.hxx>
51 #include <TNaming_Builder.hxx>
52 #include <TNaming_SameShapeIterator.hxx>
53 #include <TNaming_NewShapeIterator.hxx>
54 #include <TNaming_Iterator.hxx>
55 #include <TDataStd_Integer.hxx>
56 #include <TDataStd_UAttribute.hxx>
57 #include <TDataStd_Name.hxx>
58 #include <TopTools_ListOfShape.hxx>
59 #include <TopTools_DataMapOfShapeShape.hxx>
60 #include <TopTools_MapOfShape.hxx>
61 #include <TopExp_Explorer.hxx>
62 #include <BRep_Tool.hxx>
64 #include <TopoDS_Edge.hxx>
66 #include <TDF_ChildIterator.hxx>
67 #include <TDF_ChildIDIterator.hxx>
68 #include <TopoDS_Iterator.hxx>
69 #include <TDF_ChildIDIterator.hxx>
70 #include <Geom_Circle.hxx>
71 #include <Geom_Ellipse.hxx>
72 #include <Geom_TrimmedCurve.hxx>
73 #include <BRep_Builder.hxx>
75 //#define DEB_NAMING 1
77 #include <BRepTools.hxx>
79 /// added to the index in the packed map to signalize that the vertex of edge is selected
80 /// (multiplied by the index of the edge)
81 static const int kSTART_VERTEX_DELTA = 1000000;
82 // identifier that there is simple reference: selection equals to context
83 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
84 // reference to Part sub-object
85 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
86 // selection is invalid after recomputation
87 Standard_GUID kINVALID_SELECTION("bce47fd7-80fa-4462-9d63-2f58acddd49d");
89 // identifier of the selection of the center of circle on edge
90 Standard_GUID kCIRCLE_CENTER("d0d0e0f1-217a-4b95-8fbb-0c4132f23718");
91 // identifier of the selection of the first focus point of ellipse on edge
92 Standard_GUID kELLIPSE_CENTER1("f70df04c-3168-4dc9-87a4-f1f840c1275d");
93 // identifier of the selection of the second focus point of ellipse on edge
94 Standard_GUID kELLIPSE_CENTER2("1395ae73-8e02-4cf8-b204-06ff35873a32");
96 // prefix for the whole feature context identification
97 const static std::string kWHOLE_FEATURE = "all-in-";
99 // on this label is stored:
100 // TNaming_NamedShape - selected shape
101 // TNaming_Naming - topological selection information (for the body)
102 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
103 // TDataStd_Integer - type of the selected shape (for construction)
104 // TDF_Reference - from ReferenceAttribute, the context
105 bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
106 const std::shared_ptr<GeomAPI_Shape>& theSubShape, const bool theTemporarily)
108 if (theTemporarily) {
109 // just keep the stored without DF update
110 myTmpContext = theContext;
111 myTmpSubShape = theSubShape;
112 owner()->data()->sendAttributeUpdated(this);
115 myTmpContext.reset();
116 myTmpSubShape.reset();
117 myTmpCenterType = NOT_CENTER;
121 const std::shared_ptr<GeomAPI_Shape>& anOldShape = internalValue(aType);
122 bool isOldContext = theContext == myRef.value();
123 bool isOldShape = isOldContext &&
124 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
125 if (isOldShape) return false; // shape is the same, so context is also unchanged
126 bool aToUnblock = false;
127 // update the referenced object if needed
129 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
130 myRef.setValue(theContext);
133 // do not use naming if selected shape is result shape itself, but not sub-shape
134 TDF_Label aSelLab = selectionLabel();
135 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
136 aSelLab.ForgetAttribute(kINVALID_SELECTION);
137 aSelLab.ForgetAttribute(kCIRCLE_CENTER);
138 aSelLab.ForgetAttribute(kELLIPSE_CENTER1);
139 aSelLab.ForgetAttribute(kELLIPSE_CENTER2);
141 bool isDegeneratedEdge = false;
142 // do not use the degenerated edge as a shape, a null context and shape is used in the case
143 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
144 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
145 if (aSubShape.ShapeType() == TopAbs_EDGE)
146 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
148 if (!theContext.get() || isDegeneratedEdge) {
149 // to keep the reference attribute label
150 TDF_Label aRefLab = myRef.myRef->Label();
151 aSelLab.ForgetAllAttributes(true);
152 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
154 owner()->data()->blockSendAttributeUpdated(false);
157 bool isSelectBody = theContext->groupName() == ModelAPI_ResultBody::group();
159 ResultConstructionPtr aContextConstruction =
160 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
161 isSelectBody = aContextConstruction.get() && !aContextConstruction->isInfinite();
164 ResultPtr aContextResult = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
165 GeomShapePtr aContextShape = aContextResult->shape();
166 // do not select the whole shape for body:it is already must be in the data framework
167 // equal and null selected objects mean the same: object is equal to context,
168 if (aContextShape.get() && (aContextShape->isEqual(theSubShape) || !theSubShape.get())) {
169 aSelLab.ForgetAllAttributes(true);
170 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
172 selectBody(aContextResult, theSubShape);
174 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
175 aSelLab.ForgetAllAttributes(true); // to remove old selection data
176 std::shared_ptr<Model_ResultConstruction> aConstruction =
177 std::dynamic_pointer_cast<Model_ResultConstruction>(theContext);
178 std::shared_ptr<GeomAPI_Shape> aSubShape;
179 if (theSubShape.get() && !aConstruction->shape()->isEqual(theSubShape))
180 aSubShape = theSubShape; // the whole context
181 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
182 aSelLab.ForgetAllAttributes(true);
183 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
184 selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
185 } else if (theContext->groupName() == ModelAPI_ResultGroup::group()) {
186 aSelLab.ForgetAllAttributes(true);
187 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
188 } else { // check the feature context: parent-Part of this feature should not be used
189 FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
190 if (aFeatureContext.get()) {
191 if (owner()->document() != aFeatureContext->document()) {
192 aSelLab.ForgetAllAttributes(true);
193 myRef.setValue(ObjectPtr());
195 owner()->data()->blockSendAttributeUpdated(false);
201 owner()->data()->sendAttributeUpdated(this);
204 owner()->data()->blockSendAttributeUpdated(false);
209 void Model_AttributeSelection::setValueCenter(
210 const ObjectPtr& theContext, const std::shared_ptr<GeomAPI_Edge>& theEdge,
211 const CenterType theCenterType, const bool theTemporarily)
213 bool anUpdated = setValue(theContext, theEdge, theTemporarily);
214 if (theTemporarily) {
215 myTmpCenterType = theCenterType;
216 } else { // store in the data structure
217 TDF_Label aSelLab = selectionLabel();
218 switch(theCenterType) {
221 anUpdated = !aSelLab.IsAttribute(kCIRCLE_CENTER);
222 TDataStd_UAttribute::Set(aSelLab, kCIRCLE_CENTER);
224 case ELLIPSE_FIRST_FOCUS:
226 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER1);
227 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER1);
229 case ELLIPSE_SECOND_FOCUS:
231 anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
232 TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
236 owner()->data()->sendAttributeUpdated(this);
240 void Model_AttributeSelection::selectValue(
241 const std::shared_ptr<ModelAPI_AttributeSelection>& theSource)
244 std::shared_ptr<GeomAPI_Shape> aValue =
245 std::dynamic_pointer_cast<Model_AttributeSelection>(theSource)->internalValue(aType);
246 if (!aValue.get() || aType == NOT_CENTER) {
247 setValue(theSource->context(), aValue);
249 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge);
250 anEdge->setImpl(new TopoDS_Shape(aValue->impl<TopoDS_Shape>()));
251 setValueCenter(theSource->context(), anEdge, aType);
255 void Model_AttributeSelection::removeTemporaryValues()
257 if (myTmpContext.get() || myTmpSubShape.get()) {
258 myTmpContext.reset();
259 myTmpSubShape.reset();
263 // returns the center of the edge: circular or elliptical
264 GeomShapePtr centerByEdge(GeomShapePtr theEdge, ModelAPI_AttributeSelection::CenterType theType)
266 if (theType != ModelAPI_AttributeSelection::NOT_CENTER && theEdge.get() != NULL) {
267 TopoDS_Shape aShape = theEdge->impl<TopoDS_Shape>();
268 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) {
269 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
270 double aFirst, aLast;
271 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
272 if (!aCurve.IsNull()) {
273 TopoDS_Vertex aVertex;
274 BRep_Builder aBuilder;
275 if (theType == ModelAPI_AttributeSelection::CIRCLE_CENTER) {
276 while(!aCurve.IsNull() && aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
277 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
278 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
279 if (!aCirc.IsNull()) {
280 aBuilder.MakeVertex(aVertex, aCirc->Location(), Precision::Confusion());
283 while(!aCurve.IsNull() && aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
284 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
285 Handle(Geom_Ellipse) anEll = Handle(Geom_Ellipse)::DownCast(aCurve);
286 if (!anEll.IsNull()) {
287 aBuilder.MakeVertex(aVertex,
288 theType == ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS ?
289 anEll->Focus1() : anEll->Focus2(), Precision::Confusion());
292 if (!aVertex.IsNull()) {
293 std::shared_ptr<GeomAPI_Vertex> aResult(new GeomAPI_Vertex);
294 aResult->setImpl(new TopoDS_Vertex(aVertex));
300 return theEdge; // no vertex, so, return the initial edge
303 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
305 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
306 return std::shared_ptr<GeomAPI_Shape>();
307 CenterType aType = NOT_CENTER;
308 std::shared_ptr<GeomAPI_Shape> aResult = internalValue(aType);
309 return centerByEdge(aResult, aType);
312 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterType& theType)
314 theType = NOT_CENTER;
315 GeomShapePtr aResult;
316 if (myTmpContext.get() || myTmpSubShape.get()) {
317 theType = myTmpCenterType;
318 ResultConstructionPtr aResulConstruction =
319 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myTmpContext);
320 if(aResulConstruction.get()) {
321 // it is just reference to construction.
322 return myTmpSubShape;
324 FeaturePtr aFeature =
325 std::dynamic_pointer_cast<ModelAPI_Feature>(myTmpContext);
326 if (aFeature.get()) {
327 // it is just reference to construction.
328 return myTmpSubShape;
330 return myTmpSubShape.get() ? myTmpSubShape :
331 std::dynamic_pointer_cast<ModelAPI_Result>(myTmpContext)->shape();
334 TDF_Label aSelLab = selectionLabel();
335 if (aSelLab.IsAttribute(kINVALID_SELECTION))
338 if (aSelLab.IsAttribute(kCIRCLE_CENTER))
339 theType = CIRCLE_CENTER;
340 else if (aSelLab.IsAttribute(kELLIPSE_CENTER1))
341 theType = ELLIPSE_FIRST_FOCUS;
342 else if (aSelLab.IsAttribute(kELLIPSE_CENTER2))
343 theType = ELLIPSE_SECOND_FOCUS;
346 if (myRef.isInitialized()) {
347 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
348 ResultPtr aContext = context();
349 if (!aContext.get() || aContext->groupName() == ModelAPI_ResultConstruction::group())
350 return aResult; // empty result, for whole construction selection also
351 return aContext->shape();
353 if (aSelLab.IsAttribute(kPART_REF_ID)) {
354 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
355 if (!aPart.get() || !aPart->isActivated())
356 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
357 Handle(TDataStd_Integer) anIndex;
358 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
359 if (anIndex->Get()) { // special selection attribute was created, use it
360 return aPart->selectionValue(anIndex->Get());
361 } else { // face with name is already in the data model, so try to take it by name
362 Handle(TDataStd_Name) aName;
363 if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
364 std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
365 std::size_t aPartEnd = aSubShapeName.find('/');
366 if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
367 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
369 std::string aType; // to reuse already existing selection the type is not needed
370 return aPart->shapeInPart(aNameInPart, aType, anIndex);
377 std::shared_ptr<Model_ResultConstruction> aConstr =
378 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
380 if (aConstr->isInfinite())
381 return aResult; // empty result
383 if (!aConstr.get()) { // for construction context, return empty result as usual even
384 // the whole feature is selected
385 FeaturePtr aFeature = contextFeature();
386 if (aFeature.get()) {
387 std::list<GeomShapePtr> allShapes;
388 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
389 for (; aRes != aFeature->results().cend(); aRes++) {
390 if (aRes->get() && !(*aRes)->isDisabled()) {
391 GeomShapePtr aShape = (*aRes)->shape();
392 if (aShape.get() && !aShape->isNull()) {
393 allShapes.push_back(aShape);
397 return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
400 if (contextFeature().get()) {
401 return aResult; // for the whole sketch feature selected return null => all faces
405 Handle(TNaming_NamedShape) aSelection;
406 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
407 TopoDS_Shape aSelShape = aSelection->Get();
408 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
409 aResult->setImpl(new TopoDS_Shape(aSelShape));
410 } else if (aConstr) { // simple construction element: just shape of this construction element
411 aResult = aConstr->shape();
417 bool Model_AttributeSelection::isInvalid()
419 return selectionLabel().IsAttribute(kINVALID_SELECTION) == Standard_True;
422 bool Model_AttributeSelection::isInitialized()
424 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checks if it is initialized
425 std::shared_ptr<GeomAPI_Shape> aResult;
426 if (myRef.isInitialized()) {
427 TDF_Label aSelLab = selectionLabel();
428 // it is just reference to shape, not sub-shape
429 if (aSelLab.IsAttribute(kSIMPLE_REF_ID) || aSelLab.IsAttribute(kPART_REF_ID)) {
430 ResultPtr aContext = context();
431 return aContext.get() != NULL;
433 Handle(TNaming_NamedShape) aSelection;
434 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
435 return !aSelection->Get().IsNull();
436 } else { // for simple construction element: just shape of this construction element
437 if (myRef.value().get())
439 // check that this is on open of document, so, results are not initialized yet
440 TDF_Label aRefLab = myRef.myRef->Get();
441 if (aRefLab.IsNull() || !owner().get())
443 std::shared_ptr<Model_Document> aMyDoc =
444 std::dynamic_pointer_cast<Model_Document>(owner()->document());
447 // check at least the feature exists
448 return aMyDoc->featureByLab(aRefLab).get() != NULL;
455 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
457 myIsGeometricalSelection(false)
459 myIsInitialized = myRef.isInitialized();
463 void Model_AttributeSelection::setID(const std::string theID)
466 ModelAPI_AttributeSelection::setID(theID);
467 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
469 // to be able to select as geometrical selection and then - split
470 myIsGeometricalSelection = true; // myParent->isGeometricalSelection();
472 myIsGeometricalSelection =
473 ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
477 ResultPtr Model_AttributeSelection::context()
479 if (!ModelAPI_AttributeSelection::isInitialized() && !myTmpContext.get() && !myTmpSubShape.get())
482 if (myTmpContext.get() || myTmpSubShape.get()) {
483 return std::dynamic_pointer_cast<ModelAPI_Result>(myTmpContext);
486 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
487 // for parts there could be same-data result, so take the last enabled
489 if(aResult->groupName() == ModelAPI_ResultPart::group()) {
490 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
491 for(int a = aSize - 1; a >= 0; a--) {
492 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
493 if(aPart.get() && aPart->data() == aResult->data()) {
494 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
495 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
496 // check that this result is not this-feature result (it is forbidden to select itself)
497 if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
503 } else { // if feature - construction is selected, it has only one result, return this result
504 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
505 if (aFeature.get() && aFeature->results().size() == 1 &&
506 aFeature->firstResult()->groupName() == ModelAPI_ResultConstruction::group())
507 return aFeature->firstResult();
512 FeaturePtr Model_AttributeSelection::contextFeature() {
513 if (myTmpContext.get()) {
514 return std::dynamic_pointer_cast<ModelAPI_Feature>(myTmpContext);
516 return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
518 ObjectPtr Model_AttributeSelection::contextObject() {
519 FeaturePtr aRes = contextFeature();
526 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
528 ModelAPI_AttributeSelection::setObject(theObject);
529 myRef.setObject(theObject);
532 /// Sets the invalid flag if flag is false, or removes it if "true"
534 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
536 theLab.ForgetAttribute(kINVALID_SELECTION);
538 TDataStd_UAttribute::Set(theLab, kINVALID_SELECTION);
543 void Model_AttributeSelection::split(
544 ResultPtr theContext, TopoDS_Shape theNewShape, TopAbs_ShapeEnum theType)
546 TopTools_ListOfShape aSubs;
547 for(TopoDS_Iterator anExplorer(theNewShape); anExplorer.More(); anExplorer.Next()) {
548 if (!anExplorer.Value().IsNull() &&
549 anExplorer.Value().ShapeType() == theType) {
550 aSubs.Append(anExplorer.Value());
551 } else { // invalid case; bad result shape, so, impossible to split easily
556 if (aSubs.Extent() > 1) { // ok to split
557 TopTools_ListIteratorOfListOfShape aSub(aSubs);
558 GeomShapePtr aSubSh(new GeomAPI_Shape);
559 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
560 setValue(theContext, aSubSh);
561 for(aSub.Next(); aSub.More(); aSub.Next()) {
562 GeomShapePtr aSubSh(new GeomAPI_Shape);
563 aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
564 myParent->append(theContext, aSubSh);
569 bool Model_AttributeSelection::update()
571 FeaturePtr aContextFeature = contextFeature();
572 if (aContextFeature.get()) {
573 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
576 TDF_Label aSelLab = selectionLabel();
577 ResultPtr aContext = context();
579 return setInvalidIfFalse(aSelLab, false);
580 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
581 return setInvalidIfFalse(aSelLab, aContext->shape() && !aContext->shape()->isNull());
584 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
585 std::shared_ptr<GeomAPI_Shape> aNoSelection;
586 bool aResult = selectPart(aContext, aNoSelection, true);
587 aResult = setInvalidIfFalse(aSelLab, aResult);
589 owner()->data()->sendAttributeUpdated(this);
594 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
595 // body: just a named shape, use topological selection mechanism
596 bool aResult = false;
597 TopoDS_Shape anOldShape;
598 Handle(TNaming_NamedShape) aNS;
599 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
600 anOldShape = aNS->Get();
602 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
603 Selector_Selector aSelector(aSelLab, baseDocumentLab());
604 aResult = aSelector.restore(aContextShape);
605 setInvalidIfFalse(aSelLab, aResult);
607 TopoDS_Shape aNewShape;
608 if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
609 aNewShape = aNS->Get();
611 if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape)) {
612 // shape type should not be changed: if shape becomes compound of such shapes, then split
613 if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
614 anOldShape.ShapeType() != aNewShape.ShapeType() &&
615 aNewShape.ShapeType() == TopAbs_COMPOUND) {
616 split(aContext, aNewShape, anOldShape.ShapeType());
618 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
623 if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
625 std::shared_ptr<Model_ResultConstruction> aConstructionContext =
626 std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
627 if (!aConstructionContext->isInfinite()) {
628 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
629 Selector_Selector aSelector(aSelLab, baseDocumentLab());
630 TopoDS_Shape anOldShape = aSelector.value();
631 aResult = aSelector.restore(aContextShape);
632 setInvalidIfFalse(aSelLab, aResult);
633 if (aResult && !anOldShape.IsEqual(aSelector.value()))
634 owner()->data()->sendAttributeUpdated(this); // send updated if shape is changed
636 owner()->data()->sendAttributeUpdated(this); // send updated if "update" called in any way
640 return setInvalidIfFalse(aSelLab, false); // unknown case
643 void Model_AttributeSelection::selectBody(
644 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
646 // perform the selection
647 TopoDS_Shape aContext;
649 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
651 aContext = aBody->shape()->impl<TopoDS_Shape>();
654 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
656 aContext = aResult->shape()->impl<TopoDS_Shape>();
658 Events_InfoMessage("Model_AttributeSelection", "A result with shape is expected").send();
663 if (!aContext.IsNull()) {
664 TDF_Label aSelLab = selectionLabel();
665 TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
667 bool aSelectorOk = true;
668 Selector_Selector aSelector(aSelLab, baseDocumentLab());
670 aSelectorOk = aSelector.select(aContext, aNewSub, myIsGeometricalSelection);
672 aSelectorOk = aSelector.store(aContext);
678 TopoDS_Shape aShape = aSelector.value();
679 aSelectorOk = !aShape.IsNull() && aShape.ShapeType() == aNewSub.ShapeType();
681 setInvalidIfFalse(aSelLab, aSelectorOk);
685 bool Model_AttributeSelection::selectPart(
686 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
687 const bool theUpdate)
689 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
690 if (!aPart.get() || !aPart->isActivated())
691 return true; // postponed naming
693 Handle(TDataStd_Integer) anIndex;
694 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
695 // by internal selection
696 if (anIndex->Get() > 0) {
697 // update the selection by index
698 return aPart->updateInPart(anIndex->Get());
700 return true; // nothing to do, referencing just by name
703 return true; // nothing to do, referencing just by name
705 // store the shape (in case part is not loaded it should be useful
707 std::string aName = theContext->data()->name();
708 if (!theSubShape.get() || theSubShape->isNull()) {// the whole part shape is selected
709 aShape = theContext->shape()->impl<TopoDS_Shape>();
711 aShape = theSubShape->impl<TopoDS_Shape>();
713 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
714 TDataStd_Integer::Set(selectionLabel(), anIndex);
716 TNaming_Builder aBuilder(selectionLabel());
717 aBuilder.Select(aShape, aShape);
718 // identify by name in the part
719 TDataStd_Name::Set(selectionLabel(), aName.c_str());
720 return !aName.empty();
723 TDF_Label Model_AttributeSelection::selectionLabel()
725 return myRef.myRef->Label().FindChild(1);
728 /// prefixes of the shape names with centers defined
729 static std::map<ModelAPI_AttributeSelection::CenterType, std::string> kCENTERS_PREFIX;
731 /// returns the map that contains all possible prefixes of the center-names
732 static std::map<ModelAPI_AttributeSelection::CenterType, std::string>& centersMap()
734 if (kCENTERS_PREFIX.empty()) { // fill map by initial values
735 kCENTERS_PREFIX[ModelAPI_AttributeSelection::CIRCLE_CENTER] = "__cc";
736 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS] = "__eff";
737 kCENTERS_PREFIX[ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS] = "__esf";
739 return kCENTERS_PREFIX;
742 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
744 std::string aName("");
745 if(!this->isInitialized())
746 return !theDefaultName.empty() ? theDefaultName : aName;
748 // not argument has not parametric name (filters)
749 if (!this->isArgument() || (myParent && !myParent->isArgument())) {
750 GeomShapePtr aShape = value();
751 if (!aShape.get() && context().get())
752 aShape = context()->shape();
755 aName = aShape->shapeTypeStr();
757 aName += std::string("_") +
758 TCollection_AsciiString(selectionLabel().Father().Tag()).ToCString();
764 CenterType aCenterType = NOT_CENTER;
765 std::shared_ptr<GeomAPI_Shape> aSubSh = internalValue(aCenterType);
766 ResultPtr aCont = context();
769 (aCont->groupName() == ModelAPI_ResultConstruction::group() && contextFeature().get())) {
770 // selection of a full feature
771 FeaturePtr aFeatureCont = contextFeature();
772 if (aFeatureCont.get()) {
773 return kWHOLE_FEATURE + aFeatureCont->name();
775 // in case of selection of removed result
779 TDF_Label aSelLab = selectionLabel();
780 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
781 return contextName(aCont);
784 // if it is in result of another part
785 if (aCont->groupName() == ModelAPI_ResultPart::group()) {
786 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCont);
788 GeomShapePtr aValue = value();
790 return aPart->data()->name() + "/" + aPart->nameInPart(aValue, anIndex);
792 return aPart->data()->name();
796 // whole infinitive construction
797 if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
798 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
799 if (aConstr->isInfinite()) {
800 return contextName(aCont);
804 Selector_Selector aSelector(aSelLab, baseDocumentLab());
806 if (aCont->shape().get() && aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
807 aResult = aSelector.name(this);
808 if (aCenterType != NOT_CENTER) {
809 aResult += centersMap()[aCenterType];
814 // returns the center type and modifies the shape name if this name is center-name
815 static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::string& theShapeName)
817 std::map<ModelAPI_AttributeSelection::CenterType, std::string>::iterator aPrefixIter =
818 centersMap().begin();
819 for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
820 std::size_t aFound = theShapeName.find(aPrefixIter->second);
821 if (aFound != std::string::npos &&
822 aFound == theShapeName.size() - aPrefixIter->second.size()) {
823 theShapeName = theShapeName.substr(0, aFound);
824 return aPrefixIter->first;
827 return ModelAPI_AttributeSelection::NOT_CENTER;
830 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
831 void Model_AttributeSelection::selectSubShape(
832 const std::string& theType, const std::string& theSubShapeName)
834 if(theSubShapeName.empty() || theType.empty()) return;
836 std::string aSubShapeName = theSubShapeName;
837 CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
838 centerTypeByName(aSubShapeName) : NOT_CENTER;
839 std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
840 static const GeomShapePtr anEmptyShape;
842 // first iteration is selection by name without center prefix, second - in case of problem,
843 // try with initial name
844 for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
845 if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
846 aSubShapeName = theSubShapeName;
847 aCenterType = NOT_CENTER;
849 } else if (aUseCenter != 1) continue;
850 TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(aType));
852 std::shared_ptr<Model_Document> aDoc =
853 std::dynamic_pointer_cast<Model_Document>(owner()->document());
854 // check this is Part-name: 2 delimiters in the name
855 std::size_t aPartEnd = aSubShapeName.find('/');
856 if (aPartEnd != std::string::npos) {
857 std::string aPartName = aSubShapeName.substr(0, aPartEnd);
858 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
859 if (aPartName == aRootDoc->kind()) {
860 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
861 aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
864 owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
865 if (aFound.get()) { // found such part, so asking it for the name
866 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
867 std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
868 if (aNameInPart.empty()) { // whole part
869 setValue(aPart, anEmptyShape);
873 std::shared_ptr<GeomAPI_Shape> aSelected =
874 aPart->shapeInPart(aNameInPart, aType, anIndex);
875 if (aSelected.get()) {
876 if (aCenterType != NOT_CENTER) {
877 if (!aSelected->isEdge())
879 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
880 setValueCenter(aPart, aSelectedEdge, aCenterType);
882 setValue(aPart, aSelected);
883 TDataStd_Integer::Set(selectionLabel(), anIndex);
890 // check this is a whole feature context
891 if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
892 aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
893 std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
894 ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
896 setValue(anObj, anEmptyShape);
901 // the whole result selection check
902 if (aSubShapeName.find('/') == std::string::npos) {
903 ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
905 aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
907 aRes = aDoc->objectByName(ModelAPI_ResultGroup::group(), aSubShapeName);
910 setValue(aRes, anEmptyShape);
915 Selector_Selector aSelector(selectionLabel(), baseDocumentLab());
916 myRestoreDocument = aDoc;
917 TDF_Label aContextLabel = aSelector.restoreByName(
918 aSubShapeName, aShapeType, this, myIsGeometricalSelection);
919 myRestoreDocument.reset();
920 if (!aContextLabel.IsNull()) {
921 ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
922 if (aContext.get() && aContext->shape().get()) {
923 TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
924 if (aSelector.solve(aContextShape)) {
925 TopoDS_Shape aSelectorShape = aSelector.value();
926 GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
927 aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelectorShape));
928 // make the context result the latest existing
929 aContext = newestContext(aContext, aShapeToBeSelected);
930 if (myIsGeometricalSelection || aCenterType == NOT_CENTER) {
931 // store the currently generated name
932 selectionLabel().ForgetAllAttributes(true);
933 bool aToUnblock = false;
934 aToUnblock = !owner()->data()->blockSendAttributeUpdated(true);
935 myRef.setValue(aContext);
936 aSelector.store(aContextShape);
937 owner()->data()->sendAttributeUpdated(this);
939 owner()->data()->blockSendAttributeUpdated(false);
941 } else { // re-select center of circle/arc by context and value
942 if (!aShapeToBeSelected->isEdge())
944 std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
945 setValueCenter(aContext, aSelectedEdge, aCenterType);
953 TDF_Label aSelLab = selectionLabel();
954 setInvalidIfFalse(aSelLab, false);
958 void Model_AttributeSelection::selectSubShape(const std::string& theType,
959 const GeomPointPtr& thePoint)
961 if (theType.empty() || !thePoint)
964 // list of parent features
965 FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
966 std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
968 int aSelectionIndex = 0;
969 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
970 if (aType == GeomAPI_Shape::SHAPE) {
971 // possibly, the string consists of the type and the index,
972 // thus, try to separate them
973 size_t aUndersporePos = theType.find_first_of('_');
974 if (aUndersporePos != std::string::npos)
975 aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
977 if (aType != GeomAPI_Shape::SHAPE) {
978 for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
979 aChar != theType.end(); ++aChar) {
980 if (std::isdigit(*aChar))
981 aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
987 aSelectionIndex -= 1;
991 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
993 // collect features from PartSet and the current part
994 SessionPtr aSession = ModelAPI_Session::get();
995 std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
996 if (aSession->moduleDocument() != owner()->document()) {
997 std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
998 aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
1000 // Process results of all features from the last to the first
1001 // to find appropriate sub-shape
1002 for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
1003 anIt != aFeatures.rend(); ++anIt) {
1004 // selection cannot be linked to the parent features
1005 if (aParents.find(*anIt) != aParents.end())
1007 // check the feature is a part of composite feature (like sketch elements),
1008 // then do not process it, it will be processed in scope of composite feature
1009 bool isSubOfComposite = false;
1010 const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
1011 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
1012 aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
1013 FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
1014 CompositeFeaturePtr aCompFeature =
1015 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
1016 isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
1018 if (isSubOfComposite)
1021 // process results of the current feature to find appropriate sub-shape
1022 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
1023 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
1024 for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
1025 ++anApIt; // skip this shape, because one of the previous is selected
1027 if (anApIt != anAppropriate.end()) {
1028 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
1029 setValue(anApIt->myResult, anApIt->mySubshape);
1031 setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
1032 (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
1038 TDF_Label aSelLab = selectionLabel();
1039 setInvalidIfFalse(aSelLab, false);
1043 void Model_AttributeSelection::selectSubShape(const std::string& theType,
1044 const std::string& theContextName, const int theIndex)
1046 // selection of context by name
1047 selectSubShape(theType, theContextName);
1048 ResultPtr aContext = context();
1049 if (aContext.get()) {
1050 GeomShapePtr aContShape = aContext->shape();
1051 if (aContShape.get()) {
1052 GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
1053 GeomShapePtr aValue = aNExp.shape(theIndex);
1055 setValue(aContext, aValue);
1060 void Model_AttributeSelection::setId(int theID)
1062 std::shared_ptr<GeomAPI_Shape> aSelection;
1064 ResultPtr aContextRes = context();
1065 // support for compsolids:
1066 while(ModelAPI_Tools::bodyOwner(aContextRes).get()) {
1067 aContextRes = ModelAPI_Tools::bodyOwner(aContextRes);
1069 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
1071 TopoDS_Shape aMainShape = aContext->impl<TopoDS_Shape>();
1072 // searching for the latest main shape
1073 if (theID > 0 && aContext && !aContext->isNull())
1075 std::shared_ptr<Model_Document> aDoc =
1076 std::dynamic_pointer_cast<Model_Document>(aContextRes->document());
1078 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aMainShape, aDoc->generalLabel());
1079 if (!aNS.IsNull()) {
1080 aMainShape = TNaming_Tool::CurrentShape(aNS);
1084 TopTools_IndexedMapOfShape aSubShapesMap;
1085 TopExp::MapShapes(aMainShape, aSubShapesMap);
1086 const TopoDS_Shape& aSelShape = aSubShapesMap.FindKey(theID);
1088 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
1089 aResult->setImpl(new TopoDS_Shape(aSelShape));
1091 aSelection = aResult;
1094 setValue(aContextRes, aSelection);
1097 std::string Model_AttributeSelection::contextName(const ResultPtr& theContext) const
1099 std::string aResult;
1100 if (owner()->document() != theContext->document()) {
1101 if (theContext->document() == ModelAPI_Session::get()->moduleDocument()) {
1102 aResult = theContext->document()->kind() + "/";
1104 ResultPtr aDocRes = ModelAPI_Tools::findPartResult(
1105 ModelAPI_Session::get()->moduleDocument(), theContext->document());
1106 if (aDocRes.get()) {
1107 aResult = aDocRes->data()->name() + "/";
1111 aResult += theContext->data()->name();
1115 void Model_AttributeSelection::computeValues(
1116 ResultPtr theOldContext, ResultPtr theNewContext, TopoDS_Shape theValShape,
1117 TopTools_ListOfShape& theShapes)
1119 bool aWasWholeContext = theValShape.IsNull();
1120 if (aWasWholeContext) {
1121 theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
1123 TopAbs_ShapeEnum aValType = theValShape.ShapeType();
1124 TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
1125 // if a new value is unchanged in the new context, do nothing: value is correct
1126 TopExp_Explorer aSubExp(aNewContShape, aValType);
1127 for(; aSubExp.More(); aSubExp.Next()) {
1128 if (aSubExp.Current().IsSame(theValShape)) {
1129 theShapes.Append(theValShape);
1133 // if new context becomes compsolid, the resulting sub may be in sub-solids
1134 std::list<ResultPtr> aNewToIterate;
1135 aNewToIterate.push_back(theNewContext);
1136 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theNewContext);
1138 std::list<ResultPtr> allNewContextSubs;
1139 ModelAPI_Tools::allSubs(aComp, allNewContextSubs);
1140 std::list<ResultPtr>::iterator aSub = allNewContextSubs.begin();
1141 for(; aSub != allNewContextSubs.end(); aSub++) {
1142 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSub);
1143 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1144 aNewToIterate.push_back(aBody);
1148 // first iteration: searching for the whole shape appearance (like face of the box)
1149 // second iteration: searching for sub-shapes that contain the sub (like vertex on faces)
1150 int aToFindPart = 0;
1151 TopTools_DataMapOfShapeShape aNewToOld; // map from new containers to old containers (with val)
1152 TopTools_MapOfShape anOlds; // to know how many olds produced new containers
1153 for(; aToFindPart != 2 && theShapes.IsEmpty(); aToFindPart++) {
1154 std::list<ResultPtr>::iterator aNewContIter = aNewToIterate.begin();
1155 for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
1156 std::shared_ptr<Model_Data> aNewData =
1157 std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
1158 TDF_Label aNewLab = aNewData->shapeLab();
1159 // searching for produced sub-shape fully on some label
1160 TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID());
1161 for(; aNSIter.More(); aNSIter.Next()) {
1162 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
1163 for(TNaming_Iterator aPairIter(aNS); aPairIter.More(); aPairIter.Next()) {
1164 if (aToFindPart == 0) { // search shape is fully inside
1165 if (aPairIter.OldShape().IsSame(theValShape)) {
1166 if (aPairIter.NewShape().IsNull()) {// value was removed
1170 // don't add edges generated from faces
1171 if (aPairIter.NewShape().ShapeType() <= aValType)
1172 theShapes.Append(aPairIter.NewShape());
1174 } else if (!aPairIter.OldShape().IsNull()) { // search shape that contains this sub
1175 TopExp_Explorer anExp(aPairIter.OldShape(), aValType);
1176 for(; anExp.More(); anExp.Next()) {
1177 if (anExp.Current().IsSame(theValShape)) { // found a new container
1178 if (aPairIter.NewShape().IsNull()) // skip removed high-level shape
1180 aNewToOld.Bind(aPairIter.NewShape(), aPairIter.OldShape());
1181 anOlds.Add(aPairIter.OldShape());
1190 if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
1191 // also iterate the whole old shape to find not-modified shapes that contain this old
1192 TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
1193 NCollection_Map<TopAbs_ShapeEnum> aNewTypes; // types of shapes to iterate
1194 TopTools_DataMapOfShapeShape::Iterator aNewTypeIter(aNewToOld);
1195 for(; aNewTypeIter.More(); aNewTypeIter.Next()) {
1196 if (aNewTypeIter.Key().ShapeType() != aValType)
1197 aNewTypes.Add(aNewTypeIter.Key().ShapeType());
1199 NCollection_Map<TopAbs_ShapeEnum>::Iterator aTypeIter(aNewTypes);
1200 for(; aTypeIter.More(); aTypeIter.Next()) {
1201 for(TopExp_Explorer anExp(anOldContShape, aTypeIter.Value()); anExp.More(); anExp.Next()) {
1202 TopoDS_Shape anOld = anExp.Current();
1203 if (aNewToOld.IsBound(anOld) || anOlds.Contains(anOld)) // this was modified
1205 TopExp_Explorer aValExp(anOld, aValType);
1206 for(; aValExp.More(); aValExp.Next()) {
1207 const TopoDS_Shape& anUnchanged = aValExp.Current();
1208 if (anUnchanged.IsSame(theValShape)) {
1209 aNewToOld.Bind(anOld, anOld);
1217 // map of sub-shapes -> number of occurrences of these shapes in containers
1218 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
1219 TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
1220 for(; aContIter.More(); aContIter.Next()) {
1221 TopExp_Explorer aSubExp(aContIter.Key(), aValType);
1222 for(; aSubExp.More(); aSubExp.Next()) {
1223 if (!aSubs.IsBound(aSubExp.Current())) {
1224 aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
1226 // store old to know how many olds produced this shape
1227 aSubs.ChangeFind(aSubExp.Current()).Add(aContIter.Value());
1230 // if sub is appeared same times in containers as the number of old shapes that contain it
1231 int aCountInOld = anOlds.Size();
1232 NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher>::Iterator
1234 for(; aSubsIter.More(); aSubsIter.Next()) {
1235 if (aSubsIter.Value().Size() == aCountInOld) {
1236 TopoDS_Shape anOld = aSubsIter.Key();
1237 // check this exists in the new shape
1238 TopExp_Explorer aNew(aNewContShape, anOld.ShapeType());
1239 for (; aNew.More(); aNew.Next()) {
1240 if (aNew.Current().IsSame(anOld))
1244 theShapes.Append(anOld);
1248 if (theShapes.IsEmpty()) { // nothing was changed
1249 if (aWasWholeContext)
1250 theShapes.Append(TopoDS_Shape());
1251 else { // if theValShape exists in new context, add it without changes, otherwise - nothing
1252 for (TopExp_Explorer aNew(aNewContShape, aValType); aNew.More(); aNew.Next()){
1253 if (aNew.Current().IsSame(theValShape)) {
1254 theShapes.Append(theValShape);
1259 } else if (theShapes.Size() > 1) {
1260 // check it is possible to remove extra sub-shapes:
1261 // keep only shapes with the same number of containers if possible
1262 TopAbs_ShapeEnum anAncType = TopAbs_FACE;
1263 if (aValType == TopAbs_VERTEX)
1264 anAncType = TopAbs_EDGE;
1265 TopoDS_Shape anOldContext = theOldContext->shape()->impl<TopoDS_Shape>();
1266 TopTools_IndexedDataMapOfShapeListOfShape anOldMap;
1267 TopExp::MapShapesAndUniqueAncestors(anOldContext, aValType, anAncType, anOldMap);
1268 if (anOldMap.Contains(theValShape)) {
1269 int aNumInOld = anOldMap.FindFromKey(theValShape).Extent();
1270 TopTools_IndexedDataMapOfShapeListOfShape aNewMap;
1271 TopExp::MapShapesAndUniqueAncestors(aNewContShape, aValType, anAncType, aNewMap);
1272 TopTools_ListOfShape aNewResults;
1273 for(TopTools_ListOfShape::Iterator aNewSubs(theShapes); aNewSubs.More(); aNewSubs.Next()) {
1274 TopoDS_Shape aCand = aNewSubs.Value();
1275 if (aNewMap.Contains(aCand) && aNewMap.FindFromKey(aCand).Extent() == aNumInOld)
1276 aNewResults.Append(aCand);
1278 if (!aNewResults.IsEmpty() && aNewResults.Size() < theShapes.Size())
1279 theShapes = aNewResults;
1284 bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document> theDoc,
1285 const TopoDS_Shape theContShape, ResultPtr theContext, TopoDS_Shape theValShape,
1286 TDF_Label theAccessLabel,
1287 std::list<ResultPtr>& theResults, TopTools_ListOfShape& theValShapes)
1289 std::set<ResultPtr> aResults; // to avoid duplicates, new context, null if deleted
1290 // iterate context and shape, but also if it is sub-shape of main shape, check also it
1291 TopTools_ListOfShape aContextList;
1292 aContextList.Append(theContShape);
1293 if (theContext.get()) {
1294 ResultPtr aComposite = ModelAPI_Tools::bodyOwner(theContext);
1295 if (aComposite.get() && aComposite->shape().get() && !aComposite->shape()->isNull())
1296 aContextList.Append(aComposite->shape()->impl<TopoDS_Shape>());
1298 for(TopTools_ListOfShape::Iterator aContIter(aContextList); aContIter.More(); aContIter.Next()) {
1299 TNaming_SameShapeIterator aModifIter(aContIter.ChangeValue(), theAccessLabel);
1300 for(; aModifIter.More(); aModifIter.Next()) {
1301 TDF_Label anObjLab = aModifIter.Label().Father();
1302 ResultPtr aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1303 (theDoc->objects()->object(anObjLab));
1304 if (!aModifierObj.get()) {
1305 // #2241: shape may be sub-element of new object, not main (shell created from faces)
1306 if (!anObjLab.IsRoot())
1307 aModifierObj = std::dynamic_pointer_cast<ModelAPI_Result>
1308 (theDoc->objects()->object(anObjLab.Father()));
1309 if (!aModifierObj.get())
1312 FeaturePtr aModifierFeat = theDoc->feature(aModifierObj);
1313 if (!aModifierFeat.get())
1315 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1316 if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
1317 continue; // the modifier feature is later than this, so, should not be used
1318 FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
1319 if (aCurrentModifierFeat == aModifierFeat ||
1320 !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
1321 continue; // the current modifier is later than the found, so, useless
1322 Handle(TNaming_NamedShape) aNewNS;
1323 aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
1324 if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
1325 aResults.insert(aModifierObj);
1326 } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
1327 aResults.insert(ResultPtr());
1328 } else { // not-processed modification => don't support it
1333 // if there exist context composite and sub-result(s), leave only sub(s)
1334 for(std::set<ResultPtr>::iterator aResIter = aResults.begin(); aResIter != aResults.end();) {
1335 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1336 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1337 if (aResults.count(aParent))
1339 if (aParent.get()) { // erase from set, so, restart iteration
1340 aResults.erase(aParent);
1341 aResIter = aResults.begin();
1345 if (aResults.empty()) {
1346 // check the context become concealed by operation which is earlier than this selection
1347 std::list<ResultPtr> allRes;
1348 ResultPtr aRoot = ModelAPI_Tools::bodyOwner(theContext, true);
1351 ResultBodyPtr aRootBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aRoot);
1352 if (aRootBody.get()) {
1353 ModelAPI_Tools::allSubs(aRootBody, allRes);
1354 allRes.push_back(aRootBody);
1356 allRes.push_back(aRoot);
1358 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1359 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1360 ResultPtr aResCont = *aSub;
1361 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1362 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1363 for (; aRef != aRefs.end(); aRef++) {
1364 if (!aRef->get() || !(*aRef)->owner().get())
1366 // concealed attribute only
1367 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1368 if (aRefFeat == aThisFeature)
1370 if (!ModelAPI_Session::get()->validators()->isConcealed(
1371 aRefFeat->getKind(), (*aRef)->id()))
1373 if (theDoc->isLaterByDep(aThisFeature, aRefFeat)) {
1374 // for extrusion cut in python script the nested sketch reference may be concealed before
1375 // it is nested, so, check this composite feature is valid
1376 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
1377 // need to be validated to update the "Apply" state if not previewed
1378 if (aFactory->validate(aRefFeat)) {
1379 // there could be a reference to unmodified object, check result contain same shape
1380 std::list<ResultPtr> aRefResults;
1381 ModelAPI_Tools::allResults(aRefFeat, aRefResults);
1382 std::list<ResultPtr>::iterator aRefIter = aRefResults.begin();
1383 for(; aRefIter != aRefResults.end(); aRefIter++) {
1384 ResultBodyPtr aRefBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefIter);
1385 if (!aRefBody.get() || aRefBody->numberOfSubs() != 0) // iterate only leafs
1387 GeomShapePtr aRefShape = aRefBody->shape();
1388 if (!aRefShape.get() || aRefShape->isNull())
1390 if (aRefShape->impl<TopoDS_Shape>().IsSame(theContShape)) {
1391 // add the new context result with the same shape
1392 aResults.insert(aRefBody);
1395 if (aResults.empty())
1396 return true; // feature conceals result, return true, so the context will be removed
1401 if (aResults.empty())
1402 return false; // no modifications found, must stay the same
1404 // iterate all results to find further modifications
1405 std::set<ResultPtr>::iterator aResIter = aResults.begin();
1406 for(; aResIter != aResults.end(); aResIter++) {
1407 if (aResIter->get() != NULL) {
1408 // compute new values by two contexts: the old and the new
1409 TopTools_ListOfShape aValShapes;
1410 computeValues(theContext, *aResIter, theValShape, aValShapes);
1412 TopTools_ListIteratorOfListOfShape aNewVal(aValShapes);
1413 for(; aNewVal.More(); aNewVal.Next()) {
1414 std::list<ResultPtr> aNewRes;
1415 TopTools_ListOfShape aNewUpdatedVal;
1416 TopoDS_Shape aNewValSh = aNewVal.Value();
1417 TopoDS_Shape aNewContShape = (*aResIter)->shape()->impl<TopoDS_Shape>();
1418 if (theValShape.IsNull() && aNewContShape.IsSame(aNewValSh))
1419 aNewValSh.Nullify();
1420 if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
1421 theAccessLabel, aNewRes, aNewUpdatedVal))
1423 // append new results instead of the current ones
1424 std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
1425 TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
1426 for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
1427 theResults.push_back(*aNewIter);
1428 theValShapes.Append(aNewUpdVal.Value());
1430 } else { // the current result is good
1431 theResults.push_back(*aResIter);
1432 theValShapes.Append(aNewValSh);
1437 return true; // theResults must be empty: everything is deleted
1440 void Model_AttributeSelection::updateInHistory(bool& theRemove)
1442 ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
1443 // only bodies and parts may be modified later in the history, don't do anything otherwise
1444 if (!aContext.get() || (aContext->groupName() != ModelAPI_ResultBody::group() &&
1445 aContext->groupName() != ModelAPI_ResultPart::group()))
1447 std::shared_ptr<Model_Document> aDoc =
1448 std::dynamic_pointer_cast<Model_Document>(aContext->document());
1449 std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
1450 if (!aContData.get() || !aContData->isValid())
1452 TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
1454 // checking this may be just a reference to another context (same shape), so use that label
1455 Handle(TNaming_NamedShape) aContNS;
1456 Handle(TDF_Reference) aRefAttr;
1457 while(!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS) &&
1458 aContLab.FindAttribute(TDF_Reference::GetID(), aRefAttr))
1459 aContLab = aRefAttr->Get();
1461 if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
1462 bool aFoundNewContext = true;
1463 ResultPtr aNewContext = aContext;
1464 while(aFoundNewContext) {
1465 aFoundNewContext = false;
1466 // parts have no shape in result, so, trace references using the Part info
1467 if (aNewContext->groupName() == ModelAPI_ResultPart::group()) {
1468 ResultPartPtr aPartContext = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewContext);
1469 if (aPartContext.get()) { // searching for the up to date references to the referenced cont
1470 const std::set<AttributePtr>& aRefs = aPartContext->data()->refsToMe();
1471 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1472 for(; aRef != aRefs.end(); aRef++) {
1473 // to avoid detection of part changes by local selection only
1474 AttributeSelectionPtr aSel =
1475 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
1476 if (aSel.get() && aSel->value().get() &&
1477 !aSel->value()->isSame(aSel->context()->shape()))
1480 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1482 if (aRefFeat.get() && aRefFeat != owner() && aRefFeat->firstResult().get()) {
1483 // check the reference is concealed: #2900
1484 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1485 if (!aValidators->isConcealed(aRefFeat->getKind(), (*aRef)->id()))
1487 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1488 if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
1489 aFoundNewContext = true;
1490 aNewContext = aRefFeat->firstResult();
1497 if (aNewContext != aContext) {
1498 setValue(aNewContext, value());
1502 FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
1503 FeaturePtr aCurrentModifierFeat = aDoc->feature(aContext);
1504 // iterate the context shape modifications in order to find a feature that is upper in history
1505 // that this one and is really modifies the referenced result to refer to it
1506 ResultPtr aModifierResFound;
1507 TNaming_Iterator aPairIter(aContNS);
1508 if (!aPairIter.More())
1510 TopoDS_Shape aNewCShape = aPairIter.NewShape();
1511 bool anIterate = true;
1512 // trying to update also the sub-shape selected
1513 GeomShapePtr aSubShape = value();
1514 if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
1516 TopoDS_Shape aValShape;
1517 if (aSubShape.get()) {
1518 aValShape = aSubShape->impl<TopoDS_Shape>();
1521 std::list<ResultPtr> aNewContexts;
1522 TopTools_ListOfShape aValShapes;
1523 if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
1525 std::set<ResultPtr> allContexts, aSkippedContext;
1526 std::list<ResultPtr>::iterator aNewContext = aNewContexts.begin();
1527 for(; aNewContext != aNewContexts.end(); aNewContext++)
1528 allContexts.insert(*aNewContext);
1530 // if there exist context composite and sub-result(s), leave only sub(s)
1531 std::set<ResultPtr>::iterator aResIter = allContexts.begin();
1532 for(; aResIter != allContexts.end(); aResIter++) {
1533 ResultPtr aParent = ModelAPI_Tools::bodyOwner(*aResIter);
1534 for(; aParent.get(); aParent = ModelAPI_Tools::bodyOwner(aParent))
1535 if (allContexts.count(aParent))
1536 aSkippedContext.insert(aParent);
1539 GeomAPI_Shape::ShapeType aListShapeType = GeomAPI_Shape::SHAPE;
1541 if (myParent->selectionType() == "VERTEX" || myParent->selectionType() == "Vertices")
1542 aListShapeType = GeomAPI_Shape::VERTEX;
1543 else if (myParent->selectionType() == "EDGE" || myParent->selectionType() == "Edges")
1544 aListShapeType = GeomAPI_Shape::EDGE;
1545 else if (myParent->selectionType() == "FACE" || myParent->selectionType() == "Faces")
1546 aListShapeType = GeomAPI_Shape::FACE;
1549 std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
1550 TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
1551 bool aFirst = true; // first is set to this, next are appended to parent
1552 for(; aNewCont != aNewContexts.end(); aNewCont++, aNewValues.Next()) {
1553 if (aSkippedContext.count(*aNewCont))
1556 GeomShapePtr aValueShape;
1557 if (!aNewValues.Value().IsNull()) {
1558 aValueShape = std::make_shared<GeomAPI_Shape>();
1559 aValueShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewValues.Value()));
1561 // Check that list has the same type of shape selection before adding.
1562 GeomAPI_Shape::ShapeType aShapeShapeType = GeomAPI_Shape::SHAPE;
1563 if (aValueShape.get()) {
1564 aShapeShapeType = aValueShape->shapeType();
1566 aShapeShapeType = (*aNewCont)->shape()->shapeType();
1568 if (aListShapeType != GeomAPI_Shape::SHAPE && aListShapeType != aShapeShapeType) {
1572 ResultPtr aNewContext = *aNewCont;
1573 if (aValueShape.get()) { // #2892 if context is higher level result, search this sub in lower
1574 ResultBodyPtr aBodyContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aNewContext);
1575 if (aBodyContext.get() && aBodyContext->numberOfSubs() != 0) {
1576 std::list<ResultPtr> aLower;
1577 ModelAPI_Tools::allSubs(aBodyContext, aLower, true);
1578 for(std::list<ResultPtr>::iterator aL = aLower.begin(); aL != aLower.end(); aL++) {
1579 GeomShapePtr aLShape = (*aL)->shape();
1580 if (aLShape.get() && !aLShape->isNull()) {
1581 if (aLShape->isSubShape(aValueShape, false)) {
1591 if (!myParent || !myParent->isInList(aNewContext, aValueShape)) { // avoid duplicates
1592 setValue(aNewContext, aValueShape);
1595 } else if (myParent) {
1596 if (!myParent->isInList(aNewContext, aValueShape)) // avoid addition of duplicates
1597 myParent->append(aNewContext, aValueShape);
1600 if (aFirst) { // nothing was added, all results were deleted
1604 ResultPtr anEmptyContext;
1605 std::shared_ptr<GeomAPI_Shape> anEmptyShape;
1606 setValue(anEmptyContext, anEmptyShape); // nullify the selection
1613 void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent)
1615 myParent = theParent;
1618 std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
1620 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1621 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1622 FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1623 bool aBaseDocumnetUsed = false;
1624 if (!aFeatureOwner.get()) { // use module document
1625 aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
1626 aFeatureOwner = aDoc->featureByLab(theSelectionLab);
1627 aBaseDocumnetUsed = true;
1629 if (aFeatureOwner.get()) {
1630 // if it is sub-element of the sketch, the context name is the name of the sketch
1631 // searching also for result - real context
1633 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
1634 if (aComposite.get() && aComposite->results().size() == 1 &&
1635 aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
1636 aFeatureOwner = aComposite;
1637 aResult = aFeatureOwner->firstResult();
1639 aResult = aDoc->resultByLab(theSelectionLab);
1641 if (aResult.get()) {
1642 // this is to avoid duplicated names of results problem
1643 std::string aContextName = aResult->data()->name();
1644 // myLab corresponds to the current time
1645 TDF_Label aCurrentLab = selectionLabel();
1646 while(aCurrentLab.Depth() > 3)
1647 aCurrentLab = aCurrentLab.Father();
1649 int aNumInHistoryNames =
1650 aDoc->numberOfNameInHistory(aResult, aCurrentLab);
1651 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
1652 aContextName = "_" + aContextName;
1653 aNumInHistoryNames--;
1655 if (aBaseDocumnetUsed)
1656 aContextName = aDoc->kind() + "/" + aContextName;
1657 return aContextName;
1660 return ""; // invalid case
1663 /// This method restores by the context and value name the context label and
1664 /// sub-label where the value is. Returns true if it is valid.
1665 bool Model_AttributeSelection::restoreContext(std::string theName,
1666 TDF_Label& theContext, TDF_Label& theValue)
1668 static const GeomShapePtr anEmptyShape; // to store context only
1669 std::string aName = theName;
1670 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1671 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1673 // remove the sub-value part if exists
1674 std::string aSubShapeName = aName;
1675 std::string::size_type n = aName.find('/');
1676 if (n != std::string::npos) {
1677 aName = aName.substr(0, n);
1680 if (aName.empty()) return false;
1681 bool anUniqueContext = false;
1682 ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1683 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
1685 aDoc = std::dynamic_pointer_cast<Model_Document>(
1686 ModelAPI_Session::get()->moduleDocument());
1687 if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
1688 aSubShapeName = theName.substr(aDoc->kind().size() + 1);
1689 aName = aSubShapeName;
1690 std::string::size_type n = aName.find('/');
1691 if (n != std::string::npos) {
1692 aName = aName.substr(0, n);
1695 aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
1696 if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
1700 // searching the sub-shape
1701 static const ResultPtr anEmpty;
1702 theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
1704 // sketch sub-component shape and name is located in separated feature label, try the sub-name
1705 if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
1706 std::string::size_type aSlash = aSubShapeName.rfind('/');
1707 if (aSlash != std::string::npos) {
1708 std::string aCompName = aSubShapeName.substr(aSlash + 1);
1709 CompositeFeaturePtr aComposite =
1710 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
1711 if (aComposite.get() && aComposite->numberOfSubs()) {
1712 const int aSubNum = aComposite->numberOfSubs();
1713 for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
1714 FeaturePtr aSub = aComposite->subFeature(a);
1715 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1716 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
1717 for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
1718 if ((*aRes)->data()->name() == aCompName) {
1719 theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1721 } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
1722 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
1723 TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
1724 for(; aSubNames.More(); aSubNames.Next()) {
1725 if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
1726 IsEqual(aCompName.c_str())) {
1727 theValue = aSubNames.Value()->Label();
1739 theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
1744 bool Model_AttributeSelection::isLater(
1745 const TDF_Label theResult1, const TDF_Label theResult2) const
1747 std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
1748 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1749 FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
1752 FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
1755 return aDoc->isLaterByDep(aFeat1, aFeat2);
1758 ResultPtr Model_AttributeSelection::newestContext(
1759 const ResultPtr theCurrent, const GeomShapePtr theValue)
1761 ResultPtr aResult = theCurrent;
1762 GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
1763 std::shared_ptr<Model_Document> aDoc =
1764 std::dynamic_pointer_cast<Model_Document>(owner()->document());
1765 bool aFindNewContext = true;
1766 while (aFindNewContext && aResult.get()) {
1767 aFindNewContext = false;
1768 // try to find the last context to find the up to date shape
1769 TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
1770 if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
1771 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
1772 if (!aNS.IsNull()) {
1773 aNS = TNaming_Tool::CurrentNamedShape(aNS);
1775 && isLater(selectionLabel(), aNS->Label())
1776 && isLater(aNS->Label(),
1777 std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
1779 TDF_Label aLab = aNS->Label();
1780 ResultPtr aRes = aDoc->resultByLab(aLab);
1782 if (aRes->shape()->isSubShape(aSelectedShape)) {
1784 aFindNewContext = true;
1792 // TestFillWireVertex.py - sketch constructions for wire may participate too
1793 //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
1794 // try to search newer context by the concealment references
1795 // take references to all results: root one, any sub
1796 std::list<ResultPtr> allRes;
1797 ResultPtr aCompContext;
1798 ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
1799 if (aCompBody.get()) {
1800 ModelAPI_Tools::allSubs(aCompBody, allRes);
1801 allRes.push_back(aCompBody);
1802 aCompContext = aCompBody;
1805 allRes.push_back(aResult);
1807 for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
1808 ResultPtr aResCont = *aSub;
1809 ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
1810 if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
1811 continue; // only lower and higher level subs are counted
1812 const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
1813 std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
1814 for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
1815 if (!aRef->get() || !(*aRef)->owner().get())
1817 // concealed attribute only
1818 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1819 if (!ModelAPI_Session::get()->validators()->isConcealed(
1820 aRefFeat->getKind(), (*aRef)->id()))
1822 // search the feature result that contains sub-shape selected
1823 std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1825 // take all sub-results or one result
1826 std::list<ResultPtr> aRefFeatResults;
1827 ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
1828 std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
1829 for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
1830 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
1831 if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
1832 aResults.push_back(aBody);
1834 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
1836 // searching by sub-shape
1837 for (; aResIter != aResults.end(); aResIter++) {
1838 if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
1840 GeomShapePtr aShape = (*aResIter)->shape();
1841 if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
1842 aResult = *aResIter; // found new context (produced from this) with same subshape
1843 aFindNewContext = true; // continue searching further
1850 // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
1851 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1852 if (aComp && aComp->numberOfSubs()) {
1853 std::list<ResultPtr> allSubs;
1854 ModelAPI_Tools::allSubs(aComp, allSubs);
1855 std::list<ResultPtr>::iterator aS = allSubs.begin();
1856 for (; aS != allSubs.end(); aS++) {
1857 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
1858 if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
1859 aSub->shape()->isSubShape(aSelectedShape)) {
1865 // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
1866 // so, actually, the sketch element was selected (which is never concealed)
1867 if (aResult != theCurrent && aResult->isConcealed())
1868 aResult = theCurrent;
1872 void Model_AttributeSelection::combineGeometrical()
1874 if (myTmpContext.get() || myTmpSubShape.get())
1876 TDF_Label aSelLab = selectionLabel();
1877 if (aSelLab.IsAttribute(kINVALID_SELECTION) || !myRef.isInitialized())
1880 if (aSelLab.IsAttribute(kCIRCLE_CENTER) || aSelLab.IsAttribute(kELLIPSE_CENTER1) ||
1881 aSelLab.IsAttribute(kELLIPSE_CENTER2) || aSelLab.IsAttribute(kSIMPLE_REF_ID))
1884 if (aSelLab.IsAttribute(kPART_REF_ID)) {
1885 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
1886 if (!aPart.get() || !aPart->isActivated())
1887 return; // postponed naming needed
1888 Handle(TDataStd_Integer) anIndex;
1889 if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
1890 if (anIndex->Get()) { // special selection attribute was created, use it
1891 std::string aNewName;
1892 aPart->combineGeometrical(anIndex->Get(), aNewName);
1893 TDataStd_Name::Set(aSelLab, aNewName.c_str());
1899 std::shared_ptr<Model_ResultConstruction> aConstr =
1900 std::dynamic_pointer_cast<Model_ResultConstruction>(context());
1903 FeaturePtr aFeature = contextFeature();
1907 Selector_Selector aSelector(aSelLab, baseDocumentLab());
1908 TopoDS_Shape aContextShape = context()->shape()->impl<TopoDS_Shape>();
1909 if (aSelector.restore(aContextShape)) {
1910 aSelector.combineGeometrical(aContextShape);
1914 TDF_Label Model_AttributeSelection::baseDocumentLab()
1916 if (ModelAPI_Session::get()->moduleDocument() != owner()->document())
1917 return std::dynamic_pointer_cast<Model_Document>
1918 (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel();
1919 static TDF_Label anEmpty;