+
+std::wstring 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::wstring 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 = L"_" + aContextName;
+ aNumInHistoryNames--;
+ }
+ if (aBaseDocumnetUsed)
+ aContextName = Locale::Convert::toWString(aDoc->kind()) + L"/" + aContextName;
+ return aContextName;
+ }
+ }
+ return L""; // 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::wstring theName,
+ TDF_Label& theContext, TDF_Label& theValue)
+{
+ static const GeomShapePtr anEmptyShape; // to store context only
+ std::wstring 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::wstring aSubShapeName = aName;
+ std::wstring::size_type n = aName.find(L'/');
+ if (n != std::wstring::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(Locale::Convert::toWString(aDoc->kind())) == 0) {
+ // remove the document identifier from name if exists
+ aSubShapeName = theName.substr(aDoc->kind().size() + 1);
+ aName = aSubShapeName;
+ n = aName.find(L'/');
+ if (n != std::wstring::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::wstring::size_type aSlash = aSubShapeName.rfind(L'/');
+ if (aSlash != std::wstring::npos) {
+ std::wstring 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;
+}
+
+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)
+{
+ 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 (aRes->shape()->isSubShape(aSelectedShape)) {
+ aResult = aRes;
+ 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<DataPtr> allRes;
+ ResultPtr aCompContext;
+ ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
+ if (aCompBody.get()) {
+ std::list<ResultPtr> allSub;
+ ModelAPI_Tools::allSubs(aCompBody, allSub);
+ for(std::list<ResultPtr>::iterator anIt = allSub.begin(); anIt != allSub.end(); anIt++)
+ allRes.push_back((*anIt)->data());
+ allRes.push_back(aCompBody->data());
+ aCompContext = aCompBody;
+ }
+ if (allRes.empty())
+ allRes.push_back(aResult->data());
+ allRes.push_back(aResult->document()->feature(aResult)->data());
+
+ bool aFoundReferernce = false;
+ for (std::list<DataPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
+ DataPtr aResCont = *aSub;
+ ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont->owner());
+ if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
+ continue; // only lower and higher level subs are counted
+ const std::set<AttributePtr>& aRefs = aResCont->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();
+
+ // searching by sub-shape
+ for (; aResIter != aResults.end(); aResIter++) {
+ if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
+ continue;
+ aFoundReferernce = true;
+ GeomShapePtr aShape = (*aResIter)->shape();
+ if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
+ aResult = *aResIter; // found new context (produced from this) with same subshape
+ aFindNewContext = true; // continue searching further
+ break;
+ }
+ }
+ }
+ }
+ if (aFoundReferernce && !aFindNewContext &&
+ aResult->groupName() != ModelAPI_ResultConstruction::group()) {
+ // #19019 : result is concealed by object that contains no such sub-shape
+ return theCurrent;
+ }
+ }
+ // 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() &&
+ aSub->shape()->isSubShape(aSelectedShape)) {
+ aResult = aSub;
+ break;
+ }
+ }
+ }
+ // in case sketch line was selected for wire, but wire was concealed and there is no such line
+ // anymore, so, actually, the sketch element was selected (which is never concealed)
+ if (aResult != theCurrent && theCurrent->groupName() == ModelAPI_ResultConstruction::group()) {
+ std::list<FeaturePtr> aConcealers;
+ FeaturePtr aResFeature = aDoc->feature(aResult);
+ FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
+ concealedFeature(aResFeature, aThisFeature, false, aConcealers, aResult, true);
+ if (aConcealers.size())
+ aResult = theCurrent;
+ }
+ return aResult;
+}
+
+void Model_AttributeSelection::combineGeometrical()
+{
+ if (myTmpContext.get() || myTmpSubShape.get())
+ return;
+ TDF_Label aSelLab = selectionLabel();
+ if (aSelLab.IsAttribute(kINVALID_SELECTION) || !myRef.isInitialized())
+ return;
+
+ if (aSelLab.IsAttribute(kCIRCLE_CENTER) || aSelLab.IsAttribute(kELLIPSE_CENTER1) ||
+ aSelLab.IsAttribute(kELLIPSE_CENTER2) || aSelLab.IsAttribute(kSIMPLE_REF_ID))
+ return;
+
+ if (aSelLab.IsAttribute(kPART_REF_ID)) {
+ ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(context());
+ if (!aPart.get() || !aPart->isActivated())
+ return; // postponed naming needed
+ Handle(TDataStd_Integer) anIndex;
+ if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
+ if (anIndex->Get()) { // special selection attribute was created, use it
+ std::wstring aNewName;
+ aPart->combineGeometrical(anIndex->Get(), aNewName);
+ TDataStd_Name::Set(aSelLab, aNewName.c_str());
+ }
+ }
+ return;
+ }
+
+ std::shared_ptr<Model_ResultConstruction> aConstr =
+ std::dynamic_pointer_cast<Model_ResultConstruction>(context());
+ if (aConstr.get())
+ return;
+ FeaturePtr aFeature = contextFeature();
+ if (aFeature.get())
+ return;
+
+ Selector_Selector aSelector(aSelLab, baseDocumentLab());
+ TopoDS_Shape aContextShape = context()->shape()->impl<TopoDS_Shape>();
+ if (aSelector.restore(aContextShape)) {
+ aSelector.combineGeometrical(aContextShape);
+ }
+}
+
+TDF_Label Model_AttributeSelection::baseDocumentLab()
+{
+ if (ModelAPI_Session::get()->moduleDocument() != owner()->document())
+ return std::dynamic_pointer_cast<Model_Document>
+ (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel();
+ static TDF_Label anEmpty;
+ return anEmpty;
+}
+
+void Model_AttributeSelection::reset()
+{
+ ModelAPI_AttributeSelection::reset();
+ myRef.reset();
+}