1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_AttributeSelection.cpp
5 // Author: Mikhail PONIKAROV
7 #include "Model_AttributeSelection.h"
8 #include "Model_Application.h"
9 #include "Model_Events.h"
10 #include "Model_Data.h"
11 #include "Model_Document.h"
12 #include "Model_SelectionNaming.h"
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_ResultPart.h>
17 #include <ModelAPI_CompositeFeature.h>
18 #include <GeomAPI_Shape.h>
19 #include <GeomAPI_PlanarEdges.h>
20 #include <Events_Error.h>
22 #include <TNaming_Selector.hxx>
23 #include <TNaming_NamedShape.hxx>
24 #include <TNaming_Tool.hxx>
25 #include <TNaming_Builder.hxx>
26 #include <TNaming_Localizer.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Compound.hxx>
29 #include <TDataStd_IntPackedMap.hxx>
30 #include <TDataStd_Integer.hxx>
31 #include <TDataStd_UAttribute.hxx>
32 #include <TDataStd_Name.hxx>
33 #include <TopTools_MapOfShape.hxx>
34 #include <TopTools_IndexedMapOfShape.hxx>
35 #include <TopTools_MapIteratorOfMapOfShape.hxx>
36 #include <TopTools_ListOfShape.hxx>
37 #include <NCollection_DataMap.hxx>
38 #include <TopExp_Explorer.hxx>
39 #include <TDF_LabelMap.hxx>
40 #include <BRep_Tool.hxx>
41 #include <BRep_Builder.hxx>
42 #include <TopoDS_Edge.hxx>
45 #include <TColStd_MapOfTransient.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
50 #include <Precision.hxx>
51 #include <TDF_ChildIterator.hxx>
52 #include <TDF_ChildIDIterator.hxx>
53 #include <TDataStd_Name.hxx>
54 #include <TopAbs_ShapeEnum.hxx>
55 #include <TopoDS_Iterator.hxx>
56 #include <TNaming_Iterator.hxx>
58 //#define DEB_NAMING 1
60 #include <BRepTools.hxx>
62 /// adeed to the index in the packed map to signalize that the vertex of edge is seleted
63 /// (multiplied by the index of the edge)
64 static const int kSTART_VERTEX_DELTA = 1000000;
65 // identifier that there is simple reference: selection equals to context
66 Standard_GUID kSIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb29");
67 // simple reference in the construction
68 Standard_GUID kCONSTUCTION_SIMPLE_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb28");
69 // reference to Part sub-object
70 Standard_GUID kPART_REF_ID("635eacb2-a1d6-4dec-8348-471fae17cb27");
72 // on this label is stored:
73 // TNaming_NamedShape - selected shape
74 // TNaming_Naming - topological selection information (for the body)
75 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
76 // TDataStd_Integer - type of the selected shape (for construction)
77 // TDF_Reference - from ReferenceAttribute, the context
78 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
79 const std::shared_ptr<GeomAPI_Shape>& theSubShape)
81 const std::shared_ptr<GeomAPI_Shape>& anOldShape = value();
82 bool isOldContext = theContext == myRef.value();
83 bool isOldShape = isOldContext &&
84 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
85 if (isOldShape) return; // shape is the same, so context is also unchanged
86 // update the referenced object if needed
88 myRef.setValue(theContext);
90 // do noth use naming if selected shape is result shape itself, but not sub-shape
91 TDF_Label aSelLab = selectionLabel();
92 aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
93 aSelLab.ForgetAttribute(kCONSTUCTION_SIMPLE_REF_ID);
95 bool isDegeneratedEdge = false;
96 // do not use the degenerated edge as a shape, a null context and shape is used in the case
97 if (theSubShape.get() && !theSubShape->isNull() && theSubShape->isEdge()) {
98 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
99 if (aSubShape.ShapeType() == TopAbs_EDGE)
100 isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
102 if (!theContext.get() || isDegeneratedEdge) {
103 // to keep the reference attribute label
104 TDF_Label aRefLab = myRef.myRef->Label();
105 aSelLab.ForgetAllAttributes(true);
106 myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
109 if (theContext->groupName() == ModelAPI_ResultBody::group()) {
110 // do not select the whole shape for body:it is already must be in the data framework
111 // equal and null selected objects mean the same: object is equal to context,
112 // TODO: synchronize with GUI later that it must be null always
113 if (theContext->shape().get() &&
114 (theContext->shape()->isEqual(theSubShape) || !theSubShape.get())) {
115 aSelLab.ForgetAllAttributes(true);
116 TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
118 selectBody(theContext, theSubShape);
120 } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
121 if (!theSubShape.get()) {
122 // to sub, so the whole result is selected
123 aSelLab.ForgetAllAttributes(true);
124 TDataStd_UAttribute::Set(aSelLab, kCONSTUCTION_SIMPLE_REF_ID);
126 selectConstruction(theContext, theSubShape);
128 } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
129 aSelLab.ForgetAllAttributes(true);
130 TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
131 selectPart(theContext, theSubShape);
133 //the attribute initialized state should be changed by sendAttributeUpdated only
134 //myIsInitialized = true;
136 owner()->data()->sendAttributeUpdated(this);
138 std::string aSelName = namingName();
139 if(!aSelName.empty())
140 TDataStd_Name::Set(selectionLabel(), aSelName.c_str()); //set name
143 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
145 std::shared_ptr<GeomAPI_Shape> aResult;
146 if (myRef.isInitialized()) {
147 TDF_Label aSelLab = selectionLabel();
148 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
149 ResultPtr aContext = context();
151 return aResult; // empty result
152 return aContext->shape();
154 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) { // it is just reference to construction, nothing is in value
155 return aResult; // empty result
157 if (aSelLab.IsAttribute(kPART_REF_ID)) {
158 /* TODO: implement used text here
159 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
160 if (!aPart.get() || !aPart->isActivated())
161 return std::shared_ptr<GeomAPI_Shape>(); // postponed naming needed
162 Handle(TDataStd_Integer) anIndex;
163 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
164 return aPart->selectionValue(anIndex->Get());
166 Handle(TDataStd_Name) aName;
167 if (!selectionLabel().FindAttribute(TDataStd_Name::GetID(), aName)) {
168 return std::shared_ptr<GeomAPI_Shape>(); // something is wrong
170 return aPart->shapeInPart(TCollection_AsciiString(aName).ToCString());
174 Handle(TNaming_NamedShape) aSelection;
175 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
176 TopoDS_Shape aSelShape = aSelection->Get();
177 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
178 aResult->setImpl(new TopoDS_Shape(aSelShape));
179 } else { // for simple construction element: just shape of this construction element
180 ResultConstructionPtr aConstr =
181 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
183 return aConstr->shape();
190 bool Model_AttributeSelection::isInitialized()
192 if (ModelAPI_AttributeSelection::isInitialized()) { // additional checkings if it is initialized
193 std::shared_ptr<GeomAPI_Shape> aResult;
194 if (myRef.isInitialized()) {
195 TDF_Label aSelLab = selectionLabel();
196 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
197 ResultPtr aContext = context();
198 return aContext.get() != NULL;
200 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) { // it is just reference to construction, nothing is in value
204 Handle(TNaming_NamedShape) aSelection;
205 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
206 return !aSelection->Get().IsNull();
207 } else { // for simple construction element: just shape of this construction element
208 ResultConstructionPtr aConstr =
209 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(context());
211 return aConstr->shape().get() != NULL;
219 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
222 myIsInitialized = myRef.isInitialized();
225 void Model_AttributeSelection::setID(const std::string theID)
228 ModelAPI_AttributeSelection::setID(theID);
231 ResultPtr Model_AttributeSelection::context() {
232 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
233 // for parts there could be same-data result, so take the last enabled
234 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group()) {
235 int aSize = aResult->document()->size(ModelAPI_ResultPart::group());
236 for(int a = aSize - 1; a >= 0; a--) {
237 ObjectPtr aPart = aResult->document()->object(ModelAPI_ResultPart::group(), a);
238 if (aPart.get() && aPart->data() == aResult->data()) {
239 ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
240 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
241 // check that this result is not this-feature result (it is forbidden t oselect itself)
242 if (anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
252 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
254 ModelAPI_AttributeSelection::setObject(theObject);
255 myRef.setObject(theObject);
258 TDF_LabelMap& Model_AttributeSelection::scope()
260 if (myScope.IsEmpty()) { // create a new scope if not yet done
261 // gets all featueres with named shapes that are bofore this feature label (before in history)
262 DocumentPtr aMyDoc = owner()->document();
263 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
264 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
265 for(; aFIter != allFeatures.end(); aFIter++) {
266 if (*aFIter == owner()) break; // the left features are created later
267 if (aFIter->get() && (*aFIter)->data()->isValid()) {
268 TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
269 (*aFIter)->data())->label().Father();
270 TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), 1);
271 for(; aNSIter.More(); aNSIter.Next()) {
272 Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
273 if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
274 myScope.Add(aNS->Label());
283 /// produces theEdge orientation relatively to theContext face
284 int edgeOrientation(const TopoDS_Shape& theContext, TopoDS_Edge& theEdge)
286 if (theContext.ShapeType() != TopAbs_FACE)
288 TopoDS_Face aContext = TopoDS::Face(theContext);
289 if (theEdge.Orientation() == TopAbs_FORWARD)
291 if (theEdge.Orientation() == TopAbs_REVERSED)
296 bool Model_AttributeSelection::update()
298 ResultPtr aContext = context();
299 if (!aContext.get()) return false;
300 TDF_Label aSelLab = selectionLabel();
301 if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
302 return aContext->shape() && !aContext->shape()->isNull();
304 if (aSelLab.IsAttribute(kCONSTUCTION_SIMPLE_REF_ID)) { // it is just reference to construction, not sub-shape
305 return aContext->shape() && !aContext->shape()->isNull();
308 if (aSelLab.IsAttribute(kPART_REF_ID)) { // it is reference to the part object
309 std::shared_ptr<GeomAPI_Shape> aNoSelection;
310 return selectPart(aContext, aNoSelection, true);
313 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
314 // body: just a named shape, use selection mechanism from OCCT
315 TNaming_Selector aSelector(aSelLab);
316 bool aResult = aSelector.Solve(scope()) == Standard_True;
317 owner()->data()->sendAttributeUpdated(this);
319 } else if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
320 // construction: identification by the results indexes, recompute faces and
321 // take the face that more close by the indexes
322 ResultConstructionPtr aConstructionContext =
323 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
324 FeaturePtr aContextFeature = aContext->document()->feature(aContext);
325 // sketch sub-element
326 if (aConstructionContext &&
327 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature).get())
329 TDF_Label aLab = myRef.myRef->Label();
330 // getting a type of selected shape
331 Handle(TDataStd_Integer) aTypeAttr;
332 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
335 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
336 // selected indexes will be needed in each "if"
337 Handle(TDataStd_IntPackedMap) aSubIds;
338 std::shared_ptr<GeomAPI_Shape> aNewSelected;
340 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
341 // for now working only with composite features
342 CompositeFeaturePtr aComposite =
343 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
344 if (!aComposite.get() || aComposite->numberOfSubs() == 0) {
348 if (aShapeType == TopAbs_FACE) { // compound is for the whole sketch selection
349 // If this is a wire with plane defined thin it is a sketch-like object
350 if (!aConstructionContext->facesNum()) // no faces, update can not work correctly
352 // if there is no edges indexes, any face can be used: take the first
353 std::shared_ptr<GeomAPI_Shape> aNewSelected;
355 aNewSelected = aConstructionContext->face(0);
356 } else { // searching for most looks-like initial face by the indexes
357 // prepare edges of the current resut for the fast searching
358 NCollection_DataMap<Handle(Geom_Curve), int> allCurves; // curves and orientations of edges
359 const int aSubNum = aComposite->numberOfSubs();
360 for(int a = 0; a < aSubNum; a++) {
361 int aSubID = aComposite->subFeatureId(a);
362 if (aSubIds->Contains(aSubID)) {
363 FeaturePtr aSub = aComposite->subFeature(a);
364 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
365 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
366 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
367 ResultConstructionPtr aConstr =
368 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
369 if (aConstr->shape() && aConstr->shape()->isEdge()) {
370 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
371 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
372 if (!anEdge.IsNull()) {
373 Standard_Real aFirst, aLast;
374 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
375 // searching for orientation information
377 Handle(TDataStd_Integer) anInt;
378 if (aSelLab.FindChild(aSubID).FindAttribute(TDataStd_Integer::GetID(), anInt)){
379 anOrient = anInt->Get();
381 allCurves.Bind(aCurve, anOrient);
387 double aBestFound = 0; // best percentage of found edges
388 int aBestOrient = 0; // for the equal "BestFound" additional parameter is orientation
389 for(int aFaceIndex = 0; aFaceIndex < aConstructionContext->facesNum(); aFaceIndex++) {
390 int aFound = 0, aNotFound = 0, aSameOrientation = 0;
392 TopoDS::Face(aConstructionContext->face(aFaceIndex)->impl<TopoDS_Shape>());
393 TopExp_Explorer anEdgesExp(aFace, TopAbs_EDGE);
394 TColStd_MapOfTransient alreadyProcessed; // to avoid counting edges with same curved (841)
395 for(; anEdgesExp.More(); anEdgesExp.Next()) {
396 TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
397 if (!anEdge.IsNull()) {
398 Standard_Real aFirst, aLast;
399 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
400 if (alreadyProcessed.Contains(aCurve))
402 alreadyProcessed.Add(aCurve);
403 if (allCurves.IsBound(aCurve)) {
405 int anOrient = allCurves.Find(aCurve);
406 if (anOrient != 0) { // extra comparision score is orientation
407 if (edgeOrientation(aFace, anEdge) == anOrient)
415 if (aFound + aNotFound != 0) {
416 double aSum = aFound + aNotFound;
417 // aSameOrientation: if edges are same, take where orientation is better
418 double aPercentage = double(aFound) / double(aFound + aNotFound);
419 if (aPercentage > aBestFound ||
420 (aPercentage == aBestFound && aSameOrientation > aBestOrient)) {
421 aBestFound = aPercentage;
422 aBestOrient = aSameOrientation;
423 aNewSelected = aConstructionContext->face(aFaceIndex);
428 if (aNewSelected) { // store this new selection
429 selectConstruction(aContext, aNewSelected);
430 owner()->data()->sendAttributeUpdated(this);
433 } else if (aShapeType == TopAbs_EDGE) {
434 // just reselect the edge by the id
435 const int aSubNum = aComposite->numberOfSubs();
436 for(int a = 0; a < aSubNum; a++) {
437 // if aSubIds take any, the first appropriate
438 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
439 // found the appropriate feature
440 FeaturePtr aFeature = aComposite->subFeature(a);
441 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
442 aFeature->results().cbegin();
443 for(;aResIter != aFeature->results().cend(); aResIter++) {
444 ResultConstructionPtr aRes =
445 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
446 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
447 selectConstruction(aContext, aRes->shape());
448 owner()->data()->sendAttributeUpdated(this);
454 } else if (aShapeType == TopAbs_VERTEX) {
455 // just reselect the vertex by the id of edge
456 const int aSubNum = aComposite->numberOfSubs();
457 for(int a = 0; a < aSubNum; a++) {
458 // if aSubIds take any, the first appropriate
459 int aFeatureID = aComposite->subFeatureId(a);
460 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID) ||
461 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA) ||
462 aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) {
463 // searching for deltas
465 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
466 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA * 2)) aVertexNum = 2;
467 // found the feature with appropriate edge
468 FeaturePtr aFeature = aComposite->subFeature(a);
469 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
470 aFeature->results().cbegin();
471 for(;aResIter != aFeature->results().cend(); aResIter++) {
472 ResultConstructionPtr aRes =
473 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
474 if (aRes && aRes->shape()) {
475 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
476 selectConstruction(aContext, aRes->shape());
477 owner()->data()->sendAttributeUpdated(this);
479 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
480 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
482 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
483 if (aVIndex == aVertexNum) { // found!
484 std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
485 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
486 selectConstruction(aContext, aVertex);
487 owner()->data()->sendAttributeUpdated(this);
498 } else { // simple construction element: the selected is that needed
499 selectConstruction(aContext, aContext->shape());
500 owner()->data()->sendAttributeUpdated(this);
504 return false; // unknown case
508 void Model_AttributeSelection::selectBody(
509 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
511 // perform the selection
512 TNaming_Selector aSel(selectionLabel());
513 TopoDS_Shape aContext;
515 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
517 aContext = aBody->shape()->impl<TopoDS_Shape>();
520 std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
522 aContext = aResult->shape()->impl<TopoDS_Shape>();
524 Events_Error::send("A result with shape is expected");
528 TopoDS_Shape aNewShape = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
529 /// fix for issue 411: result modified shapes must not participate in this selection mechanism
531 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
532 if (aFeatureOwner.get())
533 aFeatureOwner->eraseResults();
535 if (!aContext.IsNull()) {
536 aSel.Select(aNewShape, aContext);
540 /// registers the name of the shape in the label (theID == 0) of sub label (theID is a tag)
541 /// if theID is zero,
542 /// theOrientation is additional information about the positioning of edge relatively to face
543 /// it is stored in the integer attribute of the edge sub-label:
544 /// -1 is out, 1 is in, 0 is not needed
545 static void registerSubShape(TDF_Label theMainLabel, TopoDS_Shape theShape,
546 const int theID, const FeaturePtr& theContextFeature, std::shared_ptr<Model_Document> theDoc,
547 std::string theAdditionalName, std::map<int, int>& theOrientations,
548 Handle(TDataStd_IntPackedMap) theRefs = Handle(TDataStd_IntPackedMap)(),
549 const int theOrientation = 0)
551 TDF_Label aLab = theID == 0 ? theMainLabel : theMainLabel.FindChild(theID);
552 if (theOrientation != 0) { // store the orientation of edge relatively to face if needed
553 TDataStd_Integer::Set(aLab, theOrientation);
555 TNaming_Builder aBuilder(aLab);
556 aBuilder.Generated(theShape);
557 std::stringstream aName;
558 aName<<theContextFeature->name()<<"/";
559 if (!theAdditionalName.empty())
560 aName<<theAdditionalName<<"/";
561 if (theShape.ShapeType() == TopAbs_FACE) aName<<"Face";
562 else if (theShape.ShapeType() == TopAbs_EDGE) aName<<"Edge";
563 else if (theShape.ShapeType() == TopAbs_VERTEX) aName<<"Vertex";
565 if (theRefs.IsNull()) {
567 if (theOrientation == 1)
569 else if (theOrientation == -1)
571 } else { // make a compisite name from all sub-elements indexes: "1_2_3_4"
572 TColStd_MapIteratorOfPackedMapOfInteger aRef(theRefs->GetMap());
573 for(; aRef.More(); aRef.Next()) {
574 aName<<"-"<<aRef.Key();
575 if (theOrientations.find(aRef.Key()) != theOrientations.end()) {
576 if (theOrientations[aRef.Key()] == 1)
578 else if (theOrientations[aRef.Key()] == -1)
584 theDoc->addNamingName(aLab, aName.str());
585 TDataStd_Name::Set(aLab, aName.str().c_str());
588 void Model_AttributeSelection::selectConstruction(
589 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
591 std::shared_ptr<Model_Document> aMyDoc =
592 std::dynamic_pointer_cast<Model_Document>(owner()->document());
593 FeaturePtr aContextFeature = theContext->document()->feature(theContext);
594 CompositeFeaturePtr aComposite =
595 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
596 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
597 if (!aComposite || aComposite->numberOfSubs() == 0) {
598 // saving of context is enough: result construction contains exactly the needed shape
599 TNaming_Builder aBuilder(selectionLabel());
600 aBuilder.Generated(aSubShape);
601 aMyDoc->addNamingName(selectionLabel(), theContext->data()->name());
602 TDataStd_Name::Set(selectionLabel(), theContext->data()->name().c_str());
605 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(owner()->data());
606 TDF_Label aLab = myRef.myRef->Label();
607 // identify the reuslts of sub-object of the composite by edges
608 // save type of the selected shape in integer attribute
609 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
610 TDataStd_Integer::Set(aLab, (int)aShapeType);
612 TColStd_MapOfTransient allCurves;
613 if (aShapeType == TopAbs_VERTEX) { // compare positions
614 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
616 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
617 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
618 Standard_Real aFirst, aLast;
619 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
620 allCurves.Add(aCurve);
623 // iterate and store the result ids of sub-elements and sub-elements to sub-labels
624 Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
625 std::map<int, int> anOrientations; //map from edges IDs to orientations of these edges in face
627 const int aSubNum = aComposite->numberOfSubs();
628 for(int a = 0; a < aSubNum; a++) {
629 FeaturePtr aSub = aComposite->subFeature(a);
630 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
631 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
632 // there may be many shapes (circle and center): register if at least one is in selection
633 for(; aRes != aResults.cend(); aRes++) {
634 ResultConstructionPtr aConstr =
635 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
636 if (!aConstr->shape()) {
639 if (aShapeType == TopAbs_VERTEX) {
640 if (aConstr->shape()->isVertex()) { // compare vertices positions
641 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
642 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
643 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
644 aRefs->Add(aComposite->subFeatureId(a));
646 } else { // get first or last vertex of the edge: last is stored with negative sign
647 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
648 int aDelta = kSTART_VERTEX_DELTA;
649 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
650 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
651 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
652 aRefs->Add(aDelta + aComposite->subFeatureId(a));
655 aDelta += kSTART_VERTEX_DELTA;
659 if (aConstr->shape()->isEdge()) {
660 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
661 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
662 if (!anEdge.IsNull()) {
663 Standard_Real aFirst, aLast;
664 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
665 if (allCurves.Contains(aCurve)) {
666 int anID = aComposite->subFeatureId(a);
668 if (aShapeType != TopAbs_EDGE) { // face nneds the sub-edges on sub-labels
669 // add edges to sub-label to support naming for edges selection
670 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE);
671 for(; anEdgeExp.More(); anEdgeExp.Next()) {
672 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
673 Standard_Real aFirst, aLast;
674 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
675 if (aFaceCurve == aCurve) {
676 int anOrient = edgeOrientation(aSubShape, anEdge);
677 anOrientations[anID] = anOrient;
679 selectionLabel(), anEdge, anID, aContextFeature, aMyDoc, "", anOrientations,
680 Handle(TDataStd_IntPackedMap)(), anOrient);
683 } else { // put vertices of the selected edge to sub-labels
684 // add edges to sub-label to support naming for edges selection
685 TopExp_Explorer anEdgeExp(aSubShape, TopAbs_VERTEX);
686 int aTagIndex = anID + kSTART_VERTEX_DELTA;
687 for(; anEdgeExp.More(); anEdgeExp.Next(), aTagIndex += kSTART_VERTEX_DELTA) {
688 TopoDS_Vertex aV = TopoDS::Vertex(anEdgeExp.Current());
690 std::stringstream anAdditionalName;
692 selectionLabel(), aV, aTagIndex, aContextFeature, aMyDoc, "", anOrientations);
701 // store the selected as primitive
702 TNaming_Builder aBuilder(selectionLabel());
703 aBuilder.Generated(aSubShape);
705 selectionLabel(), aSubShape, 0, aContextFeature, aMyDoc, "", anOrientations, aRefs);
708 bool Model_AttributeSelection::selectPart(
709 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape,
710 const bool theUpdate)
712 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
713 if (!aPart.get() || !aPart->isActivated())
714 return true; // postponed naming
716 Handle(TDataStd_Integer) anIndex;
717 if (selectionLabel().FindAttribute(TDataStd_Integer::GetID(), anIndex)) { // by internal selection
718 if (anIndex->Get() > 0) {
719 // update the selection by index
720 return aPart->updateInPart(anIndex->Get());
722 return true; // nothing to do, referencing just by name
725 return true; // nothing to do, referencing just by name
727 // store the shape (in case part is not loaded it should be usefull
729 std::string aName = theContext->data()->name();
730 if (theSubShape->isNull()) {// the whole part shape is selected
731 aShape = theContext->shape()->impl<TopoDS_Shape>();
733 aShape = theSubShape->impl<TopoDS_Shape>();
735 aName += "/" + aPart->nameInPart(theSubShape, anIndex);
736 TDataStd_Integer::Set(selectionLabel(), anIndex);
738 TNaming_Builder aBuilder(selectionLabel());
739 aBuilder.Select(aShape, aShape);
740 // identify by name in the part
741 TDataStd_Name::Set(selectionLabel(), aName.c_str());
742 return !aName.empty();
745 TDF_Label Model_AttributeSelection::selectionLabel()
747 return myRef.myRef->Label().FindChild(1);
750 std::string Model_AttributeSelection::namingName(const std::string& theDefaultName)
752 std::string aName("");
753 if(!this->isInitialized())
754 return !theDefaultName.empty() ? theDefaultName : aName;
755 Handle(TDataStd_Name) anAtt;
756 if(selectionLabel().FindAttribute(TDataStd_Name::GetID(), anAtt)) {
757 aName = TCollection_AsciiString(anAtt->Get()).ToCString();
761 std::shared_ptr<GeomAPI_Shape> aSubSh = value();
762 ResultPtr aCont = context();
764 Model_SelectionNaming aSelNaming(selectionLabel());
765 return aSelNaming.namingName(aCont, aSubSh, theDefaultName);
768 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
769 void Model_AttributeSelection::selectSubShape(
770 const std::string& theType, const std::string& theSubShapeName)
772 if(theSubShapeName.empty() || theType.empty()) return;
774 Model_SelectionNaming aSelNaming(selectionLabel());
775 std::shared_ptr<Model_Document> aDoc =
776 std::dynamic_pointer_cast<Model_Document>(owner()->document());
777 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
779 if (aSelNaming.selectSubShape(theType, theSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
780 setValue(aCont, aShapeToBeSelected);
784 int Model_AttributeSelection::Id()
786 std::shared_ptr<GeomAPI_Shape> aSelection = value();
787 std::shared_ptr<GeomAPI_Shape> aContext = context()->shape();
788 const TopoDS_Shape& aMainShape = aContext->impl<TopoDS_Shape>();
789 const TopoDS_Shape& aSubShape = aSelection->impl<TopoDS_Shape>();
791 if (aSelection && !aSelection->isNull() &&
792 aContext && !aContext->isNull())
794 TopTools_IndexedMapOfShape aSubShapesMap;
795 TopExp::MapShapes(aMainShape, aSubShapesMap);
796 anID = aSubShapesMap.FindIndex(aSubShape);