Salome HOME
Reference attributes added
authormpv <mikhail.ponikarov@opencascade.com>
Thu, 8 May 2014 06:19:00 +0000 (10:19 +0400)
committermpv <mikhail.ponikarov@opencascade.com>
Thu, 8 May 2014 06:19:00 +0000 (10:19 +0400)
13 files changed:
src/Model/CMakeLists.txt
src/Model/Model_AttributeRefAttr.cpp [new file with mode: 0644]
src/Model/Model_AttributeRefAttr.h [new file with mode: 0644]
src/Model/Model_AttributeReference.cpp [new file with mode: 0644]
src/Model/Model_AttributeReference.h [new file with mode: 0644]
src/Model/Model_Data.cpp
src/Model/Model_Data.h
src/ModelAPI/CMakeLists.txt
src/ModelAPI/ModelAPI.i
src/ModelAPI/ModelAPI_AttributeRefAttr.h [new file with mode: 0644]
src/ModelAPI/ModelAPI_AttributeReference.h [new file with mode: 0644]
src/ModelAPI/ModelAPI_Data.h
src/ModelAPI/ModelAPI_PluginManager.cpp

index ea081fac16f7e95623a5c452d05fe66ae6271f75..872f089f629ae44573fddb738bb1d1ba0fce2e90 100644 (file)
@@ -9,6 +9,8 @@ SET(PROJECT_HEADERS
     Model_Data.h
     Model_AttributeDouble.h
     Model_AttributeDocRef.h
+    Model_AttributeReference.h
+    Model_AttributeRefAttr.h
     Model_Events.h
 )
 
@@ -19,6 +21,8 @@ SET(PROJECT_SOURCES
     Model_Data.cpp
     Model_AttributeDouble.cpp
     Model_AttributeDocRef.cpp
+    Model_AttributeReference.cpp
+    Model_AttributeRefAttr.cpp
     Model_Events.cpp
 )
 
