Welcome to mirror list, hosted at ThFree Co, Russian Federation.

py_arc_welder.cpp « PyArcWelder - github.com/FormerLurker/ArcWelderLib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f811e6d6f10805c4b2e09d9aeacd63541275f8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Arc Welder: Anti-Stutter Python Extension for the OctoPrint Arc Welder plugin.
//
// Compresses many G0/G1 commands into G2/G3(arc) commands where possible, ensuring the tool paths stay within the specified resolution.
// This reduces file size and the number of gcodes per second.
//
// Copyright(C) 2020 - Brad Hochgesang
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU Affero General Public License for more details.
//
//
// You can contact the author at the following email address: 
// FormerLurker@pm.me
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "py_arc_welder.h"

PyObject* py_arc_welder::build_py_progress(const arc_welder_progress& progress, std::string guid)
{
	std::string segment_statistics = progress.segment_statistics.str();
	PyObject* pyGuid = gcode_arc_converter::PyUnicode_SafeFromString(guid);
	if (pyGuid == NULL)
		return NULL;
	PyObject* pyMessage = gcode_arc_converter::PyUnicode_SafeFromString(segment_statistics);
	if (pyMessage == NULL)
		return NULL;
	double total_count_reduction_percent = progress.segment_statistics.get_total_count_reduction_percent();
	PyObject* py_progress = Py_BuildValue("{s:d,s:d,s:d,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:f,s:f,s:f,s:f,s:i,s:i,s:f}",
		"percent_complete",
		progress.percent_complete,												//1
		"seconds_elapsed",
		progress.seconds_elapsed,													//2
		"seconds_remaining",
		progress.seconds_remaining,												//3
		"gcodes_processed",
		progress.gcodes_processed,												//4
		"lines_processed",
		progress.lines_processed,													//5
		"points_compressed",
		progress.points_compressed,												//6
		"arcs_created",
		progress.arcs_created,														//7
		"arcs_aborted_by_flowrate",
		progress.arcs_aborted_by_flow_rate,	  						//8
		"num_firmware_compensations",
		progress.num_firmware_compensations,							//9
		"source_file_position",
		progress.source_file_position,										//10
		"source_file_size",
		progress.source_file_size,												//11
		"target_file_size",
		progress.target_file_size,												//12
		"compression_ratio",
		progress.compression_ratio,												//13
		"compression_percent",
		progress.compression_percent,											//14
		"source_file_total_length",
		progress.segment_statistics.total_length_source,	//15
		"target_file_total_length",
		progress.segment_statistics.total_length_target,	//16
		"source_file_total_count",
		progress.segment_statistics.total_count_source,		//17
		"target_file_total_count",
		progress.segment_statistics.total_count_target,   //18
		"total_count_reduction_percent",
		total_count_reduction_percent                     //19
		
	);

	if (py_progress == NULL)
	{
		return NULL;
	}
	// Due to a CRAZY issue, I have to add this item after building the py_progress object,
	// else it crashes in python 2.7.  Looking forward to retiring this backwards 
	// compatible code...
	PyDict_SetItemString(py_progress, "segment_statistics_text", pyMessage);
	PyDict_SetItemString(py_progress, "guid", pyGuid);
	return py_progress;
}

bool py_arc_welder::on_progress_(const arc_welder_progress& progress)
{
	PyObject* py_dict = py_arc_welder::build_py_progress(progress, guid_);
	if (py_dict == NULL)
	{
		return false;
	}
	PyObject* func_args = Py_BuildValue("(O)", py_dict);
	if (func_args == NULL)
	{
		Py_DECREF(py_dict);
		return false;	// This was returning true, I think it was a typo.  Making a note just in case.
	}
		
	PyGILState_STATE gstate = PyGILState_Ensure();
	PyObject* pContinueProcessing = PyObject_CallObject(py_progress_callback_, func_args);
	PyGILState_Release(gstate);
	Py_DECREF(func_args);
	Py_DECREF(py_dict);
	bool continue_processing;
	if (pContinueProcessing == NULL)
	{
		// 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);
	}
	
	return continue_processing;
}