1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Model_SelectionNaming.h"
22 #include "Model_Document.h"
23 #include "Model_Objects.h"
24 #include "Model_Data.h"
25 #include <ModelAPI_Feature.h>
26 #include <Events_InfoMessage.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_ResultPart.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_CompositeFeature.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <GeomAPI_Wire.h>
33 #include <GeomAPI_Edge.h>
35 #include <TopoDS_Iterator.hxx>
37 #include <TopoDS_Compound.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_MapOfShape.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_MapIteratorOfMapOfShape.hxx>
46 #include <BRep_Builder.hxx>
47 #include <TNaming_Iterator.hxx>
48 #include <TNaming_Tool.hxx>
49 #include <TNaming_NamedShape.hxx>
50 #include <TNaming_Localizer.hxx>
51 #include <TNaming_SameShapeIterator.hxx>
52 #include <TDataStd_Name.hxx>
53 #include <TColStd_MapOfTransient.hxx>
54 #include <Precision.hxx>
59 #include <BRepTools.hxx>
62 Model_SelectionNaming::Model_SelectionNaming(TDF_Label theSelectionLab)
64 myLab = theSelectionLab;
67 // searches named shape by the shape in the given document (identified by the label)
68 // tries to find s shape nearest to the context-label
69 static Handle(TNaming_NamedShape) shapeToNS(const TDF_Label theLabAccess,
70 const TopoDS_Shape& theShape, const TDF_Label& theContextLab)
72 Handle(TNaming_NamedShape) aResult;
73 if (!TNaming_Tool::HasLabel(theLabAccess, theShape)) // no shape in the document
75 int aContextLabDepth = theContextLab.IsNull() ? 100 : theContextLab.Depth();
76 TNaming_SameShapeIterator aNSIter(theShape, theLabAccess);
77 for(; aNSIter.More(); aNSIter.Next()) {
78 TDF_Label aLabel = aNSIter.Label();
79 Handle(TNaming_NamedShape) aNS;
80 if (aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
81 if (aNS->Evolution() != TNaming_SELECTED && aNS->Evolution() != TNaming_DELETE) {
82 // check this is new shape in this named shape
84 for(TNaming_Iterator aNSIter(aNS); aNSIter.More(); aNSIter.Next())
85 if (!aNSIter.NewShape().IsNull() && aNSIter.NewShape().IsSame(theShape))
89 // check this is the context-shape
90 while(aLabel.Depth() > aContextLabDepth)
91 aLabel = aLabel.Father();
92 if (aLabel.IsEqual(theContextLab))
94 if (aResult.IsNull()) // take the first, otherwise it will get shapes from results, etc
95 aResult = aNS; // keep some result anyway - if there are no context labels return any
102 std::string Model_SelectionNaming::getShapeName(
103 std::shared_ptr<Model_Document> theDoc, const TopoDS_Shape& theShape,
104 ResultPtr& theContext, const bool theAnotherDoc, const bool theWholeContext)
107 // add the result name to the name of the shape
108 // (it was in BodyBuilder, but did not work on Result rename)
109 bool isNeedContextName = theContext->shape().get() != NULL;
110 // check if the subShape is already in DF
111 std::shared_ptr<Model_Data> aData =
112 std::dynamic_pointer_cast<Model_Data>(theContext->data());
113 TDF_Label aContextDataLab(aData.get() && aData->isValid() ? aData->label() : TDF_Label());
114 Handle(TNaming_NamedShape) aNS = shapeToNS(myLab, theShape, aContextDataLab);
115 Handle(TDataStd_Name) anAttr;
116 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
117 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
118 if (isNeedContextName && aData && aContextDataLab.IsEqual(aNS->Label())) {
119 // do nothing because this context name will be added later in this method
121 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
122 // indexes are added to sub-shapes not primitives
123 // (primitives must not be located at the same label)
124 if(!aName.empty() && aNS->Evolution() != TNaming_PRIMITIVE && isNeedContextName) {
125 const TDF_Label& aLabel = aNS->Label();
126 static const std::string aPostFix("_");
127 TNaming_Iterator anItL(aNS);
128 for(int i = 1; anItL.More(); anItL.Next(), i++) {
129 // in #1766 IsEqual produced no index of the face
130 if(anItL.NewShape().IsSame(theShape)) {
132 aName += TCollection_AsciiString (i).ToCString();
137 // if a shape is under another context, use this name, not theContext
138 std::shared_ptr<Model_Data> aContextData =
139 std::dynamic_pointer_cast<Model_Data>(theContext->data());
140 // for constructions the naming is in arguments and has no evolution, so, apply this only
142 if (isNeedContextName && theContext->groupName() == ModelAPI_ResultBody::group() &&
143 !aNS->Label().IsDescendant(aContextData->label())) {
144 isNeedContextName = false;
145 TDF_Label aNSDataLab = aNS->Label();
146 if (aNSDataLab.Depth() % 2 == 0)
147 aNSDataLab = aNSDataLab.Father();
148 ObjectPtr aNewContext = theDoc->objects()->object(aNSDataLab);
149 while(!aNewContext.get() && aNSDataLab.Depth() > 5) {
150 aNSDataLab = aNSDataLab.Father().Father();
151 aNewContext = theDoc->objects()->object(aNSDataLab);
153 if (aNewContext.get()) {
154 // this is to avoid duplicated names of results problem
155 std::string aContextName = aNewContext->data()->name();
156 // myLab corresponds to the current time
157 TDF_Label aCurrentLab = myLab;
158 while(aCurrentLab.Depth() > 3)
159 aCurrentLab = aCurrentLab.Father();
161 int aNumInHistoryNames =
162 theDoc->numberOfNameInHistory(aNewContext, aCurrentLab);
163 while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
164 aContextName = "_" + aContextName;
165 aNumInHistoryNames--;
168 aName = aContextName + "/" + aName;
175 // Name is empty and this is full context, it just add the whole context name that must be added
176 bool isEmptyName = aName.empty();
177 if (isNeedContextName && (!isEmptyName || theWholeContext)) {
178 aName = theContext->data()->name() + (isEmptyName ? "" : ("/" + aName));
180 aName = theContext->document()->kind() + "/" + aName; // PartSet
185 bool isTrivial (const TopTools_ListOfShape& theAncestors, TopTools_IndexedMapOfShape& theSMap)
187 // a trivial case: F1 & F2, aNumber = 1, i.e. intersection gives 1 edge.
188 TopoDS_Compound aCmp;
190 BB.MakeCompound(aCmp);
191 TopTools_ListIteratorOfListOfShape it(theAncestors);
192 for(;it.More();it.Next()) {
193 if (theSMap.Contains(it.Value()))
195 BB.Add(aCmp, it.Value());
196 theSMap.Add(it.Value());
199 TopTools_IndexedDataMapOfShapeListOfShape aMap2;
200 TopExp::MapShapesAndAncestors(aCmp, TopAbs_EDGE, TopAbs_FACE, aMap2);
201 for (int i = 1; i <= aMap2.Extent(); i++) {
202 const TopoDS_Shape& aKey = aMap2.FindKey(i);
203 const TopTools_ListOfShape& anAncestors = aMap2.FindFromIndex(i);
204 if(anAncestors.Extent() > 1) aNumber++;
206 if(aNumber > 1) return false;
210 const TopoDS_Shape findCommonShape(
211 const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
213 if(theList.Extent() < 1) {
214 return TopoDS_Shape();
215 } else if (theList.Extent() == 1) { // check that sub-shape is bounded by this alone shape
216 TopTools_MapOfShape aSubsInShape;
217 TopExp_Explorer anExp(theList.First(), theType);
218 for(; anExp.More(); anExp.Next()) {
219 if (aSubsInShape.Contains(anExp.Current())) { // found duplicate
220 return anExp.Current();
222 aSubsInShape.Add(anExp.Current());
226 // Store in maps sub-shapes from each face.
227 std::vector<TopTools_MapOfShape> aVec;
228 for(TopTools_ListIteratorOfListOfShape anIt(theList); anIt.More(); anIt.Next()) {
229 const TopoDS_Shape aFace = anIt.Value();
230 TopTools_MapOfShape aMap;
231 for(TopExp_Explorer anExp(aFace, theType); anExp.More(); anExp.Next()) {
232 const TopoDS_Shape& aSubShape = anExp.Current();
233 aMap.Add(anExp.Current());
235 aVec.push_back(aMap);
238 // Find sub-shape shared between all faces.
239 TopoDS_Shape aSharedShape;
240 for(TopTools_MapIteratorOfMapOfShape anIt(aVec[0]); anIt.More(); anIt.Next()) {
241 const TopoDS_Shape& aSubShape = anIt.Value();
243 for(int anIndex = 1; anIndex < aVec.size(); ++anIndex) {
244 if(aVec[anIndex].Contains(aSubShape)) {
248 if(aSharedNb == theList.Extent()) {
249 if(aSharedShape.IsNull()) {
250 aSharedShape = aSubShape;
252 // More than one shape shared between all faces, return null shape in this case.
253 return TopoDS_Shape();
261 // searches theType shape that contains theConnectionType sub-shapes in each shape from the List,
262 // so, implements the neighbours searching
264 const TopoDS_Shape findCommonShapeByNB(const TopAbs_ShapeEnum theType,
265 const TopAbs_ShapeEnum theConnectionType, const TopTools_ListOfShape& theList)
267 TopTools_MapOfShape aCheckedShapes; // already checked shapes of type theType
268 TopoDS_Shape aResult; // theType result shape
269 for(TopTools_ListIteratorOfListOfShape anIt(theList); anIt.More(); anIt.Next()) { // iterate all
270 for(TopExp_Explorer anExp(anIt.ChangeValue(), theType); anExp.More(); anExp.Next()) {
271 if (aCheckedShapes.Contains(anExp.Current()))
272 continue; // already checked
273 aCheckedShapes.Add(anExp.Current());
274 TopTools_MapOfShape aConnectors; // all connectors of the checked theType shape
275 for(TopExp_Explorer aCExp(anExp.Current(), theConnectionType); aCExp.More(); aCExp.Next()) {
276 aConnectors.Add(aCExp.Current());
278 // check that all shapes from the List contain the connector sub-shapes
280 for(TopTools_ListIteratorOfListOfShape anIt2(theList); anIt2.More() && aFound; anIt2.Next()) {
281 if (anIt2.Value().IsSame(anIt.Value()))
284 for(TopExp_Explorer anE(anIt2.ChangeValue(), theConnectionType); anE.More(); anE.Next()) {
285 if (aConnectors.Contains(anE.Current())) {
292 if (!aResult.IsNull()) // more than one result
293 return TopoDS_Shape();
294 aResult = anExp.Current();
301 std::string Model_SelectionNaming::vertexNameByEdges(TopoDS_Shape theContext, TopoDS_Shape theSub,
302 std::shared_ptr<Model_Document> theDoc, ResultPtr& theContextRes, const bool theAnotherDoc)
305 TopTools_IndexedDataMapOfShapeListOfShape aMap;
306 TopExp::MapShapesAndAncestors(theContext, TopAbs_VERTEX, TopAbs_EDGE, aMap);
307 const TopTools_ListOfShape& aList22 = aMap.FindFromKey(theSub);
308 if(aList22.Extent() >= 2) { // regular solution
309 TopTools_MapOfShape aFMap;
310 TopTools_ListOfShape aListE;
311 TopTools_ListIteratorOfListOfShape itl2(aList22);
312 for (int i = 1;itl2.More();itl2.Next(),i++) {
313 if(aFMap.Add(itl2.Value()))
314 aListE.Append(itl2.Value());
316 TopTools_ListIteratorOfListOfShape itl(aListE);
317 for (int i = 1;itl.More();itl.Next(),i++) {
318 const TopoDS_Shape& anEdge = itl.Value();
319 std::string anEdgeName = getShapeName(theDoc, anEdge, theContextRes, theAnotherDoc, false);
320 if (anEdgeName.empty()) { // edge is not in DS
325 aResult = anEdgeName;
327 aResult += "&" + anEdgeName;
333 std::string Model_SelectionNaming::namingName(ResultPtr& theContext,
334 std::shared_ptr<GeomAPI_Shape> theSubSh, const std::string& theDefaultName,
335 const bool theAnotherDoc)
337 std::string aName("Undefined name");
339 || !theContext->shape().get()
340 || theContext->shape()->isNull()) {
341 return !theDefaultName.empty() ? theDefaultName : aName;
344 // if it is in result of another part
345 std::shared_ptr<Model_Document> aDoc =
346 std::dynamic_pointer_cast<Model_Document>(theContext->document());
347 if (theContext->groupName() == ModelAPI_ResultPart::group()) {
348 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
351 return aPart->data()->name() + "/" + aPart->nameInPart(theSubSh, anIndex);
353 return aPart->data()->name();
356 if (!theSubSh.get() || theSubSh->isNull()) { // no subshape, so just the whole feature name
357 // but if it is in another Part, add this part name
358 std::string aPartName;
360 aPartName = theContext->document()->kind() + "/"; // PartSet
361 return aPartName + theContext->data()->name();
363 TopoDS_Shape aSubShape = theSubSh->impl<TopoDS_Shape>();
364 TopoDS_Shape aContext = theContext->shape()->impl<TopoDS_Shape>();
366 if(aSubShape.ShapeType() == TopAbs_COMPOUND) {
367 BRepTools::Write(aSubShape, "Selection.brep");
368 BRepTools::Write(aContext, "Context.brep");
371 aName = getShapeName(aDoc, aSubShape, theContext, theAnotherDoc,
372 theContext->shape()->isEqual(theSubSh));
374 if(aName.empty() ) { // not in the document!
375 TopAbs_ShapeEnum aType = aSubShape.ShapeType();
378 // the Face should be in DF. If it is not the case, it is an error ==> to be debugged
382 // name structure: F1 & F2 [& F3 & F4],
383 // where F1 & F2 the faces which gives the Edge in trivial case
384 // if it is not atrivial case we use localization by neighbours. F3 & F4 - neighbour faces
385 if (BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
386 aName = "Degenerated_Edge";
389 TopTools_IndexedDataMapOfShapeListOfShape aMap;
390 TopExp::MapShapesAndAncestors(aContext, TopAbs_EDGE, TopAbs_FACE, aMap);
391 TopTools_IndexedMapOfShape aSMap; // map for ancestors of the sub-shape
392 bool isTrivialCase(true);
393 if(aMap.Contains(aSubShape)) {
394 const TopTools_ListOfShape& anAncestors = aMap.FindFromKey(aSubShape);
395 // check that it is not a trivial case (F1 & F2: aNumber = 1)
396 isTrivialCase = isTrivial(anAncestors, aSMap);
397 if (!isTrivialCase) { // another try: check that common shape can be processed anyway
398 isTrivialCase = !findCommonShape(TopAbs_EDGE, anAncestors).IsNull();
402 TopTools_MapOfShape aNbs;
403 if(!isTrivialCase) { // find Neighbors
404 TNaming_Localizer aLocalizer;
405 TopTools_MapOfShape aMap3;
406 aLocalizer.FindNeighbourg(aContext, aSubShape, aMap3);
407 //int n = aMap3.Extent();
408 TopTools_MapIteratorOfMapOfShape it(aMap3);
409 for(;it.More();it.Next()) {
410 const TopoDS_Shape& aNbShape = it.Key(); // neighbor edge
411 //TopAbs_ShapeEnum aType = aNbShape.ShapeType();
412 const TopTools_ListOfShape& aList = aMap.FindFromKey(aNbShape);
413 TopTools_ListIteratorOfListOfShape it2(aList);
414 for(;it2.More();it2.Next()) {
415 if(aSMap.Contains(it2.Value())) continue; // skip this Face
416 aNbs.Add(it2.Value());
419 } // else a trivial case
421 // build name of the sub-shape Edge
422 // iterate faces of the context to get stable order, not map-order
423 TopTools_MapOfShape aStoredFaces; // to avoid duplicates
424 for(TopExp_Explorer aContExp(aContext, TopAbs_FACE); aContExp.More(); aContExp.Next()) {
425 const TopoDS_Shape& aFace = aContExp.Current();
426 if (aStoredFaces.Contains(aFace) || !(aSMap.Contains(aFace) || aNbs.Contains(aFace)))
428 aStoredFaces.Add(aFace);
429 std::string aFaceName = getShapeName(aDoc, aFace, theContext, theAnotherDoc, false);
433 aName += "&" + aFaceName;
439 // name structure (Monifold Topology):
440 // 1) F1 | F2 | F3 - intersection of 3 faces defines a vertex - trivial case.
441 // 2) F1 | F2 | F3 [|F4 [|Fn]] - redundant definition,
442 // but it should be kept as is to obtain safe recomputation
443 // 2) F1 | F2 - intersection of 2 faces definses a vertex - applicable for case
444 // when 1 faces is cylindrical, conical, spherical or revolution and etc.
445 // 3) E1 | E2 | E3 - intersection of 3 edges defines a vertex - when we have case of a shell
446 // or compound of 2 open faces.
447 // 4) E1 | E2 - intesection of 2 edges defines a vertex - when we have a case of
448 // two independent edges (wire or compound)
449 // implemented 2 first cases
451 TopTools_IndexedDataMapOfShapeListOfShape aMap;
452 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_FACE, aMap);
453 TopTools_ListOfShape aList;
454 TopTools_MapOfShape aFMap;
455 // simetimes when group is moved in history, naming may be badly updated, so
456 // avoid crash in FindFromKey (issue 1842)
457 if (aMap.Contains(aSubShape)) {
458 const TopTools_ListOfShape& aList2 = aMap.FindFromKey(aSubShape);
460 TopTools_ListIteratorOfListOfShape itl2(aList2);
461 for (int i = 1;itl2.More();itl2.Next(),i++) {
462 if(aFMap.Add(itl2.Value()))
463 aList.Append(itl2.Value());
467 int n = aList.Extent();
468 bool isByFaces = n >= 3;
469 if (isByFaces) { // check that by faces vertex is identified uniquly (2317)
470 TopoDS_Shape aVertex = findCommonShape(TopAbs_VERTEX, aList);
471 isByFaces = !aVertex.IsNull() && aVertex.ShapeType() == TopAbs_VERTEX;
474 if(!isByFaces) { // open topology case or Compound case => via edges
475 aName = vertexNameByEdges(aContext, aSubShape, aDoc, theContext, theAnotherDoc);
476 isByFaces = aName.empty();
477 if (isByFaces) { // try to find a vertex in sketch faces
478 ResultConstructionPtr aConstr =
479 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
480 if (aConstr.get() && aConstr->facesNum()) {
481 for(int aFace = aConstr->facesNum() - 1; isByFaces && aFace >= 0; aFace--) {
482 std::shared_ptr<GeomAPI_Face> aGFace = aConstr->face(aFace);
483 aName = vertexNameByEdges(aGFace->impl<TopoDS_Face>(), aSubShape,
484 aDoc, theContext, theAnotherDoc);
485 isByFaces = aName.empty();
492 TopTools_ListIteratorOfListOfShape itl(aList);
493 for (int i = 1;itl.More();itl.Next(),i++) {
494 const TopoDS_Shape& aFace = itl.Value();
495 std::string aFaceName = getShapeName(aDoc, aFace, theContext, theAnotherDoc, false);
499 aName += "&" + aFaceName;
509 TopAbs_ShapeEnum translateType (const std::string& theType)
511 // map from the textual shape types to OCCT enumeration
512 static std::map<std::string, TopAbs_ShapeEnum> aShapeTypes;
514 if(aShapeTypes.size() == 0) {
515 aShapeTypes["compound"] = TopAbs_COMPOUND;
516 aShapeTypes["compounds"] = TopAbs_COMPOUND;
517 aShapeTypes["compsolid"] = TopAbs_COMPSOLID;
518 aShapeTypes["compsolids"] = TopAbs_COMPSOLID;
519 aShapeTypes["solid"] = TopAbs_SOLID;
520 aShapeTypes["solids"] = TopAbs_SOLID;
521 aShapeTypes["shell"] = TopAbs_SHELL;
522 aShapeTypes["shells"] = TopAbs_SHELL;
523 aShapeTypes["face"] = TopAbs_FACE;
524 aShapeTypes["faces"] = TopAbs_FACE;
525 aShapeTypes["wire"] = TopAbs_WIRE;
526 aShapeTypes["wires"] = TopAbs_WIRE;
527 aShapeTypes["edge"] = TopAbs_EDGE;
528 aShapeTypes["edges"] = TopAbs_EDGE;
529 aShapeTypes["vertex"] = TopAbs_VERTEX;
530 aShapeTypes["vertices"] = TopAbs_VERTEX;
531 aShapeTypes["COMPOUND"] = TopAbs_COMPOUND;
532 aShapeTypes["COMPOUNDS"] = TopAbs_COMPOUND;
533 aShapeTypes["COMPSOLID"] = TopAbs_COMPSOLID;
534 aShapeTypes["COMPSOLIDS"] = TopAbs_COMPSOLID;
535 aShapeTypes["SOLID"] = TopAbs_SOLID;
536 aShapeTypes["SOLIDS"] = TopAbs_SOLID;
537 aShapeTypes["SHELL"] = TopAbs_SHELL;
538 aShapeTypes["SHELLS"] = TopAbs_SHELL;
539 aShapeTypes["FACE"] = TopAbs_FACE;
540 aShapeTypes["FACES"] = TopAbs_FACE;
541 aShapeTypes["WIRE"] = TopAbs_WIRE;
542 aShapeTypes["WIRES"] = TopAbs_WIRE;
543 aShapeTypes["EDGE"] = TopAbs_EDGE;
544 aShapeTypes["EDGES"] = TopAbs_EDGE;
545 aShapeTypes["VERTEX"] = TopAbs_VERTEX;
546 aShapeTypes["VERTICES"] = TopAbs_VERTEX;
548 if (aShapeTypes.find(theType) != aShapeTypes.end())
549 return aShapeTypes[theType];
550 Events_InfoMessage("Model_SelectionNaming",
551 "Shape type defined in XML is not implemented!").send();
555 const TopoDS_Shape getShapeFromNS(
556 const std::string& theSubShapeName, Handle(TNaming_NamedShape) theNS)
558 TopoDS_Shape aSelection;
559 std::string::size_type n = theSubShapeName.rfind('/');
560 if (n == std::string::npos) n = -1;
561 std::string aSubString = theSubShapeName.substr(n + 1);
562 n = aSubString.rfind('_');
564 if (n != std::string::npos) {// for primitives this is a first
565 // if we have here the same name as theSubShapeName, there is no index in compound, it is whole
566 Handle(TDataStd_Name) aName;
567 if (!theNS->Label().FindAttribute(TDataStd_Name::GetID(), aName) ||
568 aName->Get() != aSubString.c_str()) {
569 aSubString = aSubString.substr(n+1);
570 indx = atoi(aSubString.c_str());
574 TNaming_Iterator anItL(theNS);
575 for(int i = 1; anItL.More(); anItL.Next(), i++) {
577 return anItL.NewShape();
583 const TopoDS_Shape findFaceByName(
584 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc,
585 const ResultPtr theDetectedContext, bool theContextIsUnique)
588 std::string aSubString = theSubShapeName;
590 static const ResultPtr anEmpty;
591 TDF_Label aLabel = theDoc->findNamingName(aSubString,
592 theContextIsUnique ? theDetectedContext : anEmpty);
593 if (aLabel.IsNull()) { // try to remove additional artificial suffix
594 std::string::size_type n = aSubString.rfind('_');
595 if (n != std::string::npos) {
596 aSubString = aSubString.substr(0, n);
597 aLabel = theDoc->findNamingName(aSubString,
598 theContextIsUnique ? theDetectedContext : anEmpty);
601 if(aLabel.IsNull()) return aFace;
602 Handle(TNaming_NamedShape) aNS;
603 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
604 aFace = getShapeFromNS(theSubShapeName, aNS);
609 size_t ParseName(const std::string& theSubShapeName, std::list<std::string>& theList)
611 std::string aName = theSubShapeName;
612 std::string aLastName = aName;
613 size_t n1(0), n2(0); // n1 - start position, n2 - position of the delimiter
614 while ((n2 = aName.find('&', n1)) != std::string::npos) {
615 const std::string aName1 = aName.substr(n1, n2 - n1); //name of face
616 theList.push_back(aName1);
618 aLastName = aName.substr(n1);
620 if(!aLastName.empty())
621 theList.push_back(aLastName);
622 return theList.size();
625 std::string getContextName(const std::string& theSubShapeName)
628 std::string::size_type n = theSubShapeName.find('/');
629 if (n == std::string::npos) return theSubShapeName;
630 aName = theSubShapeName.substr(0, n);
634 /// Parses naming name of sketch sub-elements: takes indices and orientation
635 /// (if theOriented = true) from this name. Map theIDs constains indices ->
636 /// orientations and start/end vertices: negative is reversed, 2 - start, 3 - end
637 bool parseSubIndices(CompositeFeaturePtr theComp, //< to iterate names
638 const std::string& theName, const char* theShapeType,
639 std::map<int, int>& theIDs, const bool theOriented = false)
641 // collect all IDs in the name
642 std::map<std::string, int> aNames; // short name of sub -> ID of sub of theComp
643 const int aSubNum = theComp->numberOfSubs();
644 for(int a = 0; a < aSubNum; a++) {
645 FeaturePtr aSub = theComp->subFeature(a);
646 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
647 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
648 // there may be many shapes (circle and center)
649 for(; aRes != aResults.cend(); aRes++) {
650 ResultConstructionPtr aConstr =
651 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
653 aNames[Model_SelectionNaming::shortName(aConstr)] = theComp->subFeatureId(a);
658 size_t aPrevPos = theName.find("/") + 1, aLastNamePos;
659 bool isShape = false; // anyway the first world must be 'Vertex'
661 aLastNamePos = theName.find('-', aPrevPos);
662 std::string anID = theName.substr(aPrevPos, aLastNamePos - aPrevPos);
664 if (anID != theShapeType)
668 int anOrientation = 1; // default
669 if (theOriented) { // here must be a symbol in the end of digit 'f' or 'r'
670 std::string::iterator aSymbol = anID.end() - 1;
671 if (*aSymbol == 'r') anOrientation = -1;
672 anID.erase(aSymbol); // remove last symbol
674 // check start/end symbols
675 std::string::iterator aBack = anID.end() - 1;
678 anID.erase(aBack); // remove last symbol
679 } else if (*aBack == 'e') {
681 anID.erase(aBack); // remove last symbol
684 if (aNames.find(anID) != aNames.end()) {
685 theIDs[aNames[anID]] = anOrientation;
688 aPrevPos = aLastNamePos + 1;
689 } while (aLastNamePos != std::string::npos);
693 /// produces theEdge orientation relatively to theContext face
694 int Model_SelectionNaming::edgeOrientation(const TopoDS_Shape& theContext, TopoDS_Edge& theEdge)
696 if (theContext.ShapeType() != TopAbs_FACE && theContext.ShapeType() != TopAbs_WIRE)
698 if (theEdge.Orientation() == TopAbs_FORWARD)
700 if (theEdge.Orientation() == TopAbs_REVERSED)
705 int Model_CurvesHasher::HashCode(const Handle(Geom_Curve)& theCurve, const Standard_Integer Upper)
707 double aFirstParam = theCurve->FirstParameter();
708 if (aFirstParam < -1.e+100 || aFirstParam > 1.e+100)
710 double aLastParam = theCurve->LastParameter();
711 if (aLastParam < -1.e+100 || aLastParam > 1.e+100)
713 else aLastParam = (aLastParam + aFirstParam) / 2.; // to avoid in periodic same first and last
715 gp_XYZ aCoordSum = theCurve->Value(aFirstParam).XYZ() + theCurve->Value(aLastParam).XYZ();
716 return ::HashCode(aCoordSum.X() + aCoordSum.Y() / 123. + aCoordSum.Z() / 123456., Upper);
718 bool Model_CurvesHasher::IsEqual(const Handle(Geom_Curve)& theC1, const Handle(Geom_Curve)& theC2)
720 if (theC1->DynamicType() != theC2->DynamicType())
722 double aFirstParam1 = theC1->FirstParameter();
723 if (aFirstParam1 < -1.e+100 || aFirstParam1 > 1.e+100)
725 double aFirstParam2 = theC2->FirstParameter();
726 if (aFirstParam2 < -1.e+100 || aFirstParam2 > 1.e+100)
728 if (fabs(aFirstParam1 - aFirstParam2) > 1.e-9)
731 double aLastParam1 = theC1->LastParameter();
732 if (aLastParam1 < -1.e+100 || aLastParam1 > 1.e+100)
734 else aLastParam1 = (aLastParam1 + aFirstParam1) / 2.; // to avoid in periodic same first and last
735 double aLastParam2 = theC2->LastParameter();
736 if (aLastParam2 < -1.e+100 || aLastParam2 > 1.e+100)
738 else aLastParam2 = (aLastParam2 + aFirstParam2) / 2.; // to avoid in periodic same first and last
740 if (fabs(aLastParam1 - aLastParam2) > 1.e-9)
743 return theC1->Value(aFirstParam1).IsEqual(theC2->Value(aFirstParam2), Precision::Confusion()) &&
744 theC1->Value(aLastParam1).IsEqual(theC2->Value(aLastParam2), Precision::Confusion());
747 int Model_EdgesHasher::HashCode(const TopoDS_Edge& theEdge, const Standard_Integer Upper)
749 Standard_Real aFirst, aLast;
750 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(theEdge, aFirst, aLast);
751 return Model_CurvesHasher::HashCode(aCurve, Upper);
754 bool Model_EdgesHasher::IsEqual(const TopoDS_Edge& theE1, const TopoDS_Edge& theE2)
756 GeomEdgePtr aSh1(new GeomAPI_Edge);
757 aSh1->setImpl(new TopoDS_Shape(theE1));
758 GeomEdgePtr aSh2(new GeomAPI_Edge);
759 aSh2->setImpl(new TopoDS_Shape(theE2));
760 return aSh1->isEqual(aSh2);
763 std::shared_ptr<GeomAPI_Shape> Model_SelectionNaming::findAppropriateFace(
764 std::shared_ptr<ModelAPI_Result>& theConstr,
765 NCollection_DataMap<Handle(Geom_Curve), int, Model_CurvesHasher>& theCurves, const bool theIsWire)
767 int aBestFound = 0; // best number of found edges (not percentage: issue 1019)
768 int aBestNotFound = 1000000; // best number of not found edges (must be minimum)
769 int aBestOrient = 0; // for the equal "BestFound" additional parameter is orientation
770 std::shared_ptr<GeomAPI_Shape> aResult;
771 ResultConstructionPtr aConstructionContext =
772 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theConstr);
773 if (!aConstructionContext.get())
775 for(int aFaceIndex = 0; aFaceIndex < aConstructionContext->facesNum(); aFaceIndex++) {
776 int aFound = 0, aNotFound = 0, aSameOrientation = 0;
778 TopoDS::Face(aConstructionContext->face(aFaceIndex)->impl<TopoDS_Shape>());
779 std::list<TopoDS_Shape> aFacesWires; // faces or wires to iterate
781 for(TopExp_Explorer aWires(aFace, TopAbs_WIRE); aWires.More(); aWires.Next()) {
782 aFacesWires.push_back(aWires.Current());
785 aFacesWires.push_back(aFace);
787 std::list<TopoDS_Shape>::iterator aFW = aFacesWires.begin();
788 for(; aFW != aFacesWires.end(); aFW++) {
789 TopExp_Explorer anEdgesExp(*aFW, TopAbs_EDGE);
790 TColStd_MapOfTransient alreadyProcessed; // to avoid counting edges with same curves (841)
791 for(; anEdgesExp.More(); anEdgesExp.Next()) {
792 TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
793 if (!anEdge.IsNull()) {
794 Standard_Real aFirst, aLast;
795 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
796 if (alreadyProcessed.Contains(aCurve))
798 alreadyProcessed.Add(aCurve);
799 if (theCurves.IsBound(aCurve)) {
801 int anOrient = theCurves.Find(aCurve);
802 if (anOrient != 0) { // extra comparision score is orientation
803 if (edgeOrientation(aFace, anEdge) == anOrient)
811 if (theIsWire && aFound + aNotFound != 0) {
812 if (aBestNotFound > aNotFound || (aBestNotFound == aNotFound && aFound > aBestFound) ||
813 (aBestNotFound == aNotFound && aFound == aBestFound && aSameOrientation > aBestOrient)) {
815 aBestOrient = aSameOrientation;
816 aBestNotFound = aNotFound;
817 std::shared_ptr<GeomAPI_Wire> aWire(new GeomAPI_Wire);
818 aWire->setImpl(new TopoDS_Shape(*aFW));
823 aSameOrientation = 0;
827 if (aFound + aNotFound != 0) {
828 if (aBestNotFound > aNotFound || (aBestNotFound == aNotFound && aFound > aBestFound) ||
829 (aBestNotFound == aNotFound && aFound == aBestFound && aSameOrientation > aBestOrient)) {
831 aBestOrient = aSameOrientation;
832 aBestNotFound = aNotFound;
833 aResult = aConstructionContext->face(aFaceIndex);
841 std::string Model_SelectionNaming::shortName(
842 std::shared_ptr<ModelAPI_ResultConstruction>& theConstr, const int theEdgeVertexPos)
844 std::string aName = theConstr->data()->name();
845 // remove "-", "/" and "&" command-symbols
846 aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
847 aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
848 aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
849 // remove the last 's', 'e', 'f' and 'r' symbols:
850 // they are used as markers of start/end/forward/rewersed indicators
851 static const std::string aSyms("sefr");
852 std::string::iterator aSuffix = aName.end() - 1;
853 while(aSyms.find(*aSuffix) != std::string::npos) {
856 aName.erase(aSuffix + 1, aName.end());
858 if (theEdgeVertexPos == 1) {
859 aName += "s"; // start
860 } else if (theEdgeVertexPos == 2) {
866 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
867 bool Model_SelectionNaming::selectSubShape(const std::string& theType,
868 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc,
869 std::shared_ptr<GeomAPI_Shape>& theShapeToBeSelected, std::shared_ptr<ModelAPI_Result>& theCont)
871 if(theSubShapeName.empty() || theType.empty()) return false;
872 TopAbs_ShapeEnum aType = translateType(theType);
874 // check that it was selected in another document
875 size_t aSlash = theSubShapeName.find("/");
876 std::string aSubShapeName = theSubShapeName;
877 std::shared_ptr<Model_Document> aDoc = theDoc;
878 if (aSlash != std::string::npos) {
879 std::string aDocName = theSubShapeName.substr(0, aSlash);
880 ResultPartPtr aFoundPart;
881 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
882 if (aDocName == aRootDoc->kind()) {
883 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
885 for (int a = aRootDoc->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
886 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
887 aRootDoc->object(ModelAPI_ResultPart::group(), a));
888 if (aPart.get() && aPart->isActivated() && aPart->data()->name() == aDocName) {
889 aDoc = std::dynamic_pointer_cast<Model_Document>(aPart->partDoc());
895 if (aDoc != theDoc) {
896 // so, the first word is the document name => reduce the string for the next manips
897 aSubShapeName = theSubShapeName.substr(aSlash + 1);
898 if (aSubShapeName.empty() && aFoundPart.get()) { // the whole Part result
899 theCont = aFoundPart;
905 std::string aContName = getContextName(aSubShapeName);
906 if(aContName.empty()) return false;
907 bool anUniqueContext = false;
908 ResultPtr aCont = aDoc->findByName(aContName, aSubShapeName, anUniqueContext);
909 // possible this is body where postfix is added to distinguish several shapes on the same label
910 int aSubShapeId = -1; // -1 means sub shape not found
911 // for result body the name wihtout "_" has higher priority than with it: it is always added
912 if ((!aCont.get()/* || (aCont->groupName() == ModelAPI_ResultBody::group())*/) &&
913 aContName == aSubShapeName) {
914 size_t aPostIndex = aContName.rfind('_');
915 if (aPostIndex != std::string::npos) {
916 std::string anEmpty, aSubContName = aContName.substr(0, aPostIndex);
917 ResultPtr aSubCont = aDoc->findByName(aSubContName, anEmpty, anUniqueContext);
918 if (aSubCont.get()) {
920 std::string aNum = aContName.substr(aPostIndex + 1);
921 aSubShapeId = std::stoi(aNum);
922 } catch (std::invalid_argument&) {
925 if (aSubShapeId > 0) {
926 aContName = aSubContName;
934 static const ResultPtr anEmpty;
935 TopoDS_Shape aSelection;
941 aSelection = findFaceByName(aSubShapeName, aDoc, aCont, anUniqueContext);
946 const TDF_Label& aLabel =
947 aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
948 if(!aLabel.IsNull()) {
949 Handle(TNaming_NamedShape) aNS;
950 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
951 aSelection = getShapeFromNS(aSubShapeName, aNS);
958 const TDF_Label& aLabel =
959 aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
960 if(!aLabel.IsNull()) {
961 Handle(TNaming_NamedShape) aNS;
962 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
963 aSelection = getShapeFromNS(aSubShapeName, aNS);
968 case TopAbs_COMPOUND:
969 case TopAbs_COMPSOLID:
972 default: {//TopAbs_SHAPE
973 /// case when the whole sketch is selected, so,
974 /// selection type is compound, but there is no value
975 if (aCont.get() && aCont->shape().get()) {
976 if (aCont->shape()->impl<TopoDS_Shape>().ShapeType() == aType) {
979 } else if (aSubShapeId > 0) { // try to find sub-shape by the index
980 TopExp_Explorer anExp(aCont->shape()->impl<TopoDS_Shape>(), aType);
981 for(; aSubShapeId > 1 && anExp.More(); aSubShapeId--) {
985 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
986 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
987 theShapeToBeSelected = aShapeToBeSelected;
996 if (!aSelection.IsNull() &&
997 aSelection.ShapeType() != aType && aSelection.ShapeType() != TopAbs_COMPOUND)
998 aSelection.Nullify(); // to avoid selection of face instead of edge that is described by face
999 // another try to find edge or vertex by faces
1000 std::list<std::string> aListofNames;
1001 size_t aN = aSelection.IsNull() ? ParseName(aSubShapeName, aListofNames) : 0;
1002 if ((aSelection.IsNull() && (aType == TopAbs_EDGE || aType == TopAbs_VERTEX)) ||
1003 (!aSelection.IsNull() && aSelection.ShapeType() != aType)) { // edge by one face as example
1005 TopTools_ListOfShape aList;
1006 std::list<std::string>::iterator it = aListofNames.begin();
1007 for(; it != aListofNames.end(); it++) {
1008 ResultPtr aFaceContext = aCont;
1009 if (it != aListofNames.begin()) { // there may be other context for different sub-faces
1010 std::string aContName = getContextName(*it);
1011 if(!aContName.empty()) {
1012 aFaceContext = aDoc->findByName(aContName, *it, anUniqueContext);
1015 TopoDS_Shape aFace = findFaceByName(*it, aDoc, aFaceContext, anUniqueContext);
1016 if (aFace.IsNull() && aFaceContext.get() &&
1017 aFaceContext->groupName() == ModelAPI_ResultConstruction::group() ) {
1018 // search the construction sub-elements for the intersection if they are in the tree
1019 size_t aSlash = it->find("/");
1020 if (aSlash != std::string::npos) {
1021 std::string aSubShapeName = it->substr(aSlash + 1);
1022 aFace = findFaceByName(aSubShapeName, aDoc, aFaceContext, true);
1026 aList.Append(aFace);
1028 aSelection = findCommonShape(aType, aList);
1029 //if (aSelection.IsNull() && aType == TopAbs_EDGE) { // try to find selection by neighbours
1030 // aSelection = findCommonShapeByNB(aType, TopAbs_VERTEX, aList);
1034 // in case of construction, there is no registered names for all sub-elements,
1035 // even for the main element; so, trying to find them by name (without "&" intersections)
1036 if (aSelection.IsNull() && aN < 2) {
1037 size_t aConstrNamePos = aSubShapeName.find("/");
1038 bool isFullName = aConstrNamePos == std::string::npos;
1039 std::string anEmpty, aContrName = aContName;
1040 ResultPtr aConstr = aDoc->findByName(aContrName, anEmpty, anUniqueContext);
1041 if (aConstr.get() && aConstr->groupName() == ModelAPI_ResultConstruction::group()) {
1044 // For the full construction selection shape must be empty.
1045 //theShapeToBeSelected = aConstr->shape();
1048 // for sketch sub-elements selected
1049 CompositeFeaturePtr aComposite =
1050 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aConstr));
1051 if (aComposite.get()) {
1052 if (aType == TopAbs_VERTEX || aType == TopAbs_EDGE) {
1053 // collect all IDs in the name
1054 bool isVertexByEdge = false;
1055 std::map<int, int> anIDs;
1056 if (!parseSubIndices(aComposite, aSubShapeName,
1057 aType == TopAbs_EDGE ? "Edge" : "Vertex", anIDs)) {
1058 // there is a case when vertex is identified by one circle-edge (2253)
1059 if (aType == TopAbs_VERTEX &&
1060 parseSubIndices(aComposite, aSubShapeName, "Edge", anIDs))
1061 isVertexByEdge = true;
1066 const int aSubNum = aComposite->numberOfSubs();
1067 for(int a = 0; a < aSubNum; a++) {
1068 int aCompID = aComposite->subFeatureId(a);
1069 if (anIDs.find(aCompID) != anIDs.end()) { // found the vertex/edge shape
1070 FeaturePtr aSub = aComposite->subFeature(a);
1071 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1072 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIt = aResults.cbegin();
1073 // there may be many shapes (circle and center)
1074 for(; aRIt != aResults.cend(); aRIt++) {
1075 ResultConstructionPtr aRes =
1076 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRIt);
1078 int anOrientation = abs(anIDs[aCompID]);
1079 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
1080 if (anOrientation == 1) {
1081 if (!isVertexByEdge && aType == aShape.ShapeType()) {
1082 theShapeToBeSelected = aRes->shape();
1084 } else if (isVertexByEdge && aType != aShape.ShapeType()) {
1085 // check that there is only one vertex produces by and circular edge
1086 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
1087 TopExp_Explorer anExp(aShape, TopAbs_VERTEX);
1089 aShape = anExp.Current();
1091 if (!anExp.More() || anExp.Current().IsSame(aShape)) {
1092 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
1093 aShapeToBeSelected->setImpl(new TopoDS_Shape(aShape));
1094 theShapeToBeSelected = aShapeToBeSelected;
1098 } else { // take first or second vertex of the edge
1099 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
1100 if (aShape.ShapeType() == TopAbs_VERTEX) continue;
1101 TopExp_Explorer anExp(aShape, aType);
1102 for(; anExp.More() && anOrientation != 2; anOrientation--)
1105 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
1106 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
1107 theShapeToBeSelected = aShapeToBeSelected;
1115 // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
1116 } else if (aType == TopAbs_FACE || aType == TopAbs_WIRE) {
1117 std::map<int, int> anIDs;
1118 if (!parseSubIndices(aComposite, aSubShapeName,
1119 aType == TopAbs_FACE ? "Face" : "Wire", anIDs, true))
1122 // curves and orientations of edges
1123 NCollection_DataMap<Handle(Geom_Curve), int, Model_CurvesHasher> allCurves;
1124 const int aSubNum = aComposite->numberOfSubs();
1125 for(int a = 0; a < aSubNum; a++) {
1126 int aSubID = aComposite->subFeatureId(a);
1127 if (anIDs.find(aSubID) != anIDs.end()) {
1128 FeaturePtr aSub = aComposite->subFeature(a);
1129 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1130 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
1131 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
1132 ResultConstructionPtr aConstr =
1133 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
1134 if (aConstr->shape() && aConstr->shape()->isEdge()) {
1135 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
1136 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
1137 if (!anEdge.IsNull()) {
1138 Standard_Real aFirst, aLast;
1139 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
1140 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
1146 std::shared_ptr<GeomAPI_Shape> aFoundFW =
1147 findAppropriateFace(aConstr, allCurves, aType == TopAbs_WIRE);
1148 if (aFoundFW.get()) {
1149 theShapeToBeSelected = aFoundFW;
1152 } else if (aType == TopAbs_WIRE) {
1153 // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
1154 std::map<int, int> anIDs;
1155 if (!parseSubIndices(aComposite, aSubShapeName, "Wire", anIDs))
1158 // curves and orientations of edges
1159 NCollection_DataMap<Handle(Geom_Curve), int, Model_CurvesHasher> allCurves;
1160 const int aSubNum = aComposite->numberOfSubs();
1161 for(int a = 0; a < aSubNum; a++) {
1162 int aSubID = aComposite->subFeatureId(a);
1163 if (anIDs.find(aSubID) != anIDs.end()) {
1164 FeaturePtr aSub = aComposite->subFeature(a);
1165 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
1166 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
1167 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
1168 ResultConstructionPtr aConstr =
1169 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
1170 if (aConstr->shape() && aConstr->shape()->isEdge()) {
1171 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
1172 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
1173 if (!anEdge.IsNull()) {
1174 Standard_Real aFirst, aLast;
1175 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
1176 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
1182 std::shared_ptr<GeomAPI_Shape> aFoundFW =
1183 findAppropriateFace(aConstr, allCurves, aType == TopAbs_WIRE);
1184 if (aFoundFW.get()) {
1185 theShapeToBeSelected = aFoundFW;
1191 } else if (aSelection.IsNull() && aN >= 2 && aType == TopAbs_VERTEX) {
1192 // support of shape name as intersection separated by "&"
1193 static std::string anEdgeType = "edge"; // for now it works only with su-edges
1194 std::list<std::string>::iterator aSubNames = aListofNames.begin();
1195 TopTools_ListOfShape aSubsList;
1196 for(; aSubNames != aListofNames.end(); aSubNames++) {
1197 std::string aSubName = *aSubNames;
1198 std::shared_ptr<GeomAPI_Shape> aSubShapeFound;
1199 std::shared_ptr<ModelAPI_Result> aContextFound;
1200 if (selectSubShape(anEdgeType, aSubName, theDoc, aSubShapeFound, aContextFound)) {
1201 if (aSubShapeFound.get())
1202 aSubsList.Append(aSubShapeFound->impl<TopoDS_Shape>());
1205 aSelection = findCommonShape(TopAbs_VERTEX, aSubsList);
1207 if (!aSelection.IsNull()) {
1208 // Select it (must be after N=0 checking,
1209 // since for simple constructions the shape must be null)
1210 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
1211 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
1212 theShapeToBeSelected = aShapeToBeSelected;