diff --git a/src/Model/Model_AttributeRefAttr.cpp b/src/Model/Model_AttributeRefAttr.cpp
new file mode 100644 (file)
index 0000000..bedda92
--- /dev/null
@@ -0,0 +1,59 @@
+// File:        ModelAPI_AttributeRefAttr.cxx
+// Created:     2 Apr 2014
+// Author:      Mikhail PONIKAROV
+
+#include "Model_AttributeRefAttr.h"
+#include "Model_Application.h"
+#include "Model_Events.h"
+#include "Model_Data.h"
+#include <ModelAPI_Feature.h>
+#include <Events_Loop.h>
+
+using namespace std;
+
+void Model_AttributeRefAttr::setValue(boost::shared_ptr<ModelAPI_Attribute> theAttr)
+{
+  if (value() != theAttr) {
+    boost::shared_ptr<Model_Data> aData = 
+      boost::dynamic_pointer_cast<Model_Data>(theAttr->feature()->data());
+    if (myRef.IsNull()) {
+      boost::shared_ptr<Model_Data> aMyData = 
+        boost::dynamic_pointer_cast<Model_Data>(feature()->data());
+      TDF_Reference::Set(aMyData->label(), aData->label());
+    } else {
+      myRef->Set(aData->label());
+    }
+    myID->Set(aData->id(theAttr).c_str());
+
+    static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
+    Model_FeatureUpdatedMessage aMsg(feature(), anEvent);
+    Events_Loop::loop()->send(aMsg);
+  }
+}
+
+boost::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::value()
+{
+  if (!myRef.IsNull()) {
+    boost::shared_ptr<Model_Document> aDoc = 
+      boost::dynamic_pointer_cast<Model_Document>(feature()->document());
+    if (aDoc) {
+      TDF_Label aRefLab = myRef->Get();
+      TDF_Label aFeatureLab = aRefLab.Father();
+      boost::shared_ptr<Model_Data> aData = 
+        boost::dynamic_pointer_cast<Model_Data>(aDoc->feature(aRefLab)->data());
+      return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
+    }
+  }
+  // not initialized
+  return boost::shared_ptr<ModelAPI_Attribute>();
+}
+
+Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
+{
+  // check the attribute could be already presented in this doc (after load document)
+  if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myID)) {
+    // create attribute: not initialized by value yet
+    TDataStd_Comment::Set(theLabel, "");
+    // reference attribute is not set to the label!
+  }
+}
diff --git a/src/Model/Model_AttributeRefAttr.h b/src/Model/Model_AttributeRefAttr.h
new file mode 100644 (file)
index 0000000..8422966
--- /dev/null
@@ -0,0 +1,38 @@
+// File:        Model_AttributeRefAttr.h
+// Created:     8 May 2014
+// Author:      Mikhail PONIKAROV
+
+#ifndef Model_AttributeRefAttr_HeaderFile
+#define Model_AttributeRefAttr_HeaderFile
+
+#include "Model.h"
+#include "ModelAPI_AttributeRefAttr.h"
+#include <TDF_Label.hxx>
+#include <TDF_Reference.hxx>
+#include <TDataStd_Comment.hxx>
+
+/**\class Model_AttributeRefAttr
+ * \ingroup DataModel
+ * \brief Attribute that contains reference to an attribute of a feature 
+ * (located in the same document).
+ */
+
+class Model_AttributeRefAttr : public ModelAPI_AttributeRefAttr
+{
+  Handle_TDF_Reference myRef; ///< reference to the feature label
+  Handle_TDataStd_Comment myID; ///< ID of the referenced attirbute
+public:
+  /// Defines the attribute referenced from this attribute
+  MODEL_EXPORT virtual void setValue(boost::shared_ptr<ModelAPI_Attribute> theAttr);
+
+  /// Returns attribute referenced from this attribute
+  MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Attribute> value();
+
+protected:
+  /// Objects are created for features automatically
+  MODEL_EXPORT Model_AttributeRefAttr(TDF_Label& theLabel);
+
+  friend class Model_Data;
+};
+
+#endif
diff --git a/src/Model/Model_AttributeReference.cpp b/src/Model/Model_AttributeReference.cpp
new file mode 100644 (file)
index 0000000..ab247fd
--- /dev/null
@@ -0,0 +1,53 @@
+// File:        ModelAPI_AttributeReference.cxx
+// Created:     2 Apr 2014
+// Author:      Mikhail PONIKAROV
+
+#include "Model_AttributeReference.h"
+#include "Model_Application.h"
+#include "Model_Events.h"
+#include "Model_Data.h"
+#include <ModelAPI_Feature.h>
+#include <Events_Loop.h>
+
+using namespace std;
+
+void Model_AttributeReference::setValue(boost::shared_ptr<ModelAPI_Feature> theFeature)
+{
+  if (value() != theFeature) {
+    boost::shared_ptr<Model_Data> aData = 
+      boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
+    if (myRef.IsNull()) {
+      boost::shared_ptr<Model_Data> aMyData = 
+        boost::dynamic_pointer_cast<Model_Data>(feature()->data());
+      TDF_Reference::Set(aMyData->label(), aData->label());
+    } else {
+      myRef->Set(aData->label());
+    }
+
+    static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
+    Model_FeatureUpdatedMessage aMsg(feature(), anEvent);
+    Events_Loop::loop()->send(aMsg);
+  }
+}
+
+boost::shared_ptr<ModelAPI_Feature> Model_AttributeReference::value()
+{
+  if (!myRef.IsNull()) {
+    boost::shared_ptr<Model_Document> aDoc = 
+      boost::dynamic_pointer_cast<Model_Document>(feature()->document());
+    if (aDoc) {
+      TDF_Label aRefLab = myRef->Get();
+      return aDoc->feature(aRefLab);
+    }
+  }
+  // not initialized
+  return boost::shared_ptr<ModelAPI_Feature>();
+}
+
+Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
+{
+  // check the attribute could be already presented in this doc (after load document)
+  if (!theLabel.FindAttribute(TDF_Reference::GetID(), myRef)) {
+    // create attribute: not initialized by value yet: attribute is not set to the label!
+  }
+}
diff --git a/src/Model/Model_AttributeReference.h b/src/Model/Model_AttributeReference.h
new file mode 100644 (file)
index 0000000..e17855e
--- /dev/null
@@ -0,0 +1,35 @@
+// File:        Model_AttributeReference.h
+// Created:     8 May 2014
+// Author:      Mikhail PONIKAROV
+
+#ifndef Model_AttributeReference_HeaderFile
+#define Model_AttributeReference_HeaderFile
+
+#include "Model.h"
+#include "ModelAPI_AttributeReference.h"
+#include <TDF_Label.hxx>
+#include <TDF_Reference.hxx>
+
+/**\class Model_AttributeReference
+ * \ingroup DataModel
+ * \brief Attribute that contains reference to feature (located in the same document).
+ */
+
+class Model_AttributeReference : public ModelAPI_AttributeReference
+{
+  Handle_TDF_Reference myRef; ///< references to the feature label
+public:
+  /// Defines the feature referenced from this attribute
+  MODEL_EXPORT virtual void setValue(boost::shared_ptr<ModelAPI_Feature> theFeature);
+
+  /// Returns feature referenced from this attribute
+  MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature> value();
+
+protected:
+  /// Objects are created for features automatically
+  MODEL_EXPORT Model_AttributeReference(TDF_Label& theLabel);
+
+  friend class Model_Data;
+};
+
+#endif
index 5b26417a200191515540f1806624487bd9f6b266..0c8f77bdfb8ba6875b77d61102940e57affcdb79 100644 (file)
@@ -5,6 +5,8 @@
 #include <Model_Data.h>
 #include <Model_AttributeDocRef.h>
 #include <Model_AttributeDouble.h>
