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>
38 // TDataStd_Name - name of the object
39 // TDataStd_Integer - state of the object execution
41 Model_Data::Model_Data() : mySendAttributeUpdated(true)
45 void Model_Data::setLabel(TDF_Label theLab)
50 std::string Model_Data::name()
52 Handle(TDataStd_Name) aName;
53 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
54 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
55 return ""; // not defined
58 void Model_Data::setName(const std::string& theName)
60 bool isModified = false;
61 Handle(TDataStd_Name) aName;
62 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
63 TDataStd_Name::Set(myLab, theName.c_str());
66 isModified = !aName->Get().IsEqual(theName.c_str());
68 aName->Set(theName.c_str());
72 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
75 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
76 ModelAPI_Attribute* anAttr = 0;
77 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
78 anAttr = new Model_AttributeDocRef(anAttrLab);
79 } else if (theAttrType == Model_AttributeInteger::type()) {
80 anAttr = new Model_AttributeInteger(anAttrLab);
81 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
82 anAttr = new Model_AttributeDouble(anAttrLab);
83 } else if (theAttrType == Model_AttributeBoolean::type()) {
84 anAttr = new Model_AttributeBoolean(anAttrLab);
85 } else if (theAttrType == Model_AttributeString::type()) {
86 anAttr = new Model_AttributeString(anAttrLab);
87 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
88 anAttr = new Model_AttributeReference(anAttrLab);
89 } else if (theAttrType == ModelAPI_AttributeSelection::type()) {
90 anAttr = new Model_AttributeSelection(anAttrLab);
91 } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
92 anAttr = new Model_AttributeSelectionList(anAttrLab);
93 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
94 anAttr = new Model_AttributeRefAttr(anAttrLab);
95 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
96 anAttr = new Model_AttributeRefList(anAttrLab);
97 } else if (theAttrType == ModelAPI_AttributeIntArray::type()) {
98 anAttr = new Model_AttributeIntArray(anAttrLab);
100 // create also GeomData attributes here because only here the OCAF strucure is known
101 else if (theAttrType == GeomData_Point::type()) {
102 anAttr = new GeomData_Point(anAttrLab);
103 } else if (theAttrType == GeomData_Dir::type()) {
104 anAttr = new GeomData_Dir(anAttrLab);
105 } else if (theAttrType == GeomData_Point2D::type()) {
106 anAttr = new GeomData_Point2D(anAttrLab);
109 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
110 myAttrs[theID] = aResult;
111 anAttr->setObject(myObject);
112 anAttr->setID(theID);
114 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
119 // macro for gthe generic returning of the attribute by the ID
120 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
121 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
122 std::shared_ptr<ATTR_TYPE> aRes; \
123 std::map<std::string, AttributePtr >::iterator aFound = \
124 myAttrs.find(theID); \
125 if (aFound != myAttrs.end()) { \
126 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
130 // implement nice getting methods for all ModelAPI attributes
131 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
132 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
133 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
134 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
135 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
136 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
137 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
138 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
139 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
140 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
141 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
143 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
145 std::shared_ptr<ModelAPI_Attribute> aResult;
146 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
148 return myAttrs[theID];
151 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
153 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
155 for (; anAttr != myAttrs.end(); anAttr++) {
156 if (anAttr->second == theAttr)
157 return anAttr->first;
160 static std::string anEmpty;
164 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
166 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
168 return myLab.IsEqual(aData->myLab) == Standard_True ;
172 bool Model_Data::isValid()
174 return !myLab.IsNull() && myLab.HasAttribute();
177 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
179 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
180 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
182 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
183 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
184 aResult.push_back(anAttrsIter->second);
190 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
192 std::list<std::string> aResult;
193 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
195 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
196 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
197 aResult.push_back(anAttrsIter->first);
203 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
205 theAttr->setInitialized();
206 if (theAttr->isArgument()) {
207 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
208 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
209 if (mySendAttributeUpdated && myObject) {
210 myObject->attributeChanged(theAttr->id());
215 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
217 mySendAttributeUpdated = !theBlock;
220 void Model_Data::erase()
223 myLab.ForgetAllAttributes();
226 void Model_Data::execState(const ModelAPI_ExecState theState)
228 if (theState != ModelAPI_StateNothing)
229 TDataStd_Integer::Set(myLab, (int)theState);
232 ModelAPI_ExecState Model_Data::execState()
234 Handle(TDataStd_Integer) aStateAttr;
235 if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
236 return ModelAPI_ExecState(aStateAttr->Get());
238 return ModelAPI_StateMustBeUpdated; // default value
241 void Model_Data::setError(const std::string& theError)
243 execState(ModelAPI_StateExecFailed);
244 Events_Error::send(theError);
247 int Model_Data::featureId() const
249 return myLab.Father().Tag(); // tag of the feature label
252 void Model_Data::eraseBackReferences()
255 std::shared_ptr<ModelAPI_Result> aRes =
256 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
258 aRes->setIsConcealed(false);
261 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
263 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
264 if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
265 std::shared_ptr<ModelAPI_Result> aRes =
266 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
268 aRes->setIsConcealed(true);
273 void Model_Data::referencesToObjects(
274 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
276 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
277 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
278 for(; anAttr != myAttrs.end(); anAttr++) {
279 std::string aType = anAttr->second->attributeType();
280 if (aType == ModelAPI_AttributeReference::type()) { // reference to object
281 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
282 ModelAPI_AttributeReference>(anAttr->second);
283 aReferenced.push_back(aRef->value());
284 } else if (aType == ModelAPI_AttributeRefAttr::type()) { // reference to attribute or object
285 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
286 ModelAPI_AttributeRefAttr>(anAttr->second);
287 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
288 } else if (aType == ModelAPI_AttributeRefList::type()) { // list of references
289 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
290 } else if (aType == ModelAPI_AttributeSelection::type()) { // selection attribute
291 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
292 ModelAPI_AttributeSelection>(anAttr->second);
293 aReferenced.push_back(aRef->context());
294 } else if (aType == ModelAPI_AttributeSelectionList::type()) { // list of selection attributes
295 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
296 ModelAPI_AttributeSelectionList>(anAttr->second);
297 for(int a = aRef->size() - 1; a >= 0; a--) {
298 aReferenced.push_back(aRef->value(a)->context());
301 continue; // nothing to do, not reference
303 if (!aReferenced.empty()) {
304 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
310 /// makes copy of all attributes on the given label and all sub-labels
311 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
312 TDF_AttributeIterator anAttrIter(theSource);
313 for(; anAttrIter.More(); anAttrIter.Next()) {
314 Handle(TDF_Attribute) aTargetAttr;
315 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
316 // create a new attribute if not yet exists in the destination
317 aTargetAttr = anAttrIter.Value()->NewEmpty();
318 theDestination.AddAttribute(aTargetAttr);
320 Handle(TDF_RelocationTable) aRelocTable; // no relocation, empty map
321 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
323 // copy the sub-labels content
324 TDF_ChildIterator aSubLabsIter(theSource);
325 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
326 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
330 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
332 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
333 copyAttrs(myLab, aTargetRoot);