+void Model_AttributeSelectionList::append(const std::string& theType,
+ const std::string& theContextName, const int theIndex)
+{
+ int aNewTag = mySize->Get() + 1;
+ TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
+
+ std::shared_ptr<Model_AttributeSelection> aNewAttr =
+ std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
+ if (owner()) {
+ aNewAttr->setObject(owner());
+ aNewAttr->setParent(this);
+ }
+ aNewAttr->setID(id());
+ mySize->Set(aNewTag);
+ aNewAttr->selectSubShape(theType, theContextName, theIndex);
+ owner()->data()->sendAttributeUpdated(this);
+}
+
+void Model_AttributeSelectionList::removeTemporaryValues()
+{
+ if (myTmpAttr.get()) {
+ myTmpAttr.reset();
+ }
+}
+
+void Model_AttributeSelectionList::removeLast()
+{
+ int anOldSize = mySize->Get();
+ if (anOldSize != 0) {
+ mySize->Set(anOldSize - 1);
+ TDF_Label aLab = mySize->Label().FindChild(anOldSize);
+ std::shared_ptr<Model_AttributeSelection> aOldAttr =
+ std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
+ aOldAttr->setObject(owner());
+ REMOVE_BACK_REF(aOldAttr->context());
+ aLab.ForgetAllAttributes(Standard_True);
+ myTmpAttr.reset();
+ owner()->data()->sendAttributeUpdated(this);
+ }
+}
+
+// copies named shapes: we need the implementation of this
+static void CopyNS(const Handle(TNaming_NamedShape)& theFrom,
+ const Handle(TDF_Attribute)& theTo)
+{
+ TDF_Label aLab = theTo->Label();
+ TNaming_Builder B(aLab);
+
+ // TNaming_Iterator iterates in reversed way than it was put in Builder, so, keep it in container
+ // and iterate from end to begin
+ NCollection_List<TopoDS_Shape> aOlds;
+ NCollection_List<TopoDS_Shape> aNews;
+ NCollection_List<TNaming_Evolution> aStatuses;
+
+ TNaming_Iterator anIt (theFrom);
+ for ( ;anIt.More() ; anIt.Next()) {
+ aOlds.Prepend(anIt.OldShape());
+ aNews.Prepend(anIt.NewShape());
+ aStatuses.Prepend(anIt.Evolution());
+ }
+
+ NCollection_List<TopoDS_Shape>::Iterator aOldIter(aOlds);
+ NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNews);
+ NCollection_List<TNaming_Evolution>::Iterator aStatIter(aStatuses);
+ for(; aOldIter.More(); aOldIter.Next(), aNewIter.Next(), aStatIter.Next()) {
+ switch (aStatIter.Value()) {
+ case TNaming_PRIMITIVE :
+ B.Generated(aNewIter.Value());
+ break;
+ case TNaming_GENERATED :
+ B.Generated(aOldIter.Value(), aNewIter.Value());
+ break;
+ case TNaming_MODIFY :
+ B.Modify(aOldIter.Value(), aNewIter.Value());
+ break;
+ case TNaming_DELETE :
+ B.Delete (aOldIter.Value());
+ break;
+ case TNaming_SELECTED :
+ B.Select(aNewIter.Value(), aOldIter.Value());
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+/// makes copy of all attributes on the given label and all sub-labels
+static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
+ TDF_AttributeIterator anAttrIter(theSource);
+ for(; anAttrIter.More(); anAttrIter.Next()) {
+ Handle(TDF_Attribute) aTargetAttr;
+ if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
+ // create a new attribute if not yet exists in the destination
+ aTargetAttr = anAttrIter.Value()->NewEmpty();
+ theDestination.AddAttribute(aTargetAttr);
+ }
+ // for named shape copy exact shapes (in NamedShape Paste method the CopyTool is used)
+ Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(anAttrIter.Value());
+ if (aNS.IsNull()) {
+ // no special relocation, empty map, but self-relocation is on: copy references w/o changes
+ Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
+ anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
+ } else {
+ CopyNS(aNS, aTargetAttr);
+ }
+ }
+ // copy the sub-labels content
+ TDF_ChildIterator aSubLabsIter(theSource);
+ for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
+ copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
+ }
+}
+
+void Model_AttributeSelectionList::remove(const std::set<int>& theIndices)
+{
+ int anOldSize = mySize->Get();
+ int aRemoved = 0;
+ // iterate one by one and shifting the removed indicies
+ for(int aCurrent = 0; aCurrent < anOldSize; aCurrent++) {
+ if (theIndices.find(aCurrent) == theIndices.end()) { // not removed
+ if (aRemoved) { // but must be shifted to the removed position
+ TDF_Label aSource = mySize->Label().FindChild(aCurrent + 1);
+ TDF_Label aDest = mySize->Label().FindChild(aCurrent + 1 - aRemoved);
+ copyAttrs(aSource, aDest);
+ // remove the moved source
+ aSource.ForgetAllAttributes(Standard_True);
+ }
+ } else { // this is removed, so remove everything from this label
+ TDF_Label aLab = mySize->Label().FindChild(aCurrent + 1);
+ std::shared_ptr<Model_AttributeSelection> aOldAttr =
+ std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLab));
+ aOldAttr->setObject(owner());
+ REMOVE_BACK_REF(aOldAttr->context());
+ aLab.ForgetAllAttributes(Standard_True);
+ myTmpAttr.reset();
+ aRemoved++;
+ }
+ }
+ if (aRemoved) { // remove was performed, so, update the size and this attribute
+ mySize->Set(anOldSize - aRemoved);
+ owner()->data()->sendAttributeUpdated(this);
+ }
+}
+