diff options
author | FormerLurker <hochgebe@gmail.com> | 2020-05-02 18:06:40 +0300 |
---|---|---|
committer | FormerLurker <hochgebe@gmail.com> | 2020-05-02 18:06:40 +0300 |
commit | 8d7b572cc7fa7faf795db88a29340b6380090f8f (patch) | |
tree | 5c5b2fb251af89e0c9038be84236c6df85d098fb /PyArcWelder | |
parent | 4fcf89d4995921b89b579d06052df11b66e4879f (diff) |
Add enhanced progress and complete statistics for arc welder. Add ARC_SEGMENTS_PER_SEC to inverse processor.
Diffstat (limited to 'PyArcWelder')
-rw-r--r-- | PyArcWelder/PyArcWelder.vcxproj | 14 | ||||
-rw-r--r-- | PyArcWelder/py_arc_welder.cpp | 69 | ||||
-rw-r--r-- | PyArcWelder/py_arc_welder.h | 3 | ||||
-rw-r--r-- | PyArcWelder/py_arc_welder_extension.cpp | 63 | ||||
-rw-r--r-- | PyArcWelder/py_arc_welder_extension.h | 4 | ||||
-rw-r--r-- | PyArcWelder/py_logger.cpp | 14 |
6 files changed, 123 insertions, 44 deletions
diff --git a/PyArcWelder/PyArcWelder.vcxproj b/PyArcWelder/PyArcWelder.vcxproj index a17c4c6..1657d94 100644 --- a/PyArcWelder/PyArcWelder.vcxproj +++ b/PyArcWelder/PyArcWelder.vcxproj @@ -97,6 +97,10 @@ <GenerateDebugInformation>true</GenerateDebugInformation> <AdditionalLibraryDirectories>C:\Python27\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> + <PostBuildEvent> + <Command> + </Command> + </PostBuildEvent> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> @@ -110,6 +114,10 @@ <GenerateDebugInformation>true</GenerateDebugInformation> <AdditionalLibraryDirectories>C:\Python27\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> + <PostBuildEvent> + <Command> + </Command> + </PostBuildEvent> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ClCompile> @@ -127,6 +135,9 @@ <GenerateDebugInformation>true</GenerateDebugInformation> <AdditionalLibraryDirectories>C:\Python27\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> + <PostBuildEvent> + <Command>$(SolutionDir)\DeployCodeToPlugin.bat $(SolutionDir) C:\Users\Brad\source\repos\ArcWelderPlugin\</Command> + </PostBuildEvent> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ClCompile> @@ -144,6 +155,9 @@ <GenerateDebugInformation>true</GenerateDebugInformation> <AdditionalLibraryDirectories>C:\Python27\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> + <PostBuildEvent> + <Command>$(SolutionDir)\DeployCodeToPlugin.bat $(SolutionDir) C:\Users\Brad\source\repos\ArcWelderPlugin\</Command> + </PostBuildEvent> </ItemDefinitionGroup> <ItemGroup> <ClInclude Include="python_helpers.h" /> diff --git a/PyArcWelder/py_arc_welder.cpp b/PyArcWelder/py_arc_welder.cpp index 5768b40..0410c68 100644 --- a/PyArcWelder/py_arc_welder.cpp +++ b/PyArcWelder/py_arc_welder.cpp @@ -23,26 +23,65 @@ #include "py_arc_welder.h" -bool py_arc_welder::on_progress_(double percent_complete, double seconds_elapsed, double estimated_seconds_remaining, int gcodes_processed, int current_line, int points_compressed, int arcs_created) +PyObject* py_arc_welder::build_py_progress(arc_welder_progress progress) { - PyObject* funcArgs = Py_BuildValue("(d,d,d,i,i,i,i)", percent_complete, seconds_elapsed, estimated_seconds_remaining, gcodes_processed, current_line, points_compressed, arcs_created); - if (funcArgs == NULL) + PyObject* py_progress = Py_BuildValue("{s:d,s:d,s:d,s:i,s:i,s:i,s:i,s:i,s:i}", + u8"percent_complete", + progress.percent_complete, + u8"seconds_elapsed", + progress.seconds_elapsed, + u8"seconds_remaining", + progress.seconds_remaining, + u8"gcodes_processed", + progress.gcodes_processed, + u8"lines_processed", + progress.lines_processed, + u8"points_compressed", + progress.points_compressed, + u8"arcs_created", + progress.arcs_created, + u8"source_file_size", + progress.source_file_size, + u8"target_file_size", + progress.target_file_size + ); + return py_progress; +} + +bool py_arc_welder::on_progress_(arc_welder_progress progress) +{ + PyObject* py_dict = py_arc_welder::build_py_progress(progress); + if (py_dict == NULL) + return NULL; + PyObject* func_args = Py_BuildValue("(O)", py_dict); + if (func_args == NULL) { - return false; + Py_DECREF(py_dict); + return true; } - + PyGILState_STATE gstate = PyGILState_Ensure(); - PyObject* pContinueProcessing = PyObject_CallObject(py_progress_callback_, funcArgs); - Py_DECREF(funcArgs); - bool continue_processing = PyLong_AsLong(pContinueProcessing) > 0; - Py_DECREF(pContinueProcessing); - PyGILState_Release(gstate); - - if (pContinueProcessing == NULL || pContinueProcessing == Py_None) + PyObject* pContinueProcessing = PyObject_CallObject(py_progress_callback_, func_args); + Py_DECREF(func_args); + Py_DECREF(py_dict); + bool continue_processing; + if (pContinueProcessing == NULL) { - // no return value was supply, assume true - - return true; + // no return value was supply, assume true, but without decrefing pContinueProcessing + continue_processing = true; } + else + { + if (pContinueProcessing == Py_None) + { + continue_processing = true; + } + else + { + continue_processing = PyLong_AsLong(pContinueProcessing) > 0; + } + Py_DECREF(pContinueProcessing); + } + PyGILState_Release(gstate); return continue_processing; } diff --git a/PyArcWelder/py_arc_welder.h b/PyArcWelder/py_arc_welder.h index 64bfd71..b116791 100644 --- a/PyArcWelder/py_arc_welder.h +++ b/PyArcWelder/py_arc_welder.h @@ -41,8 +41,9 @@ public: virtual ~py_arc_welder() { } + static PyObject* build_py_progress(arc_welder_progress progress); protected: - virtual bool on_progress_(double percent_complete, double seconds_elapsed, double estimated_seconds_remaining, int gcodes_processed, int current_line, int points_compressed, int arcs_created); + virtual bool on_progress_(arc_welder_progress progress); private: PyObject* py_progress_callback_; }; diff --git a/PyArcWelder/py_arc_welder_extension.cpp b/PyArcWelder/py_arc_welder_extension.cpp index eaf9923..89410b9 100644 --- a/PyArcWelder/py_arc_welder_extension.cpp +++ b/PyArcWelder/py_arc_welder_extension.cpp @@ -39,17 +39,18 @@ int main(int argc, char* argv[]) } // Add a built-in module, before Py_Initialize - PyImport_AppendInittab("PyArcWelder", PyInit_PyGcodeArcConverter); + PyImport_AppendInittab("PyArcWelder", PyInit_PyArcWelder); // Pass argv[0] to the Python interpreter Py_SetProgramName(program); // Initialize the Python interpreter. Required. Py_Initialize(); - std::cout << "Initializing threads..."; + // We are not using threads, do not enable. + /*std::cout << "Initializing threads..."; if (!PyEval_ThreadsInitialized()) { PyEval_InitThreads(); - } + }*/ // Optionally import the module; alternatively, import can be deferred until the embedded script imports it. PyImport_ImportModule("PyArcWelder"); PyMem_RawFree(program); @@ -62,10 +63,13 @@ int main(int argc, char* argv[]) { Py_SetProgramName(argv[0]); Py_Initialize(); + // We are not using threads, do not enable. + /* std::cout << "Initializing threads..."; if (!PyEval_ThreadsInitialized()) { PyEval_InitThreads(); } - initPyGcodeArcConverter(); + */ + initPyArcWelder(); return 0; } @@ -82,61 +86,61 @@ static struct module_state _state; #endif // Python 2 module method definition -static PyMethodDef PyGcodeArcConverterMethods[] = { +static PyMethodDef PyArcWelderMethods[] = { { "ConvertFile", (PyCFunction)ConvertFile, METH_VARARGS ,"Converts segmented curve approximations to actual G2/G3 arcs within the supplied resolution." }, { NULL, NULL, 0, NULL } }; // Python 3 module method definition #if PY_MAJOR_VERSION >= 3 -static int PyGcodeArcConverter_traverse(PyObject* m, visitproc visit, void* arg) { +static int PyArcWelder_traverse(PyObject* m, visitproc visit, void* arg) { Py_VISIT(GETSTATE(m)->error); return 0; } -static int PyGcodeArcConverter_clear(PyObject* m) { +static int PyArcWelder_clear(PyObject* m) { Py_CLEAR(GETSTATE(m)->error); return 0; } static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, - "PyGcodeArcConverter", + "PyArcWelder", NULL, sizeof(struct module_state), - PyGcodeArcConverterMethods, + PyArcWelderMethods, NULL, - PyGcodeArcConverter_traverse, - PyGcodeArcConverter_clear, + PyArcWelder_traverse, + PyArcWelder_clear, NULL }; #define INITERROR return NULL PyMODINIT_FUNC -PyInit_PyGcodeArcConverter(void) +PyInit_PyArcWelder(void) #else #define INITERROR return -extern "C" void initPyGcodeArcConverter(void) +extern "C" void initPyArcWelder(void) #endif { - std::cout << "Initializing PyGcodeArcConverter V0.1.0rc1.dev0 - Copyright (C) 2019 Brad Hochgesang..."; + std::cout << "Initializing PyArcWelder V0.1.0rc1.dev0 - Copyright (C) 2019 Brad Hochgesang."; #if PY_MAJOR_VERSION >= 3 - std::cout << "Python 3+ Detected..."; + std::cout << " Python 3+ Detected..."; PyObject* module = PyModule_Create(&moduledef); #else - std::cout << "Python 2 Detected..."; - PyObject* module = Py_InitModule("PyGcodeArcConverter", PyGcodeArcConverterMethods); + std::cout << " Python 2 Detected..."; + PyObject* module = Py_InitModule("PyArcWelder", PyArcWelderMethods); #endif if (module == NULL) INITERROR; struct module_state* st = GETSTATE(module); - st->error = PyErr_NewException((char*)"PyGcodeArcConverter.Error", NULL, NULL); + st->error = PyErr_NewException((char*)"PyArcWelder.Error", NULL, NULL); if (st->error == NULL) { Py_DECREF(module); INITERROR; @@ -150,7 +154,7 @@ extern "C" void initPyGcodeArcConverter(void) std::string message = "PyArcWelder V0.1.0rc1.dev0 imported - Copyright (C) 2019 Brad Hochgesang..."; p_py_logger->log(GCODE_CONVERSION, INFO, message); p_py_logger->set_log_level_by_value(DEBUG); - std::cout << "complete\r\n"; + std::cout << " Initialization Complete\r\n"; #if PY_MAJOR_VERSION >= 3 return module; @@ -191,12 +195,27 @@ extern "C" p_py_logger->log(GCODE_CONVERSION, INFO, message); py_arc_welder arc_welder_obj(args.source_file_path, args.target_file_path, p_py_logger, args.resolution_mm, args.g90_g91_influences_extruder, 50, py_progress_callback); - arc_welder_obj.process(); + arc_welder_results results = arc_welder_obj.process(); message = "py_gcode_arc_converter.ConvertFile - Arc Conversion Complete."; p_py_logger->log(GCODE_CONVERSION, INFO, message); Py_XDECREF(py_progress_callback); - // For now just return py_none - return PyTuple_Pack(1, Py_None); + // return the arguments + PyObject* p_progress = py_arc_welder::build_py_progress(results.progress); + if (p_progress == NULL) + p_progress = Py_None; + + PyObject* p_results = Py_BuildValue( + "{s:i,s:i,s:s,s:O}", + u8"success", + results.success, + u8"cancelled", + results.cancelled, + u8"message", + results.message, + u8"progress", + p_progress + ); + return p_results; } } diff --git a/PyArcWelder/py_arc_welder_extension.h b/PyArcWelder/py_arc_welder_extension.h index 7a3786d..47b7215 100644 --- a/PyArcWelder/py_arc_welder_extension.h +++ b/PyArcWelder/py_arc_welder_extension.h @@ -33,9 +33,9 @@ extern "C" { #if PY_MAJOR_VERSION >= 3 - PyMODINIT_FUNC PyInit_PyGcodeArcConverter(void); + PyMODINIT_FUNC PyInit_PyArcWelder(void); #else - extern "C" void initPyGcodeArcConverter(void); + extern "C" void initPyArcWelder(void); #endif static PyObject* ConvertFile(PyObject* self, PyObject* args); } diff --git a/PyArcWelder/py_logger.cpp b/PyArcWelder/py_logger.cpp index d05e059..1020331 100644 --- a/PyArcWelder/py_logger.cpp +++ b/PyArcWelder/py_logger.cpp @@ -60,7 +60,6 @@ void py_logger::initialize_loggers() // Create a logging configurator PyGILState_STATE gstate = PyGILState_Ensure(); - std::cout << "Creating arguments for LoggingConfigurator creation.\r\n"; PyObject* funcArgs = Py_BuildValue("(s,s,s)", "arc_welder", "arc_welder.", "octoprint_arc_welder."); if (funcArgs == NULL) { @@ -68,7 +67,7 @@ void py_logger::initialize_loggers() PyErr_SetString(PyExc_ImportError, "Could not create LoggingConfigurator arguments."); return; } - std::cout << "Creating LoggingConfigurator..."; + py_logging_configurator = PyObject_CallObject(py_logging_configurator_name, funcArgs); std::cout << "Complete.\r\n"; Py_DECREF(funcArgs); @@ -98,8 +97,6 @@ void py_logger::initialize_loggers() py_critical_function_name = gcode_arc_converter::PyString_SafeFromString("critical"); py_get_effective_level_function_name = gcode_arc_converter::PyString_SafeFromString("getEffectiveLevel"); loggers_created_ = true; - std::cout << "Logger created successfully.\r\n"; - } void py_logger::set_internal_log_levels(bool check_real_time) @@ -199,6 +196,11 @@ void py_logger::log(const int logger_type, const int log_level, const std::strin case CRITICAL: pyFunctionName = py_critical_function_name; break; + default: + std::cout << "An unknown log level of '" << log_level << " 'was supplied for the message: " << message.c_str() << "\r\n"; + PyErr_Format(PyExc_ValueError, + "An unknown log level was supplied for the message %s.", message.c_str()); + return; } } PyObject* pyMessage = gcode_arc_converter::PyUnicode_SafeFromString(message); @@ -220,15 +222,19 @@ void py_logger::log(const int logger_type, const int log_level, const std::strin { std::cout << "Logging.arc_welder_log - null was returned from the specified logger.\r\n"; PyErr_SetString(PyExc_ValueError, "Logging.arc_welder_log - null was returned from the specified logger."); + return; } else { std::cout << "Logging.arc_welder_log - null was returned from the specified logger and an error was detected.\r\n"; + std::cout << "\tLog Level: " << log_level <<", Logger Type: " << logger_type << ", Message: " << message.c_str() << "\r\n"; + // I'm not sure what else to do here since I can't log the error. I will print it // so that it shows up in the console, but I can't log it, and there is no way to // return an error. PyErr_Print(); PyErr_Clear(); + return; } } else |