From 72fe2af9c04daae44747cdce2f03ec395a4e5511 Mon Sep 17 00:00:00 2001 From: mpv Date: Tue, 13 May 2014 09:46:38 +0400 Subject: [PATCH] Make RefAttr able point to attribute or to feature --- src/Model/Model_AttributeRefAttr.cpp | 63 +++++++++++++------ src/Model/Model_AttributeRefAttr.h | 18 ++++-- src/ModelAPI/ModelAPI_AttributeRefAttr.h | 19 ++++-- src/SketchPlugin/SketchPlugin_Constraint.cpp | 4 +- .../SketchPlugin_ConstraintDistance.cpp | 12 ++-- 5 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/Model/Model_AttributeRefAttr.cpp b/src/Model/Model_AttributeRefAttr.cpp index bedda92d5..a74bb11ba 100644 --- a/src/Model/Model_AttributeRefAttr.cpp +++ b/src/Model/Model_AttributeRefAttr.cpp @@ -11,19 +11,46 @@ using namespace std; -void Model_AttributeRefAttr::setValue(boost::shared_ptr theAttr) +bool Model_AttributeRefAttr::isFeature() { - if (value() != theAttr) { + return myID->Get().Length() == 0; +} + +void Model_AttributeRefAttr::setAttr(boost::shared_ptr theAttr) +{ + boost::shared_ptr aData = + boost::dynamic_pointer_cast(theAttr->feature()->data()); + string anID = aData->id(theAttr); + if (feature() == theAttr->feature() && myID->Get().IsEqual(anID.c_str())) + return; // nothing is changed + + 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 Model_AttributeRefAttr::attr() +{ + boost::shared_ptr aFeature = feature(); + if (aFeature) { boost::shared_ptr aData = - boost::dynamic_pointer_cast(theAttr->feature()->data()); - if (myRef.IsNull()) { - boost::shared_ptr aMyData = - boost::dynamic_pointer_cast(feature()->data()); - TDF_Reference::Set(aMyData->label(), aData->label()); - } else { - myRef->Set(aData->label()); - } - myID->Set(aData->id(theAttr).c_str()); + boost::dynamic_pointer_cast(aFeature->data()); + return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString()); + } + // not initialized + return boost::shared_ptr(); +} + +void Model_AttributeRefAttr::setFeature(boost::shared_ptr theFeature) +{ + if (myID->Get().Length() != 0 || feature() != theFeature) { + boost::shared_ptr aData = + boost::dynamic_pointer_cast(theFeature->data()); + myRef->Set(aData->label()); + myID->Set(""); // feature is identified by the empty ID static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED); Model_FeatureUpdatedMessage aMsg(feature(), anEvent); @@ -31,21 +58,19 @@ void Model_AttributeRefAttr::setValue(boost::shared_ptr theA } } -boost::shared_ptr Model_AttributeRefAttr::value() +boost::shared_ptr Model_AttributeRefAttr::feature() { - if (!myRef.IsNull()) { + if (myRef->Get() != myRef->Label()) { // initialized boost::shared_ptr aDoc = boost::dynamic_pointer_cast(feature()->document()); if (aDoc) { TDF_Label aRefLab = myRef->Get(); TDF_Label aFeatureLab = aRefLab.Father(); - boost::shared_ptr aData = - boost::dynamic_pointer_cast(aDoc->feature(aRefLab)->data()); - return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString()); + return aDoc->feature(aRefLab); } } // not initialized - return boost::shared_ptr(); + return boost::shared_ptr(); } Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel) @@ -53,7 +78,7 @@ 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! + myID = TDataStd_Comment::Set(theLabel, ""); + myRef = TDF_Reference::Set(theLabel, theLabel); // not initialized: reference to itself } } diff --git a/src/Model/Model_AttributeRefAttr.h b/src/Model/Model_AttributeRefAttr.h index 842296672..f9d2bc42e 100644 --- a/src/Model/Model_AttributeRefAttr.h +++ b/src/Model/Model_AttributeRefAttr.h @@ -20,13 +20,23 @@ class Model_AttributeRefAttr : public ModelAPI_AttributeRefAttr { Handle_TDF_Reference myRef; ///< reference to the feature label - Handle_TDataStd_Comment myID; ///< ID of the referenced attirbute + ///< ID of the referenced attirbute (empty if this is a reference to a feature) + Handle_TDataStd_Comment myID; public: - /// Defines the attribute referenced from this attribute - MODEL_EXPORT virtual void setValue(boost::shared_ptr theAttr); + /// Returns true if this attribute references to a feature (not to the attribute) + MODEL_EXPORT virtual bool isFeature(); + + /// Defines the reference to the attribute + MODEL_EXPORT virtual void setAttr(boost::shared_ptr theAttr); /// Returns attribute referenced from this attribute - MODEL_EXPORT virtual boost::shared_ptr value(); + MODEL_EXPORT virtual boost::shared_ptr attr(); + + /// Defines the reference to the feature + MODEL_EXPORT virtual void setFeature(boost::shared_ptr theFeature); + + /// Returns feature referenced from this attribute + MODEL_EXPORT virtual boost::shared_ptr feature(); protected: /// Objects are created for features automatically diff --git a/src/ModelAPI/ModelAPI_AttributeRefAttr.h b/src/ModelAPI/ModelAPI_AttributeRefAttr.h index d9c5f3a60..4fd883466 100644 --- a/src/ModelAPI/ModelAPI_AttributeRefAttr.h +++ b/src/ModelAPI/ModelAPI_AttributeRefAttr.h @@ -9,18 +9,27 @@ /**\class ModelAPI_AttributeRefAttr * \ingroup DataModel - * \brief Attribute that contains reference to an attribute of a feature - * (located in the same document). + * \brief Attribute that contains reference to an attribute of a feature or reference to + * a feature (switchable) */ class ModelAPI_AttributeRefAttr : public ModelAPI_Attribute { public: - /// Defines the attribute referenced from this attribute - MODELAPI_EXPORT virtual void setValue(boost::shared_ptr theAttr) = 0; + /// Returns true if this attribute references to a feature (not to the attribute) + MODELAPI_EXPORT virtual bool isFeature() = 0; + + /// Defines the reference to the attribute + MODELAPI_EXPORT virtual void setAttr(boost::shared_ptr theAttr) = 0; /// Returns attribute referenced from this attribute - MODELAPI_EXPORT virtual boost::shared_ptr value() = 0; + MODELAPI_EXPORT virtual boost::shared_ptr attr() = 0; + + /// Defines the reference to the feature + MODELAPI_EXPORT virtual void setFeature(boost::shared_ptr theFeature) = 0; + + /// Returns feature referenced from this attribute + MODELAPI_EXPORT virtual boost::shared_ptr feature() = 0; /// Returns the type of this class of attributes MODELAPI_EXPORT static std::string type() {return "RefAttr";} diff --git a/src/SketchPlugin/SketchPlugin_Constraint.cpp b/src/SketchPlugin/SketchPlugin_Constraint.cpp index 6269e3213..e6c9d09fd 100644 --- a/src/SketchPlugin/SketchPlugin_Constraint.cpp +++ b/src/SketchPlugin/SketchPlugin_Constraint.cpp @@ -28,8 +28,8 @@ void SketchPlugin_Constraint::addConstrainedObject( { if (!data()->attribute(theAttrID).get()) data()->addAttribute(theAttrID, theReference->type()); - boost::dynamic_pointer_cast( - data()->attribute(theAttrID))->setValue(theReference->value()); + //boost::dynamic_pointer_cast( + // data()->attribute(theAttrID))->setValue(theReference->value()); } void SketchPlugin_Constraint::getSketchParameters( diff --git a/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp b/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp index 13ef22dd8..6dfa0d0bd 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp @@ -67,8 +67,8 @@ void SketchPlugin_ConstraintDistance::setAttributes( // Assign reference to the first point myAttrList.push_back(CONSTRAINT_ATTR_POINT_A); data()->addAttribute(CONSTRAINT_ATTR_POINT_A, ModelAPI_AttributeRefAttr::type()); - boost::dynamic_pointer_cast( - data()->attribute(CONSTRAINT_ATTR_POINT_A))->setValue(thePoint); + //boost::dynamic_pointer_cast( + // data()->attribute(CONSTRAINT_ATTR_POINT_A))->setValue(thePoint); // Assign reference to the entity std::string aCurAttr; @@ -95,14 +95,14 @@ void SketchPlugin_ConstraintDistance::setAttributes( // Assign reference to the first point myAttrList.push_back(CONSTRAINT_ATTR_POINT_A); data()->addAttribute(CONSTRAINT_ATTR_POINT_A, ModelAPI_AttributeRefAttr::type()); - boost::dynamic_pointer_cast( - data()->attribute(CONSTRAINT_ATTR_POINT_A))->setValue(thePointA); + //boost::dynamic_pointer_cast( + // data()->attribute(CONSTRAINT_ATTR_POINT_A))->setValue(thePointA); // Assign reference to the second point myAttrList.push_back(CONSTRAINT_ATTR_POINT_B); data()->addAttribute(CONSTRAINT_ATTR_POINT_B, ModelAPI_AttributeRefAttr::type()); - boost::dynamic_pointer_cast( - data()->attribute(CONSTRAINT_ATTR_POINT_B))->setValue(thePointB); + //boost::dynamic_pointer_cast( + // data()->attribute(CONSTRAINT_ATTR_POINT_B))->setValue(thePointB); } void SketchPlugin_ConstraintDistance::setAttributes( -- 2.39.2