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>
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>
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>
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;
49 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
51 Model_Data::Model_Data() : mySendAttributeUpdated(true)
55 void Model_Data::setLabel(TDF_Label 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
67 std::string Model_Data::name()
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
75 void Model_Data::setName(const std::string& theName)
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());
83 isModified = !aName->Get().IsEqual(theName.c_str());
85 aName->Set(theName.c_str());
89 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
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);
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);
126 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
127 myAttrs[theID] = aResult;
128 anAttr->setObject(myObject);
129 anAttr->setID(theID);
131 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
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); \
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);
160 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
162 std::shared_ptr<ModelAPI_Attribute> aResult;
163 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
165 return myAttrs[theID];
168 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
170 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
172 for (; anAttr != myAttrs.end(); anAttr++) {
173 if (anAttr->second == theAttr)
174 return anAttr->first;
177 static std::string anEmpty;
181 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
183 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
185 return myLab.IsEqual(aData->myLab) == Standard_True ;
189 bool Model_Data::isValid()
191 return !myLab.IsNull() && myLab.HasAttribute();
194 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
196 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
197 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
199 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
200 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
201 aResult.push_back(anAttrsIter->second);
207 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
209 std::list<std::string> aResult;
210 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
212 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
213 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
214 aResult.push_back(anAttrsIter->first);
220 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
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());
232 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
234 mySendAttributeUpdated = !theBlock;
237 void Model_Data::erase()
240 myLab.ForgetAllAttributes();
243 void Model_Data::execState(const ModelAPI_ExecState theState)
245 if (theState != ModelAPI_StateNothing)
246 TDataStd_Integer::Set(myLab, (int)theState);
249 ModelAPI_ExecState Model_Data::execState()
251 Handle(TDataStd_Integer) aStateAttr;
252 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
253 return ModelAPI_ExecState(aStateAttr->Get());
255 return ModelAPI_StateMustBeUpdated; // default value
258 void Model_Data::setError(const std::string& theError, bool theSend)
260 execState(ModelAPI_StateExecFailed);
262 Events_Error::send(theError);
264 TDataStd_AsciiString::Set(myLab, theError.c_str());
267 std::string Model_Data::error() const
269 Handle(TDataStd_AsciiString) anErrorAttr;
270 if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
271 return std::string(anErrorAttr->Get().ToCString());
273 return std::string();
276 int Model_Data::featureId() const
278 return myLab.Father().Tag(); // tag of the feature label
281 void Model_Data::eraseBackReferences()
284 std::shared_ptr<ModelAPI_Result> aRes =
285 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
287 aRes->setIsConcealed(false);
290 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
292 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
293 if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
296 myRefsToMe.erase(anAttribute);
299 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
300 const bool theApplyConcealment)
302 // do not add the same attribute twice
303 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
304 if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
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 // the second condition is for history upper than concealment causer, so the feature result may
313 // be displayed and previewed; also for avoiding of quick show/hide on history
315 if (aRes && !theFeature->isDisabled()) {
316 aRes->setIsConcealed(true);
321 void Model_Data::referencesToObjects(
322 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
324 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
325 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
326 for(; anAttr != myAttrs.end(); anAttr++) {
327 std::string aType = anAttr->second->attributeType();
328 if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
329 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
330 ModelAPI_AttributeReference>(anAttr->second);
331 aReferenced.push_back(aRef->value());
332 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
333 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
334 ModelAPI_AttributeRefAttr>(anAttr->second);
335 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
336 } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
337 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
338 } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
339 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
340 ModelAPI_AttributeSelection>(anAttr->second);
341 aReferenced.push_back(aRef->context());
342 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
343 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
344 ModelAPI_AttributeSelectionList>(anAttr->second);
345 for(int a = aRef->size() - 1; a >= 0; a--) {
346 aReferenced.push_back(aRef->value(a)->context());
349 continue; // nothing to do, not reference
351 if (!aReferenced.empty()) {
352 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
358 /// makes copy of all attributes on the given label and all sub-labels
359 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
360 TDF_AttributeIterator anAttrIter(theSource);
361 for(; anAttrIter.More(); anAttrIter.Next()) {
362 Handle(TDF_Attribute) aTargetAttr;
363 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
364 // create a new attribute if not yet exists in the destination
365 aTargetAttr = anAttrIter.Value()->NewEmpty();
366 theDestination.AddAttribute(aTargetAttr);
368 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
369 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
371 // copy the sub-labels content
372 TDF_ChildIterator aSubLabsIter(theSource);
373 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
374 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
378 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
380 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
381 copyAttrs(myLab, aTargetRoot);
382 // make initialized the initialized attributes
383 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
384 for(; aMyIter != myAttrs.end(); aMyIter++) {
385 if (aMyIter->second->isInitialized()) {
386 theTarget->attribute(aMyIter->first)->setInitialized();
391 bool Model_Data::isInHistory()
393 return myFlags->Value(kFlagInHistory) == Standard_True;
396 void Model_Data::setIsInHistory(const bool theFlag)
398 return myFlags->SetValue(kFlagInHistory, theFlag);
401 bool Model_Data::isDisplayed()
403 return myFlags->Value(kFlagDisplayed) == Standard_True;
406 void Model_Data::setDisplayed(const bool theDisplay)
408 if (theDisplay != isDisplayed()) {
409 myFlags->SetValue(kFlagDisplayed, theDisplay);
410 static Events_Loop* aLoop = Events_Loop::loop();
411 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
412 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
413 aECreator->sendUpdated(myObject, EVENT_DISP);
417 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
422 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()