21
21
from UM .PluginRegistry import PluginRegistry
22
22
from UM .OutputDevice .OutputDevice import OutputDevice
23
23
from UM .OutputDevice import OutputDeviceError
24
+ from UM .Platform import Platform
24
25
25
26
from UM .i18n import i18nCatalog
26
27
from .ChituCodeWriter import ChituCodeWriter
@@ -66,8 +67,7 @@ def __init__(self, name, target_ip):
66
67
self .sock = socket (AF_INET , SOCK_DGRAM )
67
68
self .sock .setsockopt (SOL_SOCKET , SO_BROADCAST , 1 )
68
69
69
- self ._localTempGcode = Resources .getStoragePath (
70
- Resources .Resources , 'data.gcode' )
70
+ self ._localTempGcode = Resources .getStoragePath (Resources .Resources , 'data.gcode' )
71
71
self ._send_thread = None
72
72
self ._file_encode = 'utf-8'
73
73
@@ -91,27 +91,22 @@ def requestWrite(self, node, fileName=None, *args, **kwargs):
91
91
raise OutputDeviceError .DeviceBusyError ()
92
92
93
93
if fileName :
94
- fileName = os .path .splitext (fileName )[0 ] + '.gcode.tz'
94
+ fileName = os .path .splitext (fileName )[0 ]
95
95
else :
96
- fileName = "%s.gcode.tz " % Application .getInstance ().getPrintInformation ().jobName
96
+ fileName = "%s" % Application .getInstance ().getPrintInformation ().jobName
97
97
self .targetSendFileName = fileName
98
98
99
- path = os .path .join (os .path .dirname (
100
- os .path .abspath (__file__ )), 'UploadFilename.qml' )
101
- self ._dialog = CuraApplication .getInstance (
102
- ).createQmlComponent (path , {"manager" : self })
99
+ path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'UploadFilename.qml' )
100
+ self ._dialog = CuraApplication .getInstance ().createQmlComponent (path , {"manager" : self })
103
101
self ._dialog .textChanged .connect (self .onFilenameChanged )
104
102
self ._dialog .accepted .connect (self .onFilenameAccepted )
105
103
self ._dialog .show ()
106
- self ._dialog .findChild (QObject , "nameField" ).setProperty (
107
- 'text' , self .targetSendFileName )
108
- self ._dialog .findChild (QObject , "nameField" ).select (
109
- 0 , len (self .targetSendFileName ) - 9 )
104
+ self ._dialog .findChild (QObject , "nameField" ).setProperty ('text' , self .targetSendFileName )
105
+ self ._dialog .findChild (QObject , "nameField" ).select (0 , len (self .targetSendFileName ))
110
106
self ._dialog .findChild (QObject , "nameField" ).setProperty ('focus' , True )
111
107
112
108
def onFilenameChanged (self ):
113
- fileName = self ._dialog .findChild (
114
- QObject , "nameField" ).property ('text' ).strip ()
109
+ fileName = self ._dialog .findChild (QObject , "nameField" ).property ('text' ).strip ()
115
110
forbidden_characters = "\" '´`<>()[]?*\,;:&%#$!"
116
111
for forbidden_character in forbidden_characters :
117
112
if forbidden_character in fileName :
@@ -138,7 +133,6 @@ def _genTempGcodeFile(self):
138
133
if not fp :
139
134
self ._result = SendResult .FILE_NOT_SAVE
140
135
else :
141
- #writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("ChituCodeWriter"))
142
136
writer = ChituCodeWriter ()
143
137
success = writer .write (fp , None , MeshWriter .OutputMode .TextMode )
144
138
fp .close ()
@@ -160,8 +154,7 @@ def sendDatThread(self):
160
154
if tryCnt > 3 :
161
155
self ._result = SendResult .CONNECT_TIMEOUT
162
156
break
163
- self .sock .sendto (self .encodeCmd (
164
- 'M4001\r \n ' ), (self ._targetIP , self .PORT ))
157
+ self .sock .sendto (self .encodeCmd ('M4001\r \n ' ), (self ._targetIP , self .PORT ))
165
158
message , address = self .sock .recvfrom (self .RECVBUF )
166
159
Logger .log ('d' , message )
167
160
break
@@ -175,12 +168,16 @@ def sendDatThread(self):
175
168
break
176
169
if self ._abort :
177
170
break
178
- filePath = self ._localTempGcode + '.tz'
179
- Logger .log ('d' , 'compressed file path: ' + filePath )
171
+ if os .path .exists (self ._localTempGcode + '.tz' ):
172
+ filePath = self ._localTempGcode + '.tz'
173
+ self .targetSendFileName = self .targetSendFileName + '.gcode.tz'
174
+ else :
175
+ filePath = self ._localTempGcode
176
+ self .targetSendFileName = self .targetSendFileName + '.gcode'
177
+ Logger .log ('d' , 'file path: ' + filePath )
180
178
try :
181
179
self .sendMax = os .path .getsize (filePath )
182
- Logger .log ('d' , 'compressed file size: ' +
183
- str (self .sendMax ))
180
+ Logger .log ('d' , 'file size: ' + str (self .sendMax ))
184
181
if self .sendMax == 0 :
185
182
self ._result = SendResult .FILE_EMPTY
186
183
break
@@ -197,8 +194,7 @@ def sendDatThread(self):
197
194
fp .seek (0 , 0 )
198
195
cmd = 'M28 ' + self .targetSendFileName
199
196
Logger .log ('d' , 'cmd:' + cmd )
200
- self .sock .sendto (self .encodeCmd (
201
- cmd ), (self ._targetIP , self .PORT ))
197
+ self .sock .sendto (self .encodeCmd (cmd ), (self ._targetIP , self .PORT ))
202
198
message , address = self .sock .recvfrom (self .RECVBUF )
203
199
message = message .decode ('utf-8' , 'replace' )
204
200
Logger .log ('d' , 'message: ' + message )
@@ -218,7 +214,7 @@ def sendDatThread(self):
218
214
break
219
215
data = fp .read (self .BUFSIZE )
220
216
if not data :
221
- Logger .log ('f ' , 'reach file end' )
217
+ Logger .log ('d ' , 'reach file end' )
222
218
if finishedCnt >= 50 or not lastDataArray :
223
219
break
224
220
dataArray = lastDataArray
@@ -249,8 +245,7 @@ def sendDatThread(self):
249
245
dataArray [datSize + 5 ] = 131
250
246
lastDataArray = dataArray
251
247
252
- self .sock .sendto (
253
- dataArray , (self ._targetIP , self .PORT ))
248
+ self .sock .sendto (dataArray , (self ._targetIP , self .PORT ))
254
249
message , address = self .sock .recvfrom (self .RECVBUF )
255
250
timeoutCnt = 0
256
251
message = message .decode ('utf-8' , 'replace' )
@@ -271,14 +266,12 @@ def sendDatThread(self):
271
266
value = value [0 ].replace ('resend ' , '' )
272
267
oldseek = offset = int (value )
273
268
fp .seek (offset , 0 )
274
- Logger .log (
275
- 'd' , 'resend offset:' + str (offset ))
269
+ Logger .log ('d' , 'resend offset:' + str (offset ))
276
270
else :
277
271
Logger .log ('d' , 'Error offset:' + message )
278
272
except timeout :
279
273
if finishedCnt < 4 and timeoutCnt > 150 or finishedCnt > 45 :
280
- Logger .log (
281
- 'w' , 'finishedCnt: ' + str (finishedCnt ) + ' timeoutcnt: ' + str (timeoutCnt ))
274
+ Logger .log ('w' , 'finishedCnt: ' + str (finishedCnt ) + ' timeoutcnt: ' + str (timeoutCnt ))
282
275
self ._result = SendResult .CONNECT_TIMEOUT
283
276
break
284
277
timeoutCnt += 1
@@ -287,16 +280,18 @@ def sendDatThread(self):
287
280
self ._abort = True
288
281
289
282
fp .close ()
290
- os .remove (filePath )
283
+ if os .path .exists (filePath ):
284
+ os .remove (filePath )
285
+ if os .path .exists (self ._localTempGcode ):
286
+ os .remove (self ._localTempGcode )
291
287
break
292
288
293
289
if not self ._abort and self ._result == SendResult .SEND_RUNNING :
294
290
self .sock .settimeout (2 )
295
291
tryCnt = 0
296
292
while True :
297
293
try :
298
- self .sock .sendto (self .encodeCmd (
299
- 'M29' ), (self ._targetIP , self .PORT ))
294
+ self .sock .sendto (self .encodeCmd ('M29' ), (self ._targetIP , self .PORT ))
300
295
message , address = self .sock .recvfrom (self .RECVBUF )
301
296
message = message .decode ('utf-8' , 'replace' )
302
297
Logger .log ('d' , 'M29 rcv:' + message )
@@ -328,8 +323,7 @@ def dataCompressThread(self):
328
323
break
329
324
self .sock .settimeout (2 )
330
325
Logger .log ('d' , self ._targetIP )
331
- self .sock .sendto (self .encodeCmd ('M4001' ),
332
- (self ._targetIP , self .PORT ))
326
+ self .sock .sendto (self .encodeCmd ('M4001' ), (self ._targetIP , self .PORT ))
333
327
message , address = self .sock .recvfrom (self .BUFSIZE )
334
328
pattern = re .compile (self .datamask )
335
329
msg = message .decode ('utf-8' , 'ignore' )
@@ -364,15 +358,25 @@ def dataCompressThread(self):
364
358
s_z_max = _ [3 ]
365
359
elif id == 'U' :
366
360
self ._file_encode = value .replace ("'" , '' )
367
- exePath = os .path .join (os .path .dirname (
368
- os .path .abspath (__file__ )), 'VC_compress_gcode.exe' )
369
- cmd = '"' + exePath + '"' + ' "' + self ._localTempGcode + '" ' + x_mm_per_step + ' ' + y_mm_per_step + ' ' + z_mm_per_step + ' ' + \
370
- e_mm_per_step + ' "' + \
371
- os .path .dirname (self ._localTempGcode ) + '" ' + s_x_max + \
372
- ' ' + s_y_max + ' ' + s_z_max + ' ' + s_machine_type
373
- Logger .log ('d' , cmd )
374
- ret = subprocess .Popen (cmd , stdout = subprocess .PIPE , shell = True )
375
- Logger .log ('d' , ret .stdout .read ().decode ('utf-8' , 'ignore' ))
361
+ try :
362
+ if Platform .isWindows ():
363
+ exePath = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'VC_compress_gcode.exe' )
364
+ elif Platform .isOSX ():
365
+ exePath = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'VC_compress_gcode_MAC' )
366
+ if not os .path .exists (exePath ):
367
+ exePath = './VC_compress_gcode_MAC'
368
+ else :
369
+ Logger .log ('e' , "Could not apply gcode compression" )
370
+ break
371
+ cmd = '"' + exePath + '"' + ' "' + self ._localTempGcode + '" ' + x_mm_per_step + ' ' + y_mm_per_step + ' ' + z_mm_per_step + ' ' + \
372
+ e_mm_per_step + ' "' + \
373
+ os .path .dirname (self ._localTempGcode ) + '" ' + s_x_max + \
374
+ ' ' + s_y_max + ' ' + s_z_max + ' ' + s_machine_type
375
+ Logger .log ('d' , cmd )
376
+ ret = subprocess .Popen (cmd , stdout = subprocess .PIPE , shell = True )
377
+ Logger .log ('d' , ret .stdout .read ().decode ('utf-8' , 'ignore' ))
378
+ except :
379
+ Logger .log ('e' , "Could not apply gcode compression" )
376
380
break
377
381
except timeout :
378
382
tryCnt += 1
@@ -410,12 +414,9 @@ def startSendingThread(self):
410
414
return
411
415
412
416
if self ._result == SendResult .SEND_DONE :
413
- self ._message = Message (catalog .i18nc (
414
- "@info:status" , "Do you wish to print now?" ), title = catalog .i18nc ("@label" , "SUCCESS" ))
415
- self ._message .addAction ("YES" , catalog .i18nc (
416
- "@action:button" , "YES" ), None , "" )
417
- self ._message .addAction ("NO" , catalog .i18nc (
418
- "@action:button" , "NO" ), None , "" )
417
+ self ._message = Message (catalog .i18nc ("@info:status" , "Do you wish to print now?" ), title = catalog .i18nc ("@label" , "SUCCESS" ))
418
+ self ._message .addAction ("YES" , catalog .i18nc ("@action:button" , "YES" ), None , "" )
419
+ self ._message .addAction ("NO" , catalog .i18nc ("@action:button" , "NO" ), None , "" )
419
420
self ._message .actionTriggered .connect (self ._onActionTriggered )
420
421
self ._message .show ()
421
422
self .writeSuccess .emit (self )
@@ -430,8 +431,7 @@ def startSendingThread(self):
430
431
self .writeError .emit (self )
431
432
result_msg = self ._errorMsg
432
433
if 'create file' in self ._errorMsg :
433
- m = Message (catalog .i18nc (
434
- '@info:status' , ' Write error,please check that the SD card /U disk has been inserted' ), lifetime = 0 )
434
+ m = Message (catalog .i18nc ('@info:status' , ' Write error, please check that the SD card /U disk has been inserted' ), lifetime = 0 )
435
435
m .show ()
436
436
elif self ._result == SendResult .FILE_EMPTY :
437
437
self .writeError .emit (self )
@@ -446,29 +446,22 @@ def startSendingThread(self):
446
446
self .writeError .emit (self )
447
447
result_msg = "Cannot start print"
448
448
449
- self ._message = Message (catalog .i18nc (
450
- "@info:status" , result_msg ), title = catalog .i18nc ("@label" , "FAILURE" ))
449
+ self ._message = Message (catalog .i18nc ("@info:status" , result_msg ), title = catalog .i18nc ("@label" , "FAILURE" ))
451
450
self ._message .show ()
452
451
self ._stage = OutputStage .ready
453
452
Logger .log ('e' , result_msg )
454
453
455
454
def onFilenameAccepted (self ):
456
- self .targetSendFileName = self ._dialog .findChild (
457
- QObject , "nameField" ).property ('text' ).strip ()
458
- if not self .targetSendFileName .endswith ('.gcode.tz' ) and '.' not in self .targetSendFileName :
459
- self .targetSendFileName += '.gcode.tz'
460
- Logger .log ("d" , self ._name + " | Filename set to: " +
461
- self .targetSendFileName )
455
+ self .targetSendFileName = self ._dialog .findChild (QObject , "nameField" ).property ('text' ).strip ()
456
+ Logger .log ("d" , self ._name + " | Filename set to: " + self .targetSendFileName )
462
457
self ._dialog .deleteLater ()
463
458
464
459
self ._message = Message (
465
- catalog .i18nc ("@info:status" ,
466
- "Uploading to {}" ).format (self ._name ),
460
+ catalog .i18nc ("@info:status" , "Uploading to {}" ).format (self ._name ),
467
461
title = catalog .i18nc ("@label" , self ._PluginName ),
468
462
progress = - 1 , lifetime = 0 , dismissable = False , use_inactivity_timer = False
469
463
)
470
- self ._message .addAction ("ABORT" , catalog .i18nc (
471
- "@action:button" , "Cancel" ), None , "" )
464
+ self ._message .addAction ("ABORT" , catalog .i18nc ("@action:button" , "Cancel" ), None , "" )
472
465
self ._message .actionTriggered .connect (self ._onActionTriggered )
473
466
self ._message .show ()
474
467
@@ -492,8 +485,7 @@ def _onActionTriggered(self, message, action):
492
485
try :
493
486
cmd = 'M6030 ":' + self .targetSendFileName + '" I1'
494
487
Logger .log ('i' , 'Start print: ' + cmd )
495
- self .sock .sendto (self .encodeCmd (
496
- cmd ), (self ._targetIP , self .PORT ))
488
+ self .sock .sendto (self .encodeCmd (cmd ), (self ._targetIP , self .PORT ))
497
489
message , address = self .sock .recvfrom (self .RECVBUF )
498
490
message = message .decode ('utf-8' , 'replace' )
499
491
if 'Error' in message :
0 commit comments