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 Handle(TDataStd_Name) aName;
81 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
82 TDataStd_Name::Set(myLab, theName.c_str());
85 isModified = !aName->Get().IsEqual(theName.c_str());
87 aName->Set(theName.c_str());
91 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
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);
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);
128 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
129 myAttrs[theID] = aResult;
130 anAttr->setObject(myObject);
131 anAttr->setID(theID);
133 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
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); \
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);
162 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
164 std::shared_ptr<ModelAPI_Attribute> aResult;
165 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
167 return myAttrs[theID];
170 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
172 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
174 for (; anAttr != myAttrs.end(); anAttr++) {
175 if (anAttr->second == theAttr)
176 return anAttr->first;
179 static std::string anEmpty;
183 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
185 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
187 return myLab.IsEqual(aData->myLab) == Standard_True ;
191 bool Model_Data::isValid()
193 return !myLab.IsNull() && myLab.HasAttribute();
196 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
198 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
199 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
201 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
202 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
203 aResult.push_back(anAttrsIter->second);
209 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
211 std::list<std::string> aResult;
212 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
214 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
215 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
216 aResult.push_back(anAttrsIter->first);
222 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
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());
234 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
236 mySendAttributeUpdated = !theBlock;
239 void Model_Data::erase()
242 myLab.ForgetAllAttributes();
245 // indexes in the state array
247 STATE_INDEX_STATE = 1, // the state type itself
248 STATE_INDEX_TRANSACTION = 2, // transaction ID
251 /// Returns the label array, initialises it by default values if not exists
252 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
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)
263 void Model_Data::execState(const ModelAPI_ExecState theState)
265 if (theState != ModelAPI_StateNothing) {
266 stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
270 ModelAPI_ExecState Model_Data::execState()
272 return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
275 int Model_Data::updateID()
277 return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
280 void Model_Data::setUpdateID(const int theID)
282 stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
285 void Model_Data::setError(const std::string& theError, bool theSend)
287 execState(ModelAPI_StateExecFailed);
289 Events_Error::send(theError);
291 TDataStd_AsciiString::Set(myLab, theError.c_str());
294 std::string Model_Data::error() const
296 Handle(TDataStd_AsciiString) anErrorAttr;
297 if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
298 return std::string(anErrorAttr->Get().ToCString());
300 return std::string();
303 int Model_Data::featureId() const
305 return myLab.Father().Tag(); // tag of the feature label
308 void Model_Data::eraseBackReferences()
311 std::shared_ptr<ModelAPI_Result> aRes =
312 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
314 aRes->setIsConcealed(false);
317 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
319 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
320 if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
323 myRefsToMe.erase(anAttribute);
325 // remove concealment immideately: on deselection it must be posible to reselect in GUI the same
326 if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
327 std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
328 for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
329 if (aRefsIter->get()) {
330 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
331 if (aFeature.get()) {
332 if (ModelAPI_Session::get()->validators()->isConcealed(
333 aFeature->getKind(), (*aRefsIter)->id())) {
334 return; // it is still concealed, nothing to do
339 // thus, no concealment references anymore => make not-concealed
340 std::shared_ptr<ModelAPI_Result> aRes =
341 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
343 aRes->setIsConcealed(false);
344 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
345 ModelAPI_EventCreator::get()->sendUpdated(aRes, anEvent);
346 Events_Loop::loop()->flush(anEvent);
351 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
352 const bool theApplyConcealment)
354 // do not add the same attribute twice
355 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
356 if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
359 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
360 if (theApplyConcealment &&
361 ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
362 std::shared_ptr<ModelAPI_Result> aRes =
363 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
364 // the second condition is for history upper than concealment causer, so the feature result may
365 // be displayed and previewed; also for avoiding of quick show/hide on history
367 if (aRes && !theFeature->isDisabled()) {
368 aRes->setIsConcealed(true);
373 void Model_Data::referencesToObjects(
374 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
376 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
377 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
378 for(; anAttr != myAttrs.end(); anAttr++) {
379 std::string aType = anAttr->second->attributeType();
380 if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
381 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
382 ModelAPI_AttributeReference>(anAttr->second);
383 aReferenced.push_back(aRef->value());
384 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
385 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
386 ModelAPI_AttributeRefAttr>(anAttr->second);
387 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
388 } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
389 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
390 } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
391 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
392 ModelAPI_AttributeSelection>(anAttr->second);
393 aReferenced.push_back(aRef->context());
394 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
395 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
396 ModelAPI_AttributeSelectionList>(anAttr->second);
397 for(int a = aRef->size() - 1; a >= 0; a--) {
398 aReferenced.push_back(aRef->value(a)->context());
401 continue; // nothing to do, not reference
403 if (!aReferenced.empty()) {
404 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
410 /// makes copy of all attributes on the given label and all sub-labels
411 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
412 TDF_AttributeIterator anAttrIter(theSource);
413 for(; anAttrIter.More(); anAttrIter.Next()) {
414 Handle(TDF_Attribute) aTargetAttr;
415 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
416 // create a new attribute if not yet exists in the destination
417 aTargetAttr = anAttrIter.Value()->NewEmpty();
418 theDestination.AddAttribute(aTargetAttr);
420 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
421 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
423 // copy the sub-labels content
424 TDF_ChildIterator aSubLabsIter(theSource);
425 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
426 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
430 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
432 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
433 copyAttrs(myLab, aTargetRoot);
434 // make initialized the initialized attributes
435 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
436 for(; aMyIter != myAttrs.end(); aMyIter++) {
437 if (aMyIter->second->isInitialized()) {
438 theTarget->attribute(aMyIter->first)->setInitialized();
443 bool Model_Data::isInHistory()
445 return myFlags->Value(kFlagInHistory) == Standard_True;
448 void Model_Data::setIsInHistory(const bool theFlag)
450 return myFlags->SetValue(kFlagInHistory, theFlag);
453 bool Model_Data::isDisplayed()
455 if (!myObject.get() || !myObject->document().get() || // object is in valid
456 myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
458 if (myObject->document()->isActive()) // for active documents it must be ok anyway
460 // any object from the root document except part result may be displayed
461 if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
462 myObject->groupName() != ModelAPI_ResultPart::group())
467 void Model_Data::setDisplayed(const bool theDisplay)
469 if (theDisplay != isDisplayed()) {
470 myFlags->SetValue(kFlagDisplayed, theDisplay);
471 static Events_Loop* aLoop = Events_Loop::loop();
472 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
473 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
474 aECreator->sendUpdated(myObject, EVENT_DISP);
478 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
483 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()