1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Model_AttributeRefList.h"
22 #include "Model_Application.h"
23 #include "Model_Data.h"
24 #include "Model_Objects.h"
25 #include <ModelAPI_Feature.h>
26 #include <TDF_ListIteratorOfLabelList.hxx>
27 #include <TDF_Tool.hxx>
28 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
30 void Model_AttributeRefList::append(ObjectPtr theObject)
32 if (owner()->document() == theObject->document()) {
33 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
34 myRef->Append(aData->label().Father()); // store label of the object
35 } else if (theObject.get() && theObject->data()->isValid()) { // reference to the other document
36 myRef->Append(myRef->Label());
37 // if these attributes exist, the link is external: keep reference to access the label
38 std::ostringstream anIdString; // string with document Id
39 anIdString<<theObject->document()->id();
40 myExtDocRef->Append(anIdString.str().c_str());
41 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
42 TCollection_AsciiString anEntry;
43 TDF_Tool::Entry(aData->label().Father(), anEntry);
44 myExtDocRef->Append(anEntry);
45 } else return; // something is wrong
48 myHashObjects.insert(theObject);
49 myHashIndex[myRef->Extent() - 1] = theObject;
50 myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = theObject;
53 // do it before the transaction finish to make just created/removed objects know dependencies
54 // and reference from composite feature is removed automatically
55 ADD_BACK_REF(theObject);
56 owner()->data()->sendAttributeUpdated(this);
59 void Model_AttributeRefList::remove(ObjectPtr theObject)
62 if (theObject.get() != NULL) {
63 if (owner()->document() == theObject->document()) {
64 std::shared_ptr<Model_Data> aData;
65 aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
66 myRef->Remove(aData->label().Father());
67 REMOVE_BACK_REF(theObject);
68 owner()->data()->sendAttributeUpdated(this);
71 // create new lists because for the current moment remove one of the duplicated elements
72 // from the list is buggy
73 TDF_LabelList anOldList = myRef->List();
75 TDataStd_ListOfExtendedString anOldExts = myExtDocRef->List();
78 std::ostringstream anIdString; // string with document Id
79 anIdString<<theObject->document()->id();
80 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
81 TCollection_AsciiString anEntry;
82 TDF_Tool::Entry(aData->label().Father(), anEntry);
84 TDataStd_ListIteratorOfListOfExtendedString anExtIter(anOldExts);
85 for (TDF_ListIteratorOfLabelList aLIter(anOldList); aLIter.More(); aLIter.Next()) {
86 if (aLIter.Value() == myRef->Label()) {
87 if (anExtIter.Value() == anIdString.str().c_str()) {
88 TDataStd_ListIteratorOfListOfExtendedString anExtIter2 = anExtIter;
90 if (anExtIter2.Value() == anEntry) { // fully matches, so, remove(don't copy)
95 myExtDocRef->Append(anExtIter.Value());
97 myExtDocRef->Append(anExtIter.Value());
100 myRef->Append(aLIter.Value());
103 REMOVE_BACK_REF(theObject);
104 owner()->data()->sendAttributeUpdated(this);
109 else { // in case of empty object remove, the first empty object is removed from the list
110 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
111 owner()->document());
113 const TDF_LabelList& aList = myRef->List();
114 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
115 ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
116 if (anObj.get() == NULL) {
117 myRef->Remove(aLIter.Value());
118 REMOVE_BACK_REF(theObject);
119 owner()->data()->sendAttributeUpdated(this);
127 void Model_AttributeRefList::clear()
129 std::list<ObjectPtr> anOldList = list();
131 std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
132 for(; anOldIter != anOldList.end(); anOldIter++) {
133 REMOVE_BACK_REF((*anOldIter));
135 myExtDocRef->Clear();
137 owner()->data()->sendAttributeUpdated(this);
140 int Model_AttributeRefList::size(const bool theWithEmpty) const
143 return myRef->Extent();
146 return int(myHashIndexNoEmpty.size());
149 const TDF_LabelList& aList = myRef->List();
150 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
151 if (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()) aResult++;
156 bool Model_AttributeRefList::isInitialized()
158 if (size(false) == 0) {
159 // empty list is not initialized list: sketch will be not valid after add/undo
162 return ModelAPI_AttributeRefList::isInitialized();
165 ObjectPtr Model_AttributeRefList::iteratedObject(TDF_ListIteratorOfLabelList& theLIter,
166 TDataStd_ListIteratorOfListOfExtendedString& theExtIter,
167 std::shared_ptr<Model_Document> theDoc) const
170 if (!theLIter.Value().IsNull() && !theLIter.Value().IsRoot()) {
171 if (theLIter.Value() == myRef->Label()) { // external document object
172 int anID = atoi(TCollection_AsciiString(theExtIter.Value()).ToCString());
174 DocumentPtr aRefDoc = Model_Application::getApplication()->document(anID);
176 std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
178 TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
179 TCollection_AsciiString(theExtIter.Value()).ToCString(), aRefLab);
180 if (!aRefLab.IsNull()) {
181 anObj = aDR->objects()->object(aRefLab);
185 } else { // internal document object
186 anObj = theDoc->objects()->object(theLIter.Value());
192 std::list<ObjectPtr> Model_AttributeRefList::list()
195 std::list<ObjectPtr> aResult;
196 std::map<int, ObjectPtr>::iterator aHashIter = myHashIndex.begin();
197 for(; aHashIter != myHashIndex.end(); aHashIter++) {
198 aResult.push_back(aHashIter->second);
203 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
209 return myHashObjects.count(theObj) != 0;
212 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty)
215 std::map<int, ObjectPtr>::iterator aFind;
217 aFind = myHashIndex.find(theIndex);
218 if (aFind == myHashIndex.end())
221 aFind = myHashIndexNoEmpty.find(theIndex);
222 if (aFind == myHashIndexNoEmpty.end())
225 return aFind->second;
228 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
230 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
231 owner()->document());
233 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
234 if (aData.get() && aData->isValid()) {
235 TDF_Label aCurrentLab = aData->label().Father();
237 if (theNew.get() && theNew->data()->isValid()) { // the new may be null
238 std::shared_ptr<Model_Data> aNewData =
239 std::dynamic_pointer_cast<Model_Data>(theNew->data());
240 aNewLab = aNewData->label().Father();
242 aNewLab = aCurrentLab.Root(); // root means null object
244 // do the substitution
246 ADD_BACK_REF(theNew);
247 if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
248 myRef->Remove(aCurrentLab);
249 REMOVE_BACK_REF(theCurrent);
251 owner()->data()->sendAttributeUpdated(this);
256 void Model_AttributeRefList::removeLast()
258 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
259 owner()->document());
260 if (aDoc && !myRef->IsEmpty()) {
261 ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
264 myRef->Remove(myRef->Last());
265 REMOVE_BACK_REF(anObj);
266 owner()->data()->sendAttributeUpdated(this);
271 void Model_AttributeRefList::remove(const std::set<int>& theIndices)
273 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
274 owner()->document());
275 if (aDoc && !myRef->IsEmpty()) {
276 // collect labels that will be removed
277 TDF_LabelList aLabelsToRemove;
278 TDF_ListIteratorOfLabelList aLabIter(myRef->List());
279 for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) {
280 if (theIndices.find(aCurrent) != theIndices.end())
281 aLabelsToRemove.Append(aLabIter.Value());
284 for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) {
285 ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value());
287 myRef->Remove(aLabIter.Value());
288 REMOVE_BACK_REF(anObj);
291 if (!aLabelsToRemove.IsEmpty()) {
293 owner()->data()->sendAttributeUpdated(this);
298 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
304 void Model_AttributeRefList::reinit()
306 myIsInitialized = myLab.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
307 if (!myIsInitialized) {
308 myRef = TDataStd_ReferenceList::Set(myLab);
310 if (!myLab.FindAttribute(TDataStd_ExtStringList::GetID(), myExtDocRef)) {
311 myExtDocRef = TDataStd_ExtStringList::Set(myLab);
316 void Model_AttributeRefList::createHash()
321 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
322 owner()->document());
324 const TDF_LabelList& aList = myRef->List();
325 TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
326 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
327 ObjectPtr anObj = iteratedObject(aLIter, anExtIter, aDoc);
328 myHashIndex[int(myHashIndex.size())] = anObj;
330 myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = anObj;
331 myHashObjects.insert(anObj);
334 if (!myHashObjects.empty()) // on open document with multi-rotation referenced have no results
340 void Model_AttributeRefList::eraseHash()
342 myHashObjects.clear();
344 myHashIndexNoEmpty.clear();