From d2d4d74fe59b3bd7ca2ec963fdcc871cc68b90d2 Mon Sep 17 00:00:00 2001 From: sbh Date: Fri, 29 Aug 2014 11:30:43 +0400 Subject: [PATCH] New Attribute - "String" added into the model. --- src/Model/CMakeLists.txt | 2 + src/Model/Model_AttributeBoolean.h | 2 +- src/Model/Model_AttributeString.cpp | 41 +++++++++++ src/Model/Model_AttributeString.h | 38 +++++++++++ src/Model/Model_Data.cpp | 87 +++++++++++++++--------- src/Model/Model_Data.h | 29 ++++++-- src/ModelAPI/CMakeLists.txt | 1 + src/ModelAPI/ModelAPI.i | 3 + src/ModelAPI/ModelAPI_AttributeBoolean.h | 4 +- src/ModelAPI/ModelAPI_AttributeString.h | 53 +++++++++++++++ src/ModelAPI/ModelAPI_Data.h | 3 + 11 files changed, 220 insertions(+), 43 deletions(-) create mode 100644 src/Model/Model_AttributeString.cpp create mode 100644 src/Model/Model_AttributeString.h create mode 100644 src/ModelAPI/ModelAPI_AttributeString.h diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index d2d6cc78d..bd10db278 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -12,6 +12,7 @@ SET(PROJECT_HEADERS Model_AttributeRefAttr.h Model_AttributeRefList.h Model_AttributeBoolean.h + Model_AttributeString.h Model_Events.h Model_Update.h Model_Validator.h @@ -33,6 +34,7 @@ SET(PROJECT_SOURCES Model_AttributeRefAttr.cpp Model_AttributeRefList.cpp Model_AttributeBoolean.cpp + Model_AttributeString.cpp Model_Events.cpp Model_Update.cpp Model_Validator.cpp diff --git a/src/Model/Model_AttributeBoolean.h b/src/Model/Model_AttributeBoolean.h index 0e4fbdf61..f52d52691 100644 --- a/src/Model/Model_AttributeBoolean.h +++ b/src/Model/Model_AttributeBoolean.h @@ -10,7 +10,7 @@ #include #include -/**\class Model_AttributeDouble +/**\class Model_AttributeBoolean * \ingroup DataModel * \brief Attribute that contains real value with double precision. */ diff --git a/src/Model/Model_AttributeString.cpp b/src/Model/Model_AttributeString.cpp new file mode 100644 index 000000000..fa22e1022 --- /dev/null +++ b/src/Model/Model_AttributeString.cpp @@ -0,0 +1,41 @@ +// File: Model_AttributeString.cpp +// Created: 2 june 2014 +// Author: Vitaly Smetannikov + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +void Model_AttributeString::setValue(const std::string& theValue) +{ + TCollection_ExtendedString aValue(theValue.c_str()); + if (!myIsInitialized || myString->Get() != aValue) { + myString->Set(aValue); + owner()->data()->sendAttributeUpdated(this); + } +} + +std::string Model_AttributeString::value() +{ + return TCollection_AsciiString(myString->Get()).ToCString(); +} + +Model_AttributeString::Model_AttributeString(TDF_Label& theLabel) +{ + // check the attribute could be already presented in this doc (after load document) + myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), myString) == Standard_True; + if (!myIsInitialized) { + // create attribute: not initialized by value yet, just empty string + myString = TDataStd_Name::Set(theLabel, TCollection_ExtendedString()); + } +} diff --git a/src/Model/Model_AttributeString.h b/src/Model/Model_AttributeString.h new file mode 100644 index 000000000..2695cbcf5 --- /dev/null +++ b/src/Model/Model_AttributeString.h @@ -0,0 +1,38 @@ +// File: Model_AttributeString.h +// Created: 2 june 2014 +// Author: Vitaly Smetannikov + +#ifndef Model_AttributeString_H_ +#define Model_AttributeString_H_ + +#include +#include + +#include +#include + +#include + +/**\class Model_AttributeString + * \ingroup DataModel + * \brief Attribute that contains std (null terminated) string. + */ + +class Model_AttributeString : public ModelAPI_AttributeString +{ + Handle_TDataStd_Name myString; ///< double is Real attribute + public: + /// Defines the double value + MODEL_EXPORT virtual void setValue(const std::string& theValue); + + /// Returns the double value + MODEL_EXPORT virtual std::string value(); + + protected: + /// Initializes attibutes + Model_AttributeString(TDF_Label& theLabel); + + friend class Model_Data; +}; + +#endif diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 592fc75db..dc80d9024 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -9,15 +9,16 @@ #include #include #include +#include +#include + #include #include #include -#include -#include "Model_Events.h" #include #include -using namespace std; +#include Model_Data::Model_Data() { @@ -28,15 +29,15 @@ void Model_Data::setLabel(TDF_Label theLab) myLab = theLab; } -string Model_Data::name() +std::string Model_Data::name() { Handle(TDataStd_Name) aName; if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) - return string(TCollection_AsciiString(aName->Get()).ToCString()); + return std::string(TCollection_AsciiString(aName->Get()).ToCString()); return ""; // not defined } -void Model_Data::setName(const string& theName) +void Model_Data::setName(const std::string& theName) { bool isModified = false; Handle(TDataStd_Name) aName; @@ -55,29 +56,31 @@ void Model_Data::setName(const string& theName) }*/ } -void Model_Data::addAttribute(const string& theID, const string theAttrType) +void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType) { TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1); ModelAPI_Attribute* anAttr = 0; - if (theAttrType == ModelAPI_AttributeDocRef::type()) + if (theAttrType == ModelAPI_AttributeDocRef::type()) { anAttr = new Model_AttributeDocRef(anAttrLab); - else if (theAttrType == ModelAPI_AttributeDouble::type()) + } else if (theAttrType == ModelAPI_AttributeDouble::type()) { anAttr = new Model_AttributeDouble(anAttrLab); - else if (theAttrType == ModelAPI_AttributeReference::type()) + } else if (theAttrType == ModelAPI_AttributeReference::type()) { anAttr = new Model_AttributeReference(anAttrLab); - else if (theAttrType == ModelAPI_AttributeRefAttr::type()) + } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) { anAttr = new Model_AttributeRefAttr(anAttrLab); - else if (theAttrType == ModelAPI_AttributeRefList::type()) + } else if (theAttrType == ModelAPI_AttributeRefList::type()) { anAttr = new Model_AttributeRefList(anAttrLab); - else if (theAttrType == GeomData_Point::type()) + } else if (theAttrType == GeomData_Point::type()) { anAttr = new GeomData_Point(anAttrLab); - else if (theAttrType == GeomData_Dir::type()) + } else if (theAttrType == GeomData_Dir::type()) { anAttr = new GeomData_Dir(anAttrLab); - else if (theAttrType == GeomData_Point2D::type()) + } else if (theAttrType == GeomData_Point2D::type()) { anAttr = new GeomData_Point2D(anAttrLab); - else if (theAttrType == Model_AttributeBoolean::type()) + } else if (theAttrType == Model_AttributeBoolean::type()) { anAttr = new Model_AttributeBoolean(anAttrLab); - + } else if (theAttrType == Model_AttributeString::type()) { + anAttr = new Model_AttributeString(anAttrLab); + } if (anAttr) { myAttrs[theID] = boost::shared_ptr(anAttr); anAttr->setObject(myObject); @@ -86,9 +89,9 @@ void Model_Data::addAttribute(const string& theID, const string theAttrType) } } -boost::shared_ptr Model_Data::docRef(const string& theID) +boost::shared_ptr Model_Data::docRef(const std::string& theID) { - map >::iterator aFound = myAttrs.find(theID); + std::map >::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(); @@ -101,9 +104,9 @@ boost::shared_ptr Model_Data::docRef(const string& the return aRes; } -boost::shared_ptr Model_Data::real(const string& theID) +boost::shared_ptr Model_Data::real(const std::string& theID) { - map >::iterator aFound = myAttrs.find(theID); + std::map >::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(); @@ -118,7 +121,7 @@ boost::shared_ptr Model_Data::real(const string& theID boost::shared_ptr Model_Data::boolean(const std::string& theID) { - map >::iterator aFound = myAttrs.find(theID); + std::map >::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(); @@ -131,9 +134,25 @@ boost::shared_ptr Model_Data::boolean(const std::stri return aRes; } -boost::shared_ptr Model_Data::reference(const string& theID) +boost::shared_ptr Model_Data::string(const std::string& theID) +{ + std::map >::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(); + } + boost::shared_ptr aRes = + boost::dynamic_pointer_cast(aFound->second); + if (!aRes) { + // TODO: generate error on invalid attribute type request + } + return aRes; + +} + +boost::shared_ptr Model_Data::reference(const std::string& theID) { - map >::iterator aFound = myAttrs.find(theID); + std::map >::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(); @@ -146,9 +165,9 @@ boost::shared_ptr Model_Data::reference(const strin return aRes; } -boost::shared_ptr Model_Data::refattr(const string& theID) +boost::shared_ptr Model_Data::refattr(const std::string& theID) { - map >::iterator aFound = myAttrs.find(theID); + std::map >::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(); @@ -161,9 +180,9 @@ boost::shared_ptr Model_Data::refattr(const string& t return aRes; } -boost::shared_ptr Model_Data::reflist(const string& theID) +boost::shared_ptr Model_Data::reflist(const std::string& theID) { - map >::iterator aFound = myAttrs.find(theID); + std::map >::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(); @@ -184,15 +203,15 @@ boost::shared_ptr Model_Data::attribute(const std::string& t return myAttrs[theID]; } -const string& Model_Data::id(const boost::shared_ptr& theAttr) +const std::string& Model_Data::id(const boost::shared_ptr& theAttr) { - map >::iterator anAttr = myAttrs.begin(); + std::map >::iterator anAttr = myAttrs.begin(); for (; anAttr != myAttrs.end(); anAttr++) { if (anAttr->second == theAttr) return anAttr->first; } // not found - static string anEmpty; + static std::string anEmpty; return anEmpty; } @@ -209,10 +228,10 @@ bool Model_Data::isValid() return !myLab.IsNull() && myLab.HasAttribute(); } -list > Model_Data::attributes(const string& theType) +std::list > Model_Data::attributes(const std::string& theType) { - list > aResult; - map >::iterator anAttrsIter = myAttrs.begin(); + std::list > aResult; + std::map >::iterator anAttrsIter = myAttrs.begin(); for (; anAttrsIter != myAttrs.end(); anAttrsIter++) { if (theType.empty() || anAttrsIter->second->attributeType() == theType) { aResult.push_back(anAttrsIter->second); diff --git a/src/Model/Model_Data.h b/src/Model/Model_Data.h index ce939de8f..659778769 100644 --- a/src/Model/Model_Data.h +++ b/src/Model/Model_Data.h @@ -5,12 +5,26 @@ #ifndef Model_Data_H_ #define Model_Data_H_ -#include "Model.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include +#include + #include +#include + #include +#include +#include class ModelAPI_Attribute; @@ -53,23 +67,26 @@ class Model_Data : public ModelAPI_Data MODEL_EXPORT virtual boost::shared_ptr real(const std::string& theID); /// Returns the attribute that contains reference to a feature MODEL_EXPORT virtual boost::shared_ptr - reference(const std::string& theID); + reference(const std::string& theID); /// Returns the attribute that contains reference to an attribute of a feature MODEL_EXPORT virtual boost::shared_ptr - refattr(const std::string& theID); + refattr(const std::string& theID); /// Returns the attribute that contains list of references to features MODEL_EXPORT virtual boost::shared_ptr - reflist(const std::string& theID); + reflist(const std::string& theID); /// Returns the attribute that contains boolean value MODEL_EXPORT virtual boost::shared_ptr - boolean(const std::string& theID); + boolean(const std::string& theID); + /// Returns the attribute that contains real value with double precision + MODEL_EXPORT virtual boost::shared_ptr + string(const std::string& theID); /// Returns the generic attribute by identifier /// \param theID identifier of the attribute MODEL_EXPORT virtual boost::shared_ptr attribute(const std::string& theID); /// Returns all attributes ofthe feature of the given type /// or all attributes if "theType" is empty MODEL_EXPORT virtual std::list > - attributes(const std::string& theType); + attributes(const std::string& theType); /// Identifier by the id (not fast, iteration by map) /// \param theAttr attribute already created in this data diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index cc1596c0f..e10883473 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -18,6 +18,7 @@ SET(PROJECT_HEADERS ModelAPI_AttributeRefAttr.h ModelAPI_AttributeRefList.h ModelAPI_AttributeBoolean.h + ModelAPI_AttributeString.h ModelAPI_Events.h ModelAPI_Validator.h ModelAPI_FeatureValidator.h diff --git a/src/ModelAPI/ModelAPI.i b/src/ModelAPI/ModelAPI.i index 31936c53e..29e75fe30 100644 --- a/src/ModelAPI/ModelAPI.i +++ b/src/ModelAPI/ModelAPI.i @@ -12,6 +12,7 @@ #include "ModelAPI_Attribute.h" #include "ModelAPI_AttributeDocRef.h" #include "ModelAPI_AttributeDouble.h" + #include "ModelAPI_AttributeString.h" #include "ModelAPI_AttributeReference.h" #include "ModelAPI_AttributeRefAttr.h" #include "ModelAPI_Validator.h" @@ -49,6 +50,7 @@ %shared_ptr(ModelAPI_Attribute) %shared_ptr(ModelAPI_AttributeDocRef) %shared_ptr(ModelAPI_AttributeDouble) +%shared_ptr(ModelAPI_AttributeString) %shared_ptr(ModelAPI_AttributeReference) %shared_ptr(ModelAPI_AttributeRefAttr) %shared_ptr(ModelAPI_AttributeRefList) @@ -66,6 +68,7 @@ %include "ModelAPI_Attribute.h" %include "ModelAPI_AttributeDocRef.h" %include "ModelAPI_AttributeDouble.h" +%include "ModelAPI_AttributeString.h" %include "ModelAPI_AttributeReference.h" %include "ModelAPI_AttributeRefAttr.h" %include "ModelAPI_Validator.h" diff --git a/src/ModelAPI/ModelAPI_AttributeBoolean.h b/src/ModelAPI/ModelAPI_AttributeBoolean.h index 2612e0eed..dcd76efc7 100644 --- a/src/ModelAPI/ModelAPI_AttributeBoolean.h +++ b/src/ModelAPI/ModelAPI_AttributeBoolean.h @@ -2,8 +2,8 @@ // Created: 2 june 2014 // Author: Vitaly Smetannikov -#ifndef ModelAPI_AttributeBoolean_H_ -#define ModelAPI_AttributeBoolean_H_ +#ifndef MODELAPI_ATTRIBUTEBOOLEAN_H_ +#define MODELAPI_ATTRIBUTEBOOLEAN_H_ #include "ModelAPI_Attribute.h" diff --git a/src/ModelAPI/ModelAPI_AttributeString.h b/src/ModelAPI/ModelAPI_AttributeString.h new file mode 100644 index 000000000..5b037f1c0 --- /dev/null +++ b/src/ModelAPI/ModelAPI_AttributeString.h @@ -0,0 +1,53 @@ +// File: ModelAPI_AttributeString.h +// Created: 2 Apr 2014 +// Author: Mikhail PONIKAROV + +#ifndef MODELAPI_ATTRIBUTESTRING_H_ +#define MODELAPI_ATTRIBUTESTRING_H_ + +#include "ModelAPI_Attribute.h" + +#include + +/**\class ModelAPI_AttributeString + * \ingroup DataModel + * \brief API for the attribute that contains std (null terminated) string. + */ + +class ModelAPI_AttributeString : public ModelAPI_Attribute +{ + public: + /// Defines the double value + MODELAPI_EXPORT virtual void setValue(const std::string& theValue) = 0; + + /// Returns the double value + MODELAPI_EXPORT virtual std::string value() = 0; + + /// Returns the type of this class of attributes + MODELAPI_EXPORT static std::string type() + { + return "String"; + } + + /// 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_AttributeString() + { + } + + protected: + /// Objects are created for features automatically + MODELAPI_EXPORT ModelAPI_AttributeString() + { + } +}; + +//! Pointer on double attribute +typedef boost::shared_ptr AttributeStringPtr; + +#endif diff --git a/src/ModelAPI/ModelAPI_Data.h b/src/ModelAPI/ModelAPI_Data.h index d07d24052..b11bb66fb 100644 --- a/src/ModelAPI/ModelAPI_Data.h +++ b/src/ModelAPI/ModelAPI_Data.h @@ -16,6 +16,7 @@ class ModelAPI_AttributeReference; class ModelAPI_AttributeRefAttr; class ModelAPI_AttributeRefList; class ModelAPI_AttributeBoolean; +class ModelAPI_AttributeString; class ModelAPI_Document; class ModelAPI_Attribute; class GeomAPI_Shape; @@ -48,6 +49,8 @@ class MODELAPI_EXPORT ModelAPI_Data virtual boost::shared_ptr reflist(const std::string& theID) = 0; /// Returns the attribute that contains boolean value virtual boost::shared_ptr boolean(const std::string& theID) = 0; + /// Returns the attribute that contains boolean value + virtual boost::shared_ptr string(const std::string& theID) = 0; /// Returns the generic attribute by identifier /// \param theID identifier of the attribute -- 2.39.2