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);
104 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
108 // macro for gthe generic returning of the attribute by the ID
109 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
110 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
111 std::shared_ptr<ATTR_TYPE> aRes; \
112 std::map<std::string, AttributePtr >::iterator aFound = \
113 myAttrs.find(theID); \
114 if (aFound != myAttrs.end()) { \
115 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
119 // implement nice getting methods for all ModelAPI attributes
120 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
121 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
122 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
123 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
124 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
125 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
126 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
127 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
128 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
129 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
131 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
133 std::shared_ptr<ModelAPI_Attribute> aResult;
134 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
136 return myAttrs[theID];
139 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
141 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
143 for (; anAttr != myAttrs.end(); anAttr++) {
144 if (anAttr->second == theAttr)
145 return anAttr->first;
148 static std::string anEmpty;
152 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
154 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
156 return myLab.IsEqual(aData->myLab) == Standard_True ;
160 bool Model_Data::isValid()
162 return !myLab.IsNull() && myLab.HasAttribute();
165 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
167 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
168 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
170 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
171 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
172 aResult.push_back(anAttrsIter->second);
178 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
180 std::list<std::string> aResult;
181 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
183 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
184 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
185 aResult.push_back(anAttrsIter->first);
191 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
193 theAttr->setInitialized();
194 if (theAttr->isArgument()) {
195 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
196 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
198 myObject->attributeChanged();
203 void Model_Data::erase()
206 myLab.ForgetAllAttributes();
209 void Model_Data::execState(const ModelAPI_ExecState theState)
211 if (theState != ModelAPI_StateNothing)
212 TDataStd_Integer::Set(myLab, (int)theState);
215 ModelAPI_ExecState Model_Data::execState()
217 Handle(TDataStd_Integer) aStateAttr;
218 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
219 return ModelAPI_ExecState(aStateAttr->Get());
221 return ModelAPI_StateMustBeUpdated; // default value
224 void Model_Data::setError(const std::string& theError)
226 execState(ModelAPI_StateExecFailed);
227 Events_Error::send(theError);
230 int Model_Data::featureId() const
232 return myLab.Father().Tag(); // tag of the feature label
235 void Model_Data::eraseBackReferences()
238 std::shared_ptr<ModelAPI_Result> aRes =
239 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
241 aRes->setIsConcealed(false);
244 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
246 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
247 if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
248 std::shared_ptr<ModelAPI_Result> aRes =
249 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
251 aRes->setIsConcealed(true);
256 void Model_Data::referencesToObjects(
257 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
259 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
260 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
261 for(; anAttr != myAttrs.end(); anAttr++) {
262 std::string aType = anAttr->second->attributeType();
263 if (aType == ModelAPI_AttributeReference::type()) { // reference to object
264 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
265 ModelAPI_AttributeReference>(anAttr->second);
266 aReferenced.push_back(aRef->value());
267 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
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));