1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_SelectionNaming.cpp
4 // Created: 11 Aug 2015
5 // Author: Mikhail PONIKAROV
7 #include "Model_SelectionNaming.h"
8 #include "Model_Document.h"
9 #include <ModelAPI_Feature.h>
10 #include <Events_InfoMessage.h>
12 #include <TopoDS_Iterator.hxx>
14 #include <TopoDS_Compound.hxx>
16 #include <TopExp_Explorer.hxx>
17 #include <TopTools_ListOfShape.hxx>
18 #include <TopTools_MapOfShape.hxx>
19 #include <TopTools_IndexedMapOfShape.hxx>
20 #include <TopTools_ListIteratorOfListOfShape.hxx>
21 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
22 #include <TopTools_MapIteratorOfMapOfShape.hxx>
23 #include <BRep_Builder.hxx>
24 #include <TNaming_Iterator.hxx>
25 #include <TNaming_Tool.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TNaming_Localizer.hxx>
28 #include <TDataStd_Name.hxx>
31 #include <BRepTools.hxx>
34 Model_SelectionNaming::Model_SelectionNaming(TDF_Label theSelectionLab)
36 myLab = theSelectionLab;
40 std::string Model_SelectionNaming::getShapeName(
41 std::shared_ptr<Model_Document> theDoc, const TopoDS_Shape& theShape)
44 // check if the subShape is already in DF
45 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(theShape, myLab);
46 Handle(TDataStd_Name) anAttr;
47 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
48 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
49 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
51 const TDF_Label& aLabel = theDoc->findNamingName(aName);
52 /* MPV: the same shape with the same name may be duplicated on different labels (selection of the same construction object)
53 if(!aLabel.IsEqual(aNS->Label())) {
54 //aName.erase(); //something is wrong, to be checked!!!
55 aName += "_SomethingWrong";
59 static const std::string aPostFix("_");
60 TNaming_Iterator anItL(aNS);
61 for(int i = 1; anItL.More(); anItL.Next(), i++) {
62 if(anItL.NewShape() == theShape) {
64 aName += TCollection_AsciiString (i).ToCString();
76 bool isTrivial (const TopTools_ListOfShape& theAncestors, TopTools_IndexedMapOfShape& theSMap)
78 // a trivial case: F1 & F2, aNumber = 1, i.e. intersection gives 1 edge.
81 BB.MakeCompound(aCmp);
82 TopTools_ListIteratorOfListOfShape it(theAncestors);
83 for(;it.More();it.Next()) {
84 BB.Add(aCmp, it.Value());
85 theSMap.Add(it.Value());
88 TopTools_IndexedDataMapOfShapeListOfShape aMap2;
89 TopExp::MapShapesAndAncestors(aCmp, TopAbs_EDGE, TopAbs_FACE, aMap2);
90 for (int i = 1; i <= aMap2.Extent(); i++) {
91 const TopoDS_Shape& aKey = aMap2.FindKey(i);
92 const TopTools_ListOfShape& anAncestors = aMap2.FindFromIndex(i);
93 if(anAncestors.Extent() > 1) aNumber++;
95 if(aNumber > 1) return false;
99 std::string Model_SelectionNaming::namingName(ResultPtr& theContext,
100 std::shared_ptr<GeomAPI_Shape> theSubSh, const std::string& theDefaultName)
102 std::string aName("Undefined name");
103 if(!theContext.get() || theContext->shape()->isNull())
104 return !theDefaultName.empty() ? theDefaultName : aName;
105 if (!theSubSh.get() || theSubSh->isNull()) { // no subshape, so just the whole feature name
106 return theContext->data()->name();
108 TopoDS_Shape aSubShape = theSubSh->impl<TopoDS_Shape>();
109 TopoDS_Shape aContext = theContext->shape()->impl<TopoDS_Shape>();
111 if(aSubShape.ShapeType() == TopAbs_COMPOUND) {
112 BRepTools::Write(aSubShape, "Selection.brep");
113 BRepTools::Write(aContext, "Context.brep");
116 std::shared_ptr<Model_Document> aDoc =
117 std::dynamic_pointer_cast<Model_Document>(theContext->document());
119 // check if the subShape is already in DF
120 aName = getShapeName(aDoc, aSubShape);
121 if(aName.empty() ) { // not in the document!
122 TopAbs_ShapeEnum aType = aSubShape.ShapeType();
125 // the Face should be in DF. If it is not the case, it is an error ==> to be debugged
129 // name structure: F1 | F2 [| F3 | F4], where F1 & F2 the faces which gives the Edge in trivial case
130 // if it is not atrivial case we use localization by neighbours. F3 & F4 - neighbour faces
131 if (BRep_Tool::Degenerated(TopoDS::Edge(aSubShape))) {
132 aName = "Degenerated_Edge";
135 TopTools_IndexedDataMapOfShapeListOfShape aMap;
136 TopExp::MapShapesAndAncestors(aContext, TopAbs_EDGE, TopAbs_FACE, aMap);
137 TopTools_IndexedMapOfShape aSMap; // map for ancestors of the sub-shape
138 bool isTrivialCase(true);
139 if(aMap.Contains(aSubShape)) {
140 const TopTools_ListOfShape& anAncestors = aMap.FindFromKey(aSubShape);
141 // check that it is not a trivial case (F1 & F2: aNumber = 1)
142 isTrivialCase = isTrivial(anAncestors, aSMap);
145 TopTools_ListOfShape aListOfNbs;
146 if(!isTrivialCase) { // find Neighbors
147 TNaming_Localizer aLocalizer;
148 TopTools_MapOfShape aMap3;
149 aLocalizer.FindNeighbourg(aContext, aSubShape, aMap3);
150 //int n = aMap3.Extent();
151 TopTools_MapIteratorOfMapOfShape it(aMap3);
152 for(;it.More();it.Next()) {
153 const TopoDS_Shape& aNbShape = it.Key(); // neighbor edge
154 //TopAbs_ShapeEnum aType = aNbShape.ShapeType();
155 const TopTools_ListOfShape& aList = aMap.FindFromKey(aNbShape);
156 TopTools_ListIteratorOfListOfShape it2(aList);
157 for(;it2.More();it2.Next()) {
158 if(aSMap.Contains(it2.Value())) continue; // skip this Face
159 aListOfNbs.Append(it2.Value());
162 } // else a trivial case
164 // build name of the sub-shape Edge
165 for(int i=1; i <= aSMap.Extent(); i++) {
166 const TopoDS_Shape& aFace = aSMap.FindKey(i);
167 std::string aFaceName = getShapeName(aDoc, aFace);
171 aName += "&" + aFaceName;
173 TopTools_ListIteratorOfListOfShape itl(aListOfNbs);
174 for (;itl.More();itl.Next()) {
175 std::string aFaceName = getShapeName(aDoc, itl.Value());
176 aName += "&" + aFaceName;
182 // name structure (Monifold Topology):
183 // 1) F1 | F2 | F3 - intersection of 3 faces defines a vertex - trivial case.
184 // 2) F1 | F2 | F3 [|F4 [|Fn]] - redundant definition, but it should be kept as is to obtain safe recomputation
185 // 2) F1 | F2 - intersection of 2 faces definses a vertex - applicable for case
186 // when 1 faces is cylindrical, conical, spherical or revolution and etc.
187 // 3) E1 | E2 | E3 - intersection of 3 edges defines a vertex - when we have case of a shell
188 // or compound of 2 open faces.
189 // 4) E1 | E2 - intesection of 2 edges defines a vertex - when we have a case of
190 // two independent edges (wire or compound)
191 // implemented 2 first cases
193 TopTools_IndexedDataMapOfShapeListOfShape aMap;
194 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_FACE, aMap);
195 const TopTools_ListOfShape& aList2 = aMap.FindFromKey(aSubShape);
196 TopTools_ListOfShape aList;
197 TopTools_MapOfShape aFMap;
199 TopTools_ListIteratorOfListOfShape itl2(aList2);
200 for (int i = 1;itl2.More();itl2.Next(),i++) {
201 if(aFMap.Add(itl2.Value()))
202 aList.Append(itl2.Value());
204 int n = aList.Extent();
205 bool isByFaces = n >= 3;
206 if(!isByFaces) { // open topology case or Compound case => via edges
207 TopTools_IndexedDataMapOfShapeListOfShape aMap;
208 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_EDGE, aMap);
209 const TopTools_ListOfShape& aList22 = aMap.FindFromKey(aSubShape);
210 if(aList22.Extent() >= 2) { // regular solution
212 // bug! duplication; fix is below
214 TopTools_ListOfShape aListE;
215 TopTools_ListIteratorOfListOfShape itl2(aList22);
216 for (int i = 1;itl2.More();itl2.Next(),i++) {
217 if(aFMap.Add(itl2.Value()))
218 aListE.Append(itl2.Value());
221 TopTools_ListIteratorOfListOfShape itl(aListE);
222 for (int i = 1;itl.More();itl.Next(),i++) {
223 const TopoDS_Shape& anEdge = itl.Value();
224 std::string anEdgeName = getShapeName(aDoc, anEdge);
225 if (anEdgeName.empty()) { // edge is not in DS, trying by faces anyway
233 aName += "&" + anEdgeName;
236 else { // dangle vertex: if(aList22.Extent() == 1)
237 //it should be already in DF
241 TopTools_ListIteratorOfListOfShape itl(aList);
242 for (int i = 1;itl.More();itl.Next(),i++) {
243 const TopoDS_Shape& aFace = itl.Value();
244 std::string aFaceName = getShapeName(aDoc, aFace);
248 aName += "&" + aFaceName;
255 // aDoc->addNamingName(selectionLabel(), aName);
256 // the selected sub-shape will not be shared and as result it will not require registration
261 TopAbs_ShapeEnum translateType (const std::string& theType)
263 // map from the textual shape types to OCCT enumeration
264 static std::map<std::string, TopAbs_ShapeEnum> aShapeTypes;
266 if(aShapeTypes.size() == 0) {
267 aShapeTypes["compound"] = TopAbs_COMPOUND;
268 aShapeTypes["compounds"] = TopAbs_COMPOUND;
269 aShapeTypes["compsolid"] = TopAbs_COMPSOLID;
270 aShapeTypes["compsolids"] = TopAbs_COMPSOLID;
271 aShapeTypes["solid"] = TopAbs_SOLID;
272 aShapeTypes["solids"] = TopAbs_SOLID;
273 aShapeTypes["shell"] = TopAbs_SHELL;
274 aShapeTypes["shells"] = TopAbs_SHELL;
275 aShapeTypes["face"] = TopAbs_FACE;
276 aShapeTypes["faces"] = TopAbs_FACE;
277 aShapeTypes["wire"] = TopAbs_WIRE;
278 aShapeTypes["wires"] = TopAbs_WIRE;
279 aShapeTypes["edge"] = TopAbs_EDGE;
280 aShapeTypes["edges"] = TopAbs_EDGE;
281 aShapeTypes["vertex"] = TopAbs_VERTEX;
282 aShapeTypes["vertices"] = TopAbs_VERTEX;
283 aShapeTypes["COMPOUND"] = TopAbs_COMPOUND;
284 aShapeTypes["COMPOUNDS"] = TopAbs_COMPOUND;
285 aShapeTypes["COMPSOLID"] = TopAbs_COMPSOLID;
286 aShapeTypes["COMPSOLIDS"] = TopAbs_COMPSOLID;
287 aShapeTypes["SOLID"] = TopAbs_SOLID;
288 aShapeTypes["SOLIDS"] = TopAbs_SOLID;
289 aShapeTypes["SHELL"] = TopAbs_SHELL;
290 aShapeTypes["SHELLS"] = TopAbs_SHELL;
291 aShapeTypes["FACE"] = TopAbs_FACE;
292 aShapeTypes["FACES"] = TopAbs_FACE;
293 aShapeTypes["WIRE"] = TopAbs_WIRE;
294 aShapeTypes["WIRES"] = TopAbs_WIRE;
295 aShapeTypes["EDGE"] = TopAbs_EDGE;
296 aShapeTypes["EDGES"] = TopAbs_EDGE;
297 aShapeTypes["VERTEX"] = TopAbs_VERTEX;
298 aShapeTypes["VERTICES"] = TopAbs_VERTEX;
300 if (aShapeTypes.find(theType) != aShapeTypes.end())
301 return aShapeTypes[theType];
302 Events_InfoMessage("Model_SelectionNaming", "Shape type defined in XML is not implemented!").send();
306 const TopoDS_Shape getShapeFromNS(
307 const std::string& theSubShapeName, Handle(TNaming_NamedShape) theNS)
309 TopoDS_Shape aSelection;
310 std::string::size_type n = theSubShapeName.rfind('/');
311 if (n == std::string::npos) n = 0;
312 std::string aSubString = theSubShapeName.substr(n + 1);
313 n = aSubString.rfind('_');
314 if (n == std::string::npos) return aSelection;
315 aSubString = aSubString.substr(n+1);
316 int indx = atoi(aSubString.c_str());
318 TNaming_Iterator anItL(theNS);
319 for(int i = 1; anItL.More(); anItL.Next(), i++) {
321 return anItL.NewShape();
327 const TopoDS_Shape findFaceByName(
328 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc)
331 std::string::size_type n, nb = theSubShapeName.rfind('/');
332 if (nb == std::string::npos) nb = 0;
333 std::string aSubString = theSubShapeName.substr(nb + 1);
334 n = aSubString.rfind('_');
335 if (n != std::string::npos) {
336 std::string aSubStr2 = aSubString.substr(0, n);
337 aSubString = theSubShapeName.substr(0, nb + 1);
338 aSubString = aSubString + aSubStr2;
340 aSubString = theSubShapeName;
342 const TDF_Label& aLabel = theDoc->findNamingName(aSubString);
343 if(aLabel.IsNull()) return aFace;
344 Handle(TNaming_NamedShape) aNS;
345 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
346 aFace = getShapeFromNS(theSubShapeName, aNS);
351 size_t ParseName(const std::string& theSubShapeName, std::list<std::string>& theList)
353 std::string aName = theSubShapeName;
354 std::string aLastName;
355 size_t n1(0), n2(0); // n1 - start position, n2 - position of the delimiter
356 while ((n2 = aName.find('&', n1)) != std::string::npos) {
357 const std::string aName1 = aName.substr(n1, n2 - n1); //name of face
358 theList.push_back(aName1);
360 aLastName = aName.substr(n1);
362 if(!aLastName.empty())
363 theList.push_back(aLastName);
364 return theList.size();
367 const TopoDS_Shape findCommonShape(
368 const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
371 std::vector<TopTools_MapOfShape> aVec;
372 TopTools_MapOfShape aMap1, aMap2, aMap3, aMap4;
373 if(theList.Extent() > 1) {
374 aVec.push_back(aMap1);
375 aVec.push_back(aMap2);
377 if(theList.Extent() > 2)
378 aVec.push_back(aMap3);
379 if(theList.Extent() == 4)
380 aVec.push_back(aMap4);
383 TopTools_ListIteratorOfListOfShape it(theList);
384 for(int i = 0;it.More();it.Next(),i++) {
385 const TopoDS_Shape& aFace = it.Value();
387 TopExp_Explorer anExp (aFace, theType);
388 for(;anExp.More();anExp.Next()) {
389 const TopoDS_Shape& anEdge = anExp.Current();
390 if (!anEdge.IsNull())
391 aVec[i].Add(anExp.Current());
394 TopExp_Explorer anExp (aFace, TopAbs_VERTEX);
395 for(;anExp.More();anExp.Next()) {
396 const TopoDS_Shape& aVertex = anExp.Current();
397 if (!aVertex.IsNull())
398 aVec[i].Add(anExp.Current());
402 //trivial case: 2 faces
403 TopTools_ListOfShape aList;
404 TopTools_MapIteratorOfMapOfShape it2(aVec[0]);
405 for(;it2.More();it2.Next()) {
406 if(aVec[1].Contains(it2.Key())) {
408 if(theList.Extent() == 2)
411 aList.Append(it2.Key());
414 if(aList.Extent() && aVec.size() > 3) {// list of common edges ==> search ny neighbors
415 if(aVec[2].Extent() && aVec[3].Extent()) {
416 TopTools_ListIteratorOfListOfShape it(aList);
417 for(;it.More();it.Next()) {
418 const TopoDS_Shape& aCand = it.Value();
419 // not yet completelly implemented, to be rechecked
420 TopoDS_Vertex aV1, aV2;
421 TopExp::Vertices(TopoDS::Edge(aCand), aV1, aV2);
423 if(aVec[2].Contains(aV1)) aNum++;
424 else if(aVec[2].Contains(aV2)) aNum++;
425 if(aVec[3].Contains(aV1)) aNum++;
426 else if(aVec[3].Contains(aV2)) aNum++;
435 if(aList.Extent() && aVec.size() == 3) {
437 TopTools_ListIteratorOfListOfShape it(aList);
438 for(;it.More();it.Next()) {
439 const TopoDS_Shape& aCand = it.Value();
440 if(aVec[2].Contains(aCand)) {
449 std::string getContextName(const std::string& theSubShapeName)
452 std::string::size_type n = theSubShapeName.find('/');
453 if (n == std::string::npos) return aName;
454 aName = theSubShapeName.substr(0, n);
458 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
459 bool Model_SelectionNaming::selectSubShape(const std::string& theType,
460 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc,
461 std::shared_ptr<GeomAPI_Shape>& theShapeToBeSelected, std::shared_ptr<ModelAPI_Result>& theCont)
463 if(theSubShapeName.empty() || theType.empty()) return false;
464 TopAbs_ShapeEnum aType = translateType(theType);
465 std::string aContName = getContextName(theSubShapeName);
466 if(aContName.empty()) return false;
467 ResultPtr aCont = theDoc->findByName(aContName);
468 if(!aCont.get() || aCont->shape()->isNull()) return false;
469 TopoDS_Shape aContext = aCont->shape()->impl<TopoDS_Shape>();
470 TopAbs_ShapeEnum aContType = aContext.ShapeType();
471 if(aType <= aContType) return false; // not applicable
474 TopoDS_Shape aSelection;
477 case TopAbs_COMPOUND:
479 case TopAbs_COMPSOLID:
487 const TopoDS_Shape aSelection = findFaceByName(theSubShapeName, theDoc);
488 if(!aSelection.IsNull()) {// Select it
489 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
490 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
491 theShapeToBeSelected = aShapeToBeSelected;
501 TopoDS_Shape aSelection;// = findFaceByName(theSubShapeName, aDoc);
502 const TDF_Label& aLabel = theDoc->findNamingName(theSubShapeName);
503 if(!aLabel.IsNull()) {
504 Handle(TNaming_NamedShape) aNS;
505 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
506 aSelection = getShapeFromNS(theSubShapeName, aNS);
509 if(aSelection.IsNull()) {
510 std::list<std::string> aListofNames;
511 size_t n = ParseName(theSubShapeName, aListofNames);
513 TopTools_ListOfShape aList;
514 std::list<std::string>::iterator it =aListofNames.begin();
515 for(;it != aListofNames.end();it++){
516 const TopoDS_Shape aFace = findFaceByName(*it, theDoc);
519 aSelection = findCommonShape(TopAbs_EDGE, aList);
522 if(!aSelection.IsNull()) {// Select it
523 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
524 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
525 theShapeToBeSelected = aShapeToBeSelected;
533 TopoDS_Shape aSelection;
534 const TDF_Label& aLabel = theDoc->findNamingName(theSubShapeName);
535 if(!aLabel.IsNull()) {
536 Handle(TNaming_NamedShape) aNS;
537 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
538 aSelection = getShapeFromNS(theSubShapeName, aNS);
541 if(aSelection.IsNull()) {
542 std::list<std::string> aListofNames;
543 size_t n = ParseName(theSubShapeName, aListofNames);
544 if(n > 1 && n < 4) { // 2 || 3
545 TopTools_ListOfShape aList;
546 std::list<std::string>::iterator it = aListofNames.begin();
547 for(; it != aListofNames.end(); it++){
548 const TopoDS_Shape aFace = findFaceByName(*it, theDoc);
552 aSelection = findCommonShape(TopAbs_VERTEX, aList);
555 if(!aSelection.IsNull()) {// Select it
556 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
557 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
558 theShapeToBeSelected = aShapeToBeSelected;
564 default: //TopAbs_SHAPE