+ std::map<std::string, std::list<TDF_Label> >::iterator aFind = myNamingNames.find(theName);
+
+ if (aFind != myNamingNames.end()) { // to avoid duplicate-labels
+ // to keep correct order inspite of history line management
+ std::list<TDF_Label>::iterator anAddAfterThis = aFind->second.end();
+ FeaturePtr anAddedFeature = featureByLab(theLabel);
+ std::list<TDF_Label>::iterator aLabIter = aFind->second.begin();
+ while(aLabIter != aFind->second.end()) {
+ if (theLabel.IsEqual(*aLabIter)) {
+ std::list<TDF_Label>::iterator aTmpIter = aLabIter;
+ aLabIter++;
+ aFind->second.erase(aTmpIter);
+ } else {
+ FeaturePtr aCurFeature = featureByLab(*aLabIter);
+ if (aCurFeature.get() && anAddedFeature.get() &&
+ myObjs->isLater(anAddedFeature, aCurFeature))
+ anAddAfterThis = aLabIter;
+
+ aLabIter++;
+ }
+ }
+ if (anAddAfterThis != aFind->second.end()) {
+ anAddAfterThis++;
+ if (anAddAfterThis != aFind->second.end()) {
+ myNamingNames[theName].insert(anAddAfterThis, theLabel); // inserts before anAddAfterThis
+ return;
+ }
+ }
+ }
+ myNamingNames[theName].push_back(theLabel);
+}
+
+void Model_Document::changeNamingName(const std::string theOldName,
+ const std::string theNewName,
+ const TDF_Label& theLabel)
+{
+ std::map<std::string, std::list<TDF_Label> >::iterator aFind = myNamingNames.find(theOldName);
+ if (aFind != myNamingNames.end()) {
+ std::list<TDF_Label>::iterator aLabIter = aFind->second.begin();
+ for(; aLabIter != aFind->second.end(); aLabIter++) {
+ if (theLabel.IsEqual(*aLabIter)) { // found the label
+ myNamingNames[theNewName].push_back(theLabel);
+ if (aFind->second.size() == 1) { // only one element, so, just change the name
+ myNamingNames.erase(theOldName);
+ } else { // remove from the list
+ aFind->second.erase(aLabIter);
+ }
+ return;
+ }
+ }
+ }
+}
+
+TDF_Label Model_Document::findNamingName(std::string theName, ResultPtr theContext)
+{
+ std::map<std::string, std::list<TDF_Label> >::iterator aFind = myNamingNames.find(theName);
+ if (aFind != myNamingNames.end()) {
+ std::list<TDF_Label>::reverse_iterator aLabIter = aFind->second.rbegin();
+ for(; aLabIter != aFind->second.rend(); aLabIter++) {
+ if (theContext.get()) {
+ // context is defined and not like this, so, skip
+ if (theContext == myObjs->object(aLabIter->Father()))
+ return *aLabIter;
+ }
+ }
+ return *(aFind->second.rbegin()); // no more variannts, so, return the last
+ }
+ // not found exact name, try to find by sub-components
+ std::string::size_type aSlash = theName.rfind('/');
+ if (aSlash != std::string::npos) {
+ std::string anObjName = theName.substr(0, aSlash);
+ aFind = myNamingNames.find(anObjName);
+ if (aFind != myNamingNames.end()) {
+ TCollection_ExtendedString aSubName(theName.substr(aSlash + 1).c_str());
+ // iterate all possible same-named labels starting from the last one (the recent)
+ std::list<TDF_Label>::reverse_iterator aLabIter = aFind->second.rbegin();
+ for(; aLabIter != aFind->second.rend(); aLabIter++) {
+ if (theContext.get()) {
+ // context is defined and not like this, so, skip
+ if (theContext != myObjs->object(aLabIter->Father()))
+ continue;
+ }
+ // copy aSubName to avoid incorrect further processing after its suffix cutting
+ TCollection_ExtendedString aSubNameCopy(aSubName);
+ // searching sub-labels with this name
+ TDF_ChildIDIterator aNamesIter(*aLabIter, TDataStd_Name::GetID(), Standard_True);
+ for(; aNamesIter.More(); aNamesIter.Next()) {
+ Handle(TDataStd_Name) aName = Handle(TDataStd_Name)::DownCast(aNamesIter.Value());
+ if (aName->Get() == aSubNameCopy)
+ return aName->Label();
+ }
+ // If not found child label with the exact sub-name, then try to find compound with
+ // such sub-name without suffix.
+ Standard_Integer aSuffixPos = aSubNameCopy.SearchFromEnd('_');
+ if (aSuffixPos != -1 && aSuffixPos != aSubNameCopy.Length()) {
+ TCollection_ExtendedString anIndexStr = aSubNameCopy.Split(aSuffixPos);
+ aSubNameCopy.Remove(aSuffixPos);
+ aNamesIter.Initialize(*aLabIter, TDataStd_Name::GetID(), Standard_True);
+ for(; aNamesIter.More(); aNamesIter.Next()) {
+ Handle(TDataStd_Name) aName = Handle(TDataStd_Name)::DownCast(aNamesIter.Value());
+ if (aName->Get() == aSubNameCopy) {
+ return aName->Label();
+ }
+ }
+ // check also "this" label
+ Handle(TDataStd_Name) aName;
+ if (aLabIter->FindAttribute(TDataStd_Name::GetID(), aName)) {
+ if (aName->Get() == aSubNameCopy) {
+ return aName->Label();
+ }
+ }
+ }
+ }
+ // verify context's name is same as sub-component's and use context's label
+ if (aSubName.IsEqual(anObjName.c_str()))
+ return *(aFind->second.rbegin());
+ }
+ }
+ return TDF_Label(); // not found
+}
+
+bool Model_Document::isLaterByDep(FeaturePtr theThis, FeaturePtr theOther) {
+ // check dependencies first: if theOther depends on theThis, theThis is not later
+ std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
+ theOther->data()->referencesToObjects(aRefs);
+ std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
+ aRefIt = aRefs.begin();
+ for(; aRefIt != aRefs.end(); aRefIt++) {
+ std::list<ObjectPtr>::iterator aRefObjIt = aRefIt->second.begin();
+ for(; aRefObjIt != aRefIt->second.end(); aRefObjIt++) {
+ ObjectPtr aRefObj = *aRefObjIt;
+ if (aRefObj.get()) {
+ FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefObj);
+ if (!aRefFeat.get()) { // take feature of the result
+ aRefFeat = feature(std::dynamic_pointer_cast<ModelAPI_Result>(aRefObj));
+ }
+ if (aRefFeat.get()) {
+ if (aRefFeat == theThis)
+ return false; // other references to this, so other later than this
+ if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeat)) {
+ if (!isLaterByDep(theThis, aRefFeat)) // nested composites: recursion
+ return false;
+ }
+ }
+ }
+ }
+ }
+ return myObjs->isLater(theThis, theOther);
+}
+
+int Model_Document::numberOfNameInHistory(
+ const ObjectPtr& theNameObject, const TDF_Label& theStartFrom)
+{
+ std::map<std::string, std::list<TDF_Label> >::iterator aFind =
+ myNamingNames.find(theNameObject->data()->name());
+ if (aFind == myNamingNames.end() || aFind->second.size() < 2) {
+ return 1; // no need to specify the name by additional identifiers
+ }
+ // get the feature of the object for relative compare
+ FeaturePtr aStart = myObjs->feature(theStartFrom);
+ if (!aStart.get()) // strange, but can not find feature by the label
+ return 1;
+ // feature that contain result with this name
+ FeaturePtr aNameFeature;
+ ResultPtr aNameResult = std::dynamic_pointer_cast<ModelAPI_Result>(theNameObject);
+ if (aNameResult)
+ aNameFeature = myObjs->feature(aNameResult);
+ else
+ aNameFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theNameObject);
+ // iterate all labels with this name to find the nearest just before or equal relative
+ std::list<TDF_Label>::reverse_iterator aLabIter = aFind->second.rbegin();
+ for(; aLabIter != aFind->second.rend(); aLabIter++) {
+ FeaturePtr aLabFeat = featureByLab(*aLabIter);
+ if (!aLabFeat.get())
+ continue;
+ if (isLaterByDep(aStart, aLabFeat)) // skip also start: its result don't used
+ break;
+ }
+ int aResIndex = 1;
+ for(; aLabIter != aFind->second.rend(); aLabIter++) {
+ FeaturePtr aLabFeat = featureByLab(*aLabIter);
+ if (!aLabFeat.get())
+ continue;
+ if (aLabFeat == aNameFeature || isLaterByDep(aNameFeature, aLabFeat))
+ return aResIndex;
+ aResIndex++;
+ }
+ return aResIndex; // strange
+}
+
+ResultPtr Model_Document::findByName(
+ std::string& theName, std::string& theSubShapeName, bool& theUniqueContext)
+{
+ int aNumInHistory = 0;
+ std::string aName = theName;
+ ResultPtr aRes = myObjs->findByName(aName);
+ theUniqueContext = !(aRes.get() && myNamingNames.find(aName) != myNamingNames.end());
+ while(!aRes.get() && aName[0] == '_') { // this may be theContext with the history index
+ aNumInHistory++;
+ aName = aName.substr(1);
+ aRes = myObjs->findByName(aName);
+ }
+ if (aNumInHistory) {
+ std::map<std::string, std::list<TDF_Label> >::iterator aFind = myNamingNames.find(aName);
+ if (aFind != myNamingNames.end() && aFind->second.size() > aNumInHistory) {
+ std::list<TDF_Label>::reverse_iterator aLibIt = aFind->second.rbegin();
+ for(; aNumInHistory != 0; aNumInHistory--)
+ aLibIt++;
+ const TDF_Label& aResultLab = *aLibIt;
+ aRes = std::dynamic_pointer_cast<ModelAPI_Result>(myObjs->object(aResultLab.Father()));
+ if (aRes) { // modify the incoming names
+ if (!theSubShapeName.empty())
+ theSubShapeName = theSubShapeName.substr(theName.size() - aName.size());
+ theName = aName;
+ }
+ }
+ }
+ return aRes;
+}
+
+std::list<std::shared_ptr<ModelAPI_Feature> > Model_Document::allFeatures()
+{
+ return myObjs->allFeatures();
+}
+
+std::list<std::shared_ptr<ModelAPI_Object> > Model_Document::allObjects()
+{
+ return myObjs->allObjects();
+}
+
+void Model_Document::setActive(const bool theFlag)
+{
+ if (theFlag != myIsActive) {
+ myIsActive = theFlag;
+ // redisplay all the objects of this part
+ static Events_Loop* aLoop = Events_Loop::loop();
+ static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
+
+ for(int a = size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
+ object(ModelAPI_Feature::group(), a));
+ if (aFeature.get() && aFeature->data()->isValid()) {
+ std::list<ResultPtr> aResults;
+ ModelAPI_Tools::allResults(aFeature, aResults);
+ for (std::list<ResultPtr>::iterator aRes = aResults.begin();
+ aRes != aResults.end(); aRes++) {
+ ModelAPI_EventCreator::get()->sendUpdated(*aRes, aRedispEvent);
+ }
+ }
+ }
+ }
+}
+
+bool Model_Document::isActive() const
+{
+ return myIsActive;
+}
+
+int Model_Document::transactionID()
+{
+ Handle(TDataStd_Integer) anIndex;
+ if (!generalLabel().FindChild(TAG_CURRENT_TRANSACTION).
+ FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
+ anIndex = TDataStd_Integer::Set(generalLabel().FindChild(TAG_CURRENT_TRANSACTION), 1);
+ }
+ return anIndex->Get();
+}
+
+void Model_Document::incrementTransactionID()
+{
+ int aNewVal = transactionID() + 1;
+ TDataStd_Integer::Set(generalLabel().FindChild(TAG_CURRENT_TRANSACTION), aNewVal);
+}
+void Model_Document::decrementTransactionID()
+{
+ int aNewVal = transactionID() - 1;
+ TDataStd_Integer::Set(generalLabel().FindChild(TAG_CURRENT_TRANSACTION), aNewVal);