1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Data.hxx
4 // Created: 21 Mar 2014
5 // Author: Mikhail PONIKAROV
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>
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>
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>
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;
51 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
53 Model_Data::Model_Data() : mySendAttributeUpdated(true)
57 void Model_Data::setLabel(TDF_Label 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
69 std::string Model_Data::name()
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
77 void Model_Data::setName(const std::string& theName)
79 bool isModified = false;
80 std::string anOldName = name();
81 Handle(TDataStd_Name) aName;
82 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
83 TDataStd_Name::Set(myLab, theName.c_str());
86 isModified = !aName->Get().IsEqual(theName.c_str());
88 aName->Set(theName.c_str());
91 ModelAPI_ObjectRenamedMessage::send(myObject, anOldName, theName, this);
94 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
97 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
98 ModelAPI_Attribute* anAttr = 0;
99 if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
100 anAttr = new Model_AttributeDocRef(anAttrLab);
101 } else if (theAttrType == Model_AttributeInteger::typeId()) {
102 anAttr = new Model_AttributeInteger(anAttrLab);
103 } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
104 anAttr = new Model_AttributeDouble(anAttrLab);
105 } else if (theAttrType == Model_AttributeBoolean::typeId()) {
106 anAttr = new Model_AttributeBoolean(anAttrLab);
107 } else if (theAttrType == Model_AttributeString::typeId()) {
108 anAttr = new Model_AttributeString(anAttrLab);
109 } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
110 anAttr = new Model_AttributeReference(anAttrLab);
111 } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
112 anAttr = new Model_AttributeSelection(anAttrLab);
113 } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
114 anAttr = new Model_AttributeSelectionList(anAttrLab);
115 } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
116 anAttr = new Model_AttributeRefAttr(anAttrLab);
117 } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
118 anAttr = new Model_AttributeRefList(anAttrLab);
119 } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
120 anAttr = new Model_AttributeIntArray(anAttrLab);
122 // create also GeomData attributes here because only here the OCAF strucure is known
123 else if (theAttrType == GeomData_Point::typeId()) {
124 anAttr = new GeomData_Point(anAttrLab);
125 } else if (theAttrType == GeomData_Dir::typeId()) {
126 anAttr = new GeomData_Dir(anAttrLab);
127 } else if (theAttrType == GeomData_Point2D::typeId()) {
128 anAttr = new GeomData_Point2D(anAttrLab);
131 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
132 myAttrs[theID] = aResult;
133 anAttr->setObject(myObject);
134 anAttr->setID(theID);
136 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
141 // macro for gthe generic returning of the attribute by the ID
142 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
143 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
144 std::shared_ptr<ATTR_TYPE> aRes; \
145 std::map<std::string, AttributePtr >::iterator aFound = \
146 myAttrs.find(theID); \
147 if (aFound != myAttrs.end()) { \
148 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
152 // implement nice getting methods for all ModelAPI attributes
153 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
154 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
155 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
156 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
157 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
158 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
159 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
160 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
161 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
162 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
163 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
165 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
167 std::shared_ptr<ModelAPI_Attribute> aResult;
168 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
170 return myAttrs[theID];
173 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
175 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
177 for (; anAttr != myAttrs.end(); anAttr++) {
178 if (anAttr->second == theAttr)
179 return anAttr->first;
182 static std::string anEmpty;
186 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
188 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
190 return myLab.IsEqual(aData->myLab) == Standard_True ;
194 bool Model_Data::isValid()
196 return !myLab.IsNull() && myLab.HasAttribute();
199 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
201 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
202 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
204 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
205 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
206 aResult.push_back(anAttrsIter->second);
212 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
214 std::list<std::string> aResult;
215 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
217 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
218 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
219 aResult.push_back(anAttrsIter->first);
225 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
227 theAttr->setInitialized();
228 if (theAttr->isArgument()) {
229 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
230 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
231 if (mySendAttributeUpdated && myObject) {
232 myObject->attributeChanged(theAttr->id());
237 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
239 mySendAttributeUpdated = !theBlock;
242 void Model_Data::erase()
245 myLab.ForgetAllAttributes();
248 // indexes in the state array
250 STATE_INDEX_STATE = 1, // the state type itself
251 STATE_INDEX_TRANSACTION = 2, // transaction ID
254 /// Returns the label array, initialises it by default values if not exists
255 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
257 Handle(TDataStd_IntegerArray) aStateArray;
258 if (!theLab.FindAttribute(TDataStd_IntegerArray::GetID(), aStateArray)) {
259 aStateArray = TDataStd_IntegerArray::Set(theLab, 1, 2);
260 aStateArray->SetValue(STATE_INDEX_STATE, ModelAPI_StateMustBeUpdated); // default state
261 aStateArray->SetValue(STATE_INDEX_TRANSACTION, 0); // default transaction ID (not existing)
266 void Model_Data::execState(const ModelAPI_ExecState theState)
268 if (theState != ModelAPI_StateNothing) {
269 stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
273 ModelAPI_ExecState Model_Data::execState()
275 return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
278 int Model_Data::updateID()
280 return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
283 void Model_Data::setUpdateID(const int theID)
285 stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
288 void Model_Data::setError(const std::string& theError, bool theSend)
290 execState(ModelAPI_StateExecFailed);
292 Events_Error::send(theError);
294 TDataStd_AsciiString::Set(myLab, theError.c_str());
297 std::string Model_Data::error() const
299 Handle(TDataStd_AsciiString) anErrorAttr;
300 if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
301 return std::string(anErrorAttr->Get().ToCString());
303 return std::string();
306 int Model_Data::featureId() const
308 return myLab.Father().Tag(); // tag of the feature label
311 void Model_Data::eraseBackReferences()
314 std::shared_ptr<ModelAPI_Result> aRes =
315 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
317 aRes->setIsConcealed(false);
320 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
322 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
323 if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
326 myRefsToMe.erase(anAttribute);
328 // remove concealment immideately: on deselection it must be posible to reselect in GUI the same
329 if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
330 updateConcealmentFlag();
334 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
335 const bool theApplyConcealment)
337 // do not add the same attribute twice
338 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
339 if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
342 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
343 if (theApplyConcealment &&
344 ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
345 std::shared_ptr<ModelAPI_Result> aRes =
346 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
347 // the second condition is for history upper than concealment causer, so the feature result may
348 // be displayed and previewed; also for avoiding of quick show/hide on history
350 if (aRes && !theFeature->isDisabled()) {
351 aRes->setIsConcealed(true);
356 void Model_Data::updateConcealmentFlag()
358 std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
359 for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
360 if (aRefsIter->get()) {
361 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
362 if (aFeature.get() && !aFeature->isDisabled()) {
363 if (ModelAPI_Session::get()->validators()->isConcealed(
364 aFeature->getKind(), (*aRefsIter)->id())) {
365 return; // it is still concealed, nothing to do
370 // thus, no concealment references anymore => make not-concealed
371 std::shared_ptr<ModelAPI_Result> aRes =
372 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
374 aRes->setIsConcealed(false);
375 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
376 ModelAPI_EventCreator::get()->sendUpdated(aRes, anEvent);
377 Events_Loop::loop()->flush(anEvent);
381 void Model_Data::referencesToObjects(
382 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
384 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
385 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
386 for(; anAttr != myAttrs.end(); anAttr++) {
387 std::string aType = anAttr->second->attributeType();
388 if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
389 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
390 ModelAPI_AttributeReference>(anAttr->second);
391 aReferenced.push_back(aRef->value());
392 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
393 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
394 ModelAPI_AttributeRefAttr>(anAttr->second);
395 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
396 } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
397 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
398 } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
399 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
400 ModelAPI_AttributeSelection>(anAttr->second);
401 aReferenced.push_back(aRef->context());
402 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
403 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
404 ModelAPI_AttributeSelectionList>(anAttr->second);
405 for(int a = aRef->size() - 1; a >= 0; a--) {
406 aReferenced.push_back(aRef->value(a)->context());
409 continue; // nothing to do, not reference
411 if (!aReferenced.empty()) {
412 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
418 /// makes copy of all attributes on the given label and all sub-labels
419 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
420 TDF_AttributeIterator anAttrIter(theSource);
421 for(; anAttrIter.More(); anAttrIter.Next()) {
422 Handle(TDF_Attribute) aTargetAttr;
423 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
424 // create a new attribute if not yet exists in the destination
425 aTargetAttr = anAttrIter.Value()->NewEmpty();
426 theDestination.AddAttribute(aTargetAttr);
428 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
429 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
431 // copy the sub-labels content
432 TDF_ChildIterator aSubLabsIter(theSource);
433 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
434 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
438 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
440 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
441 copyAttrs(myLab, aTargetRoot);
442 // make initialized the initialized attributes
443 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
444 for(; aMyIter != myAttrs.end(); aMyIter++) {
445 if (aMyIter->second->isInitialized()) {
446 theTarget->attribute(aMyIter->first)->setInitialized();
451 bool Model_Data::isInHistory()
453 return myFlags->Value(kFlagInHistory) == Standard_True;
456 void Model_Data::setIsInHistory(const bool theFlag)
458 return myFlags->SetValue(kFlagInHistory, theFlag);
461 bool Model_Data::isDisplayed()
463 if (!myObject.get() || !myObject->document().get() || // object is in valid
464 myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
466 if (myObject->document()->isActive()) // for active documents it must be ok anyway
468 // any object from the root document except part result may be displayed
469 if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
470 myObject->groupName() != ModelAPI_ResultPart::group())
475 void Model_Data::setDisplayed(const bool theDisplay)
477 if (theDisplay != isDisplayed()) {
478 myFlags->SetValue(kFlagDisplayed, theDisplay);
479 static Events_Loop* aLoop = Events_Loop::loop();
480 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
481 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
482 aECreator->sendUpdated(myObject, EVENT_DISP);
486 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
491 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()