1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Data.hxx
4 // Created: 21 Mar 2014
5 // Author: Mikhail PONIKAROV
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_Events.h>
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_Validator.h>
22 #include <ModelAPI_Session.h>
24 #include <GeomData_Point.h>
25 #include <GeomData_Point2D.h>
26 #include <GeomData_Dir.h>
27 #include <Events_Loop.h>
28 #include <Events_Error.h>
30 #include <TDataStd_Name.hxx>
35 // TDataStd_Name - name of the object
36 // TDataStd_Integer - state of the object execution
38 Model_Data::Model_Data() : mySendAttributeUpdated(true)
42 void Model_Data::setLabel(TDF_Label theLab)
47 std::string Model_Data::name()
49 Handle(TDataStd_Name) aName;
50 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
51 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
52 return ""; // not defined
55 void Model_Data::setName(const std::string& theName)
57 bool isModified = false;
58 Handle(TDataStd_Name) aName;
59 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
60 TDataStd_Name::Set(myLab, theName.c_str());
63 isModified = !aName->Get().IsEqual(theName.c_str());
65 aName->Set(theName.c_str());
69 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
71 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
72 ModelAPI_Attribute* anAttr = 0;
73 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
74 anAttr = new Model_AttributeDocRef(anAttrLab);
75 } else if (theAttrType == Model_AttributeInteger::type()) {
76 anAttr = new Model_AttributeInteger(anAttrLab);
77 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
78 anAttr = new Model_AttributeDouble(anAttrLab);
79 } else if (theAttrType == Model_AttributeBoolean::type()) {
80 anAttr = new Model_AttributeBoolean(anAttrLab);
81 } else if (theAttrType == Model_AttributeString::type()) {
82 anAttr = new Model_AttributeString(anAttrLab);
83 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
84 anAttr = new Model_AttributeReference(anAttrLab);
85 } else if (theAttrType == ModelAPI_AttributeSelection::type()) {
86 anAttr = new Model_AttributeSelection(anAttrLab);
87 } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
88 anAttr = new Model_AttributeSelectionList(anAttrLab);
89 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
90 anAttr = new Model_AttributeRefAttr(anAttrLab);
91 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
92 anAttr = new Model_AttributeRefList(anAttrLab);
94 // create also GeomData attributes here because only here the OCAF strucure is known
95 else if (theAttrType == GeomData_Point::type()) {
96 anAttr = new GeomData_Point(anAttrLab);
97 } else if (theAttrType == GeomData_Dir::type()) {
98 anAttr = new GeomData_Dir(anAttrLab);
99 } else if (theAttrType == GeomData_Point2D::type()) {
100 anAttr = new GeomData_Point2D(anAttrLab);
103 myAttrs[theID] = std::shared_ptr<ModelAPI_Attribute>(anAttr);
104 anAttr->setObject(myObject);
105 anAttr->setID(theID);
107 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
111 // macro for gthe generic returning of the attribute by the ID
112 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
113 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
114 std::shared_ptr<ATTR_TYPE> aRes; \
115 std::map<std::string, AttributePtr >::iterator aFound = \
116 myAttrs.find(theID); \
117 if (aFound != myAttrs.end()) { \
118 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
122 // implement nice getting methods for all ModelAPI attributes
123 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
124 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
125 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
126 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
127 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
128 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
129 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
130 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
131 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
132 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
134 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
136 std::shared_ptr<ModelAPI_Attribute> aResult;
137 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
139 return myAttrs[theID];
142 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
144 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
146 for (; anAttr != myAttrs.end(); anAttr++) {
147 if (anAttr->second == theAttr)
148 return anAttr->first;
151 static std::string anEmpty;
155 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
157 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
159 return myLab.IsEqual(aData->myLab) == Standard_True ;
163 bool Model_Data::isValid()
165 return !myLab.IsNull() && myLab.HasAttribute();
168 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
170 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
171 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
173 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
174 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
175 aResult.push_back(anAttrsIter->second);
181 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
183 std::list<std::string> aResult;
184 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
186 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
187 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
188 aResult.push_back(anAttrsIter->first);
194 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
196 theAttr->setInitialized();
197 if (theAttr->isArgument()) {
198 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
199 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
200 if (mySendAttributeUpdated && myObject) {
201 myObject->attributeChanged(theAttr->id());
206 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
208 mySendAttributeUpdated = !theBlock;
211 void Model_Data::erase()
214 myLab.ForgetAllAttributes();
217 void Model_Data::execState(const ModelAPI_ExecState theState)
219 if (theState != ModelAPI_StateNothing)
220 TDataStd_Integer::Set(myLab, (int)theState);
223 ModelAPI_ExecState Model_Data::execState()
225 Handle(TDataStd_Integer) aStateAttr;
226 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
227 return ModelAPI_ExecState(aStateAttr->Get());
229 return ModelAPI_StateMustBeUpdated; // default value
232 void Model_Data::setError(const std::string& theError)
234 execState(ModelAPI_StateExecFailed);
235 Events_Error::send(theError);
238 int Model_Data::featureId() const
240 return myLab.Father().Tag(); // tag of the feature label
243 void Model_Data::eraseBackReferences()
246 std::shared_ptr<ModelAPI_Result> aRes =
247 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
249 aRes->setIsConcealed(false);
252 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
254 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
255 if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
256 std::shared_ptr<ModelAPI_Result> aRes =
257 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
259 aRes->setIsConcealed(true);
264 void Model_Data::referencesToObjects(
265 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
267 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
268 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
269 for(; anAttr != myAttrs.end(); anAttr++) {
270 std::string aType = anAttr->second->attributeType();
271 if (aType == ModelAPI_AttributeReference::type()) { // reference to object
272 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
273 ModelAPI_AttributeReference>(anAttr->second);
274 aReferenced.push_back(aRef->value());
275 } else if (aType == ModelAPI_AttributeRefAttr::type()) { // reference to attribute or object
276 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
277 ModelAPI_AttributeRefAttr>(anAttr->second);
278 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
279 } else if (aType == ModelAPI_AttributeRefList::type()) { // list of references
280 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
281 } else if (aType == ModelAPI_AttributeSelection::type()) { // selection attribute
282 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
283 ModelAPI_AttributeSelection>(anAttr->second);
284 aReferenced.push_back(aRef->context());
285 } else if (aType == ModelAPI_AttributeSelectionList::type()) { // list of selection attributes
286 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
287 ModelAPI_AttributeSelectionList>(anAttr->second);
288 for(int a = aRef->size() - 1; a >= 0; a--) {
289 aReferenced.push_back(aRef->value(a)->context());
292 continue; // nothing to do, not reference
294 if (!aReferenced.empty()) {
295 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));