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