]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Implementation of color as integer array attribute
[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
33 #include <string>
34
35 // myLab contains:
36 // TDataStd_Name - name of the object
37 // TDataStd_Integer - state of the object execution
38
39 Model_Data::Model_Data() : mySendAttributeUpdated(true)
40 {
41 }
42
43 void Model_Data::setLabel(TDF_Label theLab)
44 {
45   myLab = theLab;
46 }
47
48 std::string Model_Data::name()
49 {
50   Handle(TDataStd_Name) aName;
51   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
52     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
53   return "";  // not defined
54 }
55
56 void Model_Data::setName(const std::string& theName)
57 {
58   bool isModified = false;
59   Handle(TDataStd_Name) aName;
60   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
61     TDataStd_Name::Set(myLab, theName.c_str());
62     isModified = true;
63   } else {
64     isModified = !aName->Get().IsEqual(theName.c_str());
65     if (isModified)
66       aName->Set(theName.c_str());
67   }
68 }
69
70 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
71 {
72   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
73   ModelAPI_Attribute* anAttr = 0;
74   if (theAttrType == ModelAPI_AttributeDocRef::type()) {
75     anAttr = new Model_AttributeDocRef(anAttrLab);
76   } else if (theAttrType == Model_AttributeInteger::type()) {
77     anAttr = new Model_AttributeInteger(anAttrLab);
78   } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
79     anAttr = new Model_AttributeDouble(anAttrLab);
80   } else if (theAttrType == Model_AttributeBoolean::type()) {
81     anAttr = new Model_AttributeBoolean(anAttrLab);
82   } else if (theAttrType == Model_AttributeString::type()) {
83     anAttr = new Model_AttributeString(anAttrLab);
84   } else if (theAttrType == ModelAPI_AttributeReference::type()) {
85     anAttr = new Model_AttributeReference(anAttrLab);
86   } else if (theAttrType == ModelAPI_AttributeSelection::type()) {
87     anAttr = new Model_AttributeSelection(anAttrLab);
88   } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
89     anAttr = new Model_AttributeSelectionList(anAttrLab);
90   } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
91     anAttr = new Model_AttributeRefAttr(anAttrLab);
92   } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
93     anAttr = new Model_AttributeRefList(anAttrLab);
94   } else if (theAttrType == ModelAPI_AttributeIntArray::type()) {
95     anAttr = new Model_AttributeIntArray(anAttrLab);
96   } 
97   // create also GeomData attributes here because only here the OCAF strucure is known
98   else if (theAttrType == GeomData_Point::type()) {
99     anAttr = new GeomData_Point(anAttrLab);
100   } else if (theAttrType == GeomData_Dir::type()) {
101     anAttr = new GeomData_Dir(anAttrLab);
102   } else if (theAttrType == GeomData_Point2D::type()) {
103     anAttr = new GeomData_Point2D(anAttrLab);
104   }
105   if (anAttr) {
106     myAttrs[theID] = std::shared_ptr<ModelAPI_Attribute>(anAttr);
107     anAttr->setObject(myObject);
108     anAttr->setID(theID);
109   } else {
110     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
111   }
112 }
113
114 // macro for gthe generic returning of the attribute by the ID
115 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
116   std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
117     std::shared_ptr<ATTR_TYPE> aRes; \
118     std::map<std::string, AttributePtr >::iterator aFound = \
119       myAttrs.find(theID); \
120     if (aFound != myAttrs.end()) { \
121       aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
122     } \
123     return aRes; \
124   }
125 // implement nice getting methods for all ModelAPI attributes
126 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
127 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
128 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
129 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
130 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
131 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
132 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
133 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
134 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
135 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
136 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
137
138 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
139 {
140   std::shared_ptr<ModelAPI_Attribute> aResult;
141   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
142     return aResult;
143   return myAttrs[theID];
144 }
145
146 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
147 {
148   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = 
149     myAttrs.begin();
150   for (; anAttr != myAttrs.end(); anAttr++) {
151     if (anAttr->second == theAttr)
152       return anAttr->first;
153   }
154   // not found
155   static std::string anEmpty;
156   return anEmpty;
157 }
158
159 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
160 {
161   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
162   if (aData)
163     return myLab.IsEqual(aData->myLab) == Standard_True ;
164   return false;
165 }
166
167 bool Model_Data::isValid()
168 {
169   return !myLab.IsNull() && myLab.HasAttribute();
170 }
171
172 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
173 {
174   std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
175   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
176     myAttrs.begin();
177   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
178     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
179       aResult.push_back(anAttrsIter->second);
180     }
181   }
182   return aResult;
183 }
184
185 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
186 {
187   std::list<std::string> aResult;
188   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
189     myAttrs.begin();
190   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
191     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
192       aResult.push_back(anAttrsIter->first);
193     }
194   }
195   return aResult;
196 }
197
198 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
199 {
200   theAttr->setInitialized();
201   if (theAttr->isArgument()) {
202     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
203     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
204     if (mySendAttributeUpdated && myObject) {
205       myObject->attributeChanged(theAttr->id());
206     }
207   }
208 }
209
210 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
211 {
212   mySendAttributeUpdated = !theBlock;
213 }
214
215 void Model_Data::erase()
216 {
217   if (!myLab.IsNull())
218     myLab.ForgetAllAttributes();
219 }
220
221 void Model_Data::execState(const ModelAPI_ExecState theState)
222 {
223   if (theState != ModelAPI_StateNothing)
224     TDataStd_Integer::Set(myLab, (int)theState);
225 }
226
227 ModelAPI_ExecState Model_Data::execState()
228 {
229   Handle(TDataStd_Integer) aStateAttr;
230   if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
231     return ModelAPI_ExecState(aStateAttr->Get());
232   }
233   return ModelAPI_StateMustBeUpdated; // default value
234 }
235
236 void Model_Data::setError(const std::string& theError)
237 {
238   execState(ModelAPI_StateExecFailed);
239   Events_Error::send(theError);
240 }
241
242 int Model_Data::featureId() const
243 {
244   return myLab.Father().Tag(); // tag of the feature label
245 }
246
247 void Model_Data::eraseBackReferences()
248 {
249   myRefsToMe.clear();
250   std::shared_ptr<ModelAPI_Result> aRes = 
251     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
252   if (aRes)
253     aRes->setIsConcealed(false);
254 }
255
256 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
257 {
258   myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
259   if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
260     std::shared_ptr<ModelAPI_Result> aRes = 
261       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
262     if (aRes) {
263       aRes->setIsConcealed(true);
264     }
265   }
266 }
267
268 void Model_Data::referencesToObjects(
269   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
270 {
271   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
272   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
273   for(; anAttr != myAttrs.end(); anAttr++) {
274     std::string aType = anAttr->second->attributeType();
275     if (aType == ModelAPI_AttributeReference::type()) { // reference to object
276       std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
277           ModelAPI_AttributeReference>(anAttr->second);
278       aReferenced.push_back(aRef->value());
279     } else if (aType == ModelAPI_AttributeRefAttr::type()) { // reference to attribute or object
280       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
281           ModelAPI_AttributeRefAttr>(anAttr->second);
282       aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
283     } else if (aType == ModelAPI_AttributeRefList::type()) { // list of references
284       aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
285     } else if (aType == ModelAPI_AttributeSelection::type()) { // selection attribute
286       std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
287           ModelAPI_AttributeSelection>(anAttr->second);
288       aReferenced.push_back(aRef->context());
289     } else if (aType == ModelAPI_AttributeSelectionList::type()) { // list of selection attributes
290       std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
291           ModelAPI_AttributeSelectionList>(anAttr->second);
292       for(int a = aRef->size() - 1; a >= 0; a--) {
293         aReferenced.push_back(aRef->value(a)->context());
294       }
295     } else
296       continue; // nothing to do, not reference
297
298     if (!aReferenced.empty()) {
299       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
300       aReferenced.clear();
301     }
302   }
303 }