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 // check if the subShape is already in DF
59 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(theShape, myLab);
60 Handle(TDataStd_Name) anAttr;
61 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
62 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
63 std::shared_ptr<Model_Data> aData =
64 std::dynamic_pointer_cast<Model_Data>(theContext->data());
65 if (isNeedContextName && aData && aData->label().IsEqual(aNS->Label())) {
66 // do nothing because this context name will be added later in this method
68 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
69 // indexes are added to sub-shapes not primitives
70 // (primitives must not be located at the same label)
71 if(!aName.empty() && aNS->Evolution() != TNaming_PRIMITIVE && isNeedContextName) {
72 const TDF_Label& aLabel = aNS->Label();//theDoc->findNamingName(aName);
73 static const std::string aPostFix("_");
74 TNaming_Iterator anItL(aNS);
75 for(int i = 1; anItL.More(); anItL.Next(), i++) {
76 // in #1766 IsEqual produced no index of the face
77 if(anItL.NewShape().IsSame(theShape)) {
79 aName += TCollection_AsciiString (i).ToCString();
84 // if a shape is under another context, use this name, not theContext
85 std::shared_ptr<Model_Data> aContextData =
86 std::dynamic_pointer_cast<Model_Data>(theContext->data());
87 // for constructions the naming is in arguments and has no evolution, so, apply this only
89 if (isNeedContextName && theContext->groupName() == ModelAPI_ResultBody::group() &&
90 !aNS->Label().IsDescendant(aContextData->label())) {
91 isNeedContextName = false;
92 TDF_Label aNSDataLab = aNS->Label();
93 while(aNSDataLab.Depth() != 7 && aNSDataLab.Depth() > 5)
94 aNSDataLab = aNSDataLab.Father();
95 ObjectPtr aNewContext = theDoc->objects()->object(aNSDataLab);
96 if (!aNewContext.get() && aNSDataLab.Depth() == 7) {
97 aNSDataLab = aNSDataLab.Father().Father();
98 aNewContext = theDoc->objects()->object(aNSDataLab);
100 if (aNewContext.get()) {
101 aName = aNewContext->data()->name() + "/" + aName;
108 // Name is empty and this is full context, it just add the whole context name that must be added
109 bool isEmptyName = aName.empty();
110 if (isNeedContextName && (!isEmptyName || theWholeContext)) {
111 aName = theContext->data()->name() + (isEmptyName ? "" : ("/" + aName));
113 aName = theContext->document()->kind() + "/" + aName; // PartSet
118 bool isTrivial (const TopTools_ListOfShape& theAncestors, TopTools_IndexedMapOfShape& theSMap)
120 // a trivial case: F1 & F2, aNumber = 1, i.e. intersection gives 1 edge.
121 TopoDS_Compound aCmp;
123 BB.MakeCompound(aCmp);
124 TopTools_ListIteratorOfListOfShape it(theAncestors);
125 for(;it.More();it.Next()) {
126 BB.Add(aCmp, it.Value());
127 theSMap.Add(it.Value());
130 TopTools_IndexedDataMapOfShapeListOfShape aMap2;
131 TopExp::MapShapesAndAncestors(aCmp, TopAbs_EDGE, TopAbs_FACE, aMap2);
132 for (int i = 1; i <= aMap2.Extent(); i++) {
133 const TopoDS_Shape& aKey = aMap2.FindKey(i);
134 const TopTools_ListOfShape& anAncestors = aMap2.FindFromIndex(i);
135 if(anAncestors.Extent() > 1) aNumber++;
137 if(aNumber > 1) return false;
141 std::string Model_SelectionNaming::namingName(ResultPtr& theContext,
142 std::shared_ptr<GeomAPI_Shape> theSubSh, const std::string& theDefaultName,
143 const bool theAnotherDoc)
145 std::string aName("Undefined name");
147 || !theContext->shape().get()
148 || theContext->shape()->isNull()) {
149 return !theDefaultName.empty() ? theDefaultName : aName;
152 // if it is in result of another part
153 std::shared_ptr<Model_Document> aDoc =
154 std::dynamic_pointer_cast<Model_Document>(theContext->document());
155 if (theContext->groupName() == ModelAPI_ResultPart::group()) {
156 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theContext);
159 return aPart->data()->name() + "/" + aPart->nameInPart(theSubSh, anIndex);
161 return aPart->data()->name();
164 if (!theSubSh.get() || theSubSh->isNull()) { // no subshape, so just the whole feature name
165 // but if it is in another Part, add this part name
166 std::string aPartName;
168 aPartName = theContext->document()->kind() + "/"; // PartSet
169 return aPartName + theContext->data()->name();
171 TopoDS_Shape aSubShape = theSubSh->impl<TopoDS_Shape>();
172 TopoDS_Shape aContext = theContext->shape()->impl<TopoDS_Shape>();
174 if(aSubShape.ShapeType() == TopAbs_COMPOUND) {
175 BRepTools::Write(aSubShape, "Selection.brep");
176 BRepTools::Write(aContext, "Context.brep");
179 aName = getShapeName(aDoc, aSubShape, theContext, theAnotherDoc,
180 theContext->shape()->isEqual(theSubSh));
182 if(aName.empty() ) { // not in the document!
183 TopAbs_ShapeEnum aType = aSubShape.ShapeType();
186 // the Face should be in DF. If it is not the case, it is an error ==> to be debugged
190 // name structure: F1 & F2 [& F3 & F4],
191 // where F1 & F2 the faces which gives the Edge in trivial case
192 // if it is not atrivial case we use localization by neighbours. F3 & F4 - neighbour faces
193 if (BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
194 aName = "Degenerated_Edge";
197 TopTools_IndexedDataMapOfShapeListOfShape aMap;
198 TopExp::MapShapesAndAncestors(aContext, TopAbs_EDGE, TopAbs_FACE, aMap);
199 TopTools_IndexedMapOfShape aSMap; // map for ancestors of the sub-shape
200 bool isTrivialCase(true);
201 if(aMap.Contains(aSubShape)) {
202 const TopTools_ListOfShape& anAncestors = aMap.FindFromKey(aSubShape);
203 // check that it is not a trivial case (F1 & F2: aNumber = 1)
204 isTrivialCase = isTrivial(anAncestors, aSMap);
207 TopTools_ListOfShape aListOfNbs;
208 if(!isTrivialCase) { // find Neighbors
209 TNaming_Localizer aLocalizer;
210 TopTools_MapOfShape aMap3;
211 aLocalizer.FindNeighbourg(aContext, aSubShape, aMap3);
212 //int n = aMap3.Extent();
213 TopTools_MapIteratorOfMapOfShape it(aMap3);
214 for(;it.More();it.Next()) {
215 const TopoDS_Shape& aNbShape = it.Key(); // neighbor edge
216 //TopAbs_ShapeEnum aType = aNbShape.ShapeType();
217 const TopTools_ListOfShape& aList = aMap.FindFromKey(aNbShape);
218 TopTools_ListIteratorOfListOfShape it2(aList);
219 for(;it2.More();it2.Next()) {
220 if(aSMap.Contains(it2.Value())) continue; // skip this Face
221 aListOfNbs.Append(it2.Value());
224 } // else a trivial case
226 // build name of the sub-shape Edge
227 for(int i=1; i <= aSMap.Extent(); i++) {
228 const TopoDS_Shape& aFace = aSMap.FindKey(i);
229 std::string aFaceName = getShapeName(aDoc, aFace, theContext, theAnotherDoc, false);
233 aName += "&" + aFaceName;
235 TopTools_ListIteratorOfListOfShape itl(aListOfNbs);
236 for (;itl.More();itl.Next()) {
237 std::string aFaceName = getShapeName(aDoc, itl.Value(), theContext, theAnotherDoc, false);
238 aName += "&" + aFaceName;
244 // name structure (Monifold Topology):
245 // 1) F1 | F2 | F3 - intersection of 3 faces defines a vertex - trivial case.
246 // 2) F1 | F2 | F3 [|F4 [|Fn]] - redundant definition,
247 // but it should be kept as is to obtain safe recomputation
248 // 2) F1 | F2 - intersection of 2 faces definses a vertex - applicable for case
249 // when 1 faces is cylindrical, conical, spherical or revolution and etc.
250 // 3) E1 | E2 | E3 - intersection of 3 edges defines a vertex - when we have case of a shell
251 // or compound of 2 open faces.
252 // 4) E1 | E2 - intesection of 2 edges defines a vertex - when we have a case of
253 // two independent edges (wire or compound)
254 // implemented 2 first cases
256 TopTools_IndexedDataMapOfShapeListOfShape aMap;
257 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_FACE, aMap);
258 TopTools_ListOfShape aList;
259 TopTools_MapOfShape aFMap;
260 // simetimes when group is moved in history, naming may be badly updated, so
261 // avoid crash in FindFromKey (issue 1842)
262 if (aMap.Contains(aSubShape)) {
263 const TopTools_ListOfShape& aList2 = aMap.FindFromKey(aSubShape);
265 TopTools_ListIteratorOfListOfShape itl2(aList2);
266 for (int i = 1;itl2.More();itl2.Next(),i++) {
267 if(aFMap.Add(itl2.Value()))
268 aList.Append(itl2.Value());
272 int n = aList.Extent();
273 bool isByFaces = n >= 3;
274 if(!isByFaces) { // open topology case or Compound case => via edges
275 TopTools_IndexedDataMapOfShapeListOfShape aMap;
276 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_EDGE, aMap);
277 const TopTools_ListOfShape& aList22 = aMap.FindFromKey(aSubShape);
278 if(aList22.Extent() >= 2) { // regular solution
280 // bug! duplication; fix is below
282 TopTools_ListOfShape aListE;
283 TopTools_ListIteratorOfListOfShape itl2(aList22);
284 for (int i = 1;itl2.More();itl2.Next(),i++) {
285 if(aFMap.Add(itl2.Value()))
286 aListE.Append(itl2.Value());
289 TopTools_ListIteratorOfListOfShape itl(aListE);
290 for (int i = 1;itl.More();itl.Next(),i++) {
291 const TopoDS_Shape& anEdge = itl.Value();
292 std::string anEdgeName = getShapeName(aDoc, anEdge, theContext, theAnotherDoc, false);
293 if (anEdgeName.empty()) { // edge is not in DS, trying by faces anyway
301 aName += "&" + anEdgeName;
304 else { // dangle vertex: if(aList22.Extent() == 1)
305 //it should be already in DF
309 TopTools_ListIteratorOfListOfShape itl(aList);
310 for (int i = 1;itl.More();itl.Next(),i++) {
311 const TopoDS_Shape& aFace = itl.Value();
312 std::string aFaceName = getShapeName(aDoc, aFace, theContext, theAnotherDoc, false);
316 aName += "&" + aFaceName;
327 TopAbs_ShapeEnum translateType (const std::string& theType)
329 // map from the textual shape types to OCCT enumeration
330 static std::map<std::string, TopAbs_ShapeEnum> aShapeTypes;
332 if(aShapeTypes.size() == 0) {
333 aShapeTypes["compound"] = TopAbs_COMPOUND;
334 aShapeTypes["compounds"] = TopAbs_COMPOUND;
335 aShapeTypes["compsolid"] = TopAbs_COMPSOLID;
336 aShapeTypes["compsolids"] = TopAbs_COMPSOLID;
337 aShapeTypes["solid"] = TopAbs_SOLID;
338 aShapeTypes["solids"] = TopAbs_SOLID;
339 aShapeTypes["shell"] = TopAbs_SHELL;
340 aShapeTypes["shells"] = TopAbs_SHELL;
341 aShapeTypes["face"] = TopAbs_FACE;
342 aShapeTypes["faces"] = TopAbs_FACE;
343 aShapeTypes["wire"] = TopAbs_WIRE;
344 aShapeTypes["wires"] = TopAbs_WIRE;
345 aShapeTypes["edge"] = TopAbs_EDGE;
346 aShapeTypes["edges"] = TopAbs_EDGE;
347 aShapeTypes["vertex"] = TopAbs_VERTEX;
348 aShapeTypes["vertices"] = TopAbs_VERTEX;
349 aShapeTypes["COMPOUND"] = TopAbs_COMPOUND;
350 aShapeTypes["COMPOUNDS"] = TopAbs_COMPOUND;
351 aShapeTypes["COMPSOLID"] = TopAbs_COMPSOLID;
352 aShapeTypes["COMPSOLIDS"] = TopAbs_COMPSOLID;
353 aShapeTypes["SOLID"] = TopAbs_SOLID;
354 aShapeTypes["SOLIDS"] = TopAbs_SOLID;
355 aShapeTypes["SHELL"] = TopAbs_SHELL;
356 aShapeTypes["SHELLS"] = TopAbs_SHELL;
357 aShapeTypes["FACE"] = TopAbs_FACE;
358 aShapeTypes["FACES"] = TopAbs_FACE;
359 aShapeTypes["WIRE"] = TopAbs_WIRE;
360 aShapeTypes["WIRES"] = TopAbs_WIRE;
361 aShapeTypes["EDGE"] = TopAbs_EDGE;
362 aShapeTypes["EDGES"] = TopAbs_EDGE;
363 aShapeTypes["VERTEX"] = TopAbs_VERTEX;
364 aShapeTypes["VERTICES"] = TopAbs_VERTEX;
366 if (aShapeTypes.find(theType) != aShapeTypes.end())
367 return aShapeTypes[theType];
368 Events_InfoMessage("Model_SelectionNaming",
369 "Shape type defined in XML is not implemented!").send();
373 const TopoDS_Shape getShapeFromNS(
374 const std::string& theSubShapeName, Handle(TNaming_NamedShape) theNS)
376 TopoDS_Shape aSelection;
377 std::string::size_type n = theSubShapeName.rfind('/');
378 if (n == std::string::npos) n = -1;
379 std::string aSubString = theSubShapeName.substr(n + 1);
380 n = aSubString.rfind('_');
382 if (n != std::string::npos) {// for primitives this is a first
383 // if we have here the same name as theSubShapeName, there is no index in compound, it is whole
384 Handle(TDataStd_Name) aName;
385 if (!theNS->Label().FindAttribute(TDataStd_Name::GetID(), aName) ||
386 aName->Get() != aSubString.c_str()) {
387 aSubString = aSubString.substr(n+1);
388 indx = atoi(aSubString.c_str());
392 TNaming_Iterator anItL(theNS);
393 for(int i = 1; anItL.More(); anItL.Next(), i++) {
395 return anItL.NewShape();
401 const TopoDS_Shape findFaceByName(
402 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc)
405 //std::string::size_type n, nb = theSubShapeName.rfind('/');
406 //if (nb == std::string::npos) nb = 0;
407 //std::string aSubString = theSubShapeName.substr(nb + 1);
408 std::string aSubString = theSubShapeName;
410 TDF_Label aLabel = theDoc->findNamingName(aSubString);
411 if (aLabel.IsNull()) { // try to remove additional artificial suffix
412 std::string::size_type n = aSubString.rfind('_');
413 if (n != std::string::npos) {
414 aSubString = aSubString.substr(0, n);
415 aLabel = theDoc->findNamingName(aSubString);
418 if(aLabel.IsNull()) return aFace;
419 Handle(TNaming_NamedShape) aNS;
420 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
421 aFace = getShapeFromNS(theSubShapeName, aNS);
426 size_t ParseName(const std::string& theSubShapeName, std::list<std::string>& theList)
428 std::string aName = theSubShapeName;
429 std::string aLastName;
430 size_t n1(0), n2(0); // n1 - start position, n2 - position of the delimiter
431 while ((n2 = aName.find('&', n1)) != std::string::npos) {
432 const std::string aName1 = aName.substr(n1, n2 - n1); //name of face
433 theList.push_back(aName1);
435 aLastName = aName.substr(n1);
437 if(!aLastName.empty())
438 theList.push_back(aLastName);
439 return theList.size();
442 const TopoDS_Shape findCommonShape(
443 const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
445 if(theList.Extent() < 2) {
446 return TopoDS_Shape();
449 // Store in maps sub-shapes from each face.
450 std::vector<TopTools_MapOfShape> aVec;
451 for(TopTools_ListIteratorOfListOfShape anIt(theList); anIt.More(); anIt.Next()) {
452 const TopoDS_Shape aFace = anIt.Value();
453 TopTools_MapOfShape aMap;
454 for(TopExp_Explorer anExp(aFace, theType); anExp.More(); anExp.Next()) {
455 const TopoDS_Shape& aSubShape = anExp.Current();
456 aMap.Add(anExp.Current());
458 aVec.push_back(aMap);
461 // Find sub-shape shared between all faces.
462 TopoDS_Shape aSharedShape;
463 for(TopTools_MapIteratorOfMapOfShape anIt(aVec[0]); anIt.More(); anIt.Next()) {
464 const TopoDS_Shape& aSubShape = anIt.Value();
466 for(int anIndex = 1; anIndex < aVec.size(); ++anIndex) {
467 if(aVec[anIndex].Contains(aSubShape)) {
471 if(aSharedNb == theList.Extent()) {
472 if(aSharedShape.IsNull()) {
473 aSharedShape = aSubShape;
475 // More than one shape shared between all faces, return null shape in this case.
476 return TopoDS_Shape();
484 std::string getContextName(const std::string& theSubShapeName)
487 std::string::size_type n = theSubShapeName.find('/');
488 if (n == std::string::npos) return theSubShapeName;
489 aName = theSubShapeName.substr(0, n);
493 /// Parses naming name of sketch sub-elements: takes indices and orientation
494 /// (if theOriented = true) from this name. Map theIDs constains indices ->
495 /// orientations and start/end vertices: negative is reversed, 2 - start, 3 - end
496 bool parseSubIndices(CompositeFeaturePtr theComp, //< to iterate names
497 const std::string& theName, const char* theShapeType,
498 std::map<int, int>& theIDs, const bool theOriented = false)
500 // collect all IDs in the name
501 std::map<std::string, int> aNames; // short name of sub -> ID of sub of theComp
502 const int aSubNum = theComp->numberOfSubs();
503 for(int a = 0; a < aSubNum; a++) {
504 FeaturePtr aSub = theComp->subFeature(a);
505 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
506 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
507 // there may be many shapes (circle and center)
508 for(; aRes != aResults.cend(); aRes++) {
509 ResultConstructionPtr aConstr =
510 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
512 aNames[Model_SelectionNaming::shortName(aConstr)] = theComp->subFeatureId(a);
517 size_t aPrevPos = theName.find("/") + 1, aLastNamePos;
518 bool isShape = false; // anyway the first world must be 'Vertex'
520 aLastNamePos = theName.find('-', aPrevPos);
521 std::string anID = theName.substr(aPrevPos, aLastNamePos - aPrevPos);
523 if (anID != theShapeType)
527 int anOrientation = 1; // default
528 if (theOriented) { // here must be a symbol in the end of digit 'f' or 'r'
529 std::string::iterator aSymbol = anID.end() - 1;
530 if (*aSymbol == 'r') anOrientation = -1;
531 anID.erase(aSymbol); // remove last symbol
533 // check start/end symbols
534 std::string::iterator aBack = anID.end() - 1;
537 anID.erase(aBack); // remove last symbol
538 } else if (*aBack == 'e') {
540 anID.erase(aBack); // remove last symbol
543 if (aNames.find(anID) != aNames.end()) {
544 theIDs[aNames[anID]] = anOrientation;
547 aPrevPos = aLastNamePos + 1;
548 } while (aLastNamePos != std::string::npos);
552 /// produces theEdge orientation relatively to theContext face
553 int Model_SelectionNaming::edgeOrientation(const TopoDS_Shape& theContext, TopoDS_Edge& theEdge)
555 if (theContext.ShapeType() != TopAbs_FACE && theContext.ShapeType() != TopAbs_WIRE)
557 if (theEdge.Orientation() == TopAbs_FORWARD)
559 if (theEdge.Orientation() == TopAbs_REVERSED)
564 std::shared_ptr<GeomAPI_Shape> Model_SelectionNaming::findAppropriateFace(
565 std::shared_ptr<ModelAPI_Result>& theConstr,
566 NCollection_DataMap<Handle(Geom_Curve), int>& theCurves, const bool theIsWire)
568 int aBestFound = 0; // best number of found edges (not percentage: issue 1019)
569 int aBestOrient = 0; // for the equal "BestFound" additional parameter is orientation
570 std::shared_ptr<GeomAPI_Shape> aResult;
571 ResultConstructionPtr aConstructionContext =
572 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theConstr);
573 if (!aConstructionContext.get())
575 for(int aFaceIndex = 0; aFaceIndex < aConstructionContext->facesNum(); aFaceIndex++) {
576 int aFound = 0, aNotFound = 0, aSameOrientation = 0;
578 TopoDS::Face(aConstructionContext->face(aFaceIndex)->impl<TopoDS_Shape>());
579 std::list<TopoDS_Shape> aFacesWires; // faces or wires to iterate
581 for(TopExp_Explorer aWires(aFace, TopAbs_WIRE); aWires.More(); aWires.Next()) {
582 aFacesWires.push_back(aWires.Current());
585 aFacesWires.push_back(aFace);
587 std::list<TopoDS_Shape>::iterator aFW = aFacesWires.begin();
588 for(; aFW != aFacesWires.end(); aFW++) {
589 TopExp_Explorer anEdgesExp(*aFW, TopAbs_EDGE);
590 TColStd_MapOfTransient alreadyProcessed; // to avoid counting edges with same curved (841)
591 for(; anEdgesExp.More(); anEdgesExp.Next()) {
592 TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
593 if (!anEdge.IsNull()) {
594 Standard_Real aFirst, aLast;
595 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
596 if (alreadyProcessed.Contains(aCurve))
598 alreadyProcessed.Add(aCurve);
599 if (theCurves.IsBound(aCurve)) {
601 int anOrient = theCurves.Find(aCurve);
602 if (anOrient != 0) { // extra comparision score is orientation
603 if (edgeOrientation(aFace, anEdge) == anOrient)
611 if (aFound + aNotFound != 0) {
612 if (aFound > aBestFound ||
613 (aFound == aBestFound && aSameOrientation > aBestOrient)) {
615 aBestOrient = aSameOrientation;
617 std::shared_ptr<GeomAPI_Wire> aWire(new GeomAPI_Wire);
618 aWire->setImpl(new TopoDS_Shape(*aFW));
621 aResult = aConstructionContext->face(aFaceIndex);
630 std::string Model_SelectionNaming::shortName(
631 std::shared_ptr<ModelAPI_ResultConstruction>& theConstr, const int theEdgeVertexPos)
633 std::string aName = theConstr->data()->name();
634 // remove "-", "/" and "&" command-symbols
635 aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
636 aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
637 aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
638 // remove the last 's', 'e', 'f' and 'r' symbols:
639 // they are used as markers of start/end/forward/rewersed indicators
640 static const std::string aSyms("sefr");
641 std::string::iterator aSuffix = aName.end() - 1;
642 while(aSyms.find(*aSuffix) != std::string::npos) {
645 aName.erase(aSuffix + 1, aName.end());
647 if (theEdgeVertexPos == 1) {
648 aName += "s"; // start
649 } else if (theEdgeVertexPos == 2) {
655 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
656 bool Model_SelectionNaming::selectSubShape(const std::string& theType,
657 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc,
658 std::shared_ptr<GeomAPI_Shape>& theShapeToBeSelected, std::shared_ptr<ModelAPI_Result>& theCont)
660 if(theSubShapeName.empty() || theType.empty()) return false;
661 TopAbs_ShapeEnum aType = translateType(theType);
663 // check that it was selected in another document
664 size_t aSlash = theSubShapeName.find("/");
665 std::string aSubShapeName = theSubShapeName;
666 std::shared_ptr<Model_Document> aDoc = theDoc;
667 if (aSlash != std::string::npos) {
668 std::string aDocName = theSubShapeName.substr(0, aSlash);
669 ResultPartPtr aFoundPart;
670 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
671 if (aDocName == aRootDoc->kind()) {
672 aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
674 for (int a = aRootDoc->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
675 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
676 aRootDoc->object(ModelAPI_ResultPart::group(), a));
677 if (aPart.get() && aPart->isActivated() && aPart->data()->name() == aDocName) {
678 aDoc = std::dynamic_pointer_cast<Model_Document>(aPart->partDoc());
684 if (aDoc != theDoc) {
685 // so, the first word is the document name => reduce the string for the next manips
686 aSubShapeName = theSubShapeName.substr(aSlash + 1);
687 if (aSubShapeName.empty() && aFoundPart.get()) { // the whole Part result
688 theCont = aFoundPart;
694 std::string aContName = getContextName(aSubShapeName);
695 if(aContName.empty()) return false;
696 ResultPtr aCont = aDoc->findByName(aContName);
697 // possible this is body where postfix is added to distinguish several shapes on the same label
698 int aSubShapeId = -1; // -1 means sub shape not found
699 // for result body the name wihtout "_" has higher priority than with it: it is always added
700 if ((!aCont.get()/* || (aCont->groupName() == ModelAPI_ResultBody::group())*/) &&
701 aContName == aSubShapeName) {
702 size_t aPostIndex = aContName.rfind('_');
703 if (aPostIndex != std::string::npos) {
704 std::string aSubContName = aContName.substr(0, aPostIndex);
705 ResultPtr aSubCont = aDoc->findByName(aSubContName);
706 if (aSubCont.get()) {
708 std::string aNum = aContName.substr(aPostIndex + 1);
709 aSubShapeId = std::stoi(aNum);
710 } catch (std::invalid_argument&) {
713 if (aSubShapeId > 0) {
714 aContName = aSubContName;
722 TopoDS_Shape aSelection;
728 aSelection = findFaceByName(aSubShapeName, aDoc);
733 const TDF_Label& aLabel = aDoc->findNamingName(aSubShapeName);
734 if(!aLabel.IsNull()) {
735 Handle(TNaming_NamedShape) aNS;
736 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
737 aSelection = getShapeFromNS(aSubShapeName, aNS);
744 const TDF_Label& aLabel = aDoc->findNamingName(aSubShapeName);
745 if(!aLabel.IsNull()) {
746 Handle(TNaming_NamedShape) aNS;
747 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
748 aSelection = getShapeFromNS(aSubShapeName, aNS);
753 case TopAbs_COMPOUND:
754 case TopAbs_COMPSOLID:
757 default: {//TopAbs_SHAPE
758 /// case when the whole sketch is selected, so,
759 /// selection type is compound, but there is no value
760 if (aCont.get() && aCont->shape().get()) {
761 if (aCont->shape()->impl<TopoDS_Shape>().ShapeType() == aType) {
764 } else if (aSubShapeId > 0) { // try to find sub-shape by the index
765 TopExp_Explorer anExp(aCont->shape()->impl<TopoDS_Shape>(), aType);
766 for(; aSubShapeId > 1 && anExp.More(); aSubShapeId--) {
770 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
771 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
772 theShapeToBeSelected = aShapeToBeSelected;
781 // another try to find edge or vertex by faces
782 std::list<std::string> aListofNames;
783 size_t aN = aSelection.IsNull() ? ParseName(aSubShapeName, aListofNames) : 0;
784 if (aSelection.IsNull() && (aType == TopAbs_EDGE || aType == TopAbs_VERTEX)) {
786 TopTools_ListOfShape aList;
787 std::list<std::string>::iterator it = aListofNames.begin();
788 for(; it != aListofNames.end(); it++){
789 const TopoDS_Shape aFace = findFaceByName(*it, aDoc);
793 aSelection = findCommonShape(aType, aList);
796 // in case of construction, there is no registered names for all sub-elements,
797 // even for the main element; so, trying to find them by name (without "&" intersections)
799 size_t aConstrNamePos = aSubShapeName.find("/");
800 bool isFullName = aConstrNamePos == std::string::npos;
801 std::string aContrName = aContName;
802 ResultPtr aConstr = aDoc->findByName(aContrName);
803 if (aConstr.get() && aConstr->groupName() == ModelAPI_ResultConstruction::group()) {
806 // For the full construction selection shape must be empty.
807 //theShapeToBeSelected = aConstr->shape();
810 // for sketch sub-elements selected
811 CompositeFeaturePtr aComposite =
812 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aConstr));
813 if (aComposite.get()) {
814 if (aType == TopAbs_VERTEX || aType == TopAbs_EDGE) {
815 // collect all IDs in the name
816 std::map<int, int> anIDs;
817 if (!parseSubIndices(aComposite, aSubShapeName,
818 aType == TopAbs_EDGE ? "Edge" : "Vertex", anIDs))
821 const int aSubNum = aComposite->numberOfSubs();
822 for(int a = 0; a < aSubNum; a++) {
823 int aCompID = aComposite->subFeatureId(a);
824 if (anIDs.find(aCompID) != anIDs.end()) { // found the vertex/edge shape
825 FeaturePtr aSub = aComposite->subFeature(a);
826 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
827 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIt = aResults.cbegin();
828 // there may be many shapes (circle and center)
829 for(; aRIt != aResults.cend(); aRIt++) {
830 ResultConstructionPtr aRes =
831 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRIt);
833 int anOrientation = abs(anIDs[aCompID]);
834 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
835 if (anOrientation == 1) {
836 if (aType == aShape.ShapeType()) {
837 theShapeToBeSelected = aRes->shape();
840 } else { // take first or second vertex of the edge
841 TopoDS_Shape aShape = aRes->shape()->impl<TopoDS_Shape>();
842 if (aShape.ShapeType() == TopAbs_VERTEX) continue;
843 TopExp_Explorer anExp(aShape, aType);
844 for(; anExp.More() && anOrientation != 2; anOrientation--)
847 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
848 aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current()));
849 theShapeToBeSelected = aShapeToBeSelected;
857 // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
858 } else if (aType == TopAbs_FACE || aType == TopAbs_WIRE) {
859 std::map<int, int> anIDs;
860 if (!parseSubIndices(aComposite, aSubShapeName,
861 aType == TopAbs_FACE ? "Face" : "Wire", anIDs, true))
864 // curves and orientations of edges
865 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
866 const int aSubNum = aComposite->numberOfSubs();
867 for(int a = 0; a < aSubNum; a++) {
868 int aSubID = aComposite->subFeatureId(a);
869 if (anIDs.find(aSubID) != anIDs.end()) {
870 FeaturePtr aSub = aComposite->subFeature(a);
871 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
872 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
873 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
874 ResultConstructionPtr aConstr =
875 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
876 if (aConstr->shape() && aConstr->shape()->isEdge()) {
877 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
878 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
879 if (!anEdge.IsNull()) {
880 Standard_Real aFirst, aLast;
881 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
882 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
888 std::shared_ptr<GeomAPI_Shape> aFoundFW =
889 findAppropriateFace(aConstr, allCurves, aType == TopAbs_WIRE);
890 if (aFoundFW.get()) {
891 theShapeToBeSelected = aFoundFW;
894 } else if (aType == TopAbs_WIRE) {
895 // sketch faces is identified by format "Sketch_1/Face-2f-8f-11r"
896 std::map<int, int> anIDs;
897 if (!parseSubIndices(aComposite, aSubShapeName, "Wire", anIDs))
900 // curves and orientations of edges
901 NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
902 const int aSubNum = aComposite->numberOfSubs();
903 for(int a = 0; a < aSubNum; a++) {
904 int aSubID = aComposite->subFeatureId(a);
905 if (anIDs.find(aSubID) != anIDs.end()) {
906 FeaturePtr aSub = aComposite->subFeature(a);
907 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
908 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
909 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
910 ResultConstructionPtr aConstr =
911 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
912 if (aConstr->shape() && aConstr->shape()->isEdge()) {
913 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
914 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
915 if (!anEdge.IsNull()) {
916 Standard_Real aFirst, aLast;
917 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
918 allCurves.Bind(aCurve, anIDs[aSubID] > 0 ? 1 : -1);
924 std::shared_ptr<GeomAPI_Shape> aFoundFW =
925 findAppropriateFace(aConstr, allCurves, aType == TopAbs_WIRE);
926 if (aFoundFW.get()) {
927 theShapeToBeSelected = aFoundFW;
934 if (!aSelection.IsNull()) {
935 // Select it (must be after N=0 checking,
936 // since for simple constructions the shape must be null)
937 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
938 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
939 theShapeToBeSelected = aShapeToBeSelected;