+#include <Model_AttributeReference.h>
+#include <Model_AttributeRefAttr.h>
 #include <GeomData_Point.h>
 #include <GeomData_Point2D.h>
 #include <GeomData_Dir.h>
@@ -42,6 +44,10 @@ void Model_Data::addAttribute(string theID, string theAttrType)
     anAttr = new Model_AttributeDocRef(anAttrLab);
   else if (theAttrType == ModelAPI_AttributeDouble::type())
     anAttr = new Model_AttributeDouble(anAttrLab);
+  else if (theAttrType == ModelAPI_AttributeReference::type())
+    anAttr = new Model_AttributeReference(anAttrLab);
+  else if (theAttrType == ModelAPI_AttributeRefAttr::type())
+    anAttr = new Model_AttributeRefAttr(anAttrLab);
   else if (theAttrType == GeomData_Point::type())
     anAttr = new GeomData_Point(anAttrLab);
   else if (theAttrType == GeomData_Dir::type())
@@ -87,6 +93,36 @@ boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
   return aRes;
 }
 
+boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
+  if (aFound == myAttrs.end()) {
+    // TODO: generate error on unknown attribute request and/or add mechanism for customization
+    return boost::shared_ptr<ModelAPI_AttributeReference>();
+  }
+  boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
+  if (!aRes) {
+    // TODO: generate error on invalid attribute type request
+  }
+  return aRes;
+}
+
+boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
+  if (aFound == myAttrs.end()) {
+    // TODO: generate error on unknown attribute request and/or add mechanism for customization
+    return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
+  }
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
+  if (!aRes) {
+    // TODO: generate error on invalid attribute type request
+  }
+  return aRes;
+}
+
 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
 {
   boost::shared_ptr<ModelAPI_Attribute> aResult;
@@ -94,3 +130,14 @@ boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string th
     return aResult;
   return myAttrs[theID];
 }
+
+const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
+{
+  map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
+  for(; anAttr != myAttrs.end(); anAttr++) {
+    if (anAttr->second == theAttr) return anAttr->first;
+  }
+  // not found
+  static string anEmpty;
+  return anEmpty;
+}
index 0cb633666de769388f272733148f6962dd47afed..56d81e1235cda5054d40a5b776dbc05c47dfd04d 100644 (file)
@@ -31,9 +31,12 @@ class Model_Data: public ModelAPI_Data
 
   Model_Data();
 
+  /// Returns label of this feature
   TDF_Label label() {return myLab;}
 
   friend class Model_Document;
+  friend class Model_AttributeReference;
+  friend class Model_AttributeRefAttr;
 
 public:
   /// Returns the name of the feature visible by the user in the object browser
