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_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>
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>
31 #include <TDataStd_Name.hxx>
36 // TDataStd_Name - name of the object
37 // TDataStd_Integer - state of the object execution
39 Model_Data::Model_Data() : mySendAttributeUpdated(true)
43 void Model_Data::setLabel(TDF_Label theLab)
48 std::string Model_Data::name()
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
56 void Model_Data::setName(const std::string& theName)
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());
64 isModified = !aName->Get().IsEqual(theName.c_str());
66 aName->Set(theName.c_str());
70 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
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);
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);
107 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
108 myAttrs[theID] = aResult;
109 anAttr->setObject(myObject);
110 anAttr->setID(theID);
112 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
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); \
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);
141 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
143 std::shared_ptr<ModelAPI_Attribute> aResult;
144 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
146 return myAttrs[theID];
149 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
151 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
153 for (; anAttr != myAttrs.end(); anAttr++) {
154 if (anAttr->second == theAttr)
155 return anAttr->first;
158 static std::string anEmpty;
162 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
164 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
166 return myLab.IsEqual(aData->myLab) == Standard_True ;
170 bool Model_Data::isValid()
172 return !myLab.IsNull() && myLab.HasAttribute();
175 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
177 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
178 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
180 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
181 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
182 aResult.push_back(anAttrsIter->second);
188 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
190 std::list<std::string> aResult;
191 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
193 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
194 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
195 aResult.push_back(anAttrsIter->first);
201 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
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());
213 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
215 mySendAttributeUpdated = !theBlock;
218 void Model_Data::erase()
221 myLab.ForgetAllAttributes();
224 void Model_Data::execState(const ModelAPI_ExecState theState)
226 if (theState != ModelAPI_StateNothing)
227 TDataStd_Integer::Set(myLab, (int)theState);
230 ModelAPI_ExecState Model_Data::execState()
232 Handle(TDataStd_Integer) aStateAttr;
233 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
234 return ModelAPI_ExecState(aStateAttr->Get());
236 return ModelAPI_StateMustBeUpdated; // default value
239 void Model_Data::setError(const std::string& theError)
241 execState(ModelAPI_StateExecFailed);
242 Events_Error::send(theError);
245 int Model_Data::featureId() const
247 return myLab.Father().Tag(); // tag of the feature label
250 void Model_Data::eraseBackReferences()
253 std::shared_ptr<ModelAPI_Result> aRes =
254 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
256 aRes->setIsConcealed(false);
259 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
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);
266 aRes->setIsConcealed(true);
271 void Model_Data::referencesToObjects(
272 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
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());
299 continue; // nothing to do, not reference
301 if (!aReferenced.empty()) {
302 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));