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