@@ -44,9 +47,18 @@ public:
   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_AttributeDocRef> docRef(const std::string theID);
   /// Returns the attribute that contains real value with double precision
   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_AttributeDouble> real(const std::string theID);
+  /// Returns the attribute that contains reference to a feature
+  MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_AttributeReference> 
+    reference(const std::string theID);
+  /// Returns the attribute that contains reference to an attribute of a feature
+  MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_AttributeRefAttr>
+    refattr(const std::string theID);
   /// Returns the generic attribute by identifier
   /// \param theID identifier of the attribute
   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Attribute> attribute(const std::string theID);
+  /// Identifier by the id (not fast, iteration by map)
+  /// \param theAttr attribute already created in this data
+  MODEL_EXPORT virtual const std::string& id(const boost::shared_ptr<ModelAPI_Attribute> theAttr);
 
   /// Initializes object by the attributes: must be called just after the object is created
   /// for each attribute of the object
index 2fe5b53597751ef83cbb2a21f70d5c2612d3a3f2..9c6015acd55206770a3e65a92b9face8dece5e4c 100644 (file)
@@ -12,6 +12,8 @@ SET(PROJECT_HEADERS
     ModelAPI_Attribute.h
     ModelAPI_AttributeDouble.h
     ModelAPI_AttributeDocRef.h
+    ModelAPI_AttributeReference.h
+    ModelAPI_AttributeRefAttr.h
 )
 
 SET(PROJECT_SOURCES
index 119f73d1678d706d8a6a1ed52b3f411344e3181e..097bdcfd25b523cb85e70a3f0b0a65a97f7fedc3 100644 (file)
@@ -9,6 +9,8 @@
   #include "ModelAPI_Attribute.h"
   #include "ModelAPI_AttributeDocRef.h"
   #include "ModelAPI_AttributeDouble.h"
+  #include "ModelAPI_AttributeReference.h"
+  #include "ModelAPI_AttributeRefAttr.h"
 %}
 
 // to avoid error on this
@@ -28,6 +30,8 @@
 %shared_ptr(ModelAPI_Attribute)
 %shared_ptr(ModelAPI_AttributeDocRef)
 %shared_ptr(ModelAPI_AttributeDouble)
+%shared_ptr(ModelAPI_AttributeReference)
+%shared_ptr(ModelAPI_AttributeRefAttr)
 
 // all supported interfaces
 %include "ModelAPI_Document.h"
@@ -37,3 +41,5 @@
 %include "ModelAPI_Attribute.h"
 %include "ModelAPI_AttributeDocRef.h"
 %include "ModelAPI_AttributeDouble.h"
