}
}
-int Model_AttributeRefList::size() const
+int Model_AttributeRefList::size(const bool theWithEmpty) const
{
- return myRef->Extent();
+ if (theWithEmpty)
+ return myRef->Extent();
+ int aResult = 0;
+ const TDF_LabelList& aList = myRef->List();
+ for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
+ if (!aLIter.Value().IsNull()) aResult++;
+ }
+ return aResult;
}
bool Model_AttributeRefList::isInitialized()
if (aDoc) {
const TDF_LabelList& aList = myRef->List();
for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
- ObjectPtr anObj = aDoc->objects()->object(aLIter.Value());
+ ObjectPtr anObj;
+ if (!aLIter.Value().IsNull())
+ anObj = aDoc->objects()->object(aLIter.Value());
aResult.push_back(anObj);
}
}
return false;
}
-ObjectPtr Model_AttributeRefList::object(const int theIndex) const
+ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const
{
std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
owner()->document());
if (aDoc) {
const TDF_LabelList& aList = myRef->List();
- int anIndex = 0;
- for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next(), anIndex++) {
+ int anIndex = -1;
+ for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
+ if (theWithEmpty || !aLIter.Value().IsNull())
+ anIndex++;
if (anIndex == theIndex)
return aDoc->objects()->object(aLIter.Value());
}
return ObjectPtr();
}
+void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
+{
+ std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+ owner()->document());
+ if (aDoc) {
+ std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theCurrent->data());
+ if (aData.get() && aData->isValid()) {
+ TDF_Label aCurrentLab = aData->label().Father();
+ TDF_Label aNewLab;
+ if (theNew.get() && theNew->data()->isValid()) { // the new may be null
+ std::shared_ptr<Model_Data> aNewData =
+ std::dynamic_pointer_cast<Model_Data>(theNew->data());
+ aNewLab = aNewData->label().Father();
+ }
+ // do the substitution
+ if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
+ myRef->Remove(aCurrentLab);
+ }
+ }
+ }
+}
+
Model_AttributeRefList::Model_AttributeRefList(TDF_Label& theLabel)
{
myIsInitialized = theLabel.FindAttribute(TDataStd_ReferenceList::GetID(), myRef) == Standard_True;
MODEL_EXPORT virtual void remove(ObjectPtr theObject);
/// Returns number of features in the list
- MODEL_EXPORT virtual int size() const;
+ ///\param theWithEmpty if it is false, returns the number of not-empty referenced objects
+ MODEL_EXPORT virtual int size(const bool theWithEmpty = true) const;
/// Removes all references from the list
MODEL_EXPORT virtual void clear();
MODEL_EXPORT virtual bool isInList(const ObjectPtr& theObj);
/// Returns the list of features
- MODEL_EXPORT virtual ObjectPtr object(const int theIndex) const;
+ ///\param theIndex zero-based index in the list
+ ///\param theWithEmpty if it is false, counts the not-empty referenced objects only
+ MODEL_EXPORT virtual ObjectPtr object(const int theIndex, const bool theWithEmpty = true) const;
+
+ /// Substitutes the feature by another one. Does nothing if such object is not found.
+ MODEL_EXPORT virtual void substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew);
/// Returns true if attribute was initialized by some value
MODEL_EXPORT virtual bool isInitialized();
MODELAPI_EXPORT virtual std::string attributeType();
/// Appends the feature to the end of a list
- MODELAPI_EXPORT virtual void append(ObjectPtr theObject) = 0;
+ virtual void append(ObjectPtr theObject) = 0;
/// Erases the first meet of the feature in the list
- MODELAPI_EXPORT virtual void remove(ObjectPtr theObject) = 0;
+ virtual void remove(ObjectPtr theObject) = 0;
/// Removes all references from the list
- MODELAPI_EXPORT virtual void clear() = 0;
+ virtual void clear() = 0;
/// Returns number of features in the list
- MODELAPI_EXPORT virtual int size() const = 0;
+ ///\param theWithEmpty if it is false, returns the number of not-empty referenced objects
+ virtual int size(const bool theWithEmpty = true) const = 0;
/// Returns the list of features
- MODELAPI_EXPORT virtual std::list<ObjectPtr> list() = 0;
+ virtual std::list<ObjectPtr> list() = 0;
/// Returns true if the object is in list
- MODELAPI_EXPORT virtual bool isInList(const ObjectPtr& theObj) = 0;
+ virtual bool isInList(const ObjectPtr& theObj) = 0;
- /// Returns the referenced object by the zero-based index
- MODELAPI_EXPORT virtual ObjectPtr object(const int theIndex) const = 0;
+ /// Returns the referenced object by the zero-based index
+ ///\param theIndex zero-based index in the list
+ ///\param theWithEmpty if it is false, counts the not-empty referenced objects only
+ virtual ObjectPtr object(const int theIndex, const bool theWithEmpty = true) const = 0;
+
+ /// Substitutes the feature by another one. Does nothing if such object is not found.
+ virtual void substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew) = 0;
MODELAPI_EXPORT virtual ~ModelAPI_AttributeRefList();
protected:
new GeomAPI_Pnt2d(theClickedX, theClickedY));
for (; anIt != aLast; anIt++) {
FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
- if (theFeature == aFeature)
+ if (!aFeature.get() || theFeature == aFeature)
continue;
// find the given point in the feature attributes
anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
{
if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
return;
+ // to keep the persistent sub-elements indexing, do not remove elements from list,
+ // but substitute by nulls
+ reflist(SketchPlugin_Sketch::FEATURES_ID())->substitute(theFeature, ObjectPtr());
+ /*
list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
bool isRemoved = false;
// Find the first empty element and remove it
if (!isRemoved && aHasEmtpyFeature)
data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
+ */
}
int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
{
if (forTree)
return 0;
- return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
+ return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size(false);
}
-std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex, bool forTree) const
+std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
+ const int theIndex, bool forTree) const
{
if (forTree)
return FeaturePtr();
- ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
+ ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
}
int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
{
- return subFeature(theIndex)->data()->featureId();
+ std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
+ ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
+ std::list<ObjectPtr> aFeatures = aRefList->list();
+ std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
+ int aResultIndex = 1; // number of the counted (created) features, started from 1
+ int aFeatureIndex = -1; // number of the not-empty features in the list
+ for (; anIt != aFeatures.end(); anIt++) {
+ if (anIt->get())
+ aFeatureIndex++;
+ if (aFeatureIndex == theIndex)
+ break;
+ aResultIndex++;
+ }
+ return aResultIndex;
}
bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
std::list<ObjectPtr>::iterator aSub = aSubs.begin();
for(; aSub != aSubs.end(); aSub++) {
- ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
+ if (aSub->get())
+ ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
}
}
}
SKETCHPLUGIN_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
subFeature(const int theIndex, bool forTree = false) const;
- /// Returns the sub-feature unique identifier in this composite feature by zero-base index
+ /// Returns the sub-feature unique identifier in this composite feature by index
SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;
/// Returns true if feature or reuslt belong to this composite feature as subs