@@ -299,18 +299,27 @@ def processExportLayer(self):
299299 #create pcb folder
300300 if not os .path .exists (os .path .join (output_path )):
301301 os .makedirs (os .path .join (output_path ))
302-
303302 #create library folder
304303 if not os .path .exists (os .path .join (output_path , self .library_folder )):
305304 os .makedirs (os .path .join (output_path , self .library_folder ))
306-
307305 #create images folder
308306 if not os .path .exists (os .path .join (output_path , self .export_image_folder )):
309307 os .makedirs (os .path .join (output_path , self .export_image_folder ))
310308
311309 #create cache folder
312310 if not os .path .exists (os .path .join (output_path , self .export_cache_folder )):
313311 os .makedirs (os .path .join (output_path , self .export_cache_folder ))
312+
313+ OUTPUT_FOLDER_LOCATION = output_path
314+ LIBRARY_FOLDER_LOCATION = os .path .join (output_path , self .library_folder )
315+ IMAGE_FOLDER_LOCATION = os .path .join (output_path , self .export_image_folder )
316+ CACHE_FOLDER_LOCATION = os .path .join (output_path , self .export_cache_folder )
317+
318+ KICAD_PCB_PATH = os .path .join (OUTPUT_FOLDER_LOCATION , self .kicad_pcb_file )
319+ KICAD_LIB_PATH = os .path .join (OUTPUT_FOLDER_LOCATION , self .library_table_file )
320+ KICAD_PRO_PATH = os .path .join (OUTPUT_FOLDER_LOCATION , self .kicad_project_file )
321+ KICAD_MOD_PATH = os .path .join (OUTPUT_FOLDER_LOCATION , self .kicad_mod_file )
322+
314323 options_path = os .path .join (CACHE_FOLDER_LOCATION , 'svg2shenzhen-options' )
315324
316325 if os .path .exists (options_path ):
@@ -327,6 +336,7 @@ def processExportLayer(self):
327336
328337 with open (options_path , 'w' ) as f :
329338 pickle .dump (self .options , f )
339+
330340 layer_arguments = []
331341 temp_svg_paths = []
332342 for (layer_id , layer_label , layer_type ) in layers :
@@ -337,23 +347,24 @@ def processExportLayer(self):
337347 if ("-invert" in layer_label ):
338348 layer_label = layer_label .replace ("-invert" , "" )
339349 invert = "false"
340- hash_sum_path = os .path .join (tempfile . gettempdir () , 'svg2shenzhen-{}-{}-{}-{}' .format (layer_id , layer_label , layer_type , invert ))
350+ hash_sum_path = os .path .join (CACHE_FOLDER_LOCATION , 'svg2shenzhen-{}-{}-{}-{}' .format (layer_id , layer_label , layer_type , invert ))
341351
342352 prev_hash_sum = None
343353 if os .path .exists (hash_sum_path ):
344354 with open (hash_sum_path , 'r' ) as f :
345355 prev_hash_sum = f .read ()
346-
347- fd , layer_dest_svg_path = tempfile .mkstemp ()
348- os .close (fd )
356+
357+ # generate unique filename each layer
358+ temp_name = next (tempfile ._get_candidate_names ())
359+ layer_dest_svg_path = os .path .join (CACHE_FOLDER_LOCATION , temp_name )
349360 hash_sum = self .export_layers (layer_dest_svg_path , show_layer_ids )
350361 temp_svg_paths .append (layer_dest_svg_path )
351362
352363 if self .options .filetype == "kicad_pcb" or self .options .filetype == "kicad_module" :
353- layer_dest_png_path = os .path .join (output_path , self . export_image_folder , "%s_%s.png" % (layer_label , layer_id ))
364+ layer_dest_png_path = os .path .join (IMAGE_FOLDER_LOCATION , "%s_%s.png" % (layer_label , layer_id ))
354365 else :
355- layer_dest_png_path = os .path .join (output_path , "%s_%s.png" % (layer_label , layer_id ))
356- layer_dest_kicad_path = os .path .join (output_path , self . library_folder , "%s_%s.kicad_mod" % (layer_label , layer_id ))
366+ layer_dest_png_path = os .path .join (IMAGE_FOLDER_LOCATION , "%s_%s.png" % (layer_label , layer_id ))
367+ layer_dest_kicad_path = os .path .join (LIBRARY_FOLDER_LOCATION , "%s_%s.kicad_mod" % (layer_label , layer_id ))
357368 kicad_mod_files .append (layer_dest_kicad_path )
358369
359370
@@ -395,24 +406,22 @@ def processExportLayer(self):
395406 for kicad_file in kicad_mod_files :
396407 with open (kicad_file , 'r' ) as f :
397408 kicad_modules_string += f .read ()
398- kicad_pcb_path = os .path .join (output_path , self .kicad_pcb_file )
399- kicad_lib_path = os .path .join (output_path , self .library_table_file )
400- kicad_pro_path = os .path .join (output_path , self .kicad_project_file )
401- with open (kicad_pcb_path , 'w' ) as f :
409+
410+ with open (KICAD_PCB_PATH , 'w' ) as f :
402411 f .write (pcb_header )
403412 f .write (kicad_modules_string )
404413 f .write (kicad_edgecut_string )
405414 f .write (kicad_drill_string )
406415 f .write (pcb_footer )
407416
408- with open (kicad_lib_path , 'w' ) as f :
417+ with open (KICAD_LIB_PATH , 'w' ) as f :
409418 f .write (pcb_lib_table % (self .library_folder ))
410419
411- with open (kicad_pro_path , 'w' ) as f :
420+ with open (KICAD_PRO_PATH , 'w' ) as f :
412421 f .write (pcb_project_file )
413422
414423 if (self .options .openkicad ):
415- self .openKicad (kicad_pcb_path )
424+ self .openKicad (KICAD_PCB_PATH )
416425
417426 elif self .options .filetype == "kicad_module" :
418427 kicad_modules_string = "(module pcbart (layer F.Cu)"
@@ -423,8 +432,7 @@ def processExportLayer(self):
423432 kicad_modules_string += kicad_edgecut_string
424433 kicad_modules_string += kicad_drill_string
425434 kicad_modules_string += ")"
426- kicad_mod_path = os .path .join (output_path , self .kicad_mod_file )
427- with open (kicad_mod_path , 'w' ) as f :
435+ with open (KICAD_MOD_PATH , 'w' ) as f :
428436 f .write (kicad_modules_string )
429437
430438
@@ -508,13 +516,13 @@ def exportToKicad(self, png_path, output_path, layer_type, invert = "true"):
508516 else :
509517 bitmap2component_exe = os .path .join (plugin_path , 'bitmap2component.exe' )
510518
511- command = "'%s' '%s' '%s' '%s' '%s' '%s' '%s' " % (bitmap2component_exe , png_path , output_path , layer_type , invert , str (int (self .options .dpi )) , str (int (self .options .threshold )))
519+ command = "\" %s \" \" %s \" \" %s \" %s %s %s %s " % (bitmap2component_exe , png_path , output_path , layer_type , invert , str (int (self .options .dpi )) , str (int (self .options .threshold )))
512520 return subprocess .Popen (command .encode ("utf-8" ), shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
513521
514522
515523 def exportToPng (self , svg_path , output_path ):
516524 area_param = '-D' if self .options .crop else 'C'
517- command = "inkscape '%s' -d '%s' -e '%s' '%s' " % (area_param , self .options .dpi , output_path , svg_path )
525+ command = "inkscape %s -d %s -e \" %s \" \" %s \" " % (area_param , self .options .dpi , output_path , svg_path )
518526 return subprocess .Popen (command .encode ("utf-8" ), shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
519527
520528
0 commit comments