return mySubs.at(theIndex);
}
-bool Model_ResultCompSolid::isSub(ObjectPtr theObject) const
+bool Model_ResultCompSolid::isSub(ObjectPtr theObject, int& theIndex) const
{
- std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
- for(; aSubIter != mySubs.cend(); aSubIter++)
- if (*aSubIter == theObject)
- return true;
+ std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
+ if (aFound != mySubsMap.end()) {
+ theIndex = aFound->second;
+ return true;
+ }
return false;
}
if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
aSub = anObjects->createBody(this->data(), aSubIndex);
mySubs.push_back(aSub);
+ mySubsMap[aSub] = int(mySubs.size() - 1);
} else { // just update shape of this result
aSub = mySubs[aSubIndex];
}
while(mySubs.size() > aSubIndex) {
ResultBodyPtr anErased = *(mySubs.rbegin());
anErased->setDisabled(anErased, true);
+ mySubsMap.erase(anErased);
mySubs.pop_back();
}
if (aWasEmpty) { // erase all subs
anErased->setDisabled(anErased, true);
mySubs.pop_back();
}
+ mySubsMap.clear();
// redisplay this because result with and without subs are displayed differently
aECreator->sendUpdated(data()->owner(), EVENT_DISP);
}
#include "Model.h"
#include <ModelAPI_ResultCompSolid.h>
#include <vector>
+#include <map>
/**\class Model_ResultCompSolid
* \ingroup DataModel
{
/// Sub-bodies if this is compsolid: zero base index to subs
std::vector<std::shared_ptr<ModelAPI_ResultBody> > mySubs;
+ /// Also keep map of result to index in mySubs to facilitate speed of access from OB
+ std::map<ObjectPtr, int> mySubsMap;
/// Flag that stores the previous state of "concealed": if it is changed,
/// The event must be generated to redisplay this and all subs.
bool myLastConcealed;
bool forTree = false) const;
/// Returns true if feature or reuslt belong to this composite feature as subs
- MODEL_EXPORT virtual bool isSub(ObjectPtr theObject) const;
+ /// Returns theIndex - zero based index of sub if found
+ MODEL_EXPORT virtual bool isSub(ObjectPtr theObject, int& theIndex) const;
/// Returns the parameters of color definition in the resources config manager
MODEL_EXPORT virtual void colorConfigInfo(std::string& theSection, std::string& theName,
bool forTree = false) const = 0;
/// Returns true if feature or reuslt belong to this composite feature as subs
- virtual bool isSub(ObjectPtr theObject) const = 0;
+ /// Returns theIndex - zero based index of sub if found
+ virtual bool isSub(ObjectPtr theObject, int& theIndex) const = 0;
/// Set displayed flag to the result and all sub results
/// \param theDisplay a boolean value
ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
{
+ int anIndex;
ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
if (aBody.get()) {
FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
aFeatureOwner->results().cbegin();
for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
- if (aComp && aComp->isSub(aBody))
+ if (aComp && aComp->isSub(aBody, anIndex))
return aComp;
}
}
return ResultCompSolidPtr(); // not found
}
+int compSolidIndex(const ResultPtr& theSub)
+{
+ int anIndex = -1;
+ ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
+ if (aBody.get()) {
+ FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
+ if (aFeatureOwner.get()) {
+ std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
+ aFeatureOwner->results().cbegin();
+ for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
+ ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
+ if (aComp && aComp->isSub(aBody, anIndex))
+ return anIndex;
+ }
+ }
+ }
+ return anIndex; // not found
+}
+
bool hasSubResults(const ResultPtr& theResult)
{
ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
*/
MODELAPI_EXPORT std::shared_ptr<ModelAPI_ResultCompSolid> compSolidOwner(
const std::shared_ptr<ModelAPI_Result>& theSub);
+/*!
+ * Returns index of this result in parent (if parent exists, returned by compSolidOwner)
+ * \returns zero-base index, or -1 if not found
+ */
+MODELAPI_EXPORT int compSolidIndex(const std::shared_ptr<ModelAPI_Result>& theSub);
/*!
* Returns true if the result contains a not empty list of sub results.
if (aResult.get()) {
ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
if (aCompRes.get()) {
- for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
- if (aCompRes->subResult(i, true) == theObject) {
- aRow = i;
- break;
- }
- }
+ aRow = ModelAPI_Tools::compSolidIndex(aResult);
}
}
}