+%include "ModelAPI_AttributeReference.h"
+%include "ModelAPI_AttributeRefAttr.h"
diff --git a/src/ModelAPI/ModelAPI_AttributeRefAttr.h b/src/ModelAPI/ModelAPI_AttributeRefAttr.h
new file mode 100644 (file)
index 0000000..d9c5f3a
--- /dev/null
@@ -0,0 +1,40 @@
+// File:        ModelAPI_AttributeRefAttr.h
+// Created:     8 May 2014
+// Author:      Mikhail PONIKAROV
+
+#ifndef ModelAPI_AttributeRefAttr_HeaderFile
+#define ModelAPI_AttributeRefAttr_HeaderFile
+
+#include "ModelAPI_Attribute.h"
+
+/**\class ModelAPI_AttributeRefAttr
+ * \ingroup DataModel
+ * \brief Attribute that contains reference to an attribute of a feature 
+ * (located in the same document).
+ */
+
+class ModelAPI_AttributeRefAttr : public ModelAPI_Attribute
+{
+public:
+  /// Defines the attribute referenced from this attribute
+  MODELAPI_EXPORT virtual void setValue(boost::shared_ptr<ModelAPI_Attribute> theAttr) = 0;
+
+  /// Returns attribute referenced from this attribute
+  MODELAPI_EXPORT virtual boost::shared_ptr<ModelAPI_Attribute> value() = 0;
+
+  /// Returns the type of this class of attributes
+  MODELAPI_EXPORT static std::string type() {return "RefAttr";}
+
+  /// Returns the type of this class of attributes, not static method
+  MODELAPI_EXPORT virtual std::string attributeType() {return type();}
+
+  /// To virtually destroy the fields of successors
+  MODELAPI_EXPORT virtual ~ModelAPI_AttributeRefAttr() {}
+
+protected:
+  /// Objects are created for features automatically
+  MODELAPI_EXPORT ModelAPI_AttributeRefAttr()
+  {}
+};
+
+#endif
diff --git a/src/ModelAPI/ModelAPI_AttributeReference.h b/src/ModelAPI/ModelAPI_AttributeReference.h
new file mode 100644 (file)
index 0000000..37b2256
--- /dev/null
@@ -0,0 +1,39 @@
+// File:        ModelAPI_AttributeReference.h
+// Created:     8 May 2014
+// Author:      Mikhail PONIKAROV
+
+#ifndef ModelAPI_AttributeReference_HeaderFile
+#define ModelAPI_AttributeReference_HeaderFile
+
+#include "ModelAPI_Attribute.h"
+
+/**\class ModelAPI_AttributeReference
+ * \ingroup DataModel
+ * \brief Attribute that contains reference to feature (located in the same document).
+ */
+
+class ModelAPI_AttributeReference : public ModelAPI_Attribute
+{
+public:
+  /// Defines the feature referenced from this attribute
+  MODELAPI_EXPORT virtual void setValue(boost::shared_ptr<ModelAPI_Feature> theFeature) = 0;
+
+  /// Returns feature referenced from this attribute
+  MODELAPI_EXPORT virtual boost::shared_ptr<ModelAPI_Feature> value() = 0;
+
+  /// Returns the type of this class of attributes
+  MODELAPI_EXPORT static std::string type() {return "Reference";}
+
+  /// Returns the type of this class of attributes, not static method
+  MODELAPI_EXPORT virtual std::string attributeType() {return type();}
+
+  /// To virtually destroy the fields of successors
+  MODELAPI_EXPORT virtual ~ModelAPI_AttributeReference() {}
+
+protected:
+  /// Objects are created for features automatically
+  MODELAPI_EXPORT ModelAPI_AttributeReference()
+  {}
+};
+
+#endif
index 2922f6d9b3891b4875e51e509a96a4d401670c94..86fc564096fcb24ff5d541c613304fb7fd1e932e 100644 (file)
@@ -11,6 +11,8 @@
 
 class ModelAPI_AttributeDocRef;
 class ModelAPI_AttributeDouble;
+class ModelAPI_AttributeReference;
+class ModelAPI_AttributeRefAttr;
 class ModelAPI_Document;
 class ModelAPI_Attribute;
 
@@ -34,10 +36,17 @@ public:
   virtual boost::shared_ptr<ModelAPI_AttributeDocRef> docRef(const std::string theID) = 0;
   /// Returns the attribute that contains real value with double precision
   virtual boost::shared_ptr<ModelAPI_AttributeDouble> real(const std::string theID) = 0;
+  /// Returns the attribute that contains reference to a feature
+  virtual boost::shared_ptr<ModelAPI_AttributeReference> reference(const std::string theID) = 0;
+  /// Returns the attribute that contains reference to an attribute of a feature
+  virtual boost::shared_ptr<ModelAPI_AttributeRefAttr> refattr(const std::string theID) = 0;
 
   /// Returns the generic attribute by identifier
   /// \param theID identifier of the attribute
   virtual boost::shared_ptr<ModelAPI_Attribute> attribute(const std::string theID) = 0;
+  /// Identifier by the id (not fast, iteration by map)
+  /// \param theAttr attribute already created in this data
+  virtual const std::string& id(const boost::shared_ptr<ModelAPI_Attribute> theAttr) = 0;
 
   /// Initializes object by the attributes: must be called just after the object is created
   /// for each attribute of the object
index 285bb4031acc83853ee41bc72bca610ef0433684..554c1c2281d2f2e08a7efd7db94f432130175b6a 100644 (file)
@@ -14,6 +14,8 @@
 #include <ModelAPI_Attribute.h>
 #include <ModelAPI_AttributeDocRef.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeReference.h>
+#include <ModelAPI_AttributeRefAttr.h>
 
 #ifdef WIN32
 #include <windows.h>