+bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
+{
+ if(!theObj.get()) {
+ return false;
+ }
+ createHash();
+ return myHashObjects.count(theObj) != 0;
+}
+
+ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty)
+{
+ createHash();
+ std::map<int, ObjectPtr>::iterator aFind;
+ if (theWithEmpty) {
+ aFind = myHashIndex.find(theIndex);
+ if (aFind == myHashIndex.end())
+ return ObjectPtr();
+ } else {
+ aFind = myHashIndexNoEmpty.find(theIndex);
+ if (aFind == myHashIndexNoEmpty.end())
+ return ObjectPtr();
+ }
+ return aFind->second;
+}
+
+void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
+{
+ std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+ owner()->document());
+ if (aDoc) {
+ std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(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<Model_Data> aNewData =
+ std::dynamic_pointer_cast<Model_Data>(theNew->data());
+ aNewLab = aNewData->label().Father();
+ } else {
+ aNewLab = aCurrentLab.Root(); // root means null object
+ }
+ // do the substitution
+ eraseHash();
+ ADD_BACK_REF(theNew);
+ if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
+ myRef->Remove(aCurrentLab);
+ REMOVE_BACK_REF(theCurrent);
+ }
+ owner()->data()->sendAttributeUpdated(this);
+ }
+ }
+}
+
+void Model_AttributeRefList::removeLast()
+{
+ std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+ owner()->document());
+ if (aDoc && !myRef->IsEmpty()) {
+ ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
+ if (anObj.get()) {
+ eraseHash();
+ myRef->Remove(myRef->Last());
+ REMOVE_BACK_REF(anObj);
+ owner()->data()->sendAttributeUpdated(this);
+ }
+ }
+}
+
+void Model_AttributeRefList::remove(const std::set<int>& theIndices)
+{
+ std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+ owner()->document());
+ if (aDoc && !myRef->IsEmpty()) {
+ // collect 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()) {
+ eraseHash();
+ owner()->data()->sendAttributeUpdated(this);
+ }
+ }
+}
+