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>
32 #include <TDataStd_AsciiString.hxx>
33 #include <TDF_AttributeIterator.hxx>
34 #include <TDF_ChildIterator.hxx>
35 #include <TDF_RelocationTable.hxx>
40 // TDataStd_Name - name of the object
41 // TDataStd_Integer - state of the object execution
43 Model_Data::Model_Data() : mySendAttributeUpdated(true)
47 void Model_Data::setLabel(TDF_Label theLab)
52 std::string Model_Data::name()
54 Handle(TDataStd_Name) aName;
55 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
56 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
57 return ""; // not defined
60 void Model_Data::setName(const std::string& theName)
62 bool isModified = false;
63 Handle(TDataStd_Name) aName;
64 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
65 TDataStd_Name::Set(myLab, theName.c_str());
68 isModified = !aName->Get().IsEqual(theName.c_str());
70 aName->Set(theName.c_str());
74 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
77 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
78 ModelAPI_Attribute* anAttr = 0;
79 if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
80 anAttr = new Model_AttributeDocRef(anAttrLab);
81 } else if (theAttrType == Model_AttributeInteger::typeId()) {
82 anAttr = new Model_AttributeInteger(anAttrLab);
83 } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
84 anAttr = new Model_AttributeDouble(anAttrLab);
85 } else if (theAttrType == Model_AttributeBoolean::typeId()) {
86 anAttr = new Model_AttributeBoolean(anAttrLab);
87 } else if (theAttrType == Model_AttributeString::typeId()) {
88 anAttr = new Model_AttributeString(anAttrLab);
89 } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
90 anAttr = new Model_AttributeReference(anAttrLab);
91 } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
92 anAttr = new Model_AttributeSelection(anAttrLab);
93 } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
94 anAttr = new Model_AttributeSelectionList(anAttrLab);
95 } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
96 anAttr = new Model_AttributeRefAttr(anAttrLab);
97 } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
98 anAttr = new Model_AttributeRefList(anAttrLab);
99 } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
100 anAttr = new Model_AttributeIntArray(anAttrLab);
102 // create also GeomData attributes here because only here the OCAF strucure is known
103 else if (theAttrType == GeomData_Point::typeId()) {
104 anAttr = new GeomData_Point(anAttrLab);
105 } else if (theAttrType == GeomData_Dir::typeId()) {
106 anAttr = new GeomData_Dir(anAttrLab);
107 } else if (theAttrType == GeomData_Point2D::typeId()) {
108 anAttr = new GeomData_Point2D(anAttrLab);
111 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
112 myAttrs[theID] = aResult;
113 anAttr->setObject(myObject);
114 anAttr->setID(theID);
116 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
121 // macro for gthe generic returning of the attribute by the ID
122 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
123 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
124 std::shared_ptr<ATTR_TYPE> aRes; \
125 std::map<std::string, AttributePtr >::iterator aFound = \
126 myAttrs.find(theID); \
127 if (aFound != myAttrs.end()) { \
128 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
132 // implement nice getting methods for all ModelAPI attributes
133 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
134 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
135 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
136 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
137 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
138 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
139 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
140 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
141 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
142 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
143 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
145 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
147 std::shared_ptr<ModelAPI_Attribute> aResult;
148 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
150 return myAttrs[theID];
153 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
155 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
157 for (; anAttr != myAttrs.end(); anAttr++) {
158 if (anAttr->second == theAttr)
159 return anAttr->first;
162 static std::string anEmpty;
166 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
168 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
170 return myLab.IsEqual(aData->myLab) == Standard_True ;
174 bool Model_Data::isValid()
176 return !myLab.IsNull() && myLab.HasAttribute();
179 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
181 std::list<std::shared_ptr<ModelAPI_Attribute> > 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->second);
192 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
194 std::list<std::string> aResult;
195 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
197 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
198 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
199 aResult.push_back(anAttrsIter->first);
205 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
207 theAttr->setInitialized();
208 if (theAttr->isArgument()) {
209 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
210 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
211 if (mySendAttributeUpdated && myObject) {
212 myObject->attributeChanged(theAttr->id());
217 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
219 mySendAttributeUpdated = !theBlock;
222 void Model_Data::erase()
225 myLab.ForgetAllAttributes();
228 void Model_Data::execState(const ModelAPI_ExecState theState)
230 if (theState != ModelAPI_StateNothing)
231 TDataStd_Integer::Set(myLab, (int)theState);
234 ModelAPI_ExecState Model_Data::execState()
236 Handle(TDataStd_Integer) aStateAttr;
237 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
238 return ModelAPI_ExecState(aStateAttr->Get());
240 return ModelAPI_StateMustBeUpdated; // default value
243 void Model_Data::setError(const std::string& theError, bool theSend)
245 execState(ModelAPI_StateExecFailed);
247 Events_Error::send(theError);
249 TDataStd_AsciiString::Set(myLab, theError.c_str());
252 std::string Model_Data::error() const
254 Handle(TDataStd_AsciiString) anErrorAttr;
255 if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
256 return std::string(anErrorAttr->Get().ToCString());
258 return std::string();
261 int Model_Data::featureId() const
263 return myLab.Father().Tag(); // tag of the feature label
266 void Model_Data::eraseBackReferences()
269 std::shared_ptr<ModelAPI_Result> aRes =
270 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
272 aRes->setIsConcealed(false);
275 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
277 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
278 if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
281 myRefsToMe.erase(anAttribute);
284 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
285 const bool theApplyConcealment)
287 // do not add the same attribute twice
288 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
289 if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
292 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
293 if (theApplyConcealment &&
294 ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
295 std::shared_ptr<ModelAPI_Result> aRes =
296 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
298 aRes->setIsConcealed(true);
303 void Model_Data::referencesToObjects(
304 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
306 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
307 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
308 for(; anAttr != myAttrs.end(); anAttr++) {
309 std::string aType = anAttr->second->attributeType();
310 if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
311 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
312 ModelAPI_AttributeReference>(anAttr->second);
313 aReferenced.push_back(aRef->value());
314 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
315 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
316 ModelAPI_AttributeRefAttr>(anAttr->second);
317 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
318 } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
319 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
320 } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
321 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
322 ModelAPI_AttributeSelection>(anAttr->second);
323 aReferenced.push_back(aRef->context());
324 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
325 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
326 ModelAPI_AttributeSelectionList>(anAttr->second);
327 for(int a = aRef->size() - 1; a >= 0; a--) {
328 aReferenced.push_back(aRef->value(a)->context());
331 continue; // nothing to do, not reference
333 if (!aReferenced.empty()) {
334 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
340 /// makes copy of all attributes on the given label and all sub-labels
341 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
342 TDF_AttributeIterator anAttrIter(theSource);
343 for(; anAttrIter.More(); anAttrIter.Next()) {
344 Handle(TDF_Attribute) aTargetAttr;
345 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
346 // create a new attribute if not yet exists in the destination
347 aTargetAttr = anAttrIter.Value()->NewEmpty();
348 theDestination.AddAttribute(aTargetAttr);
350 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
351 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
353 // copy the sub-labels content
354 TDF_ChildIterator aSubLabsIter(theSource);
355 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
356 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
360 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
362 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
363 copyAttrs(myLab, aTargetRoot);
364 // make initialized the initialized attributes
365 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
366 for(; aMyIter != myAttrs.end(); aMyIter++) {
367 if (aMyIter->second->isInitialized()) {
368 theTarget->attribute(aMyIter->first)->setInitialized();