[CheckGeometry] add new shapecontent builder instead of OCCT's to correct some errors in OCCT's values and to add for some more advanced information, e.g. volume or area, add new method to Base::Interpreter runStringWithKey() -- allows to run a python script and get a string return value

This commit is contained in:
mwganson
2020-07-26 13:53:57 -05:00
committed by Yorik van Havre
parent 8d938785ab
commit ed3ff7db5e
4 changed files with 160 additions and 9 deletions

View File

@@ -293,6 +293,45 @@ std::string InterpreterSingleton::runString(const char *sCmd)
}
}
/** runStringWithKey(psCmd, key, key_initial_value)
* psCmd is python script to run
* key is the name of a python string variable the script will have read/write
* access to during script execution. It will be our return value.
* key_initial_value is the initial value c++ will set before calling the script
* To check for runtime errors during script execution compare the return value
* of runStringWithKey() to the inital value set. If they are the same then
* there was a runtime error (presuming the script is written to change the key).
*
* example: runStringWithKey("_key = 'new string'", "_key", "old string")
* If the return value of runStringWithKey() = "old string" then there was an error
* Enable logging and copy/paste the script to the console or to a macro to debug.
*/
std::string InterpreterSingleton::runStringWithKey(const char *psCmd, const char *key, const char *key_initial_value){
PyGILStateLocker locker;
PyObject* main = PP_Load_Module("__main__");
if (main == NULL)
throw PyException();
PyObject* globalDictionary = PyModule_GetDict(main);
if (globalDictionary == NULL)
throw PyException();
PyObject* localDictionary = PyDict_New();
if (localDictionary == NULL)
throw PyException();
PyObject* initial_value = PyUnicode_FromString(key_initial_value);
PyDict_SetItemString(localDictionary, key, initial_value);
PyRun_String(psCmd, Py_file_input, globalDictionary, localDictionary);
PyObject* key_return_value = PyDict_GetItemString(localDictionary, key);
#if PY_MAJOR_VERSION >= 3
PyObject* str = PyUnicode_AsEncodedString(key_return_value, "utf-8", "~E~");
#else
PyObject* str = PyString_FromString(key_return_value, "utf-8", "~E~");
#endif
const char* result = PyBytes_AS_STRING(str);
return result;
}
Py::Object InterpreterSingleton::runStringObject(const char *sCmd)
{
PyObject *module, *dict, *presult; /* "exec code in d, d" */

View File

@@ -218,6 +218,8 @@ public:
//@{
/// Run a statement on the python interpreter and gives back a string with the representation of the result.
std::string runString(const char *psCmd);
/// Run a statement on the python interpreter with a key for exchanging strings
std::string runStringWithKey(const char *psCmd, const char *key, const char *key_initial_value="");
/// Run a statement on the python interpreter and return back the result object.
Py::Object runStringObject(const char *sCmd);
/// Run a statement on the python interpreter and gives back a string with the representation of the result.