X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModel%2FModel_AttributeRefAttr.cpp;h=0c24fe3e1b762cfa047e634473aca9408b288c2c;hb=b6f308f901ef00da365c40bab25c5f03b547adf0;hp=bedda92d5d92a704c0465cd42609a00dd1e52018;hpb=e9da4a58641d63176d3214d2f5e3440b917f611d;p=modules%2Fshaper.git diff --git a/src/Model/Model_AttributeRefAttr.cpp b/src/Model/Model_AttributeRefAttr.cpp index bedda92d5..0c24fe3e1 100644 --- a/src/Model/Model_AttributeRefAttr.cpp +++ b/src/Model/Model_AttributeRefAttr.cpp @@ -1,59 +1,105 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + // 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 "Model_Objects.h" #include -#include using namespace std; -void Model_AttributeRefAttr::setValue(boost::shared_ptr theAttr) +bool Model_AttributeRefAttr::isObject() { - if (value() != theAttr) { - 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()); + return myID->Get().Length() == 0; +} + +void Model_AttributeRefAttr::setAttr(std::shared_ptr theAttr) +{ + std::shared_ptr aData = std::dynamic_pointer_cast( + theAttr->owner()->data()); + string anID = aData->id(theAttr); + if (myIsInitialized && object() == theAttr->owner() && myID->Get().IsEqual(anID.c_str())) + return; // nothing is changed + REMOVE_BACK_REF(theAttr->owner()); + myRef->Set(aData->label().Father()); + myID->Set(aData->id(theAttr).c_str()); + ADD_BACK_REF(theAttr->owner()); + owner()->data()->sendAttributeUpdated(this); +} - static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED); - Model_FeatureUpdatedMessage aMsg(feature(), anEvent); - Events_Loop::loop()->send(aMsg); +std::shared_ptr Model_AttributeRefAttr::attr() +{ + ObjectPtr anObj = object(); + if (anObj && anObj->data()) { + std::shared_ptr aData = std::dynamic_pointer_cast(anObj->data()); + return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString()); } + // not initialized + return std::shared_ptr(); } -boost::shared_ptr Model_AttributeRefAttr::value() +void Model_AttributeRefAttr::setObject(ObjectPtr theObject) { - if (!myRef.IsNull()) { - boost::shared_ptr aDoc = - boost::dynamic_pointer_cast(feature()->document()); + // the back reference from the previous object to the attribute should be removed + ObjectPtr anObject = object(); + if (theObject.get() && (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject)) { + REMOVE_BACK_REF(anObject); + + std::shared_ptr aData = std::dynamic_pointer_cast( + theObject->data()); + myRef->Set(aData->label().Father()); + myID->Set(""); // feature is identified by the empty ID + + // do it before the transaction finish to make just created/removed objects know dependencies + // and reference from composite feature is removed automatically + FeaturePtr anOwnerFeature = std::dynamic_pointer_cast(owner()); + if (anOwnerFeature.get()) { + aData->addBackReference(anOwnerFeature, id(), false); + } + ADD_BACK_REF(theObject); + owner()->data()->sendAttributeUpdated(this); + } else if (theObject.get() == NULL) { + REMOVE_BACK_REF(anObject); + myRef->Set(myRef->Label()); // reference to itself means that object is null + myID->Set(""); // feature is identified by the empty ID + owner()->data()->sendAttributeUpdated(this); + } +} + +ObjectPtr Model_AttributeRefAttr::object() +{ + if (myRef->Get() != myRef->Label()) { // initialized + std::shared_ptr aDoc = std::dynamic_pointer_cast( + owner()->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->objects()->object(aRefLab); } } // not initialized - return boost::shared_ptr(); + return ObjectPtr(); +} + +bool Model_AttributeRefAttr::isInitialized() +{ + if (myRef->Get() == myRef->Label()) { // empty is not initialized: sketch parallelity + return false; + } + return ModelAPI_AttributeRefAttr::isInitialized(); } 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)) { + myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True; + if (!myIsInitialized) { // 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 + } else { + theLabel.FindAttribute(TDF_Reference::GetID(), myRef); } }