+bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
+{
+ if(!theObj.get()) {
+ return false;
+ }
+ if (theObj->document() == owner()->document()) { // this document object
+ 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>(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;
+ }
+ }
+ }
+ }
+ } else { // external document object
+ // create new lists because for the current moment remove one of the duplicated elements
+ // from the list is buggy
+ std::ostringstream anIdString; // string with document Id
+ anIdString<<theObj->document()->id();
+ std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
+ TCollection_AsciiString anEntry;
+ TDF_Tool::Entry(aData->label().Father(), anEntry);
+ bool aFound = false;
+ TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
+ for (; anExtIter.More(); anExtIter.Next()) {
+ if (anExtIter.Value() == anIdString.str().c_str()) {
+ anExtIter.Next();
+ if (anExtIter.Value() == anEntry) { // fully maches
+ return true;
+ }
+ } else {
+ anExtIter.Next();
+ }
+ }
+ }
+ return false;
+}
+
+ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const
+{
+ std::shared_ptr<Model_Document> aDoc =
+ std::dynamic_pointer_cast<Model_Document>(owner()->document());
+ if (aDoc) {
+ int anIndex = -1;
+ TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
+ for (TDF_ListIteratorOfLabelList aLIter(myRef->List()); aLIter.More(); aLIter.Next()) {
+ if (theWithEmpty || (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()))
+ anIndex++;
+ if (anIndex == theIndex) {
+ return iteratedObject(aLIter, anExtIter, aDoc);
+ }
+ if (aLIter.Value() == myRef->Label()) {
+ anExtIter.Next();
+ anExtIter.Next();
+ }
+ }
+ }
+ return ObjectPtr();
+}
+
+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
+ 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<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+ owner()->document());
+ if (aDoc) {
+ std::shared_ptr<Model_Data> aData1 = std::dynamic_pointer_cast<Model_Data>(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<Model_Data> aData2 =
+ std::dynamic_pointer_cast<Model_Data>(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<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()) {
+ 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()) {
+ // 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);
+ }
+ }
+}
+