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