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_Error.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 /* for (int i = 1; i <= aMap.Extent(); i++) {
140 const TopoDS_Shape& aKey = aMap.FindKey(i);
141 //if (aKey.IsNotEqual(aSubShape)) continue; // find exactly the selected key
142 if (aKey.IsSame(aSubShape)) continue;
143 const TopTools_ListOfShape& anAncestors = aMap.FindFromIndex(i);
144 // check that it is not a trivial case (F1 & F2: aNumber = 1)
145 isTrivialCase = isTrivial(anAncestors, aSMap);
148 if(aMap.Contains(aSubShape)) {
149 const TopTools_ListOfShape& anAncestors = aMap.FindFromKey(aSubShape);
150 // check that it is not a trivial case (F1 & F2: aNumber = 1)
151 isTrivialCase = isTrivial(anAncestors, aSMap);
154 TopTools_ListOfShape aListOfNbs;
155 if(!isTrivialCase) { // find Neighbors
156 TNaming_Localizer aLocalizer;
157 TopTools_MapOfShape aMap3;
158 aLocalizer.FindNeighbourg(aContext, aSubShape, aMap3);
159 //int n = aMap3.Extent();
160 TopTools_MapIteratorOfMapOfShape it(aMap3);
161 for(;it.More();it.Next()) {
162 const TopoDS_Shape& aNbShape = it.Key(); // neighbor edge
163 //TopAbs_ShapeEnum aType = aNbShape.ShapeType();
164 const TopTools_ListOfShape& aList = aMap.FindFromKey(aNbShape);
165 TopTools_ListIteratorOfListOfShape it2(aList);
166 for(;it2.More();it2.Next()) {
167 if(aSMap.Contains(it2.Value())) continue; // skip this Face
168 aListOfNbs.Append(it2.Value());
171 } // else a trivial case
173 // build name of the sub-shape Edge
174 for(int i=1; i <= aSMap.Extent(); i++) {
175 const TopoDS_Shape& aFace = aSMap.FindKey(i);
176 std::string aFaceName = getShapeName(aDoc, aFace);
180 aName += "|" + aFaceName;
182 TopTools_ListIteratorOfListOfShape itl(aListOfNbs);
183 for (;itl.More();itl.Next()) {
184 std::string aFaceName = getShapeName(aDoc, itl.Value());
185 aName += "|" + aFaceName;
191 // name structure (Monifold Topology):
192 // 1) F1 | F2 | F3 - intersection of 3 faces defines a vertex - trivial case.
193 // 2) F1 | F2 | F3 [|F4 [|Fn]] - redundant definition, but it should be kept as is to obtain safe recomputation
194 // 2) F1 | F2 - intersection of 2 faces definses a vertex - applicable for case
195 // when 1 faces is cylindrical, conical, spherical or revolution and etc.
196 // 3) E1 | E2 | E3 - intersection of 3 edges defines a vertex - when we have case of a shell
197 // or compound of 2 open faces.
198 // 4) E1 | E2 - intesection of 2 edges defines a vertex - when we have a case of
199 // two independent edges (wire or compound)
200 // implemented 2 first cases
202 TopTools_IndexedDataMapOfShapeListOfShape aMap;
203 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_FACE, aMap);
204 const TopTools_ListOfShape& aList2 = aMap.FindFromKey(aSubShape);
205 TopTools_ListOfShape aList;
206 TopTools_MapOfShape aFMap;
207 //int n = aList2.Extent(); //bug! duplication
209 TopTools_ListIteratorOfListOfShape itl2(aList2);
210 for (int i = 1;itl2.More();itl2.Next(),i++) {
211 if(aFMap.Add(itl2.Value()))
212 aList.Append(itl2.Value());
214 //n = aList.Extent();
215 int n = aList.Extent();
216 if(n < 3) { // open topology case or Compound case => via edges
217 TopTools_IndexedDataMapOfShapeListOfShape aMap;
218 TopExp::MapShapesAndAncestors(aContext, TopAbs_VERTEX, TopAbs_EDGE, aMap);
219 const TopTools_ListOfShape& aList22 = aMap.FindFromKey(aSubShape);
220 if(aList22.Extent() >= 2) { // regular solution
222 // bug! duplication; fix is below
224 TopTools_ListOfShape aListE;
225 TopTools_ListIteratorOfListOfShape itl2(aList22);
226 for (int i = 1;itl2.More();itl2.Next(),i++) {
227 if(aFMap.Add(itl2.Value()))
228 aListE.Append(itl2.Value());
231 TopTools_ListIteratorOfListOfShape itl(aListE);
232 for (int i = 1;itl.More();itl.Next(),i++) {
233 const TopoDS_Shape& anEdge = itl.Value();
234 std::string anEdgeName = getShapeName(aDoc, anEdge);
238 aName += "|" + anEdgeName;
241 else { // dangle vertex: if(aList22.Extent() == 1)
242 //it should be already in DF
246 TopTools_ListIteratorOfListOfShape itl(aList);
247 for (int i = 1;itl.More();itl.Next(),i++) {
248 const TopoDS_Shape& aFace = itl.Value();
249 std::string aFaceName = getShapeName(aDoc, aFace);
253 aName += "|" + aFaceName;
260 // aDoc->addNamingName(selectionLabel(), aName);
261 // the selected sub-shape will not be shared and as result it will not require registration
266 TopAbs_ShapeEnum translateType (const std::string& theType)
268 // map from the textual shape types to OCCT enumeration
269 static std::map<std::string, TopAbs_ShapeEnum> MyShapeTypes;
270 if (MyShapeTypes.size() == 0) {
271 MyShapeTypes["face"] = TopAbs_FACE;
272 MyShapeTypes["faces"] = TopAbs_FACE;
273 MyShapeTypes["vertex"] = TopAbs_VERTEX;
274 MyShapeTypes["vertices"] = TopAbs_VERTEX;
275 MyShapeTypes["wire"] = TopAbs_WIRE;
276 MyShapeTypes["edge"] = TopAbs_EDGE;
277 MyShapeTypes["edges"] = TopAbs_EDGE;
278 MyShapeTypes["shell"] = TopAbs_SHELL;
279 MyShapeTypes["solid"] = TopAbs_SOLID;
280 MyShapeTypes["solids"] = TopAbs_SOLID;
281 MyShapeTypes["FACE"] = TopAbs_FACE;
282 MyShapeTypes["FACES"] = TopAbs_FACE;
283 MyShapeTypes["VERTEX"] = TopAbs_VERTEX;
284 MyShapeTypes["VERTICES"] = TopAbs_VERTEX;
285 MyShapeTypes["WIRE"] = TopAbs_WIRE;
286 MyShapeTypes["EDGE"] = TopAbs_EDGE;
287 MyShapeTypes["EDGES"] = TopAbs_EDGE;
288 MyShapeTypes["SHELL"] = TopAbs_SHELL;
289 MyShapeTypes["SOLID"] = TopAbs_SOLID;
290 MyShapeTypes["SOLIDS"] = TopAbs_SOLID;
292 if (MyShapeTypes.find(theType) != MyShapeTypes.end())
293 return MyShapeTypes[theType];
294 Events_Error::send("Shape type defined in XML is not implemented!");
298 const TopoDS_Shape getShapeFromNS(
299 const std::string& theSubShapeName, Handle(TNaming_NamedShape) theNS)
301 TopoDS_Shape aSelection;
302 std::string::size_type n = theSubShapeName.rfind('/');
303 if (n == std::string::npos) n = 0;
304 std::string aSubString = theSubShapeName.substr(n + 1);
305 n = aSubString.rfind('_');
306 if (n == std::string::npos) return aSelection;
307 aSubString = aSubString.substr(n+1);
308 int indx = atoi(aSubString.c_str());
310 TNaming_Iterator anItL(theNS);
311 for(int i = 1; anItL.More(); anItL.Next(), i++) {
313 return anItL.NewShape();
319 const TopoDS_Shape findFaceByName(
320 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc)
323 std::string::size_type n, nb = theSubShapeName.rfind('/');
324 if (nb == std::string::npos) nb = 0;
325 std::string aSubString = theSubShapeName.substr(nb + 1);
326 n = aSubString.rfind('_');
327 if (n != std::string::npos) {
328 std::string aSubStr2 = aSubString.substr(0, n);
329 aSubString = theSubShapeName.substr(0, nb + 1);
330 aSubString = aSubString + aSubStr2;
332 aSubString = theSubShapeName;
334 const TDF_Label& aLabel = theDoc->findNamingName(aSubString);
335 if(aLabel.IsNull()) return aFace;
336 Handle(TNaming_NamedShape) aNS;
337 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
338 aFace = getShapeFromNS(theSubShapeName, aNS);
343 int ParseName(const std::string& theSubShapeName, std::list<std::string>& theList)
345 std::string aName = theSubShapeName;
346 std::string aLastName;
347 int n1(0), n2(0); // n1 - start position, n2 - position of the delimiter
348 while ((n2 = aName.find('|', n1)) != std::string::npos) {
349 const std::string aName1 = aName.substr(n1, n2 - n1); //name of face
350 theList.push_back(aName1);
352 aLastName = aName.substr(n1);
354 if(!aLastName.empty())
355 theList.push_back(aLastName);
356 return theList.size();
359 const TopoDS_Shape findCommonShape(
360 const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
363 std::vector<TopTools_MapOfShape> aVec;
364 TopTools_MapOfShape aMap1, aMap2, aMap3, aMap4;
365 if(theList.Extent() > 1) {
366 aVec.push_back(aMap1);
367 aVec.push_back(aMap2);
369 if(theList.Extent() > 2)
370 aVec.push_back(aMap3);
371 if(theList.Extent() == 4)
372 aVec.push_back(aMap4);
375 TopTools_ListIteratorOfListOfShape it(theList);
376 for(int i = 0;it.More();it.Next(),i++) {
377 const TopoDS_Shape& aFace = it.Value();
379 TopExp_Explorer anExp (aFace, theType);
380 for(;anExp.More();anExp.Next()) {
381 const TopoDS_Shape& anEdge = anExp.Current();
382 if (!anEdge.IsNull())
383 aVec[i].Add(anExp.Current());
386 TopExp_Explorer anExp (aFace, TopAbs_VERTEX);
387 for(;anExp.More();anExp.Next()) {
388 const TopoDS_Shape& aVertex = anExp.Current();
389 if (!aVertex.IsNull())
390 aVec[i].Add(anExp.Current());
394 //trivial case: 2 faces
395 TopTools_ListOfShape aList;
396 TopTools_MapIteratorOfMapOfShape it2(aVec[0]);
397 for(;it2.More();it2.Next()) {
398 if(aVec[1].Contains(it2.Key())) {
400 if(theList.Extent() == 2)
403 aList.Append(it2.Key());
406 if(aList.Extent() && aVec.size() > 3) {// list of common edges ==> search ny neighbors
407 if(aVec[2].Extent() && aVec[3].Extent()) {
408 TopTools_ListIteratorOfListOfShape it(aList);
409 for(;it.More();it.Next()) {
410 const TopoDS_Shape& aCand = it.Value();
411 // not yet completelly implemented, to be rechecked
412 TopoDS_Vertex aV1, aV2;
413 TopExp::Vertices(TopoDS::Edge(aCand), aV1, aV2);
415 if(aVec[2].Contains(aV1)) aNum++;
416 else if(aVec[2].Contains(aV2)) aNum++;
417 if(aVec[3].Contains(aV1)) aNum++;
418 else if(aVec[3].Contains(aV2)) aNum++;
427 if(aList.Extent() && aVec.size() == 3) {
429 TopTools_ListIteratorOfListOfShape it(aList);
430 for(;it.More();it.Next()) {
431 const TopoDS_Shape& aCand = it.Value();
432 if(aVec[2].Contains(aCand)) {
441 std::string getContextName(const std::string& theSubShapeName)
444 std::string::size_type n = theSubShapeName.find('/');
445 if (n == std::string::npos) return aName;
446 aName = theSubShapeName.substr(0, n);
450 // type ::= COMP | COMS | SOLD | SHEL | FACE | WIRE | EDGE | VERT
451 bool Model_SelectionNaming::selectSubShape(const std::string& theType,
452 const std::string& theSubShapeName, std::shared_ptr<Model_Document> theDoc,
453 std::shared_ptr<GeomAPI_Shape>& theShapeToBeSelected, std::shared_ptr<ModelAPI_Result>& theCont)
455 if(theSubShapeName.empty() || theType.empty()) return false;
456 TopAbs_ShapeEnum aType = translateType(theType);
457 std::string aContName = getContextName(theSubShapeName);
458 if(aContName.empty()) return false;
459 ResultPtr aCont = theDoc->findByName(aContName);
460 if(!aCont.get() || aCont->shape()->isNull()) return false;
461 TopoDS_Shape aContext = aCont->shape()->impl<TopoDS_Shape>();
462 TopAbs_ShapeEnum aContType = aContext.ShapeType();
463 if(aType <= aContType) return false; // not applicable
466 TopoDS_Shape aSelection;
469 case TopAbs_COMPOUND:
471 case TopAbs_COMPSOLID:
479 const TopoDS_Shape aSelection = findFaceByName(theSubShapeName, theDoc);
480 if(!aSelection.IsNull()) {// Select it
481 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
482 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
483 theShapeToBeSelected = aShapeToBeSelected;
493 TopoDS_Shape aSelection;// = findFaceByName(theSubShapeName, aDoc);
494 const TDF_Label& aLabel = theDoc->findNamingName(theSubShapeName);
495 if(!aLabel.IsNull()) {
496 Handle(TNaming_NamedShape) aNS;
497 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
498 aSelection = getShapeFromNS(theSubShapeName, aNS);
501 if(aSelection.IsNull()) {
502 std::list<std::string> aListofNames;
503 int n = ParseName(theSubShapeName, aListofNames);
505 TopTools_ListOfShape aList;
506 std::list<std::string>::iterator it =aListofNames.begin();
507 for(;it != aListofNames.end();it++){
508 const TopoDS_Shape aFace = findFaceByName(*it, theDoc);
511 aSelection = findCommonShape(TopAbs_EDGE, aList);
514 if(!aSelection.IsNull()) {// Select it
515 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
516 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
517 theShapeToBeSelected = aShapeToBeSelected;
525 TopoDS_Shape aSelection;
526 const TDF_Label& aLabel = theDoc->findNamingName(theSubShapeName);
527 if(!aLabel.IsNull()) {
528 Handle(TNaming_NamedShape) aNS;
529 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
530 aSelection = getShapeFromNS(theSubShapeName, aNS);
533 if(aSelection.IsNull()) {
534 std::list<std::string> aListofNames;
535 int n = ParseName(theSubShapeName, aListofNames);
536 if(n > 1 && n < 4) { // 2 || 3
537 TopTools_ListOfShape aList;
538 std::list<std::string>::iterator it = aListofNames.begin();
539 for(; it != aListofNames.end(); it++){
540 const TopoDS_Shape aFace = findFaceByName(*it, theDoc);
544 aSelection = findCommonShape(TopAbs_VERTEX, aList);
547 if(!aSelection.IsNull()) {// Select it
548 std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected(new GeomAPI_Shape());
549 aShapeToBeSelected->setImpl(new TopoDS_Shape(aSelection));
550 theShapeToBeSelected = aShapeToBeSelected;
556 default: //TopAbs_SHAPE