X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModel%2FModel_AttributeRefList.cpp;h=a1e0aeb666779046950ce637308c46a517661ec3;hb=061a63480f6840b6d945f7744b3b972e2d4cb25d;hp=b7719ca78f5011e5b4b32980831b452729c773f8;hpb=506840f28e9ee8b4c520230193bcb28835757f59;p=modules%2Fshaper.git diff --git a/src/Model/Model_AttributeRefList.cpp b/src/Model/Model_AttributeRefList.cpp index b7719ca78..a1e0aeb66 100644 --- a/src/Model/Model_AttributeRefList.cpp +++ b/src/Model/Model_AttributeRefList.cpp @@ -58,16 +58,24 @@ void Model_AttributeRefList::clear() for(; anOldIter != anOldList.end(); anOldIter++) { REMOVE_BACK_REF((*anOldIter)); } + owner()->data()->sendAttributeUpdated(this); } -int Model_AttributeRefList::size() const +int Model_AttributeRefList::size(const bool theWithEmpty) const { - return myRef->Extent(); + if (theWithEmpty) + return myRef->Extent(); + int aResult = 0; + const TDF_LabelList& aList = myRef->List(); + for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) { + if (!aLIter.Value().IsNull()) aResult++; + } + return aResult; } bool Model_AttributeRefList::isInitialized() { - if (size() == 0) { // empty list is not initialized list: sketch will be not valid after add/undo + if (size(false) == 0) { // empty list is not initialized list: sketch will be not valid after add/undo return false; } return ModelAPI_AttributeRefList::isInitialized(); @@ -81,28 +89,155 @@ list Model_AttributeRefList::list() if (aDoc) { const TDF_LabelList& aList = myRef->List(); for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) { - ObjectPtr anObj = aDoc->objects()->object(aLIter.Value()); + ObjectPtr anObj; + if (!aLIter.Value().IsNull()) + anObj = aDoc->objects()->object(aLIter.Value()); aResult.push_back(anObj); } } return aResult; } -ObjectPtr Model_AttributeRefList::object(const int theIndex) const +bool Model_AttributeRefList::isInList(const ObjectPtr& theObj) +{ + if(!theObj.get()) { + return false; + } + std::list aResult; + std::shared_ptr aDoc = std::dynamic_pointer_cast( + owner()->document()); + if (aDoc) { + std::shared_ptr aData = std::dynamic_pointer_cast(theObj->data()); + if (aData.get() && aData->isValid()) { + TDF_Label anObjLab = aData->label().Father(); + const TDF_LabelList& aList = myRef->List(); + for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) { + if (aLIter.Value().IsEqual(anObjLab)) { + return true; + } + } + } + } + return false; +} + +ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const { std::shared_ptr aDoc = std::dynamic_pointer_cast( owner()->document()); if (aDoc) { const TDF_LabelList& aList = myRef->List(); - int anIndex = 0; - for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) { - if (anIndex == theIndex) + int anIndex = -1; + for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) { + if (theWithEmpty || !aLIter.Value().IsNull()) + anIndex++; + if (anIndex == theIndex) { + if (aLIter.Value().IsNull()) { // null label => null sub + return ObjectPtr(); + } return aDoc->objects()->object(aLIter.Value()); + } } } return ObjectPtr(); } +void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew) +{ + std::shared_ptr aDoc = std::dynamic_pointer_cast( + owner()->document()); + if (aDoc) { + std::shared_ptr aData = std::dynamic_pointer_cast(theCurrent->data()); + if (aData.get() && aData->isValid()) { + TDF_Label aCurrentLab = aData->label().Father(); + TDF_Label aNewLab; + if (theNew.get() && theNew->data()->isValid()) { // the new may be null + std::shared_ptr aNewData = + std::dynamic_pointer_cast(theNew->data()); + aNewLab = aNewData->label().Father(); + } + // do the substitution + ADD_BACK_REF(theNew); + if (myRef->InsertAfter(aNewLab, aCurrentLab)) { + myRef->Remove(aCurrentLab); + REMOVE_BACK_REF(theCurrent); + } + owner()->data()->sendAttributeUpdated(this); + } + } +} + +void Model_AttributeRefList::exchange(const ObjectPtr& theObject1, const ObjectPtr& theObject2) +{ + std::shared_ptr aDoc = std::dynamic_pointer_cast( + owner()->document()); + if (aDoc) { + std::shared_ptr aData1 = std::dynamic_pointer_cast(theObject1->data()); + if (aData1.get() && aData1->isValid()) { + TDF_Label aLab1 = aData1->label().Father(); + if (theObject2.get() && theObject2->data()->isValid()) { // the new may be null + std::shared_ptr aData2 = + std::dynamic_pointer_cast(theObject2->data()); + if (aData2.get() && aData2->isValid()) { + TDF_Label aLab2 = aData2->label().Father(); + // do the substitution: use the temporary label, as usually in exchange + TDF_Label aTmpLab = aLab1.Root(); + if (myRef->InsertAfter(aTmpLab, aLab1)) { + myRef->Remove(aLab1); + } + if (myRef->InsertAfter(aLab1, aLab2)) { + myRef->Remove(aLab2); + } + if (myRef->InsertAfter(aLab2, aTmpLab)) { + myRef->Remove(aTmpLab); + } + owner()->data()->sendAttributeUpdated(this); + } + } + } + } +} + +void Model_AttributeRefList::removeLast() +{ + std::shared_ptr aDoc = std::dynamic_pointer_cast( + owner()->document()); + if (aDoc && !myRef->IsEmpty()) { + ObjectPtr anObj = aDoc->objects()->object(myRef->Last()); + if (anObj.get()) { + myRef->Remove(myRef->Last()); + REMOVE_BACK_REF(anObj); + owner()->data()->sendAttributeUpdated(this); + } + } +} + +void Model_AttributeRefList::remove(const std::set& theIndices) +{ + std::shared_ptr aDoc = std::dynamic_pointer_cast( + owner()->document()); + if (aDoc && !myRef->IsEmpty()) { + // collet labels that will be removed + TDF_LabelList aLabelsToRemove; + TDF_ListIteratorOfLabelList aLabIter(myRef->List()); + for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) { + if (theIndices.find(aCurrent) != theIndices.end()) + aLabelsToRemove.Append(aLabIter.Value()); + } + // remove labels + for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) { + ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value()); + if (anObj.get()) { + myRef->Remove(aLabIter.Value()); + REMOVE_BACK_REF(anObj); + } + } + if (!aLabelsToRemove.IsEmpty()) { + owner()->data()->sendAttributeUpdated(this); + } + } +} + Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel) { myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;