return anUsedParameters;
}
-std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters)
+std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters,
+ const DocumentPtr& theDocument)
{
std::list<ResultParameterPtr> aResult;
std::set<std::string>::const_iterator aParamIt = theParameters.cbegin();
const std::string& aName = *aParamIt;
double aValue;
ResultParameterPtr aParam;
- if (ModelAPI_Tools::findVariable(aName, aValue, aParam))
+ if (ModelAPI_Tools::findVariable(aName, aValue, aParam, theDocument))
aResult.push_back(aParam);
}
return aResult;
AttributeDoublePtr anAttribute =
std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr->second);
std::set<std::string> anUsedParameters = anAttribute->usedParameters();
- std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
+ std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters, aMyFeature->document());
aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
} else if (aType == GeomDataAPI_Point::typeId()) { // point attribute
AttributePointPtr anAttribute =
std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->second);
std::set<std::string> anUsedParameters = usedParameters(anAttribute);
- std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
+ std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters, aMyFeature->document());
aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
} else if (aType == GeomDataAPI_Point2D::typeId()) { // point attribute
AttributePoint2DPtr anAttribute =
std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->second);
std::set<std::string> anUsedParameters = usedParameters(anAttribute);
- std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
+ std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters, aMyFeature->document());
aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
} else
continue; // nothing to do, not reference
return theResult->shape();
}
-bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam)
+ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
{
- SessionPtr aSession = ModelAPI_Session::get();
- std::list<DocumentPtr> aDocList;
- DocumentPtr aDocument = aSession->activeDocument();
- DocumentPtr aRootDocument = aSession->moduleDocument();
- aDocList.push_back(aDocument);
- if (aDocument != aRootDocument) {
- aDocList.push_back(aRootDocument);
+ for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
+ ObjectPtr anObject = theDocument->object(theGroup, anIndex);
+ if (anObject->data()->name() == theName)
+ return anObject;
}
- for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
- ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName);
- theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
- if(!theParam.get())
- continue;
- AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
- outValue = aValueAttribute->value();
- return true;
- }
- return false;
+ // not found
+ return ObjectPtr();
}
-bool findVariable(const DocumentPtr& theDocument, const std::string& theName,
- double& outValue, ResultParameterPtr& theParam)
+bool findVariable(const DocumentPtr& theDocument,
+ const std::string& theName, double& outValue, ResultParameterPtr& theParam)
{
- ObjectPtr aParamObj = theDocument->objectByName(ModelAPI_ResultParameter::group(), theName);
+ ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
if (!theParam.get())
return false;
return true;
}
+bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam,
+ const DocumentPtr& theDocument /*= DocumentPtr()*/)
+{
+ SessionPtr aSession = ModelAPI_Session::get();
+ std::list<DocumentPtr> aDocList;
+ DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
+ DocumentPtr aRootDocument = aSession->moduleDocument();
+ aDocList.push_back(aDocument);
+ if (aDocument != aRootDocument) {
+ aDocList.push_back(aRootDocument);
+ }
+ for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
+ if (findVariable(*it, theName, outValue, theParam))
+ return true;
+ }
+ return false;
+}
+
static std::map<int, std::vector<int> > myColorMap;
void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
MODELAPI_EXPORT std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult);
/*!
- * Searches for variable with name \param theName in the active document (Part), when
- * in the root document (PartSet). If found, set it value in the \param outValue
- * and returns true.
+ * Searches for variable with name \param theName in \param theDocument.
+ * If found, set it value in the \param outValue and returns true.
*/
-MODELAPI_EXPORT bool findVariable(const std::string& theName, double& outValue,
- ResultParameterPtr& theParam);
+MODELAPI_EXPORT bool findVariable(const DocumentPtr& theDocument,
+ const std::string& theName, double& outValue, ResultParameterPtr& theParam);
/*!
- * Searches for variable with name \param theName in the document.
- * If found, set it value in the \param outValue and returns true.
+ * Searches for variable with name \param theName in the active document (Part), when
+ * in the root document (PartSet). If found, set it value in the \param outValue
+ * and returns true. If \param theDocument is empty active document is used.
*/
-MODELAPI_EXPORT bool findVariable(const DocumentPtr& theDocument, const std::string& theName,
- double& outValue, ResultParameterPtr& theParam);
+MODELAPI_EXPORT bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam,
+ const DocumentPtr& theDocument = DocumentPtr());
/*!
* Returns the values of the next random color. The values are in range [0, 255]
#include <Events_Error.h>
+#include <ModelAPI_AttributeRefList.h>
#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeValidator.h>
#include <ModelAPI_Document.h>
#include <ModelAPI_Events.h>
#include <ModelAPI_Session.h>
#include <ModelAPI_Tools.h>
-#include <ModelAPI_AttributeValidator.h>
#include <ModelAPI_AttributeDouble.h>
#include <GeomDataAPI_Point.h>
}
}
-double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
- std::string& theError)
+double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, std::string& theError,
+ const std::shared_ptr<ModelAPI_Document>& theDocument)
{
std::list<std::string> anExprParams = myInterp->compile(theExpression);
// find expression's params in the model
for ( ; it != anExprParams.end(); it++) {
double aValue;
ResultParameterPtr aParamRes;
- if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue;
+ if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, theDocument)) continue;
std::ostringstream sstream;
sstream << aValue;
AttributeDoublePtr anAttribute =
std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
std::string anError;
- double aValue = evaluate(anAttribute->text(), anError);
+ double aValue = evaluate(anAttribute->text(), anError, anAttribute->owner()->document());
bool isValid = anError.empty();
if (isValid)
anAttribute->setCalculatedValue(aValue);
};
for (int i = 0; i < 3; ++i) {
std::string anError;
- double aValue = evaluate(aText[i], anError);
+ double aValue = evaluate(aText[i], anError, anAttribute->owner()->document());
bool isValid = anError.empty();
if (isValid) aCalculatedValue[i] = aValue;
anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
};
for (int i = 0; i < 2; ++i) {
std::string anError;
- double aValue = evaluate(aText[i], anError);
+ double aValue = evaluate(aText[i], anError, anAttribute->owner()->document());
bool isValid = anError.empty();
if (isValid) aCalculatedValue[i] = aValue;
anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
return;
}
- // List of documents to process
- std::list<DocumentPtr> aDocList;
- SessionPtr aSession = ModelAPI_Session::get();
- DocumentPtr aDocument = aSession->activeDocument();
- DocumentPtr aRootDocument = aSession->moduleDocument();
- aDocList.push_back(aDocument);
- if (aDocument != aRootDocument) {
- aDocList.push_back(aRootDocument);
- }
-
- // Find all features
- for (std::list<DocumentPtr>::const_iterator aDicumentIt = aDocList.begin();
- aDicumentIt != aDocList.end(); ++aDicumentIt) {
- const DocumentPtr& aDocument = *aDicumentIt;
- std::list<FeaturePtr> aFeatures = aDocument->allFeatures();
- std::list<FeaturePtr>::iterator aFeatureIt = aFeatures.begin();
- for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) {
- const FeaturePtr& aFeature = *aFeatureIt;
-
- // If Parameter feature then rename its expression
+ std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
+ aResultParameter->data()->refsToMe();
+ std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
+ anAttributes.cbegin();
+ for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
+ const AttributePtr& anAttribute = *anAttributeIt;
+ AttributeRefListPtr anAttributeRefList =
+ std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
+ if (anAttributeRefList.get()) {
std::shared_ptr<ParametersPlugin_Parameter> aParameter =
- std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
- if (aParameter.get()) {
+ std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+ anAttributeRefList->owner());
+ if (aParameter.get())
// Rename
renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
- continue;
- }
-
- // Find all attributes
- std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
- std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
- for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
- const AttributePtr& anAttribute = *anAttributeIt;
-
+ } else
// Rename
renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());
- }
- }
}
}
#include <Events_Loop.h>
class ModelAPI_Attribute;
+class ModelAPI_Document;
class ParametersPlugin_Parameter;
class ParametersPlugin_PyInterp;
virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
protected:
- double evaluate(const std::string& theExpression, std::string& theError);
+ double evaluate(const std::string& theExpression, std::string& theError,
+ const std::shared_ptr<ModelAPI_Document>& theDocument);
void processEvaluationEvent(const std::shared_ptr<Events_Message>& theMessage);
void processObjectRenamedEvent(const std::shared_ptr<Events_Message>& theMessage);
data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId());
data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId());
data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId());
+ data()->reflist(ARGUMENTS_ID())->setIsArgument(false);
ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID());
}
for ( ; it != anExprParams.end(); it++) {
double aValue;
ResultParameterPtr aParamRes;
- if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue;
+ if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, document())) continue;
aParamsList.push_back(aParamRes);
std::ostringstream sstream;