From 524ca442f3eeea09b5a2de794c84a7bd69954cdb Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Tue, 27 Apr 2021 11:55:56 +0200 Subject: [PATCH] Old python scripts: fix issues due to code change, and make them Python3/3 compatible --- .../gen_gerber_and_drill_files_board.py | 14 ++-- pcbnew/python/examples/createPcb.py | 79 ++++++++++--------- .../examples/hidePcbValuesShowReferences.py | 5 +- pcbnew/python/examples/listPcb.py | 34 ++++---- 4 files changed, 73 insertions(+), 59 deletions(-) diff --git a/demos/python_scripts_examples/gen_gerber_and_drill_files_board.py b/demos/python_scripts_examples/gen_gerber_and_drill_files_board.py index e5cccd766f..2dceb7c31a 100644 --- a/demos/python_scripts_examples/gen_gerber_and_drill_files_board.py +++ b/demos/python_scripts_examples/gen_gerber_and_drill_files_board.py @@ -24,8 +24,8 @@ import sys import os from pcbnew import * -# A helper function to convert a string filename for python2 or python3 -def getFName( afilename ): +# A helper function to convert a UTF8 string for python2 or python3 +def fromUTF8Text( afilename ): if sys.version_info < (3, 0): return afilename.encode() else: @@ -100,7 +100,7 @@ for layer_info in plot_plan: pctl.SetLayer(layer_info[1]) pctl.OpenPlotfile(layer_info[0], PLOT_FORMAT_GERBER, layer_info[2]) - print( 'plot %s' % getFName( pctl.GetPlotFileName() ) ) + print( 'plot %s' % fromUTF8Text( pctl.GetPlotFileName() ) ) if gen_job_file == True: jobfile_writer.AddGbrFile( layer_info[1], os.path.basename(pctl.GetPlotFileName()) ); @@ -116,7 +116,7 @@ for innerlyr in range ( 1, lyrcnt-1 ): lyrname = 'inner%s' % innerlyr pctl.OpenPlotfile(lyrname, PLOT_FORMAT_GERBER, "inner") - print( "plot %s" % getFName( pctl.GetPlotFileName() ) ) + print( "plot %s" % fromUTF8Text( pctl.GetPlotFileName() ) ) if pctl.PlotLayer() == False: print( "plot error" ) @@ -144,12 +144,12 @@ drlwriter.SetFormat( metricFmt ) genDrl = True genMap = True -print( 'create drill and map files in %s' % getFName( pctl.GetPlotFileName() ) ) +print( 'create drill and map files in %s' % fromUTF8Text( pctl.GetPlotFileName() ) ) drlwriter.CreateDrillandMapFilesSet( pctl.GetPlotDirName(), genDrl, genMap ); # One can create a text file to report drill statistics rptfn = pctl.GetPlotDirName() + 'drill_report.rpt' -print( 'report: %s' % getFName( rptfn ) ) +print( 'report: %s' % fromUTF8Text( rptfn ) ) drlwriter.GenDrillReportFile( rptfn ); if gen_job_file == True: @@ -157,7 +157,7 @@ if gen_job_file == True: job_fn=os.path.dirname(pctl.GetPlotFileName()) + '/' + os.path.basename(filename) job_fn=os.path.splitext(job_fn)[0] + '.gbrjob' - print( 'create job file %s ' % getFName( job_fn ) ) + print( 'create job file %s ' % fromUTF8Text( job_fn ) ) jobfile_writer.CreateJobFile( job_fn ) diff --git a/pcbnew/python/examples/createPcb.py b/pcbnew/python/examples/createPcb.py index 99094c7387..c3d5fccd8a 100644 --- a/pcbnew/python/examples/createPcb.py +++ b/pcbnew/python/examples/createPcb.py @@ -1,51 +1,58 @@ #!/usr/bin/env python from __future__ import print_function - +import sys from pcbnew import * -size_0_6mm = wxSizeMM(0.6,0.6) -size_1_0mm = wxSizeMM(1.0,1.0) +# A helper function to convert a UTF8 string coming from Kicad for python2 or python3 +def fromUTF8Text( aText ): + if sys.version_info < (3, 0): + return aText.encode() + else: + return aText -# create a blank board -pcb = BOARD() +def GenerateBoard(): + size_0_6mm = wxSizeMM(0.6,0.6) + size_1_0mm = wxSizeMM(1.0,1.0) -pcb.GetNetClasses().GetDefault().SetClearance(FromMM(0.1)) + # create a blank board + pcb = CreateEmptyBoard() -# create a new module, it's parent is our previously created pcb -module = FOOTPRINT(pcb) -module.SetReference("M1") # give it a reference name -module.Reference().SetPos0(wxPointMM(6,-2)) -module.Reference().SetDrawCoord() -pcb.Add(module) # add it to our pcb -m_pos = wxPointMM(50,50) -module.SetPosition(m_pos) + # create a new footprint, it's parent is our previously created pcb + footprint = FOOTPRINT(pcb) + footprint.SetReference("M1") # give it a reference name + footprint.Reference().SetPos0(wxPointMM(6,-2)) + footprint.Reference().SetDrawCoord() + pcb.Add(footprint) # add it to our pcb + m_pos = wxPointMM(50,50) + footprint.SetPosition(m_pos) -# create a pad array and add it to the module -n = 1 -for y in range (0,10): - for x in range (0,10): - pad = PAD(module) - pad.SetDrillSize(size_0_6mm) - pad.SetSize(size_1_0mm) - pt = wxPointMM(1.27*x,1.27*y) - pad.SetPos0(pt); - pad.SetDrawCoord() - pad.SetName(str(n)) - module.Add(pad) - n+=1 + # create a pad array and add it to the footprint + n = 1 + for y in range (0,5): + for x in range (0,5): + pad = PAD(footprint) + pad.SetDrillSize(size_0_6mm) + pad.SetSize(size_1_0mm) + pt = wxPointMM(1.27*x,1.27*y) + pad.SetPos0(pt); + pad.SetDrawCoord() + pad.SetName(str(n)) + footprint.Add(pad) + n+=1 -# save the PCB to disk -pcb.Save("my2.kicad_pcb") + # save the PCB to disk + pcb.Save("my2.kicad_pcb") -pcb = LoadBoard("my2.kicad_pcb") +def ReadBoard(): + pcb = LoadBoard("my2.kicad_pcb") -print(map( lambda x: x.GetReference() , list(pcb.GetFootprints()))) + for m in pcb.GetFootprints(): + print( 'footprint: ', fromUTF8Text( m.GetReference() ) ) -for m in pcb.GetFootprints(): - for p in m.Pads(): - print('pad ', p.GetName(), p.GetPosition(), p.GetOffset()) + for p in m.Pads(): + print('pad ', fromUTF8Text( p.GetName() ), ToMM(p.GetPosition()), ToMM(p.GetOffset())) - -# pcb.GetDesignSettings() +GenerateBoard() +ReadBoard() diff --git a/pcbnew/python/examples/hidePcbValuesShowReferences.py b/pcbnew/python/examples/hidePcbValuesShowReferences.py index fb27103c48..dbe2886548 100644 --- a/pcbnew/python/examples/hidePcbValuesShowReferences.py +++ b/pcbnew/python/examples/hidePcbValuesShowReferences.py @@ -7,7 +7,10 @@ filename=sys.argv[1] pcb = LoadBoard(filename) for module in pcb.GetFootprints(): - print("* Module: %s" % module.GetReference()) + if sys.version_info < (3, 0): + print("* Module: %s" % module.GetReference().encode()) + else: + print("* Module: %s" % module.GetReference()) module.Value().SetVisible(False) # set Value as Hidden module.Reference().SetVisible(True) # set Reference as Visible diff --git a/pcbnew/python/examples/listPcb.py b/pcbnew/python/examples/listPcb.py index 32ef5577d5..069b8846f1 100644 --- a/pcbnew/python/examples/listPcb.py +++ b/pcbnew/python/examples/listPcb.py @@ -5,6 +5,13 @@ from __future__ import print_function import sys from pcbnew import * +# A helper function to convert a UTF8 string for python2 or python3 +def fromUTF8Text( afilename ): + if sys.version_info < (3, 0): + return afilename.encode() + else: + return afilename + filename=sys.argv[1] pcb = LoadBoard(filename) @@ -14,7 +21,7 @@ FromUnits = FromMM #ToUnits=ToMils #FromUnits=FromMils -print("LISTING VIAS:") +print("List vias:") for item in pcb.GetTracks(): if type(item) is VIA: @@ -36,21 +43,21 @@ for item in pcb.GetTracks(): print("Unknown type %s" % type(item)) print("") -print("LIST DRAWINGS:") +print("List drawings:") for item in pcb.GetDrawings(): - if type(item) is TEXTE_PCB: - print("* Text: '%s' at %s" % (item.GetText(), item.GetPosition())) - elif type(item) is DRAWSEGMENT: - print("* Drawing: %s" % item.GetShapeStr()) # dir(item) + if type(item) is PCB_TEXT: + print("* Text: '%s' at %s" % ( fromUTF8Text( item.GetText() ), ToUnits(item.GetPosition()) ) ) + elif type(item) is PCB_SHAPE: + print( "* Drawing: %s" % fromUTF8Text( item.GetShapeStr() ) ) else: - print(type(item)) + print("* Drawing type: %s" % type(item)) print("") -print("LIST MODULES:") +print("List footprints:") -for module in pcb.GetFootprints(): - print("* Module: %s at %s" % (module.GetReference(), ToUnits(module.GetPosition()))) +for footprint in pcb.Footprints(): + print("* Module: %s at %s" % ( fromUTF8Text( footprint.GetReference() ), ToUnits(footprint.GetPosition()))) print("") print("Nets cnt: ", pcb.GetNetCount()) @@ -58,11 +65,8 @@ print("track w cnt:", len(pcb.GetTrackWidthList())) print("via s cnt:", len(pcb.GetViasDimensionsList())) print("") -print("LIST ZONES:", pcb.GetAreaCount()) +print("List zones:", pcb.GetAreaCount()) for idx in range(0, pcb.GetAreaCount()): zone=pcb.GetArea(idx) - print("zone:", idx, "priority:", zone.GetPriority(), "netname", zone.GetNetname()) - -print("") -print("NetClasses:", pcb.GetNetClasses().GetCount()) + print("zone:", idx, "priority:", zone.GetPriority(), "netname", fromUTF8Text( zone.GetNetname() ) )