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