1 // File: Model_Data.hxx
2 // Created: 21 Mar 2014
3 // Author: Mikhail PONIKAROV
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>
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>
28 #include <TDataStd_Name.hxx>
33 // TDataStd_Name - name of the object
34 // TDataStd_Integer - state of the object execution
36 Model_Data::Model_Data()
40 void Model_Data::setLabel(TDF_Label theLab)
45 std::string Model_Data::name()
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
53 void Model_Data::setName(const std::string& theName)
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());
61 isModified = !aName->Get().IsEqual(theName.c_str());
63 aName->Set(theName.c_str());
67 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
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);
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);
101 myAttrs[theID] = std::shared_ptr<ModelAPI_Attribute>(anAttr);
102 anAttr->setObject(myObject);
103 anAttr->setID(theID);
105 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
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); \
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);
132 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
134 std::shared_ptr<ModelAPI_Attribute> aResult;
135 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
137 return myAttrs[theID];
140 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
142 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
144 for (; anAttr != myAttrs.end(); anAttr++) {
145 if (anAttr->second == theAttr)
146 return anAttr->first;
149 static std::string anEmpty;
153 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
155 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
157 return myLab.IsEqual(aData->myLab) == Standard_True ;
161 bool Model_Data::isValid()
163 return !myLab.IsNull() && myLab.HasAttribute();
166 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
168 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
169 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
171 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
172 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
173 aResult.push_back(anAttrsIter->second);
179 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
181 std::list<std::string> aResult;
182 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
184 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
185 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
186 aResult.push_back(anAttrsIter->first);
192 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
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);
199 myObject->attributeChanged(theAttr->id());
204 void Model_Data::erase()
207 myLab.ForgetAllAttributes();
210 void Model_Data::execState(const ModelAPI_ExecState theState)
212 if (theState != ModelAPI_StateNothing)
213 TDataStd_Integer::Set(myLab, (int)theState);
216 ModelAPI_ExecState Model_Data::execState()
218 Handle(TDataStd_Integer) aStateAttr;
219 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
220 return ModelAPI_ExecState(aStateAttr->Get());
222 return ModelAPI_StateMustBeUpdated; // default value
225 void Model_Data::setError(const std::string& theError)
227 execState(ModelAPI_StateExecFailed);
228 Events_Error::send(theError);
231 int Model_Data::featureId() const
233 return myLab.Father().Tag(); // tag of the feature label
236 void Model_Data::eraseBackReferences()
239 std::shared_ptr<ModelAPI_Result> aRes =
240 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
242 aRes->setIsConcealed(false);
245 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
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);
252 aRes->setIsConcealed(true);
257 void Model_Data::referencesToObjects(
258 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
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());
285 continue; // nothing to do, not reference
287 if (!aReferenced.empty()) {
288 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));