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