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