+
+std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
+{
+ std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+ std::dynamic_pointer_cast<Model_Document>(owner()->document());
+ FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
+ bool aBaseDocumnetUsed = false;
+ if (!aFeatureOwner.get()) { // use module document
+ aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
+ aFeatureOwner = aDoc->featureByLab(theSelectionLab);
+ aBaseDocumnetUsed = true;
+ }
+ if (aFeatureOwner.get()) {
+ // if it is sub-element of the sketch, the context name is the name of the sketch
+ // searching also for result - real context
+ ResultPtr aResult;
+ FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
+ if (aComposite.get() && aComposite->results().size() == 1 &&
+ aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
+ aFeatureOwner = aComposite;
+ aResult = aFeatureOwner->firstResult();
+ } else {
+ aResult = aDoc->resultByLab(theSelectionLab);
+ }
+ if (aResult.get()) {
+ // this is to avoid duplicated names of results problem
+ std::string aContextName = aResult->data()->name();
+ // myLab corresponds to the current time
+ TDF_Label aCurrentLab = selectionLabel();
+ while(aCurrentLab.Depth() > 3)
+ aCurrentLab = aCurrentLab.Father();
+
+ int aNumInHistoryNames =
+ aDoc->numberOfNameInHistory(aResult, aCurrentLab);
+ while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
+ aContextName = "_" + aContextName;
+ aNumInHistoryNames--;
+ }
+ if (aBaseDocumnetUsed)
+ aContextName = aDoc->kind() + "/" + aContextName;
+ return aContextName;
+ }
+ }
+ return ""; // invalid case
+}
+
+/// This method restores by the context and value name the context label and
+/// sub-label where the value is. Returns true if it is valid.
+bool Model_AttributeSelection::restoreContext(std::string theName,
+ TDF_Label& theContext, TDF_Label& theValue)
+{
+ static const GeomShapePtr anEmptyShape; // to store context only
+ std::string aName = theName;
+ std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+ std::dynamic_pointer_cast<Model_Document>(owner()->document());
+
+ // remove the sub-value part if exists
+ std::string aSubShapeName = aName;
+ std::string::size_type n = aName.find('/');
+ if (n != std::string::npos) {
+ aName = aName.substr(0, n);
+ }
+
+ if (aName.empty()) return false;
+ bool anUniqueContext = false;
+ ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
+ if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
+ // name in PartSet?
+ aDoc = std::dynamic_pointer_cast<Model_Document>(
+ ModelAPI_Session::get()->moduleDocument());
+ if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
+ aSubShapeName = theName.substr(aDoc->kind().size() + 1);
+ aName = aSubShapeName;
+ std::string::size_type n = aName.find('/');
+ if (n != std::string::npos) {
+ aName = aName.substr(0, n);
+ }
+ }
+ aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
+ if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
+ return false;
+ }
+
+ // searching the sub-shape
+ static const ResultPtr anEmpty;
+ theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
+
+ // sketch sub-component shape and name is located in separated feature label, try the sub-name
+ if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
+ std::string::size_type aSlash = aSubShapeName.rfind('/');
+ if (aSlash != std::string::npos) {
+ std::string aCompName = aSubShapeName.substr(aSlash + 1);
+ CompositeFeaturePtr aComposite =
+ std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
+ if (aComposite.get() && aComposite->numberOfSubs()) {
+ const int aSubNum = aComposite->numberOfSubs();
+ for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
+ FeaturePtr aSub = aComposite->subFeature(a);
+ const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
+ std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
+ for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
+ if ((*aRes)->data()->name() == aCompName) {
+ theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
+ break;
+ } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
+ TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
+ TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
+ for(; aSubNames.More(); aSubNames.Next()) {
+ if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
+ IsEqual(aCompName.c_str())) {
+ theValue = aSubNames.Value()->Label();
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (aCont.get()) {
+ theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
+ }
+ return true;
+}
+
+TDF_Label Model_AttributeSelection::newestContext(const TDF_Label theCurrentContext) {
+ std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+ std::dynamic_pointer_cast<Model_Document>(owner()->document());
+ ResultPtr aContext = aDoc->resultByLab(theCurrentContext);
+ if (aContext.get()) {
+ aContext = newestContext(aContext, GeomShapePtr(), true);
+ if (aContext.get())
+ return std::dynamic_pointer_cast<Model_Data>(aContext->data())->label();
+ }
+ return theCurrentContext; // nothing is changed
+}
+
+bool Model_AttributeSelection::isLater(
+ const TDF_Label theResult1, const TDF_Label theResult2) const
+{
+ std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+ std::dynamic_pointer_cast<Model_Document>(owner()->document());
+ FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
+ if (!aFeat1.get())
+ return false;
+ FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
+ if (!aFeat2.get())
+ return false;
+ return aDoc->isLaterByDep(aFeat1, aFeat2);
+}
+
+ResultPtr Model_AttributeSelection::newestContext(
+ const ResultPtr theCurrent, const GeomShapePtr theValue, const bool theAnyValue)
+{
+ ResultPtr aResult = theCurrent;
+ GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
+ std::shared_ptr<Model_Document> aDoc =
+ std::dynamic_pointer_cast<Model_Document>(owner()->document());
+ bool aFindNewContext = true;
+ while (aFindNewContext && aResult.get()) {
+ aFindNewContext = false;
+ // try to find the last context to find the up to date shape
+ TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
+ if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
+ Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
+ if (!aNS.IsNull()) {
+ aNS = TNaming_Tool::CurrentNamedShape(aNS);
+ if (!aNS.IsNull()
+ && isLater(selectionLabel(), aNS->Label())
+ && isLater(aNS->Label(),
+ std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
+ {
+ TDF_Label aLab = aNS->Label();
+ ResultPtr aRes = aDoc->resultByLab(aLab);
+ if (aRes.get()) {
+ if (theAnyValue || aRes->shape()->isSubShape(aSelectedShape)) {
+ aResult = aRes;
+ aFindNewContext = true;
+ continue;
+ }
+ }
+ }
+ }
+ }
+ if (theAnyValue) { // only for neighbors for now
+ // try to find modification of sub-shapes: the best number of matches
+ std::map<ResultPtr, int> aMatches; // result -> number of matches of shapes to find the best
+ TDF_Label aResLab = std::dynamic_pointer_cast<Model_Data>(aResult->data())->shapeLab();
+ TDF_ChildIDIterator aModifIter(aResLab, TNaming_NamedShape::GetID());
+ for(; aModifIter.More(); aModifIter.Next()) {
+ Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aModifIter.Value());
+ if (aNS->Evolution() == TNaming_MODIFY || aNS->Evolution() == TNaming_GENERATED) {
+ for(TNaming_Iterator aNSIter(aNS); aNSIter.More(); aNSIter.Next()) {
+ TNaming_NewShapeIterator aNewIter(aNSIter.NewShape(), aNS->Label());
+ for(; aNewIter.More(); aNewIter.Next()) {
+ TDF_Label aLab = aNewIter.Label();
+ if (isLater(aLab, aNS->Label()) && isLater(selectionLabel(), aLab)) {
+ ResultPtr aRes = aDoc->resultByLab(aLab);
+ if (aRes.get()) {
+ if (aMatches.find(aRes) == aMatches.end())
+ aMatches[aRes] = 0;
+ aMatches[aRes]++; // found result, add matches
+ }
+ }
+ }
+ }
+ }
+ }
+ // searching for the best result-candidate
+ int aBest = 0;
+ ResultPtr aBestResult;
+ std::map<ResultPtr, int>::iterator aMatchIter = aMatches.begin();
+ for(; aMatchIter != aMatches.end(); aMatchIter++) {
+ if (aMatchIter->second > aBest) {
+ aBest = aMatchIter->second;
+ aBestResult = aMatchIter->first;
+ }
+ }
+ if (aBestResult.get()) {
+ aResult = aBestResult;
+ aFindNewContext = true;
+ continue;
+ }
+ }
+
+
+ // TestFillWireVertex.py - sketch constructions for wire may participate too
+ //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
+ // try to search newer context by the concealment references
+ // take references to all results: root one, any sub
+ std::list<ResultPtr> allRes;
+ ResultPtr aCompContext;
+ ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
+ if (aCompBody.get()) {
+ ModelAPI_Tools::allSubs(aCompBody, allRes);
+ allRes.push_back(aCompBody);
+ aCompContext = aCompBody;
+ }
+ if (allRes.empty())
+ allRes.push_back(aResult);
+
+ for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
+ ResultPtr aResCont = *aSub;
+ ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
+ if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
+ continue; // only lower and higher level subs are counted
+ const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
+ std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
+ for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
+ if (!aRef->get() || !(*aRef)->owner().get())
+ continue;
+ // concealed attribute only
+ FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
+ if (!ModelAPI_Session::get()->validators()->isConcealed(
+ aRefFeat->getKind(), (*aRef)->id()))
+ continue;
+ // search the feature result that contains sub-shape selected
+ std::list<std::shared_ptr<ModelAPI_Result> > aResults;
+
+ // take all sub-results or one result
+ std::list<ResultPtr> aRefFeatResults;
+ ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
+ std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
+ for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
+ ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
+ if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
+ aResults.push_back(aBody);
+ }
+ std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
+ for (; aResIter != aResults.end(); aResIter++) {
+ if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
+ continue;
+ GeomShapePtr aShape = (*aResIter)->shape();
+ if (aShape.get() && (theAnyValue || aShape->isSubShape(aSelectedShape, false))) {
+ aResult = *aResIter; // found new context (produced from this) with same subshape
+ aFindNewContext = true; // continue searching further
+ break;
+ }
+ }
+ }
+ }
+ }
+ // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
+ ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
+ if (aComp && aComp->numberOfSubs()) {
+ std::list<ResultPtr> allSubs;
+ ModelAPI_Tools::allSubs(aComp, allSubs);
+ std::list<ResultPtr>::iterator aS = allSubs.begin();
+ for (; aS != allSubs.end(); aS++) {
+ ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
+ if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
+ (theAnyValue || aSub->shape()->isSubShape(aSelectedShape))) {
+ aResult = aSub;
+ break;
+ }
+ }
+ }
+ // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
+ // so, actually, the sketch element was selected (which is never concealed)
+ if (aResult != theCurrent && aResult->isConcealed())
+ aResult = theCurrent;
+ return aResult;
+}