From b2392a7380b299a0c0687edc26aa58c1223199ff Mon Sep 17 00:00:00 2001 From: mpv Date: Mon, 12 May 2014 16:23:00 +0400 Subject: [PATCH] List of references attribute and usage of it in the sketch --- src/Model/CMakeLists.txt | 2 + src/Model/Model_AttributeRefList.cpp | 59 ++++++++++++++++++++++++ src/Model/Model_AttributeRefList.h | 40 ++++++++++++++++ src/Model/Model_Data.cpp | 18 ++++++++ src/Model/Model_Data.h | 4 ++ src/Model/Model_Document.cpp | 24 +++++----- src/ModelAPI/CMakeLists.txt | 1 + src/ModelAPI/ModelAPI_AttributeRefList.h | 43 +++++++++++++++++ src/ModelAPI/ModelAPI_Data.h | 3 ++ src/ModelAPI/ModelAPI_Feature.h | 3 ++ src/ModelAPI/ModelAPI_PluginManager.cpp | 1 + src/SketchPlugin/SketchPlugin_Feature.h | 3 ++ src/SketchPlugin/SketchPlugin_Sketch.cpp | 3 ++ src/SketchPlugin/SketchPlugin_Sketch.h | 5 ++ 14 files changed, 198 insertions(+), 11 deletions(-) create mode 100644 src/Model/Model_AttributeRefList.cpp create mode 100644 src/Model/Model_AttributeRefList.h create mode 100644 src/ModelAPI/ModelAPI_AttributeRefList.h diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index 872f089f6..c5764b545 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -11,6 +11,7 @@ SET(PROJECT_HEADERS Model_AttributeDocRef.h Model_AttributeReference.h Model_AttributeRefAttr.h + Model_AttributeRefList.h Model_Events.h ) @@ -23,6 +24,7 @@ SET(PROJECT_SOURCES Model_AttributeDocRef.cpp Model_AttributeReference.cpp Model_AttributeRefAttr.cpp + Model_AttributeRefList.cpp Model_Events.cpp ) diff --git a/src/Model/Model_AttributeRefList.cpp b/src/Model/Model_AttributeRefList.cpp new file mode 100644 index 000000000..0366e0a77 --- /dev/null +++ b/src/Model/Model_AttributeRefList.cpp @@ -0,0 +1,59 @@ +// File: ModelAPI_AttributeRefList.cxx +// Created: 8 May 2014 +// Author: Mikhail PONIKAROV + +#include "Model_AttributeRefList.h" +#include "Model_Application.h" +#include "Model_Events.h" +#include "Model_Data.h" +#include +#include +#include + +using namespace std; + +void Model_AttributeRefList::append(boost::shared_ptr theFeature) +{ + boost::shared_ptr aData = + boost::dynamic_pointer_cast(theFeature->data()); + myRef->Append(aData->label()); + + static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED); + Model_FeatureUpdatedMessage aMsg(feature(), anEvent); + Events_Loop::loop()->send(aMsg); +} + +void Model_AttributeRefList::remove(boost::shared_ptr theFeature) +{ + boost::shared_ptr aData = + boost::dynamic_pointer_cast(theFeature->data()); + myRef->Remove(aData->label()); + +} + +int Model_AttributeRefList::size() +{ + return myRef->Extent(); +} + +list > Model_AttributeRefList::list() +{ + std::list< boost::shared_ptr > aResult; + boost::shared_ptr aDoc = + boost::dynamic_pointer_cast(feature()->document()); + if (aDoc) { + const TDF_LabelList& aList = myRef->List(); + for(TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) { + aResult.push_back(aDoc->feature(aLIter.Value())); + } + } + return aResult; +} + +Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel) +{ + // check the attribute could be already presented in this doc (after load document) + if (!theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef)) { + myRef = TDataStd_ReferenceList::Set(theLabel); + } +} diff --git a/src/Model/Model_AttributeRefList.h b/src/Model/Model_AttributeRefList.h new file mode 100644 index 000000000..bf6290c07 --- /dev/null +++ b/src/Model/Model_AttributeRefList.h @@ -0,0 +1,40 @@ +// File: Model_AttributeRefList.h +// Created: 8 May 2014 +// Author: Mikhail PONIKAROV + +#ifndef Model_AttributeRefList_HeaderFile +#define Model_AttributeRefList_HeaderFile + +#include "Model.h" +#include "ModelAPI_AttributeRefList.h" +#include + +/**\class Model_AttributeRefList + * \ingroup DataModel + * \brief Attribute that contains list of references to features (located in the same document). + */ + +class Model_AttributeRefList : public ModelAPI_AttributeRefList +{ + Handle_TDataStd_ReferenceList myRef; ///< references to the features labels +public: + /// Appends the feature to the end of a list + MODEL_EXPORT virtual void append(boost::shared_ptr theFeature); + + /// Erases the first meet of the feature in the list + MODEL_EXPORT virtual void remove(boost::shared_ptr theFeature); + + /// Returns number of features in the list + MODEL_EXPORT virtual int size(); + + /// Returns the list of features + MODEL_EXPORT virtual std::list > list(); + +protected: + /// Objects are created for features automatically + MODEL_EXPORT Model_AttributeRefList(TDF_Label& theLabel); + + friend class Model_Data; +}; + +#endif diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 0c8f77bdf..487d0ca91 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -48,6 +49,8 @@ void Model_Data::addAttribute(string theID, string theAttrType) anAttr = new Model_AttributeReference(anAttrLab); else if (theAttrType == ModelAPI_AttributeRefAttr::type()) anAttr = new Model_AttributeRefAttr(anAttrLab); + else if (theAttrType == ModelAPI_AttributeRefList::type()) + anAttr = new Model_AttributeRefList(anAttrLab); else if (theAttrType == GeomData_Point::type()) anAttr = new GeomData_Point(anAttrLab); else if (theAttrType == GeomData_Dir::type()) @@ -123,6 +126,21 @@ boost::shared_ptr Model_Data::refattr(const string th return aRes; } +boost::shared_ptr Model_Data::reflist(const string theID) +{ + 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::attribute(const std::string theID) { boost::shared_ptr aResult; diff --git a/src/Model/Model_Data.h b/src/Model/Model_Data.h index 56d81e123..1484f68c8 100644 --- a/src/Model/Model_Data.h +++ b/src/Model/Model_Data.h @@ -37,6 +37,7 @@ class Model_Data: public ModelAPI_Data friend class Model_Document; friend class Model_AttributeReference; friend class Model_AttributeRefAttr; + friend class Model_AttributeRefList; public: /// Returns the name of the feature visible by the user in the object browser @@ -53,6 +54,9 @@ public: /// Returns the attribute that contains reference to an attribute of a feature MODEL_EXPORT virtual boost::shared_ptr 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); /// Returns the generic attribute by identifier /// \param theID identifier of the attribute MODEL_EXPORT virtual boost::shared_ptr attribute(const std::string theID); diff --git a/src/Model/Model_Document.cpp b/src/Model/Model_Document.cpp index 0255a4f93..8c305effa 100644 --- a/src/Model/Model_Document.cpp +++ b/src/Model/Model_Document.cpp @@ -239,18 +239,20 @@ void Model_Document::addFeature(const boost::shared_ptr theFea TDataStd_Integer::Set(anObjLab, myFeatures[aGroup].size()); myFeatures[aGroup].push_back(theFeature); // store feature in the history of features array - Handle(TDataStd_ReferenceArray) aRefs; - if (!groupLabel(FEATURES_GROUP).FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) { - aRefs = TDataStd_ReferenceArray::Set(groupLabel(FEATURES_GROUP), 0, 0); - aRefs->SetValue(0, anObjLab); - } else { // extend array by one more element - Handle(TDataStd_HLabelArray1) aNewArray = - new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() + 1); - for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { - aNewArray->SetValue(a, aRefs->Value(a)); + if (theFeature->isInHistory()) { + Handle(TDataStd_ReferenceArray) aRefs; + if (!groupLabel(FEATURES_GROUP).FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) { + aRefs = TDataStd_ReferenceArray::Set(groupLabel(FEATURES_GROUP), 0, 0); + aRefs->SetValue(0, anObjLab); + } else { // extend array by one more element + Handle(TDataStd_HLabelArray1) aNewArray = + new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() + 1); + for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { + aNewArray->SetValue(a, aRefs->Value(a)); + } + aNewArray->SetValue(aRefs->Upper() + 1, anObjLab); + aRefs->SetInternalArray(aNewArray); } - aNewArray->SetValue(aRefs->Upper() + 1, anObjLab); - aRefs->SetInternalArray(aNewArray); } // event: feature is added diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 9c6015acd..c8a1e56ff 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -14,6 +14,7 @@ SET(PROJECT_HEADERS ModelAPI_AttributeDocRef.h ModelAPI_AttributeReference.h ModelAPI_AttributeRefAttr.h + ModelAPI_AttributeRefList.h ) SET(PROJECT_SOURCES diff --git a/src/ModelAPI/ModelAPI_AttributeRefList.h b/src/ModelAPI/ModelAPI_AttributeRefList.h new file mode 100644 index 000000000..973fbc46d --- /dev/null +++ b/src/ModelAPI/ModelAPI_AttributeRefList.h @@ -0,0 +1,43 @@ +// File: ModelAPI_AttributeRefList.h +// Created: 8 May 2014 +// Author: Mikhail PONIKAROV + +#ifndef ModelAPI_AttributeRefList_HeaderFile +#define ModelAPI_AttributeRefList_HeaderFile + +#include "ModelAPI_Attribute.h" +#include + +/**\class ModelAPI_AttributeRefList + * \ingroup DataModel + * \brief Attribute that contains list of references to features (located in the same document). + */ + +class ModelAPI_AttributeRefList : public ModelAPI_Attribute +{ +public: + /// Returns the type of this class of attributes + MODELAPI_EXPORT static std::string type() {return "RefList";} + + /// Returns the type of this class of attributes, not static method + MODELAPI_EXPORT virtual std::string attributeType() {return type();} + + /// Appends the feature to the end of a list + MODELAPI_EXPORT virtual void append(boost::shared_ptr theFeature) = 0; + + /// Erases the first meet of the feature in the list + MODELAPI_EXPORT virtual void remove(boost::shared_ptr theFeature) = 0; + + /// Returns number of features in the list + MODELAPI_EXPORT virtual int size() = 0; + + /// Returns the list of features + MODELAPI_EXPORT virtual std::list > list() = 0; + +protected: + /// Objects are created for features automatically + MODELAPI_EXPORT ModelAPI_AttributeRefList() + {} +}; + +#endif diff --git a/src/ModelAPI/ModelAPI_Data.h b/src/ModelAPI/ModelAPI_Data.h index 86fc56409..3b46bc4a0 100644 --- a/src/ModelAPI/ModelAPI_Data.h +++ b/src/ModelAPI/ModelAPI_Data.h @@ -13,6 +13,7 @@ class ModelAPI_AttributeDocRef; class ModelAPI_AttributeDouble; class ModelAPI_AttributeReference; class ModelAPI_AttributeRefAttr; +class ModelAPI_AttributeRefList; class ModelAPI_Document; class ModelAPI_Attribute; @@ -40,6 +41,8 @@ public: virtual boost::shared_ptr reference(const std::string theID) = 0; /// Returns the attribute that contains reference to an attribute of a feature virtual boost::shared_ptr refattr(const std::string theID) = 0; + /// Returns the attribute that contains list of references to features + virtual boost::shared_ptr reflist(const std::string theID) = 0; /// Returns the generic attribute by identifier /// \param theID identifier of the attribute diff --git a/src/ModelAPI/ModelAPI_Feature.h b/src/ModelAPI/ModelAPI_Feature.h index 6c458bab4..b55e79846 100644 --- a/src/ModelAPI/ModelAPI_Feature.h +++ b/src/ModelAPI/ModelAPI_Feature.h @@ -37,6 +37,9 @@ public: /// Computes or recomputes the result MODELAPI_EXPORT virtual void execute() = 0; + /// Returns true if this feature must be displayed in the history (top level of Part tree) + MODELAPI_EXPORT virtual bool isInHistory() {return true;} + /// Returns the data manager of this feature MODELAPI_EXPORT virtual boost::shared_ptr data() {return myData;} diff --git a/src/ModelAPI/ModelAPI_PluginManager.cpp b/src/ModelAPI/ModelAPI_PluginManager.cpp index 554c1c228..63bca2d8a 100644 --- a/src/ModelAPI/ModelAPI_PluginManager.cpp +++ b/src/ModelAPI/ModelAPI_PluginManager.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef WIN32 #include diff --git a/src/SketchPlugin/SketchPlugin_Feature.h b/src/SketchPlugin/SketchPlugin_Feature.h index 0419550a8..d02728011 100644 --- a/src/SketchPlugin/SketchPlugin_Feature.h +++ b/src/SketchPlugin/SketchPlugin_Feature.h @@ -29,6 +29,9 @@ public: SKETCHPLUGIN_EXPORT virtual const void addSub( const boost::shared_ptr& theFeature) = 0; + /// Returns true if this feature must be displayed in the history (top level of Part tree) + SKETCHPLUGIN_EXPORT virtual bool isInHistory() {return false;} + protected: /// Set the shape to the internal preview field /// \param theShape a preview shape diff --git a/src/SketchPlugin/SketchPlugin_Sketch.cpp b/src/SketchPlugin/SketchPlugin_Sketch.cpp index 35d90ab5d..6691c6f85 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.cpp +++ b/src/SketchPlugin/SketchPlugin_Sketch.cpp @@ -4,6 +4,7 @@ #include "SketchPlugin_Sketch.h" #include +#include #include #include #include @@ -28,6 +29,7 @@ void SketchPlugin_Sketch::initAttributes() data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type()); data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type()); data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type()); + data()->addAttribute(SKETCH_ATTR_FEATURES, ModelAPI_AttributeRefList::type()); } void SketchPlugin_Sketch::execute() @@ -50,6 +52,7 @@ const boost::shared_ptr& SketchPlugin_Sketch::preview() const void SketchPlugin_Sketch::addSub(const boost::shared_ptr& theFeature) { boost::dynamic_pointer_cast(theFeature)->setSketch(this); + data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature); } void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ, diff --git a/src/SketchPlugin/SketchPlugin_Sketch.h b/src/SketchPlugin/SketchPlugin_Sketch.h index 0fc24fb11..371943ec5 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.h +++ b/src/SketchPlugin/SketchPlugin_Sketch.h @@ -18,6 +18,8 @@ const std::string SKETCH_ATTR_DIRX("DirX"); const std::string SKETCH_ATTR_DIRY("DirY"); /// Vector Z, normal to the sketch plane const std::string SKETCH_ATTR_NORM("Norm"); +/// All features of this sketch (list of references) +const std::string SKETCH_ATTR_FEATURES("Features"); /**\class SketchPlugin_Sketch * \ingroup DataModel @@ -52,6 +54,9 @@ public: SKETCHPLUGIN_EXPORT boost::shared_ptr to3D( const double theX, const double theY); + /// Returns true if this feature must be displayed in the history (top level of Part tree) + SKETCHPLUGIN_EXPORT virtual bool isInHistory() {return true;} + /// Use plugin manager for features creation SketchPlugin_Sketch(); protected: -- 2.39.2