Salome HOME
Debug of Box macro feature to the updated architecture
[modules/shaper.git] / src / Model / Model_Data.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Data.hxx
4 // Created:     21 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Data.h>
8 #include <Model_AttributeDocRef.h>
9 #include <Model_AttributeInteger.h>
10 #include <Model_AttributeDouble.h>
11 #include <Model_AttributeReference.h>
12 #include <Model_AttributeRefAttr.h>
13 #include <Model_AttributeRefList.h>
14 #include <Model_AttributeBoolean.h>
15 #include <Model_AttributeString.h>
16 #include <Model_AttributeSelection.h>
17 #include <Model_AttributeSelectionList.h>
18 #include <Model_AttributeIntArray.h>
19 #include <Model_Events.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_Session.h>
24
25 #include <GeomData_Point.h>
26 #include <GeomData_Point2D.h>
27 #include <GeomData_Dir.h>
28 #include <Events_Loop.h>
29 #include <Events_Error.h>
30
31 #include <TDataStd_Name.hxx>
32 #include <TDataStd_AsciiString.hxx>
33 #include <TDF_AttributeIterator.hxx>
34 #include <TDF_ChildIterator.hxx>
35 #include <TDF_RelocationTable.hxx>
36
37 #include <string>
38
39 // myLab contains:
40 // TDataStd_Name - name of the object
41 // TDataStd_Integer - state of the object execution
42 // TDataStd_BooleanArray - array of flags of this data:
43 //                             0 - is in history or not
44 static const int kFlagInHistory = 0;
45 //                             1 - is displayed or not
46 static const int kFlagDisplayed = 1;
47
48 // invalid data
49 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
50
51 Model_Data::Model_Data() : mySendAttributeUpdated(true)
52 {
53 }
54
55 void Model_Data::setLabel(TDF_Label theLab)
56 {
57   myLab = theLab;
58   // set or get the default flags
59   if (!myLab.FindAttribute(TDataStd_BooleanArray::GetID(), myFlags)) {
60     // set default values if not found
61     myFlags = TDataStd_BooleanArray::Set(myLab, 0, 1);
62     myFlags->SetValue(kFlagInHistory, Standard_True); // is in history by default is true
63     myFlags->SetValue(kFlagDisplayed, Standard_True); // is displayed by default is true
64   }
65 }
66
67 std::string Model_Data::name()
68 {
69   Handle(TDataStd_Name) aName;
70   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
71     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
72   return "";  // not defined
73 }
74
75 void Model_Data::setName(const std::string& theName)
76 {
77   bool isModified = false;
78   Handle(TDataStd_Name) aName;
79   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
80     TDataStd_Name::Set(myLab, theName.c_str());
81     isModified = true;
82   } else {
83     isModified = !aName->Get().IsEqual(theName.c_str());
84     if (isModified)
85       aName->Set(theName.c_str());
86   }
87 }
88
89 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
90 {
91   AttributePtr aResult;
92   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
93   ModelAPI_Attribute* anAttr = 0;
94   if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
95     anAttr = new Model_AttributeDocRef(anAttrLab);
96   } else if (theAttrType == Model_AttributeInteger::typeId()) {
97     anAttr = new Model_AttributeInteger(anAttrLab);
98   } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
99     anAttr = new Model_AttributeDouble(anAttrLab);
100   } else if (theAttrType == Model_AttributeBoolean::typeId()) {
101     anAttr = new Model_AttributeBoolean(anAttrLab);
102   } else if (theAttrType == Model_AttributeString::typeId()) {
103     anAttr = new Model_AttributeString(anAttrLab);
104   } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
105     anAttr = new Model_AttributeReference(anAttrLab);
106   } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
107     anAttr = new Model_AttributeSelection(anAttrLab);
108   } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
109     anAttr = new Model_AttributeSelectionList(anAttrLab);
110   } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
111     anAttr = new Model_AttributeRefAttr(anAttrLab);
112   } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
113     anAttr = new Model_AttributeRefList(anAttrLab);
114   } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
115     anAttr = new Model_AttributeIntArray(anAttrLab);
116   } 
117   // create also GeomData attributes here because only here the OCAF strucure is known
118   else if (theAttrType == GeomData_Point::typeId()) {
119     anAttr = new GeomData_Point(anAttrLab);
120   } else if (theAttrType == GeomData_Dir::typeId()) {
121     anAttr = new GeomData_Dir(anAttrLab);
122   } else if (theAttrType == GeomData_Point2D::typeId()) {
123     anAttr = new GeomData_Point2D(anAttrLab);
124   }
125   if (anAttr) {
126     aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
127     myAttrs[theID] = aResult;
128     anAttr->setObject(myObject);
129     anAttr->setID(theID);
130   } else {
131     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
132   }
133   return aResult;
134 }
135
136 // macro for gthe generic returning of the attribute by the ID
137 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
138   std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
139     std::shared_ptr<ATTR_TYPE> aRes; \
140     std::map<std::string, AttributePtr >::iterator aFound = \
141       myAttrs.find(theID); \
142     if (aFound != myAttrs.end()) { \
143       aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
144     } \
145     return aRes; \
146   }
147 // implement nice getting methods for all ModelAPI attributes
148 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
149 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
150 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
151 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
152 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
153 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
154 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
155 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
156 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
157 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
158 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
159
160 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
161 {
162   std::shared_ptr<ModelAPI_Attribute> aResult;
163   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
164     return aResult;
165   return myAttrs[theID];
166 }
167
168 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
169 {
170   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = 
171     myAttrs.begin();
172   for (; anAttr != myAttrs.end(); anAttr++) {
173     if (anAttr->second == theAttr)
174       return anAttr->first;
175   }
176   // not found
177   static std::string anEmpty;
178   return anEmpty;
179 }
180
181 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
182 {
183   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
184   if (aData)
185     return myLab.IsEqual(aData->myLab) == Standard_True ;
186   return false;
187 }
188
189 bool Model_Data::isValid()
190 {
191   return !myLab.IsNull() && myLab.HasAttribute();
192 }
193
194 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
195 {
196   std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
197   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
198     myAttrs.begin();
199   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
200     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
201       aResult.push_back(anAttrsIter->second);
202     }
203   }
204   return aResult;
205 }
206
207 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
208 {
209   std::list<std::string> aResult;
210   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
211     myAttrs.begin();
212   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
213     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
214       aResult.push_back(anAttrsIter->first);
215     }
216   }
217   return aResult;
218 }
219
220 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
221 {
222   theAttr->setInitialized();
223   if (theAttr->isArgument()) {
224     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
225     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
226     if (mySendAttributeUpdated && myObject) {
227       myObject->attributeChanged(theAttr->id());
228     }
229   }
230 }
231
232 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
233 {
234   mySendAttributeUpdated = !theBlock;
235 }
236
237 void Model_Data::erase()
238 {
239   if (!myLab.IsNull())
240     myLab.ForgetAllAttributes();
241 }
242
243 void Model_Data::execState(const ModelAPI_ExecState theState)
244 {
245   if (theState != ModelAPI_StateNothing)
246     TDataStd_Integer::Set(myLab, (int)theState);
247 }
248
249 ModelAPI_ExecState Model_Data::execState()
250 {
251   Handle(TDataStd_Integer) aStateAttr;
252   if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
253     return ModelAPI_ExecState(aStateAttr->Get());
254   }
255   return ModelAPI_StateMustBeUpdated; // default value
256 }
257
258 void Model_Data::setError(const std::string& theError, bool theSend)
259 {
260   execState(ModelAPI_StateExecFailed);
261   if (theSend) {
262     Events_Error::send(theError);
263   }
264   TDataStd_AsciiString::Set(myLab, theError.c_str());
265 }
266
267 std::string Model_Data::error() const
268 {
269   Handle(TDataStd_AsciiString) anErrorAttr;
270   if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
271     return std::string(anErrorAttr->Get().ToCString());
272   }
273   return std::string();
274 }
275
276 int Model_Data::featureId() const
277 {
278   return myLab.Father().Tag(); // tag of the feature label
279 }
280
281 void Model_Data::eraseBackReferences()
282 {
283   myRefsToMe.clear();
284   std::shared_ptr<ModelAPI_Result> aRes = 
285     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
286   if (aRes)
287     aRes->setIsConcealed(false);
288 }
289
290 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
291 {
292   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
293   if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
294     return;
295
296   myRefsToMe.erase(anAttribute);
297 }
298
299 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID, 
300    const bool theApplyConcealment)
301 {
302   // do not add the same attribute twice
303   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
304   if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
305     return;
306
307   myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
308   if (theApplyConcealment && 
309       ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
310     std::shared_ptr<ModelAPI_Result> aRes = 
311       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
312     if (aRes) {
313       aRes->setIsConcealed(true);
314     }
315   }
316 }
317
318 void Model_Data::referencesToObjects(
319   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
320 {
321   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
322   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
323   for(; anAttr != myAttrs.end(); anAttr++) {
324     std::string aType = anAttr->second->attributeType();
325     if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
326       std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
327           ModelAPI_AttributeReference>(anAttr->second);
328       aReferenced.push_back(aRef->value());
329     } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
330       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
331           ModelAPI_AttributeRefAttr>(anAttr->second);
332       aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
333     } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
334       aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
335     } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
336       std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
337           ModelAPI_AttributeSelection>(anAttr->second);
338       aReferenced.push_back(aRef->context());
339     } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
340       std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
341           ModelAPI_AttributeSelectionList>(anAttr->second);
342       for(int a = aRef->size() - 1; a >= 0; a--) {
343         aReferenced.push_back(aRef->value(a)->context());
344       }
345     } else
346       continue; // nothing to do, not reference
347
348     if (!aReferenced.empty()) {
349       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
350       aReferenced.clear();
351     }
352   }
353 }
354
355 /// makes copy of all attributes on the given label and all sub-labels
356 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
357   TDF_AttributeIterator anAttrIter(theSource);
358   for(; anAttrIter.More(); anAttrIter.Next()) {
359     Handle(TDF_Attribute) aTargetAttr;
360     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
361       // create a new attribute if not yet exists in the destination
362             aTargetAttr = anAttrIter.Value()->NewEmpty();
363       theDestination.AddAttribute(aTargetAttr);
364     }
365     Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
366     anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
367   }
368   // copy the sub-labels content
369   TDF_ChildIterator aSubLabsIter(theSource);
370   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
371     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
372   }
373 }
374
375 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
376 {
377   TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
378   copyAttrs(myLab, aTargetRoot);
379   // make initialized the initialized attributes
380   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
381   for(; aMyIter != myAttrs.end(); aMyIter++) {
382     if (aMyIter->second->isInitialized()) {
383       theTarget->attribute(aMyIter->first)->setInitialized();
384     }
385   }
386 }
387
388 bool Model_Data::isInHistory()
389 {
390   return myFlags->Value(kFlagInHistory) == Standard_True;
391 }
392
393 void Model_Data::setIsInHistory(const bool theFlag)
394 {
395   return myFlags->SetValue(kFlagInHistory, theFlag);
396 }
397
398 bool Model_Data::isDisplayed()
399 {
400   return myFlags->Value(kFlagDisplayed) == Standard_True;
401 }
402
403 void Model_Data::setDisplayed(const bool theDisplay)
404 {
405   if (theDisplay != isDisplayed()) {
406     myFlags->SetValue(kFlagDisplayed, theDisplay);
407     static Events_Loop* aLoop = Events_Loop::loop();
408     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
409     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
410     aECreator->sendUpdated(myObject, EVENT_DISP);
411   }
412 }
413
414 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
415 {
416   return kInvalid;
417 }