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 "Model_Objects.h"
10 #include "Model_Data.h"
11 #include <ModelAPI_Feature.h>
12 #include <Events_InfoMessage.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_ResultPart.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_CompositeFeature.h>
17 #include <ModelAPI_ResultBody.h>
18 #include <GeomAPI_Wire.h>
20 #include <TopoDS_Iterator.hxx>
22 #include <TopoDS_Compound.hxx>
24 #include <TopExp_Explorer.hxx>
25 #include <TopTools_ListOfShape.hxx>
26 #include <TopTools_MapOfShape.hxx>
27 #include <TopTools_IndexedMapOfShape.hxx>
28 #include <TopTools_ListIteratorOfListOfShape.hxx>
29 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
30 #include <TopTools_MapIteratorOfMapOfShape.hxx>
31 #include <BRep_Builder.hxx>
32 #include <TNaming_Iterator.hxx>
33 #include <TNaming_Tool.hxx>
34 #include <TNaming_NamedShape.hxx>
35 #include <TNaming_Localizer.hxx>
36 #include <TDataStd_Name.hxx>
37 #include <TColStd_MapOfTransient.hxx>
42 #include <BRepTools.hxx>
45 Model_SelectionNaming::Model_SelectionNaming(TDF_Label theSelectionLab)
47 myLab = theSelectionLab;
50 std::string Model_SelectionNaming::getShapeName(
51 std::shared_ptr<Model_Document> theDoc, const TopoDS_Shape& theShape,
52 ResultPtr& theContext, const bool theAnotherDoc, const bool theWholeContext)
55 // add the result name to the name of the shape
56 // (it was in BodyBuilder, but did not work on Result rename)
57 bool isNeedContextName = theContext->shape().get() != NULL;
58 // && !theContext->shape()->isEqual(theSubSh);
59 // check if the subShape is already in DF
60 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(theShape, myLab);
61 Handle(TDataStd_Name) anAttr;
62 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
63 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
64 std::shared_ptr<Model_Data> aData =
65 std::dynamic_pointer_cast<Model_Data>(theContext->data());
66 if (isNeedContextName && aData && aData->label().IsEqual(aNS->Label())) {
67 // do nothing because this context name will be added later in this method
69 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
70 // indexes are added to sub-shapes not primitives
71 // (primitives must not be located at the same label)
72 if(!aName.empty() && aNS->Evolution() != TNaming_PRIMITIVE && isNeedContextName) {
73 const TDF_Label& aLabel = aNS->Label();//theDoc->findNamingName(aName);
74 static const std::string aPostFix("_");
75 TNaming_Iterator anItL(aNS);
76 for(int i = 1; anItL.More(); anItL.Next(), i++) {
77 // in #1766 IsEqual produced no index of the face
78 if(anItL.NewShape().IsSame(theShape)) {
80 aName += TCollection_AsciiString (i).ToCString();
85 // if a shape is under another context, use this name, not theContext
86 std::shared_ptr<Model_Data> aContextData =
87 std::dynamic_pointer_cast<Model_Data>(theContext->data());
88 // for constructions the naming is in arguments and has no evolution, so, apply this only
90 if (isNeedContextName && theContext->groupName() == ModelAPI_ResultBody::group() &&
91 !aNS->Label().IsDescendant(aContextData->label())) {
92 isNeedContextName = false;
93 TDF_Label aNSDataLab = aNS->Label();
94 while(aNSDataLab.Depth() != 7 && aNSDataLab.Depth() > 5)
95 aNSDataLab = aNSDataLab.Father();
96 ObjectPtr aNewContext = theDoc->objects()->object(aNSDataLab);
97 if (!aNewContext.get() && aNSDataLab.Depth() == 7) {
98 aNSDataLab = aNSDataLab.Father().Father();
99 aNewContext = theDoc->objects()->object(aNSDataLab);
101 if (aNewContext.get()) {
102 aName = aNewContext->data()->name() + "/" + aName;
109 // Name is empty and this is full context, it just add the whole context name that must be added
110 bool isEmptyName = aName.empty();
111 if (isNeedContextName && (!isEmptyName || theWholeContext)) {
112 aName = theContext->data()->name() + (isEmptyName ? "" : ("/" + aName));
114 aName = theContext->document()->kind() + "/" + aName; // PartSet
119 bool isTrivial (const TopTools_ListOfShape& theAncestors, TopTools_IndexedMapOfShape& theSMap)
121 // a trivial case: F1 & F2, aNumber = 1, i.e. intersection gives 1 edge.
122 TopoDS_Compound aCmp;
124 BB.MakeCompound(aCmp);
125 TopTools_ListIteratorOfListOfShape it(theAncestors);
126 for(;it.More();it.Next()) {
127 BB.Add(aCmp, it.Value());
128 theSMap.Add(it.Value());
131 TopTools_IndexedDataMapOfShapeListOfShape aMap2;
132 TopExp::MapShapesAndAncestors(aCmp, TopAbs_EDGE, TopAbs_FACE, aMap2);
133 for (int i = 1; i <= aMap2.Extent(); i++) {
134 const TopoDS_Shape& aKey = aMap2.FindKey(i);
135 const TopTools_ListOfShape& anAncestors = aMap2.FindFromIndex(i);
136 if(anAncestors.Extent() > 1) aNumber++;
138 if(aNumber > 1) return false;
142 std::string Model_SelectionNaming::namingName(ResultPtr& theContext,
143 std::shared_ptr<GeomAPI_Shape> theSubSh, const std::string& theDefaultName,
144 const bool theAnotherDoc)
146 std::string aName("Undefined name");
147 if(!theContext.get() || theContext->shape()->isNull())
148 return !theDefaultName.empty() ? theDefaultName : aName;
150 // if it is in result of another part
151 std::shared_ptr<Model_Document> aDoc =
152 std::dynamic_pointer_cast<Model_Document>(theContext->document());
153 if (theContext->groupName() == ModelAPI_ResultPart::group()) {
154 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
157 return aPart->data()->name() + "/" + aPart->nameInPart(theSubSh, anIndex);
159 return aPart->data()->name();
162 if (!theSubSh.get() || theSubSh->isNull()) { // no subshape, so just the whole feature name
163 // but if it is in another Part, add this part name
164 std::string aPartName;
166 aPartName = theContext->document()->kind() + "/"; // PartSet
167 return aPartName + theContext->data()->name();
169 TopoDS_Shape aSubShape = theSubSh->impl<TopoDS_Shape>();
170 TopoDS_Shape aContext = theContext->shape()->impl<TopoDS_Shape>();
172 if(aSubShape.ShapeType() == TopAbs_COMPOUND) {
173 BRepTools::Write(aSubShape, "Selection.brep");
174 BRepTools::Write(aContext, "Context.brep");
177 aName = getShapeName(aDoc, aSubShape, theContext, theAnotherDoc,
178 theContext->shape()->isEqual(theSubSh));
180 if(aName.empty() ) { // not in the document!
181 TopAbs_ShapeEnum aType = aSubShape.ShapeType();
184 // the Face should be in DF. If it is not the case, it is an error ==> to be debugged
188 // name structure: F1 & F2 [& F3 & F4],
189 // where F1 & F2 the faces which gives the Edge in trivial case
190 // if it is not atrivial case we use localization by neighbours. F3 & F4 - neighbour faces
191 if (BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
192 aName = "Degenerated_Edge";
195 TopTools_IndexedDataMapOfShapeListOfShape aMap;
196 TopExp::MapShapesAndAncestors(aContext, TopAbs_EDGE, TopAbs_FACE, aMap);
197 TopTools_IndexedMapOfShape aSMap; // map for ancestors of the sub-shape
198 bool isTrivialCase(true);
199 if(aMap.Contains(aSubShape)) {
200 const TopTools_ListOfShape& anAncestors = aMap.FindFromKey(aSubShape);
201 // check that it is not a trivial case (F1 & F2: aNumber = 1)
202 isTrivialCase = isTrivial(anAncestors, aSMap);
205 TopTools_ListOfShape aListOfNbs;
206 if(!isTrivialCase) { // find Neighbors
207 TNaming_Localizer aLocalizer;
208 TopTools_MapOfShape aMap3;
209 aLocalizer.FindNeighbourg(aContext, aSubShape, aMap3);
210 //int n = aMap3.Extent();
211 TopTools_MapIteratorOfMapOfShape it(aMap3);
212 for(;it.More();it.Next()) {
213 const TopoDS_Shape& aNbShape = it.Key(); // neighbor edge
214 //TopAbs_ShapeEnum aType = aNbShape.ShapeType();
215 const TopTools_ListOfShape& aList = aMap.FindFromKey(aNbShape);
216 TopTools_ListIteratorOfListOfShape it2(aList);
217 for(;it2.More();it2.Next()) {
218 if(aSMap.Contains(it2.Value())) continue; // skip this Face
219 aListOfNbs.Append(it2.Value());
222 } // else a trivial case
224 // build name of the sub-shape Edge
225 for(int i=1; i <= aSMap.Extent(); i++) {
226 const TopoDS_Shape& aFace = aSMap.FindKey(i);
227 std::string aFaceName = getShapeName(aDoc, aFace, theContext, theAnotherDoc, false);
231 aName += "&" + aFaceName;
233 TopTools_ListIteratorOfListOfShape itl(aListOfNbs);
234 for (;itl.More();itl.Next()) {
235 std::string aFaceName = getShapeName(aDoc, itl.Value(), theContext, theAnotherDoc, false);
236 aName += "&" + aFaceName;
242 // name structure (Monifold Topology):
243 // 1) F1 | F2 | F3 - intersection of 3 faces defines a vertex - trivial case.
244 // 2) F1 | F2 | F3 [|F4 [|Fn]] - redundant definition,
245 // but it should be kept as is to obtain safe recomputation
246 // 2) F1 | F2 - intersection of 2 faces definses a vertex - applicable for case
247 // when 1 faces is cylindrical, conical, spherical or revolution and etc.
248 // 3) E1 | E2 | E3 - intersection of 3 edges defines a vertex - when we have case of a shell
249 // or compound of 2 open faces.
250 // 4) E1 | E2 - intesection of 2 edges defines a vertex - when we have a case of
251 // two independent edges (wire or compound)
252 // implemented 2 first cases
254 TopTools_IndexedDataMapOfShapeListOfShape aMap;
255 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_FACE, aMap);
256 TopTools_ListOfShape aList;
257 TopTools_MapOfShape aFMap;
258 // simetimes when group is moved in history, naming may be badly updated, so
259 // avoid crash in FindFromKey (issue 1842)
260 if (aMap.Contains(aSubShape)) {
261 const TopTools_ListOfShape& aList2 = aMap.FindFromKey(aSubShape);
263 TopTools_ListIteratorOfListOfShape itl2(aList2);
264 for (int i = 1;itl2.More();itl2.Next(),i++) {
265 if(aFMap.Add(itl2.Value()))
266 aList.Append(itl2.Value());
270 int n = aList.Extent();
271 bool isByFaces = n >= 3;
272 if(!isByFaces) { // open topology case or Compound case => via edges
273 TopTools_IndexedDataMapOfShapeListOfShape aMap;
274 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_EDGE, aMap);
275 const TopTools_ListOfShape& aList22 = aMap.FindFromKey(aSubShape);
276 if(aList22.Extent() >= 2) { // regular solution
278 // bug! duplication; fix is below
280 TopTools_ListOfShape aListE;
281 TopTools_ListIteratorOfListOfShape itl2(aList22);
282 for (int i = 1;itl2.More();itl2.Next(),i++) {
283 if(aFMap.Add(itl2.Value()))
284 aListE.Append(itl2.Value());
287 TopTools_ListIteratorOfListOfShape itl(aListE);
288 for (int i = 1;itl.More();itl.Next(),i++) {
289 const TopoDS_Shape& anEdge = itl.Value();
290 std::string anEdgeName = getShapeName(aDoc, anEdge, theContext, theAnotherDoc, false);
291 if (anEdgeName.empty()) { // edge is not in DS, trying by faces anyway
299 aName += "&" + anEdgeName;
302 else { // dangle vertex: if(aList22.Extent() == 1)
303 //it should be already in DF
307 TopTools_ListIteratorOfListOfShape itl(aList);
308 for (int i = 1;itl.More();itl.Next(),i++) {
309 const TopoDS_Shape& aFace = itl.Value();
310 std::string aFaceName = getShapeName(aDoc, aFace, theContext, theAnotherDoc, false);
314 aName += "&" + aFaceName;
325 TopAbs_ShapeEnum translateType (const std::string& theType)
327 // map from the textual shape types to OCCT enumeration
328 static std::map<std::string, TopAbs_ShapeEnum> aShapeTypes;
330 if(aShapeTypes.size() == 0) {
331 aShapeTypes["compound"] = TopAbs_COMPOUND;
332 aShapeTypes["compounds"] = TopAbs_COMPOUND;
333 aShapeTypes["compsolid"] = TopAbs_COMPSOLID;
334 aShapeTypes["compsolids"] = TopAbs_COMPSOLID;
335 aShapeTypes["solid"] = TopAbs_SOLID;
336 aShapeTypes["solids"] = TopAbs_SOLID;
337 aShapeTypes["shell"] = TopAbs_SHELL;
338 aShapeTypes["shells"] = TopAbs_SHELL;
339 aShapeTypes["face"] = TopAbs_FACE;
340 aShapeTypes["faces"] = TopAbs_FACE;
341 aShapeTypes["wire"] = TopAbs_WIRE;
342 aShapeTypes["wires"] = TopAbs_WIRE;
343 aShapeTypes["edge"] = TopAbs_EDGE;
344 aShapeTypes["edges"] = TopAbs_EDGE;
345 aShapeTypes["vertex"] = TopAbs_VERTEX;
346 aShapeTypes["vertices"] = TopAbs_VERTEX;
347 aShapeTypes["COMPOUND"] = TopAbs_COMPOUND;
348 aShapeTypes["COMPOUNDS"] = TopAbs_COMPOUND;
349 aShapeTypes["COMPSOLID"] = TopAbs_COMPSOLID;
350 aShapeTypes["COMPSOLIDS"] = TopAbs_COMPSOLID;
351 aShapeTypes["SOLID"] = TopAbs_SOLID;
352 aShapeTypes["SOLIDS"] = TopAbs_SOLID;
353 aShapeTypes["SHELL"] = TopAbs_SHELL;
354 aShapeTypes["SHELLS"] = TopAbs_SHELL;
355 aShapeTypes["FACE"] = TopAbs_FACE;
356 aShapeTypes["FACES"] = TopAbs_FACE;
357 aShapeTypes["WIRE"] = TopAbs_WIRE;
358 aShapeTypes["WIRES"] = TopAbs_WIRE;
359 aShapeTypes["EDGE"] = TopAbs_EDGE;
360 aShapeTypes["EDGES"] = TopAbs_EDGE;
361 aShapeTypes["VERTEX"] = TopAbs_VERTEX;
362 aShapeTypes["VERTICES"] = TopAbs_VERTEX;
364 if (aShapeTypes.find(theType) != aShapeTypes.end())
365 return aShapeTypes[theType];
366 Events_InfoMessage("Model_SelectionNaming",
367 "Shape type defined in XML is not implemented!").send();
371 const TopoDS_Shape getShapeFromNS(
372 const std::string& theSubShapeName, Handle(TNaming_NamedShape) theNS)
374 TopoDS_Shape aSelection;
375 std::string::size_type n = theSubShapeName.rfind('/');
376 if (n == std::string::npos) n = -1;
377 std::string aSubString = theSubShapeName.substr(n + 1);
378 n = aSubString.rfind('_');
380 if (n != std::string::npos) {// for primitives this is a first
381 // if we have here the same name as theSubShapeName, there is no index in compound, it is whole
382 Handle(TDataStd_Name) aName;
383 if (!theNS->Label().FindAttribute(TDataStd_Name::GetID(), aName) ||
384 aName->Get() != aSubString.c_str()) {
385 aSubString = aSubString.substr(n+1);
386 indx = atoi(aSubString.c_str());
390 TNaming_Iterator anItL(theNS);
391 for(int i = 1; anItL.More(); anItL.Next(), i++) {
393 return anItL.NewShape();
399 const TopoDS_Shape findFaceByName(
400 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc)
403 //std::string::size_type n, nb = theSubShapeName.rfind('/');
404 //if (nb == std::string::npos) nb = 0;
405 //std::string aSubString = theSubShapeName.substr(nb + 1);
406 std::string aSubString = theSubShapeName;
408 TDF_Label aLabel = theDoc->findNamingName(aSubString);
409 if (aLabel.IsNull()) { // try to remove additional artificial suffix
410 std::string::size_type n = aSubString.rfind('_');
411 if (n != std::string::npos) {
412 aSubString = aSubString.substr(0, n);
413 aLabel = theDoc->findNamingName(aSubString);
416 if(aLabel.IsNull()) return aFace;
417 Handle(TNaming_NamedShape) aNS;
418 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
419 aFace = getShapeFromNS(theSubShapeName, aNS);
424 size_t ParseName(const std::string& theSubShapeName, std::list<std::string>& theList)
426 std::string aName = theSubShapeName;
427 std::string aLastName;
428 size_t n1(0), n2(0); // n1 - start position, n2 - position of the delimiter
429 while ((n2 = aName.find('&', n1)) != std::string::npos) {
430 const std::string aName1 = aName.substr(n1, n2 - n1); //name of face
431 theList.push_back(aName1);
433 aLastName = aName.substr(n1);
435 if(!aLastName.empty())
436 theList.push_back(aLastName);
437 return theList.size();
440 const TopoDS_Shape findCommonShape(
441 const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
443 if(theList.Extent() < 2) {
444 return TopoDS_Shape();
447 // Store in maps sub-shapes from each face.
448 std::vector<TopTools_MapOfShape> aVec;
449 for(TopTools_ListIteratorOfListOfShape anIt(theList); anIt.More(); anIt.Next()) {
450 const TopoDS_Shape aFace = anIt.Value();
451 TopTools_MapOfShape aMap;
452 for(TopExp_Explorer anExp(aFace, theType); anExp.More(); anExp.Next()) {
453 const TopoDS_Shape& aSubShape = anExp.Current();
454 aMap.Add(anExp.Current());
456 aVec.push_back(aMap);
459 // Find sub-shape shared between all faces.
460 TopoDS_Shape aSharedShape;
461 for(TopTools_MapIteratorOfMapOfShape anIt(aVec[0]); anIt.More(); anIt.Next()) {
462 const TopoDS_Shape& aSubShape = anIt.Value();
464 for(int anIndex = 1; anIndex < aVec.size(); ++anIndex) {
465 if(aVec[anIndex].Contains(aSubShape)) {
469 if(aSharedNb == theList.Extent()) {
470 if(aSharedShape.IsNull()) {
471 aSharedShape = aSubShape;
473 // More than one shape shared between all faces, return null shape in this case.
474 return TopoDS_Shape();
482 std::string getContextName(const std::string& theSubShapeName)
485 std::string::size_type n = theSubShapeName.find('/');
486 if (n == std::string::npos) return theSubShapeName;
487 aName = theSubShapeName.substr(0, n);
491 /// Parses naming name of sketch sub-elements: takes indices and orientation
492 /// (if theOriented = true) from this name. Map theIDs constains indices ->
493 /// orientations and start/end vertices: negative is reversed, 2 - start, 3 - end
494 bool parseSubIndices(CompositeFeaturePtr theComp, //< to iterate names
495 const std::string& theName, const char* theShapeType,
496 std::map<int, int>& theIDs, const bool theOriented = false)
498 // collect all IDs in the name
499 std::map<std::string, int> aNames; // short name of sub -> ID of sub of theComp
500 const int aSubNum = theComp->numberOfSubs();
501 for(int a = 0; a < aSubNum; a++) {
502 FeaturePtr aSub = theComp->subFeature(a);
503 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
504 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
505 // there may be many shapes (circle and center)
506 for(; aRes != aResults.cend(); aRes++) {
507 ResultConstructionPtr aConstr =
508 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
510 aNames[Model_SelectionNaming::shortName(aConstr)] = theComp->subFeatureId(a);
515 size_t aPrevPos = theName.find("/") + 1, aLastNamePos;
516 bool isShape = false; // anyway the first world must be 'Vertex'
518 aLastNamePos = theName.find('-', aPrevPos);
519 std::string anID = theName.substr(aPrevPos, aLastNamePos - aPrevPos);
521 if (anID != theShapeType)
525 int anOrientation = 1; // default
526 if (theOriented) { // here must be a symbol in the end of digit 'f' or 'r'
527 std::string::iterator aSymbol = anID.end() - 1;
528 if (*aSymbol == 'r') anOrientation = -1;
529 anID.erase(aSymbol); // remove last symbol
531 // check start/end symbols
532 std::string::iterator aBack = anID.end() - 1;
535 anID.erase(aBack); // remove last symbol
536 } else if (*aBack == 'e') {
538 anID.erase(aBack); // remove last symbol
541 if (aNames.find(anID) != aNames.end()) {
542 theIDs[aNames[anID]] = anOrientation;
545 aPrevPos = aLastNamePos + 1;
546 } while (aLastNamePos != std::string::npos);
550 /// produces theEdge orientation relatively to theContext face
551 int Model_SelectionNaming::edgeOrientation(const TopoDS_Shape& theContext, TopoDS_Edge& theEdge)
553 if (theContext.ShapeType() != TopAbs_FACE && theContext.ShapeType() != TopAbs_WIRE)
555 if (theEdge.Orientation() == TopAbs_FORWARD)
557 if (theEdge.Orientation() == TopAbs_REVERSED)
562 std::shared_ptr<GeomAPI_Shape> Model_SelectionNaming::findAppropriateFace(
563 std::shared_ptr<ModelAPI_Result>& theConstr,
564 NCollection_DataMap<Handle(Geom_Curve), int>& theCurves, const bool theIsWire)
566 int aBestFound = 0; // best number of found edges (not percentage: issue 1019)
567 int aBestOrient = 0; // for the equal "BestFound" additional parameter is orientation
568 std::shared_ptr<GeomAPI_Shape> aResult;
569 ResultConstructionPtr aConstructionContext =
570 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theConstr);
571 if (!aConstructionContext.get())
573 for(int aFaceIndex = 0; aFaceIndex < aConstructionContext->facesNum(); aFaceIndex++) {
574 int aFound = 0, aNotFound = 0, aSameOrientation = 0;
576 TopoDS::Face(aConstructionContext->face(aFaceIndex)->impl<TopoDS_Shape>());
577 std::list<TopoDS_Shape> aFacesWires; // faces or wires to iterate
579 for(TopExp_Explorer aWires(aFace, TopAbs_WIRE); aWires.More(); aWires.Next()) {
580 aFacesWires.push_back(aWires.Current());
583 aFacesWires.push_back(aFace);
585 std::list<TopoDS_Shape>::iterator aFW = aFacesWires.begin();
586 for(; aFW != aFacesWires.end(); aFW++) {
587 TopExp_Explorer anEdgesExp(*aFW, TopAbs_EDGE);
588 TColStd_MapOfTransient alreadyProcessed; // to avoid counting edges with same curved (841)
589 for(; anEdgesExp.More(); anEdgesExp.Next()) {
590 TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
591 if (!anEdge.IsNull()) {
592 Standard_Real aFirst, aLast;
593 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
594 if (alreadyProcessed.Contains(aCurve))
596 alreadyProcessed.Add(aCurve);
597 if (theCurves.IsBound(aCurve)) {
599 int anOrient = theCurves.Find(aCurve);
600 if (anOrient != 0) { // extra comparision score is orientation
601 if (edgeOrientation(aFace, anEdge) == anOrient)
609 if (aFound + aNotFound != 0) {
610 if (aFound > aBestFound ||
611 (aFound == aBestFound && aSameOrientation > aBestOrient)) {
613 aBestOrient = aSameOrientation;
615 std::shared_ptr<GeomAPI_Wire> aWire(new GeomAPI_Wire);
616 aWire->setImpl(new TopoDS_Shape(*aFW));
619 aResult = aConstructionContext->face(aFaceIndex);
628 std::string Model_SelectionNaming::shortName(
629 std::shared_ptr<ModelAPI_ResultConstruction>& theConstr, const int theEdgeVertexPos)
631 std::string aName = theConstr->data()->name();
632 // remove "-", "/" and "&" command-symbols
633 aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
634 aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
635 aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
636 // remove the last 's', 'e', 'f' and 'r' symbols:
637 // they are used as markers of start/end/forward/rewersed indicators
638 static const std::string aSyms("sefr");
639 std::string::iterator aSuffix = aName.end() - 1;
640 while(aSyms.find(*aSuffix) != std::string::npos) {
643 aName.erase(aSuffix + 1, aName.end());
645 if (theEdgeVertexPos == 1) {
646 aName += "s"; // start
647 } else if (theEdgeVertexPos == 2) {
653 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
654 bool Model_SelectionNaming::selectSubShape(const std::string& theType,
655 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc,
656 std::shared_ptr<GeomAPI_Shape>& theShapeToBeSelected, std::shared_ptr<ModelAPI_Result>& theCont)
658 if(theSubShapeName.empty() || theType.empty()) return false;
659 TopAbs_ShapeEnum aType = translateType(theType);
661 // check that it was selected in another document
662 size_t aSlash = theSubShapeName.find("/");
663 std::string aSubShapeName = theSubShapeName;
664 std::shared_ptr<Model_Document> aDoc = theDoc;
665 if (aSlash != std::string::npos) {
666 std::string aDocName = theSubShapeName.substr(0, aSlash);
667 ResultPartPtr aFoundPart;
668 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
669 if (aDocName == aRootDoc->kind()) {
670 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
672 for (int a = aRootDoc->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
673 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
674 aRootDoc->object(ModelAPI_ResultPart::group(), a));
675 if (aPart.get() && aPart->isActivated() && aPart->data()->name() == aDocName) {
676 aDoc = std::dynamic_pointer_cast<Model_Document>(aPart->partDoc());
682 if (aDoc != theDoc) {
683 // so, the first word is the document name => reduce the string for the next manips
684 aSubShapeName = theSubShapeName.substr(aSlash + 1);
685 if (aSubShapeName.empty() && aFoundPart.get()) { // the whole Part result
686 theCont = aFoundPart;
692 std::string aContName = getContextName(aSubShapeName);
693 if(aContName.empty()) return false;
694 ResultPtr aCont = aDoc->findByName(aContName);
695 // possible this is body where postfix is added to distinguish several shapes on the same label
696 int aSubShapeId = -1; // -1 means sub shape not found
697 // for result body the name wihtout "_" has higher priority than with it: it is always added
698 if ((!aCont.get()/* || (aCont->groupName() == ModelAPI_ResultBody::group())*/) &&
699 aContName == aSubShapeName) {
700 size_t aPostIndex = aContName.rfind('_');
701 if (aPostIndex != std::string::npos) {
702 std::string aSubContName = aContName.substr(0, aPostIndex);
703 ResultPtr aSubCont = aDoc->findByName(aSubContName);
704 if (aSubCont.get()) {
706 std::string aNum = aContName.substr(aPostIndex + 1);
707 aSubShapeId = std::stoi(aNum);
708 } catch (std::invalid_argument&) {
711 if (aSubShapeId > 0) {
712 aContName = aSubContName;
720 TopoDS_Shape aSelection;
726 aSelection = findFaceByName(aSubShapeName, aDoc);
731 const TDF_Label& aLabel = aDoc->findNamingName(aSubShapeName);
732 if(!aLabel.IsNull()) {
733 Handle(TNaming_NamedShape) aNS;
734 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
735 aSelection = getShapeFromNS(aSubShapeName, aNS);
742 const TDF_Label& aLabel = aDoc->findNamingName(aSubShapeName);
743 if(!aLabel.IsNull()) {
744 Handle(TNaming_NamedShape) aNS;
745 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
746 aSelection = getShapeFromNS(aSubShapeName, aNS);
751 case TopAbs_COMPOUND:
752 case TopAbs_COMPSOLID:
755 default: {//TopAbs_SHAPE
756 /// case when the whole sketch is selected, so,
757 /// selection type is compound, but there is no value
758 if (aCont.get() && aCont->shape().get()) {
759 if (aCont->shape()->impl<TopoDS_Shape>().ShapeType() == aType) {
762 } else if (aSubShapeId > 0) { // try to find sub-shape by the index
763 TopExp_Explorer anExp(aCont->shape()->impl<TopoDS_Shape>(), aType);
764 for(; aSubShapeId > 1 && anExp.More(); aSubShapeId--) {
768 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
769 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
770 theShapeToBeSelected = aShapeToBeSelected;
779 // another try to find edge or vertex by faces
780 std::list<std::string> aListofNames;
781 size_t aN = aSelection.IsNull() ? ParseName(aSubShapeName, aListofNames) : 0;
782 if (aSelection.IsNull() && (aType == TopAbs_EDGE || aType == TopAbs_VERTEX)) {
784 TopTools_ListOfShape aList;
785 std::list<std::string>::iterator it = aListofNames.begin();
786 for(; it != aListofNames.end(); it++){
787 const TopoDS_Shape aFace = findFaceByName(*it, aDoc);
791 aSelection = findCommonShape(aType, aList);
794 // in case of construction, there is no registered names for all sub-elements,
795 // even for the main element; so, trying to find them by name (without "&" intersections)
797 size_t aConstrNamePos = aSubShapeName.find("/");
798 bool isFullName = aConstrNamePos == std::string::npos;
799 std::string aContrName = aContName;
800 ResultPtr aConstr = aDoc->findByName(aContrName);
801 if (aConstr.get() && aConstr->groupName() == ModelAPI_ResultConstruction::group()) {
804 // For the full construction selection shape must be empty.
805 //theShapeToBeSelected = aConstr->shape();
808 // for sketch sub-elements selected
809 CompositeFeaturePtr aComposite =
810 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aConstr));
811 if (aComposite.get()) {
812 if (aType == TopAbs_VERTEX || aType == TopAbs_EDGE) {
813 // collect all IDs in the name
814 std::map<int, int> anIDs;
815 if (!parseSubIndices(aComposite, aSubShapeName,
816 aType == TopAbs_EDGE ? "Edge" : "Vertex", anIDs))
819 const int aSubNum = aComposite->numberOfSubs();
820 for(int a = 0; a < aSubNum; a++) {
821 int aCompID = aComposite->subFeatureId(a);
822 if (anIDs.find(aCompID) != anIDs.end()) { // found the vertex/edge shape
823 FeaturePtr aSub = aComposite->subFeature(a);
824 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
825 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIt = aResults.cbegin();
826 // there may be many shapes (circle and center)
827 for(; aRIt != aResults.cend(); aRIt++) {
828 ResultConstructionPtr aRes =
829 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRIt);
831 int anOrientation = abs(anIDs[aCompID]);
832 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
833 if (anOrientation == 1) {
834 if (aType == aShape.ShapeType()) {
835 theShapeToBeSelected = aRes->shape();
838 } else { // take first or second vertex of the edge
839 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
840 if (aShape.ShapeType() == TopAbs_VERTEX) continue;
841 TopExp_Explorer anExp(aShape, aType);
842 for(; anExp.More() && anOrientation != 2; anOrientation--)
845 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
846 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
847 theShapeToBeSelected = aShapeToBeSelected;
855 // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
856 } else if (aType == TopAbs_FACE || aType == TopAbs_WIRE) {
857 std::map<int, int> anIDs;
858 if (!parseSubIndices(aComposite, aSubShapeName,
859 aType == TopAbs_FACE ? "Face" : "Wire", anIDs, true))
862 // curves and orientations of edges
863 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
864 const int aSubNum = aComposite->numberOfSubs();
865 for(int a = 0; a < aSubNum; a++) {
866 int aSubID = aComposite->subFeatureId(a);
867 if (anIDs.find(aSubID) != anIDs.end()) {
868 FeaturePtr aSub = aComposite->subFeature(a);
869 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
870 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
871 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
872 ResultConstructionPtr aConstr =
873 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
874 if (aConstr->shape() && aConstr->shape()->isEdge()) {
875 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
876 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
877 if (!anEdge.IsNull()) {
878 Standard_Real aFirst, aLast;
879 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
880 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
886 std::shared_ptr<GeomAPI_Shape> aFoundFW =
887 findAppropriateFace(aConstr, allCurves, aType == TopAbs_WIRE);
888 if (aFoundFW.get()) {
889 theShapeToBeSelected = aFoundFW;
892 } else if (aType == TopAbs_WIRE) {
893 // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
894 std::map<int, int> anIDs;
895 if (!parseSubIndices(aComposite, aSubShapeName, "Wire", anIDs))
898 // curves and orientations of edges
899 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
900 const int aSubNum = aComposite->numberOfSubs();
901 for(int a = 0; a < aSubNum; a++) {
902 int aSubID = aComposite->subFeatureId(a);
903 if (anIDs.find(aSubID) != anIDs.end()) {
904 FeaturePtr aSub = aComposite->subFeature(a);
905 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
906 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
907 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
908 ResultConstructionPtr aConstr =
909 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
910 if (aConstr->shape() && aConstr->shape()->isEdge()) {
911 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
912 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
913 if (!anEdge.IsNull()) {
914 Standard_Real aFirst, aLast;
915 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
916 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
922 std::shared_ptr<GeomAPI_Shape> aFoundFW =
923 findAppropriateFace(aConstr, allCurves, aType == TopAbs_WIRE);
924 if (aFoundFW.get()) {
925 theShapeToBeSelected = aFoundFW;
932 if (!aSelection.IsNull()) {
933 // Select it (must be after N=0 checking,
934 // since for simple constructions the shape must be null)
935 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
936 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
937 theShapeToBeSelected = aShapeToBeSelected;