From c62a4254fbf27b069449f4766f9c849fe2286109 Mon Sep 17 00:00:00 2001 From: mpv Date: Wed, 10 Aug 2016 17:58:50 +0300 Subject: [PATCH] Make naming name works on bodies with additional prefixes (extrusion solid in the Boolean) (in the frames of the python dump, issue #1648 ) --- src/Model/Model_SelectionNaming.cpp | 50 ++++++++++++++++++++++------- src/SketchAPI/SketchAPI_Sketch.cpp | 4 +-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/Model/Model_SelectionNaming.cpp b/src/Model/Model_SelectionNaming.cpp index 68311d6f2..5c5c6132b 100644 --- a/src/Model/Model_SelectionNaming.cpp +++ b/src/Model/Model_SelectionNaming.cpp @@ -614,10 +614,25 @@ bool Model_SelectionNaming::selectSubShape(const std::string& theType, std::string aContName = getContextName(theSubShapeName); if(aContName.empty()) return false; ResultPtr aCont = theDoc->findByName(aContName); - //if(!aCont.get() || aCont->shape()->isNull()) return false; - //TopoDS_Shape aContext = aCont->shape()->impl(); - //TopAbs_ShapeEnum aContType = aContext.ShapeType(); - //if(aType <= aContType) return false; // not applicable + // possible this is body where postfix is added to distinguish several shapes on the same label + int aSubShapeId = -1; // -1 means sub shape not found + if (!aCont.get() && aContName == theSubShapeName) { + size_t aPostIndex = aContName.rfind('_'); + if (aPostIndex != std::string::npos) { + std::string aSubContName = aContName.substr(0, aPostIndex); + aCont = theDoc->findByName(aSubContName); + if (aCont.get()) { + try { + std::string aNum = aContName.substr(aPostIndex + 1); + aSubShapeId = std::stoi(aNum); + } catch (std::invalid_argument&) { + aSubShapeId = -1; + } + if (aSubShapeId > 0) + aContName = aSubContName; + } + } + } TopoDS_Shape aSelection; @@ -657,13 +672,26 @@ bool Model_SelectionNaming::selectSubShape(const std::string& theType, case TopAbs_WIRE: default: {//TopAbs_SHAPE /// case when the whole sketch is selected, so, selection type is compound, but there is no value - if (aCont.get() && aCont->shape().get() && - aCont->shape()->impl().ShapeType() == aType) { - theCont = aCont; - return true; + if (aCont.get() && aCont->shape().get()) { + if (aCont->shape()->impl().ShapeType() == aType) { + theCont = aCont; + return true; + } else if (aSubShapeId > 0) { // try to find sub-shape by the index + TopExp_Explorer anExp(aCont->shape()->impl(), aType); + for(; aSubShapeId > 0 && anExp.More(); aSubShapeId--) { + anExp.Next(); + } + if (anExp.More()) { + std::shared_ptr aShapeToBeSelected(new GeomAPI_Shape()); + aShapeToBeSelected->setImpl(new TopoDS_Shape(anExp.Current())); + theShapeToBeSelected = aShapeToBeSelected; + theCont = aCont; + return true; + } + } } return false; - } + } } // another try to find edge or vertex by faces std::list aListofNames; @@ -692,8 +720,8 @@ bool Model_SelectionNaming::selectSubShape(const std::string& theType, if (aN == 0) { size_t aConstrNamePos = theSubShapeName.find("/"); bool isFullName = aConstrNamePos == std::string::npos; - std::string aContrName = - isFullName ? theSubShapeName : theSubShapeName.substr(0, aConstrNamePos); + std::string aContrName = aContName; + // isFullName ? theSubShapeName : theSubShapeName.substr(0, aConstrNamePos); ResultPtr aConstr = theDoc->findByName(aContrName); if (aConstr.get() && aConstr->groupName() == ModelAPI_ResultConstruction::group()) { theCont = aConstr; diff --git a/src/SketchAPI/SketchAPI_Sketch.cpp b/src/SketchAPI/SketchAPI_Sketch.cpp index ef4962054..09411948d 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -625,9 +625,7 @@ void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()); if (anExternal->value()) { - FeaturePtr aPlnFeature = ModelAPI_Feature::feature(anExternal->context()->data()->owner()); - const std::string& aPlaneName = theDumper.name(aPlnFeature); - theDumper << aBase << " = model.addSketch(" << aDocName << ", \"" << aPlaneName << "\")" << std::endl; + theDumper << aBase << " = model.addSketch(" << aDocName << ", " << anExternal << ")" << std::endl; } else { // Sketch is base on a plane. std::shared_ptr anOrigin = std::dynamic_pointer_cast( -- 2.39.2