1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_AttributeRefList.cxx
5 // Author: Mikhail PONIKAROV
7 #include "Model_AttributeRefList.h"
8 #include "Model_Application.h"
9 #include "Model_Data.h"
10 #include "Model_Objects.h"
11 #include <ModelAPI_Feature.h>
12 #include <TDF_ListIteratorOfLabelList.hxx>
13 #include <TDF_Tool.hxx>
14 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
16 void Model_AttributeRefList::append(ObjectPtr theObject)
18 if (owner()->document() == theObject->document()) {
19 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
20 myRef->Append(aData->label().Father()); // store label of the object
21 } else if (theObject.get() && theObject->data()->isValid()) { // reference to the other document
22 myRef->Append(myRef->Label());
23 // if these attributes exist, the link is external: keep reference to access the label
24 std::ostringstream anIdString; // string with document Id
25 anIdString<<theObject->document()->id();
26 myExtDocRef->Append(anIdString.str().c_str());
27 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
28 TCollection_AsciiString anEntry;
29 TDF_Tool::Entry(aData->label().Father(), anEntry);
30 myExtDocRef->Append(anEntry);
31 } else return; // something is wrong
33 // do it before the transaction finish to make just created/removed objects know dependencies
34 // and reference from composite feature is removed automatically
35 ADD_BACK_REF(theObject);
36 owner()->data()->sendAttributeUpdated(this);
39 void Model_AttributeRefList::remove(ObjectPtr theObject)
41 if (theObject.get() != NULL) {
42 if (owner()->document() == theObject->document()) {
43 std::shared_ptr<Model_Data> aData;
44 aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
45 myRef->Remove(aData->label().Father());
46 REMOVE_BACK_REF(theObject);
47 owner()->data()->sendAttributeUpdated(this);
49 // create new lists because for the current moment remove one of the duplicated elements
50 // from the list is buggy
51 TDF_LabelList anOldList = myRef->List();
53 TDataStd_ListOfExtendedString anOldExts = myExtDocRef->List();
56 std::ostringstream anIdString; // string with document Id
57 anIdString<<theObject->document()->id();
58 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
59 TCollection_AsciiString anEntry;
60 TDF_Tool::Entry(aData->label().Father(), anEntry);
62 TDataStd_ListIteratorOfListOfExtendedString anExtIter(anOldExts);
63 for (TDF_ListIteratorOfLabelList aLIter(anOldList); aLIter.More(); aLIter.Next()) {
64 if (aLIter.Value() == myRef->Label()) {
65 if (anExtIter.Value() == anIdString.str().c_str()) {
66 TDataStd_ListIteratorOfListOfExtendedString anExtIter2 = anExtIter;
68 if (anExtIter2.Value() == anEntry) { // fully maches, so, remove(don't copy)
73 myExtDocRef->Append(anExtIter.Value());
75 myExtDocRef->Append(anExtIter.Value());
78 myRef->Append(aLIter.Value());
81 REMOVE_BACK_REF(theObject);
82 owner()->data()->sendAttributeUpdated(this);
86 else { // in case of empty object remove, the first empty object is removed from the list
87 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
90 const TDF_LabelList& aList = myRef->List();
91 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
92 ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
93 if (anObj.get() == NULL) {
94 myRef->Remove(aLIter.Value());
95 REMOVE_BACK_REF(theObject);
96 owner()->data()->sendAttributeUpdated(this);
104 void Model_AttributeRefList::clear()
106 std::list<ObjectPtr> anOldList = list();
108 std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
109 for(; anOldIter != anOldList.end(); anOldIter++) {
110 REMOVE_BACK_REF((*anOldIter));
112 myExtDocRef->Clear();
113 owner()->data()->sendAttributeUpdated(this);
116 int Model_AttributeRefList::size(const bool theWithEmpty) const
119 return myRef->Extent();
121 const TDF_LabelList& aList = myRef->List();
122 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
123 if (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()) aResult++;
128 bool Model_AttributeRefList::isInitialized()
130 if (size(false) == 0) {
131 // empty list is not initialized list: sketch will be not valid after add/undo
134 return ModelAPI_AttributeRefList::isInitialized();
137 ObjectPtr Model_AttributeRefList::iteratedObject(TDF_ListIteratorOfLabelList& theLIter,
138 TDataStd_ListIteratorOfListOfExtendedString& theExtIter,
139 std::shared_ptr<Model_Document> theDoc) const
142 if (!theLIter.Value().IsNull() && !theLIter.Value().IsRoot()) {
143 if (theLIter.Value() == myRef->Label()) { // external document object
144 int anID = atoi(TCollection_AsciiString(theExtIter.Value()).ToCString());
146 DocumentPtr aRefDoc = Model_Application::getApplication()->document(anID);
148 std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
150 TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
151 TCollection_AsciiString(theExtIter.Value()).ToCString(), aRefLab);
152 if (!aRefLab.IsNull()) {
153 anObj = aDR->objects()->object(aRefLab);
157 } else { // internal document object
158 anObj = theDoc->objects()->object(theLIter.Value());
164 std::list<ObjectPtr> Model_AttributeRefList::list()
166 std::list<ObjectPtr> aResult;
167 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
168 owner()->document());
170 const TDF_LabelList& aList = myRef->List();
171 TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
172 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
173 aResult.push_back(iteratedObject(aLIter, anExtIter, aDoc));
179 bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
184 if (theObj->document() == owner()->document()) { // this document object
185 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
186 owner()->document());
188 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
189 if (aData.get() && aData->isValid()) {
190 TDF_Label anObjLab = aData->label().Father();
191 const TDF_LabelList& aList = myRef->List();
192 for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
193 if (aLIter.Value().IsEqual(anObjLab)) {
199 } else { // external document object
200 // create new lists because for the current moment remove one of the duplicated elements
201 // from the list is buggy
202 std::ostringstream anIdString; // string with document Id
203 anIdString<<theObj->document()->id();
204 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
205 TCollection_AsciiString anEntry;
206 TDF_Tool::Entry(aData->label().Father(), anEntry);
208 TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
209 for (; anExtIter.More(); anExtIter.Next()) {
210 if (anExtIter.Value() == anIdString.str().c_str()) {
212 if (anExtIter.Value() == anEntry) { // fully maches
223 ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const
225 std::shared_ptr<Model_Document> aDoc =
226 std::dynamic_pointer_cast<Model_Document>(owner()->document());
229 TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
230 for (TDF_ListIteratorOfLabelList aLIter(myRef->List()); aLIter.More(); aLIter.Next()) {
231 if (theWithEmpty || (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()))
233 if (anIndex == theIndex) {
234 return iteratedObject(aLIter, anExtIter, aDoc);
236 if (aLIter.Value() == myRef->Label()) {
245 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
247 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
248 owner()->document());
250 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
251 if (aData.get() && aData->isValid()) {
252 TDF_Label aCurrentLab = aData->label().Father();
254 if (theNew.get() && theNew->data()->isValid()) { // the new may be null
255 std::shared_ptr<Model_Data> aNewData =
256 std::dynamic_pointer_cast<Model_Data>(theNew->data());
257 aNewLab = aNewData->label().Father();
259 aNewLab = aCurrentLab.Root(); // root means null object
261 // do the substitution
262 ADD_BACK_REF(theNew);
263 if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
264 myRef->Remove(aCurrentLab);
265 REMOVE_BACK_REF(theCurrent);
267 owner()->data()->sendAttributeUpdated(this);
272 void Model_AttributeRefList::exchange(const ObjectPtr& theObject1, const ObjectPtr& theObject2)
274 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
275 owner()->document());
277 std::shared_ptr<Model_Data> aData1 = std::dynamic_pointer_cast<Model_Data>(theObject1->data());
278 if (aData1.get() && aData1->isValid()) {
279 TDF_Label aLab1 = aData1->label().Father();
280 if (theObject2.get() && theObject2->data()->isValid()) { // the new may be null
281 std::shared_ptr<Model_Data> aData2 =
282 std::dynamic_pointer_cast<Model_Data>(theObject2->data());
283 if (aData2.get() && aData2->isValid()) {
284 TDF_Label aLab2 = aData2->label().Father();
285 // do the substitution: use the temporary label, as usually in exchange
286 TDF_Label aTmpLab = aLab1.Root();
287 if (myRef->InsertAfter(aTmpLab, aLab1)) {
288 myRef->Remove(aLab1);
290 if (myRef->InsertAfter(aLab1, aLab2)) {
291 myRef->Remove(aLab2);
293 if (myRef->InsertAfter(aLab2, aTmpLab)) {
294 myRef->Remove(aTmpLab);
296 owner()->data()->sendAttributeUpdated(this);
303 void Model_AttributeRefList::removeLast()
305 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
306 owner()->document());
307 if (aDoc && !myRef->IsEmpty()) {
308 ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
310 myRef->Remove(myRef->Last());
311 REMOVE_BACK_REF(anObj);
312 owner()->data()->sendAttributeUpdated(this);
317 void Model_AttributeRefList::remove(const std::set<int>& theIndices)
319 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
320 owner()->document());
321 if (aDoc && !myRef->IsEmpty()) {
322 // collet labels that will be removed
323 TDF_LabelList aLabelsToRemove;
324 TDF_ListIteratorOfLabelList aLabIter(myRef->List());
325 for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) {
326 if (theIndices.find(aCurrent) != theIndices.end())
327 aLabelsToRemove.Append(aLabIter.Value());
330 for(aLabIter.Initialize(aLabelsToRemove); aLabIter.More(); aLabIter.Next()) {
331 ObjectPtr anObj = aDoc->objects()->object(aLabIter.Value());
333 myRef->Remove(aLabIter.Value());
334 REMOVE_BACK_REF(anObj);
337 if (!aLabelsToRemove.IsEmpty()) {
338 owner()->data()->sendAttributeUpdated(this);
343 Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
349 void Model_AttributeRefList::reinit()
351 myIsInitialized = myLab.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
352 if (!myIsInitialized) {
353 myRef = TDataStd_ReferenceList::Set(myLab);
355 if (!myLab.FindAttribute(TDataStd_ExtStringList::GetID(), myExtDocRef)) {
356 myExtDocRef = TDataStd_ExtStringList::Set(myLab);