1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_SelectionNaming.cpp
4 // Created: 11 Aug 2015
5 // Author: Mikhail PONIKAROV
7 #include "Model_SelectionNaming.h"
8 #include "Model_Document.h"
9 #include <ModelAPI_Feature.h>
10 #include <Events_InfoMessage.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_ResultPart.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_CompositeFeature.h>
15 #include <ModelAPI_ResultBody.h>
17 #include <TopoDS_Iterator.hxx>
19 #include <TopoDS_Compound.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <TopTools_ListOfShape.hxx>
23 #include <TopTools_MapOfShape.hxx>
24 #include <TopTools_IndexedMapOfShape.hxx>
25 #include <TopTools_ListIteratorOfListOfShape.hxx>
26 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
27 #include <TopTools_MapIteratorOfMapOfShape.hxx>
28 #include <BRep_Builder.hxx>
29 #include <TNaming_Iterator.hxx>
30 #include <TNaming_Tool.hxx>
31 #include <TNaming_NamedShape.hxx>
32 #include <TNaming_Localizer.hxx>
33 #include <TDataStd_Name.hxx>
34 #include <TColStd_MapOfTransient.hxx>
38 #include <BRepTools.hxx>
41 Model_SelectionNaming::Model_SelectionNaming(TDF_Label theSelectionLab)
43 myLab = theSelectionLab;
46 std::string Model_SelectionNaming::getShapeName(
47 std::shared_ptr<Model_Document> theDoc, const TopoDS_Shape& theShape)
50 // check if the subShape is already in DF
51 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(theShape, myLab);
52 Handle(TDataStd_Name) anAttr;
53 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
54 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
55 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
57 const TDF_Label& aLabel = theDoc->findNamingName(aName);
58 static const std::string aPostFix("_");
59 TNaming_Iterator anItL(aNS);
60 for(int i = 1; anItL.More(); anItL.Next(), i++) {
61 if(anItL.NewShape() == theShape) {
63 aName += TCollection_AsciiString (i).ToCString();
73 bool isTrivial (const TopTools_ListOfShape& theAncestors, TopTools_IndexedMapOfShape& theSMap)
75 // a trivial case: F1 & F2, aNumber = 1, i.e. intersection gives 1 edge.
78 BB.MakeCompound(aCmp);
79 TopTools_ListIteratorOfListOfShape it(theAncestors);
80 for(;it.More();it.Next()) {
81 BB.Add(aCmp, it.Value());
82 theSMap.Add(it.Value());
85 TopTools_IndexedDataMapOfShapeListOfShape aMap2;
86 TopExp::MapShapesAndAncestors(aCmp, TopAbs_EDGE, TopAbs_FACE, aMap2);
87 for (int i = 1; i <= aMap2.Extent(); i++) {
88 const TopoDS_Shape& aKey = aMap2.FindKey(i);
89 const TopTools_ListOfShape& anAncestors = aMap2.FindFromIndex(i);
90 if(anAncestors.Extent() > 1) aNumber++;
92 if(aNumber > 1) return false;
96 std::string Model_SelectionNaming::namingName(ResultPtr& theContext,
97 std::shared_ptr<GeomAPI_Shape> theSubSh, const std::string& theDefaultName)
99 std::string aName("Undefined name");
100 if(!theContext.get() || theContext->shape()->isNull())
101 return !theDefaultName.empty() ? theDefaultName : aName;
102 if (!theSubSh.get() || theSubSh->isNull()) { // no subshape, so just the whole feature name
103 return theContext->data()->name();
105 TopoDS_Shape aSubShape = theSubSh->impl<TopoDS_Shape>();
106 TopoDS_Shape aContext = theContext->shape()->impl<TopoDS_Shape>();
108 if(aSubShape.ShapeType() == TopAbs_COMPOUND) {
109 BRepTools::Write(aSubShape, "Selection.brep");
110 BRepTools::Write(aContext, "Context.brep");
113 std::shared_ptr<Model_Document> aDoc =
114 std::dynamic_pointer_cast<Model_Document>(theContext->document());
116 // check if the subShape is already in DF
117 aName = getShapeName(aDoc, aSubShape);
118 if(aName.empty() ) { // not in the document!
119 TopAbs_ShapeEnum aType = aSubShape.ShapeType();
122 // the Face should be in DF. If it is not the case, it is an error ==> to be debugged
126 // name structure: F1 & F2 [& F3 & F4], where F1 & F2 the faces which gives the Edge in trivial case
127 // if it is not atrivial case we use localization by neighbours. F3 & F4 - neighbour faces
128 if (BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
129 aName = "Degenerated_Edge";
132 TopTools_IndexedDataMapOfShapeListOfShape aMap;
133 TopExp::MapShapesAndAncestors(aContext, TopAbs_EDGE, TopAbs_FACE, aMap);
134 TopTools_IndexedMapOfShape aSMap; // map for ancestors of the sub-shape
135 bool isTrivialCase(true);
136 if(aMap.Contains(aSubShape)) {
137 const TopTools_ListOfShape& anAncestors = aMap.FindFromKey(aSubShape);
138 // check that it is not a trivial case (F1 & F2: aNumber = 1)
139 isTrivialCase = isTrivial(anAncestors, aSMap);
142 TopTools_ListOfShape aListOfNbs;
143 if(!isTrivialCase) { // find Neighbors
144 TNaming_Localizer aLocalizer;
145 TopTools_MapOfShape aMap3;
146 aLocalizer.FindNeighbourg(aContext, aSubShape, aMap3);
147 //int n = aMap3.Extent();
148 TopTools_MapIteratorOfMapOfShape it(aMap3);
149 for(;it.More();it.Next()) {
150 const TopoDS_Shape& aNbShape = it.Key(); // neighbor edge
151 //TopAbs_ShapeEnum aType = aNbShape.ShapeType();
152 const TopTools_ListOfShape& aList = aMap.FindFromKey(aNbShape);
153 TopTools_ListIteratorOfListOfShape it2(aList);
154 for(;it2.More();it2.Next()) {
155 if(aSMap.Contains(it2.Value())) continue; // skip this Face
156 aListOfNbs.Append(it2.Value());
159 } // else a trivial case
161 // build name of the sub-shape Edge
162 for(int i=1; i <= aSMap.Extent(); i++) {
163 const TopoDS_Shape& aFace = aSMap.FindKey(i);
164 std::string aFaceName = getShapeName(aDoc, aFace);
168 aName += "&" + aFaceName;
170 TopTools_ListIteratorOfListOfShape itl(aListOfNbs);
171 for (;itl.More();itl.Next()) {
172 std::string aFaceName = getShapeName(aDoc, itl.Value());
173 aName += "&" + aFaceName;
179 // name structure (Monifold Topology):
180 // 1) F1 | F2 | F3 - intersection of 3 faces defines a vertex - trivial case.
181 // 2) F1 | F2 | F3 [|F4 [|Fn]] - redundant definition, but it should be kept as is to obtain safe recomputation
182 // 2) F1 | F2 - intersection of 2 faces definses a vertex - applicable for case
183 // when 1 faces is cylindrical, conical, spherical or revolution and etc.
184 // 3) E1 | E2 | E3 - intersection of 3 edges defines a vertex - when we have case of a shell
185 // or compound of 2 open faces.
186 // 4) E1 | E2 - intesection of 2 edges defines a vertex - when we have a case of
187 // two independent edges (wire or compound)
188 // implemented 2 first cases
190 TopTools_IndexedDataMapOfShapeListOfShape aMap;
191 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_FACE, aMap);
192 const TopTools_ListOfShape& aList2 = aMap.FindFromKey(aSubShape);
193 TopTools_ListOfShape aList;
194 TopTools_MapOfShape aFMap;
196 TopTools_ListIteratorOfListOfShape itl2(aList2);
197 for (int i = 1;itl2.More();itl2.Next(),i++) {
198 if(aFMap.Add(itl2.Value()))
199 aList.Append(itl2.Value());
201 int n = aList.Extent();
202 bool isByFaces = n >= 3;
203 if(!isByFaces) { // open topology case or Compound case => via edges
204 TopTools_IndexedDataMapOfShapeListOfShape aMap;
205 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_EDGE, aMap);
206 const TopTools_ListOfShape& aList22 = aMap.FindFromKey(aSubShape);
207 if(aList22.Extent() >= 2) { // regular solution
209 // bug! duplication; fix is below
211 TopTools_ListOfShape aListE;
212 TopTools_ListIteratorOfListOfShape itl2(aList22);
213 for (int i = 1;itl2.More();itl2.Next(),i++) {
214 if(aFMap.Add(itl2.Value()))
215 aListE.Append(itl2.Value());
218 TopTools_ListIteratorOfListOfShape itl(aListE);
219 for (int i = 1;itl.More();itl.Next(),i++) {
220 const TopoDS_Shape& anEdge = itl.Value();
221 std::string anEdgeName = getShapeName(aDoc, anEdge);
222 if (anEdgeName.empty()) { // edge is not in DS, trying by faces anyway
230 aName += "&" + anEdgeName;
233 else { // dangle vertex: if(aList22.Extent() == 1)
234 //it should be already in DF
238 TopTools_ListIteratorOfListOfShape itl(aList);
239 for (int i = 1;itl.More();itl.Next(),i++) {
240 const TopoDS_Shape& aFace = itl.Value();
241 std::string aFaceName = getShapeName(aDoc, aFace);
245 aName += "&" + aFaceName;
252 // aDoc->addNamingName(selectionLabel(), aName);
253 // the selected sub-shape will not be shared and as result it will not require registration
258 TopAbs_ShapeEnum translateType (const std::string& theType)
260 // map from the textual shape types to OCCT enumeration
261 static std::map<std::string, TopAbs_ShapeEnum> aShapeTypes;
263 if(aShapeTypes.size() == 0) {
264 aShapeTypes["compound"] = TopAbs_COMPOUND;
265 aShapeTypes["compounds"] = TopAbs_COMPOUND;
266 aShapeTypes["compsolid"] = TopAbs_COMPSOLID;
267 aShapeTypes["compsolids"] = TopAbs_COMPSOLID;
268 aShapeTypes["solid"] = TopAbs_SOLID;
269 aShapeTypes["solids"] = TopAbs_SOLID;
270 aShapeTypes["shell"] = TopAbs_SHELL;
271 aShapeTypes["shells"] = TopAbs_SHELL;
272 aShapeTypes["face"] = TopAbs_FACE;
273 aShapeTypes["faces"] = TopAbs_FACE;
274 aShapeTypes["wire"] = TopAbs_WIRE;
275 aShapeTypes["wires"] = TopAbs_WIRE;
276 aShapeTypes["edge"] = TopAbs_EDGE;
277 aShapeTypes["edges"] = TopAbs_EDGE;
278 aShapeTypes["vertex"] = TopAbs_VERTEX;
279 aShapeTypes["vertices"] = TopAbs_VERTEX;
280 aShapeTypes["COMPOUND"] = TopAbs_COMPOUND;
281 aShapeTypes["COMPOUNDS"] = TopAbs_COMPOUND;
282 aShapeTypes["COMPSOLID"] = TopAbs_COMPSOLID;
283 aShapeTypes["COMPSOLIDS"] = TopAbs_COMPSOLID;
284 aShapeTypes["SOLID"] = TopAbs_SOLID;
285 aShapeTypes["SOLIDS"] = TopAbs_SOLID;
286 aShapeTypes["SHELL"] = TopAbs_SHELL;
287 aShapeTypes["SHELLS"] = TopAbs_SHELL;
288 aShapeTypes["FACE"] = TopAbs_FACE;
289 aShapeTypes["FACES"] = TopAbs_FACE;
290 aShapeTypes["WIRE"] = TopAbs_WIRE;
291 aShapeTypes["WIRES"] = TopAbs_WIRE;
292 aShapeTypes["EDGE"] = TopAbs_EDGE;
293 aShapeTypes["EDGES"] = TopAbs_EDGE;
294 aShapeTypes["VERTEX"] = TopAbs_VERTEX;
295 aShapeTypes["VERTICES"] = TopAbs_VERTEX;
297 if (aShapeTypes.find(theType) != aShapeTypes.end())
298 return aShapeTypes[theType];
299 Events_InfoMessage("Model_SelectionNaming", "Shape type defined in XML is not implemented!").send();
303 const TopoDS_Shape getShapeFromNS(
304 const std::string& theSubShapeName, Handle(TNaming_NamedShape) theNS)
306 TopoDS_Shape aSelection;
307 std::string::size_type n = theSubShapeName.rfind('/');
308 if (n == std::string::npos) n = 0;
309 std::string aSubString = theSubShapeName.substr(n + 1);
310 n = aSubString.rfind('_');
311 if (n == std::string::npos) return aSelection;
312 aSubString = aSubString.substr(n+1);
313 int indx = atoi(aSubString.c_str());
315 TNaming_Iterator anItL(theNS);
316 for(int i = 1; anItL.More(); anItL.Next(), i++) {
318 return anItL.NewShape();
324 const TopoDS_Shape findFaceByName(
325 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc)
328 std::string::size_type n, nb = theSubShapeName.rfind('/');
329 if (nb == std::string::npos) nb = 0;
330 std::string aSubString = theSubShapeName.substr(nb + 1);
331 n = aSubString.rfind('_');
332 if (n != std::string::npos) {
333 std::string aSubStr2 = aSubString.substr(0, n);
334 aSubString = theSubShapeName.substr(0, nb + 1);
335 aSubString = aSubString + aSubStr2;
337 aSubString = theSubShapeName;
339 const TDF_Label& aLabel = theDoc->findNamingName(aSubString);
340 if(aLabel.IsNull()) return aFace;
341 Handle(TNaming_NamedShape) aNS;
342 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
343 aFace = getShapeFromNS(theSubShapeName, aNS);
348 size_t ParseName(const std::string& theSubShapeName, std::list<std::string>& theList)
350 std::string aName = theSubShapeName;
351 std::string aLastName;
352 size_t n1(0), n2(0); // n1 - start position, n2 - position of the delimiter
353 while ((n2 = aName.find('&', n1)) != std::string::npos) {
354 const std::string aName1 = aName.substr(n1, n2 - n1); //name of face
355 theList.push_back(aName1);
357 aLastName = aName.substr(n1);
359 if(!aLastName.empty())
360 theList.push_back(aLastName);
361 return theList.size();
364 const TopoDS_Shape findCommonShape(
365 const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
368 std::vector<TopTools_MapOfShape> aVec;
369 TopTools_MapOfShape aMap1, aMap2, aMap3, aMap4;
370 if(theList.Extent() > 1) {
371 aVec.push_back(aMap1);
372 aVec.push_back(aMap2);
374 if(theList.Extent() > 2)
375 aVec.push_back(aMap3);
376 if(theList.Extent() == 4)
377 aVec.push_back(aMap4);
380 TopTools_ListIteratorOfListOfShape it(theList);
381 for(int i = 0;it.More();it.Next(),i++) {
382 const TopoDS_Shape& aFace = it.Value();
384 TopExp_Explorer anExp (aFace, theType);
385 for(;anExp.More();anExp.Next()) {
386 const TopoDS_Shape& anEdge = anExp.Current();
387 if (!anEdge.IsNull())
388 aVec[i].Add(anExp.Current());
391 TopExp_Explorer anExp (aFace, TopAbs_VERTEX);
392 for(;anExp.More();anExp.Next()) {
393 const TopoDS_Shape& aVertex = anExp.Current();
394 if (!aVertex.IsNull())
395 aVec[i].Add(anExp.Current());
399 //trivial case: 2 faces
400 TopTools_ListOfShape aList;
401 TopTools_MapIteratorOfMapOfShape it2(aVec[0]);
402 for(;it2.More();it2.Next()) {
403 if(aVec[1].Contains(it2.Key())) {
405 if(theList.Extent() == 2)
408 aList.Append(it2.Key());
411 if(aList.Extent() && aVec.size() > 3) {// list of common edges ==> search ny neighbors
412 if(aVec[2].Extent() && aVec[3].Extent()) {
413 TopTools_ListIteratorOfListOfShape it(aList);
414 for(;it.More();it.Next()) {
415 const TopoDS_Shape& aCand = it.Value();
416 // not yet completelly implemented, to be rechecked
417 TopoDS_Vertex aV1, aV2;
418 TopExp::Vertices(TopoDS::Edge(aCand), aV1, aV2);
420 if(aVec[2].Contains(aV1)) aNum++;
421 else if(aVec[2].Contains(aV2)) aNum++;
422 if(aVec[3].Contains(aV1)) aNum++;
423 else if(aVec[3].Contains(aV2)) aNum++;
432 if(aList.Extent() && aVec.size() == 3) {
434 TopTools_ListIteratorOfListOfShape it(aList);
435 for(;it.More();it.Next()) {
436 const TopoDS_Shape& aCand = it.Value();
437 if(aVec[2].Contains(aCand)) {
446 std::string getContextName(const std::string& theSubShapeName)
449 std::string::size_type n = theSubShapeName.find('/');
450 if (n == std::string::npos) return theSubShapeName;
451 aName = theSubShapeName.substr(0, n);
455 /// Parses naming name of sketch sub-elements: takes indices and orientation
456 /// (if theOriented = true) from this name. Map theIDs constains indices ->
457 /// orientations and start/end vertices: negative is reversed, 2 - start, 3 - end
458 bool parseSubIndices(CompositeFeaturePtr theComp, //< to iterate names
459 const std::string& theName, const char* theShapeType,
460 std::map<int, int>& theIDs, const bool theOriented = false)
462 // collect all IDs in the name
463 std::map<std::string, int> aNames; // short name of sub -> ID of sub of theComp
464 const int aSubNum = theComp->numberOfSubs();
465 for(int a = 0; a < aSubNum; a++) {
466 FeaturePtr aSub = theComp->subFeature(a);
467 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
468 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
469 // there may be many shapes (circle and center)
470 for(; aRes != aResults.cend(); aRes++) {
471 ResultConstructionPtr aConstr =
472 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
474 aNames[Model_SelectionNaming::shortName(aConstr)] = theComp->subFeatureId(a);
479 size_t aPrevPos = theName.find("/") + 1, aLastNamePos;
480 bool isShape = false; // anyway the first world must be 'Vertex'
482 aLastNamePos = theName.find('-', aPrevPos);
483 std::string anID = theName.substr(aPrevPos, aLastNamePos - aPrevPos);
485 if (anID != theShapeType)
489 int anOrientation = 1; // default
490 if (theOriented) { // here must be a symbol in the end of digit 'f' or 'r'
491 const char aSymbol = anID.back();
492 if (aSymbol == 'r') anOrientation = -1;
495 // check start/end symbols
496 if (anID.back() == 's') {
499 } else if (anID.back() == 'e') {
504 if (aNames.find(anID) != aNames.end()) {
505 theIDs[aNames[anID]] = anOrientation;
508 aPrevPos = aLastNamePos + 1;
509 } while (aLastNamePos != std::string::npos);
513 /// produces theEdge orientation relatively to theContext face
514 int Model_SelectionNaming::edgeOrientation(const TopoDS_Shape& theContext, TopoDS_Edge& theEdge)
516 if (theContext.ShapeType() != TopAbs_FACE && theContext.ShapeType() != TopAbs_WIRE)
518 if (theEdge.Orientation() == TopAbs_FORWARD)
520 if (theEdge.Orientation() == TopAbs_REVERSED)
525 std::shared_ptr<GeomAPI_Shape> Model_SelectionNaming::findAppropriateFace(
526 std::shared_ptr<ModelAPI_Result>& theConstr,
527 NCollection_DataMap<Handle(Geom_Curve), int>& theCurves)
529 int aBestFound = 0; // best number of found edges (not percentage: issue 1019)
530 int aBestOrient = 0; // for the equal "BestFound" additional parameter is orientation
531 std::shared_ptr<GeomAPI_Shape> aResult;
532 ResultConstructionPtr aConstructionContext =
533 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theConstr);
534 if (!aConstructionContext.get())
536 for(int aFaceIndex = 0; aFaceIndex < aConstructionContext->facesNum(); aFaceIndex++) {
537 int aFound = 0, aNotFound = 0, aSameOrientation = 0;
539 TopoDS::Face(aConstructionContext->face(aFaceIndex)->impl<TopoDS_Shape>());
540 TopExp_Explorer anEdgesExp(aFace, TopAbs_EDGE);
541 TColStd_MapOfTransient alreadyProcessed; // to avoid counting edges with same curved (841)
542 for(; anEdgesExp.More(); anEdgesExp.Next()) {
543 TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
544 if (!anEdge.IsNull()) {
545 Standard_Real aFirst, aLast;
546 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
547 if (alreadyProcessed.Contains(aCurve))
549 alreadyProcessed.Add(aCurve);
550 if (theCurves.IsBound(aCurve)) {
552 int anOrient = theCurves.Find(aCurve);
553 if (anOrient != 0) { // extra comparision score is orientation
554 if (edgeOrientation(aFace, anEdge) == anOrient)
562 if (aFound + aNotFound != 0) {
563 if (aFound > aBestFound ||
564 (aFound == aBestFound && aSameOrientation > aBestOrient)) {
566 aBestOrient = aSameOrientation;
567 aResult = aConstructionContext->face(aFaceIndex);
574 std::string Model_SelectionNaming::shortName(
575 std::shared_ptr<ModelAPI_ResultConstruction>& theConstr, const int theEdgeVertexPos)
577 std::string aName = theConstr->data()->name();
578 // remove "-", "/" and "&" command-symbols
579 aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
580 aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
581 aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
582 // remove the last 's', 'e', 'f' and 'r' symbols: they are used as markers of start/end/forward/rewersed indicators
583 static const std::string aSyms("sefr");
584 while(aSyms.find(aName.back()) != std::string::npos) {
587 if (theEdgeVertexPos == 1) {
588 aName += "s"; // start
589 } else if (theEdgeVertexPos == 2) {
595 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
596 bool Model_SelectionNaming::selectSubShape(const std::string& theType,
597 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc,
598 std::shared_ptr<GeomAPI_Shape>& theShapeToBeSelected, std::shared_ptr<ModelAPI_Result>& theCont)
600 if(theSubShapeName.empty() || theType.empty()) return false;
601 TopAbs_ShapeEnum aType = translateType(theType);
603 // check that it was selected in another document
604 size_t aSlash = theSubShapeName.find("/");
605 std::string aSubShapeName = theSubShapeName;
606 std::shared_ptr<Model_Document> aDoc = theDoc;
607 if (aSlash != std::string::npos) {
608 std::string aDocName = theSubShapeName.substr(0, aSlash);
609 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
610 if (aDocName == aRootDoc->kind()) {
611 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
613 for (int a = aRootDoc->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
614 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
615 aRootDoc->object(ModelAPI_ResultPart::group(), a));
616 if (aPart.get() && aPart->isActivated() && aPart->data()->name() == aDocName) {
617 aDoc = std::dynamic_pointer_cast<Model_Document>(aPart->partDoc());
621 if (aDoc != theDoc) { // so, the first word is the document name => reduce the string for the next manips
622 aSubShapeName = theSubShapeName.substr(aSlash + 1);
626 std::string aContName = getContextName(aSubShapeName);
627 if(aContName.empty()) return false;
628 ResultPtr aCont = aDoc->findByName(aContName);
629 // possible this is body where postfix is added to distinguish several shapes on the same label
630 int aSubShapeId = -1; // -1 means sub shape not found
631 // for result body the name wihtout "_" has higher priority than with it: it is always added
632 if ((!aCont.get() || (aCont->groupName() == ModelAPI_ResultBody::group())) &&
633 aContName == aSubShapeName) {
634 size_t aPostIndex = aContName.rfind('_');
635 if (aPostIndex != std::string::npos) {
636 std::string aSubContName = aContName.substr(0, aPostIndex);
637 ResultPtr aSubCont = aDoc->findByName(aSubContName);
638 if (aSubCont.get()) {
640 std::string aNum = aContName.substr(aPostIndex + 1);
641 aSubShapeId = std::stoi(aNum);
642 } catch (std::invalid_argument&) {
645 if (aSubShapeId > 0) {
646 aContName = aSubContName;
654 TopoDS_Shape aSelection;
660 aSelection = findFaceByName(aSubShapeName, aDoc);
665 const TDF_Label& aLabel = aDoc->findNamingName(aSubShapeName);
666 if(!aLabel.IsNull()) {
667 Handle(TNaming_NamedShape) aNS;
668 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
669 aSelection = getShapeFromNS(aSubShapeName, aNS);
676 const TDF_Label& aLabel = aDoc->findNamingName(aSubShapeName);
677 if(!aLabel.IsNull()) {
678 Handle(TNaming_NamedShape) aNS;
679 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
680 aSelection = getShapeFromNS(aSubShapeName, aNS);
685 case TopAbs_COMPOUND:
686 case TopAbs_COMPSOLID:
689 default: {//TopAbs_SHAPE
690 /// case when the whole sketch is selected, so, selection type is compound, but there is no value
691 if (aCont.get() && aCont->shape().get()) {
692 if (aCont->shape()->impl<TopoDS_Shape>().ShapeType() == aType) {
695 } else if (aSubShapeId > 0) { // try to find sub-shape by the index
696 TopExp_Explorer anExp(aCont->shape()->impl<TopoDS_Shape>(), aType);
697 for(; aSubShapeId > 0 && anExp.More(); aSubShapeId--) {
701 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
702 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
703 theShapeToBeSelected = aShapeToBeSelected;
712 // another try to find edge or vertex by faces
713 std::list<std::string> aListofNames;
714 size_t aN = aSelection.IsNull() ? ParseName(aSubShapeName, aListofNames) : 0;
715 if (aSelection.IsNull() && (aType == TopAbs_EDGE || aType == TopAbs_VERTEX)) {
716 if(aN > 1 && (aN < 4 || (aType == TopAbs_EDGE && aN < 5))) { // 2 || 3 or 4 for EDGE
717 TopTools_ListOfShape aList;
718 std::list<std::string>::iterator it = aListofNames.begin();
719 for(; it != aListofNames.end(); it++){
720 const TopoDS_Shape aFace = findFaceByName(*it, aDoc);
724 aSelection = findCommonShape(aType, aList);
727 if (!aSelection.IsNull()) {// Select it
728 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
729 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
730 theShapeToBeSelected = aShapeToBeSelected;
734 // in case of construction, there is no registered names for all sub-elements,
735 // even for the main element; so, trying to find them by name (without "&" intersections)
737 size_t aConstrNamePos = aSubShapeName.find("/");
738 bool isFullName = aConstrNamePos == std::string::npos;
739 std::string aContrName = aContName;
740 ResultPtr aConstr = aDoc->findByName(aContrName);
741 if (aConstr.get() && aConstr->groupName() == ModelAPI_ResultConstruction::group()) {
744 theShapeToBeSelected = aConstr->shape();
747 // for sketch sub-elements selected
748 CompositeFeaturePtr aComposite =
749 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aConstr));
750 if (aComposite.get()) {
751 if (aType == TopAbs_VERTEX || aType == TopAbs_EDGE) {
752 // collect all IDs in the name
753 std::map<int, int> anIDs;
754 if (!parseSubIndices(aComposite, aSubShapeName,
755 aType == TopAbs_EDGE ? "Edge" : "Vertex", anIDs))
758 const int aSubNum = aComposite->numberOfSubs();
759 for(int a = 0; a < aSubNum; a++) {
760 int aCompID = aComposite->subFeatureId(a);
761 if (anIDs.find(aCompID) != anIDs.end()) { // found the vertex/edge shape
762 FeaturePtr aSub = aComposite->subFeature(a);
763 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
764 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIt = aResults.cbegin();
765 // there may be many shapes (circle and center)
766 for(; aRIt != aResults.cend(); aRIt++) {
767 ResultConstructionPtr aRes =
768 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRIt);
770 int anOrientation = abs(anIDs[aCompID]);
771 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
772 if (anOrientation == 1) {
773 if (aType == aShape.ShapeType()) {
774 theShapeToBeSelected = aRes->shape();
777 } else { // take first or second vertex of the edge
778 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
779 TopExp_Explorer anExp(aShape, aType);
780 for(; anExp.More() && anOrientation != 2; anOrientation--)
783 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
784 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
785 theShapeToBeSelected = aShapeToBeSelected;
793 // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
794 } else if (aType == TopAbs_FACE || aType == TopAbs_WIRE) {
795 std::map<int, int> anIDs;
796 if (!parseSubIndices(aComposite, aSubShapeName,
797 aType == TopAbs_FACE ? "Face" : "Wire", anIDs, true))
800 NCollection_DataMap<Handle(Geom_Curve), int> allCurves; // curves and orientations of edges
801 const int aSubNum = aComposite->numberOfSubs();
802 for(int a = 0; a < aSubNum; a++) {
803 int aSubID = aComposite->subFeatureId(a);
804 if (anIDs.find(aSubID) != anIDs.end()) {
805 FeaturePtr aSub = aComposite->subFeature(a);
806 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
807 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
808 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
809 ResultConstructionPtr aConstr =
810 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
811 if (aConstr->shape() && aConstr->shape()->isEdge()) {
812 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
813 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
814 if (!anEdge.IsNull()) {
815 Standard_Real aFirst, aLast;
816 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
817 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
823 std::shared_ptr<GeomAPI_Shape> aFoundFace = findAppropriateFace(aConstr, allCurves);
824 if (aFoundFace.get()) {
825 if (aType == TopAbs_WIRE) { // just get a wire from face to have wire
826 TopExp_Explorer aWireExp(aFoundFace->impl<TopoDS_Shape>(), TopAbs_WIRE);
827 if (aWireExp.More()) {
828 theShapeToBeSelected.reset(new GeomAPI_Shape);
829 theShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aWireExp.Current()));
832 theShapeToBeSelected = aFoundFace;
836 } else if (aType == TopAbs_WIRE) { // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
837 std::map<int, int> anIDs;
838 if (!parseSubIndices(aComposite, aSubShapeName, "Wire", anIDs))
841 NCollection_DataMap<Handle(Geom_Curve), int> allCurves; // curves and orientations of edges
842 const int aSubNum = aComposite->numberOfSubs();
843 for(int a = 0; a < aSubNum; a++) {
844 int aSubID = aComposite->subFeatureId(a);
845 if (anIDs.find(aSubID) != anIDs.end()) {
846 FeaturePtr aSub = aComposite->subFeature(a);
847 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
848 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
849 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
850 ResultConstructionPtr aConstr =
851 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
852 if (aConstr->shape() && aConstr->shape()->isEdge()) {
853 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
854 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
855 if (!anEdge.IsNull()) {
856 Standard_Real aFirst, aLast;
857 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
858 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
864 std::shared_ptr<GeomAPI_Shape> aFoundFace = findAppropriateFace(aConstr, allCurves);
865 if (aFoundFace.get()) {
866 theShapeToBeSelected = aFoundFace;