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