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 <TDF_AttributeIterator.hxx>
33 #include <TDF_ChildIterator.hxx>
34 #include <TDF_RelocationTable.hxx>
39 // TDataStd_Name - name of the object
40 // TDataStd_Integer - state of the object execution
42 Model_Data::Model_Data() : mySendAttributeUpdated(true)
46 void Model_Data::setLabel(TDF_Label theLab)
51 std::string Model_Data::name()
53 Handle(TDataStd_Name) aName;
54 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
55 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
56 return ""; // not defined
59 void Model_Data::setName(const std::string& theName)
61 bool isModified = false;
62 Handle(TDataStd_Name) aName;
63 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
64 TDataStd_Name::Set(myLab, theName.c_str());
67 isModified = !aName->Get().IsEqual(theName.c_str());
69 aName->Set(theName.c_str());
73 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
76 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
77 ModelAPI_Attribute* anAttr = 0;
78 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
79 anAttr = new Model_AttributeDocRef(anAttrLab);
80 } else if (theAttrType == Model_AttributeInteger::type()) {
81 anAttr = new Model_AttributeInteger(anAttrLab);
82 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
83 anAttr = new Model_AttributeDouble(anAttrLab);
84 } else if (theAttrType == Model_AttributeBoolean::type()) {
85 anAttr = new Model_AttributeBoolean(anAttrLab);
86 } else if (theAttrType == Model_AttributeString::type()) {
87 anAttr = new Model_AttributeString(anAttrLab);
88 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
89 anAttr = new Model_AttributeReference(anAttrLab);
90 } else if (theAttrType == ModelAPI_AttributeSelection::type()) {
91 anAttr = new Model_AttributeSelection(anAttrLab);
92 } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
93 anAttr = new Model_AttributeSelectionList(anAttrLab);
94 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
95 anAttr = new Model_AttributeRefAttr(anAttrLab);
96 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
97 anAttr = new Model_AttributeRefList(anAttrLab);
98 } else if (theAttrType == ModelAPI_AttributeIntArray::type()) {
99 anAttr = new Model_AttributeIntArray(anAttrLab);
101 // create also GeomData attributes here because only here the OCAF strucure is known
102 else if (theAttrType == GeomData_Point::type()) {
103 anAttr = new GeomData_Point(anAttrLab);
104 } else if (theAttrType == GeomData_Dir::type()) {
105 anAttr = new GeomData_Dir(anAttrLab);
106 } else if (theAttrType == GeomData_Point2D::type()) {
107 anAttr = new GeomData_Point2D(anAttrLab);
110 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
111 myAttrs[theID] = aResult;
112 anAttr->setObject(myObject);
113 anAttr->setID(theID);
115 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
120 // macro for gthe generic returning of the attribute by the ID
121 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
122 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
123 std::shared_ptr<ATTR_TYPE> aRes; \
124 std::map<std::string, AttributePtr >::iterator aFound = \
125 myAttrs.find(theID); \
126 if (aFound != myAttrs.end()) { \
127 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
131 // implement nice getting methods for all ModelAPI attributes
132 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
133 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
134 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
135 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
136 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
137 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
138 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
139 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
140 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
141 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
142 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
144 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
146 std::shared_ptr<ModelAPI_Attribute> aResult;
147 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
149 return myAttrs[theID];
152 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
154 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
156 for (; anAttr != myAttrs.end(); anAttr++) {
157 if (anAttr->second == theAttr)
158 return anAttr->first;
161 static std::string anEmpty;
165 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
167 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
169 return myLab.IsEqual(aData->myLab) == Standard_True ;
173 bool Model_Data::isValid()
175 return !myLab.IsNull() && myLab.HasAttribute();
178 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
180 std::list<std::shared_ptr<ModelAPI_Attribute> > 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->second);
191 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
193 std::list<std::string> aResult;
194 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
196 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
197 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
198 aResult.push_back(anAttrsIter->first);
204 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
206 theAttr->setInitialized();
207 if (theAttr->isArgument()) {
208 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
209 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
210 if (mySendAttributeUpdated && myObject) {
211 myObject->attributeChanged(theAttr->id());
216 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
218 mySendAttributeUpdated = !theBlock;
221 void Model_Data::erase()
224 myLab.ForgetAllAttributes();
227 void Model_Data::execState(const ModelAPI_ExecState theState)
229 if (theState != ModelAPI_StateNothing)
230 TDataStd_Integer::Set(myLab, (int)theState);
233 ModelAPI_ExecState Model_Data::execState()
235 Handle(TDataStd_Integer) aStateAttr;
236 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
237 return ModelAPI_ExecState(aStateAttr->Get());
239 return ModelAPI_StateMustBeUpdated; // default value
242 void Model_Data::setError(const std::string& theError)
244 execState(ModelAPI_StateExecFailed);
245 Events_Error::send(theError);
248 int Model_Data::featureId() const
250 return myLab.Father().Tag(); // tag of the feature label
253 void Model_Data::eraseBackReferences()
256 std::shared_ptr<ModelAPI_Result> aRes =
257 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
259 aRes->setIsConcealed(false);
262 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
264 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
265 if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
266 std::shared_ptr<ModelAPI_Result> aRes =
267 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
269 aRes->setIsConcealed(true);
274 void Model_Data::referencesToObjects(
275 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
277 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
278 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
279 for(; anAttr != myAttrs.end(); anAttr++) {
280 std::string aType = anAttr->second->attributeType();
281 if (aType == ModelAPI_AttributeReference::type()) { // reference to object
282 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
283 ModelAPI_AttributeReference>(anAttr->second);
284 aReferenced.push_back(aRef->value());
285 } else if (aType == ModelAPI_AttributeRefAttr::type()) { // reference to attribute or object
286 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
287 ModelAPI_AttributeRefAttr>(anAttr->second);
288 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
289 } else if (aType == ModelAPI_AttributeRefList::type()) { // list of references
290 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
291 } else if (aType == ModelAPI_AttributeSelection::type()) { // selection attribute
292 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
293 ModelAPI_AttributeSelection>(anAttr->second);
294 aReferenced.push_back(aRef->context());
295 } else if (aType == ModelAPI_AttributeSelectionList::type()) { // list of selection attributes
296 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
297 ModelAPI_AttributeSelectionList>(anAttr->second);
298 for(int a = aRef->size() - 1; a >= 0; a--) {
299 aReferenced.push_back(aRef->value(a)->context());
302 continue; // nothing to do, not reference
304 if (!aReferenced.empty()) {
305 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
311 /// makes copy of all attributes on the given label and all sub-labels
312 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
313 TDF_AttributeIterator anAttrIter(theSource);
314 for(; anAttrIter.More(); anAttrIter.Next()) {
315 Handle(TDF_Attribute) aTargetAttr;
316 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
317 // create a new attribute if not yet exists in the destination
318 aTargetAttr = anAttrIter.Value()->NewEmpty();
319 theDestination.AddAttribute(aTargetAttr);
321 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
322 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
324 // copy the sub-labels content
325 TDF_ChildIterator aSubLabsIter(theSource);
326 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
327 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
331 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
333 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
334 copyAttrs(myLab, aTargetRoot);
335 // make initialized the initialized attributes
336 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
337 for(; aMyIter != myAttrs.end(); aMyIter++) {
338 if (aMyIter->second->isInitialized()) {
339 theTarget->attribute(aMyIter->first)->setInitialized();