Old python scripts: fix issues due to code change, and make them Python3/3 compatible

This commit is contained in:
jean-pierre charras 2021-04-27 11:55:56 +02:00
parent b63c482347
commit 524ca442f3
4 changed files with 73 additions and 59 deletions

View File

@ -24,8 +24,8 @@ import sys
import os import os
from pcbnew import * from pcbnew import *
# A helper function to convert a string filename for python2 or python3 # A helper function to convert a UTF8 string for python2 or python3
def getFName( afilename ): def fromUTF8Text( afilename ):
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
return afilename.encode() return afilename.encode()
else: else:
@ -100,7 +100,7 @@ for layer_info in plot_plan:
pctl.SetLayer(layer_info[1]) pctl.SetLayer(layer_info[1])
pctl.OpenPlotfile(layer_info[0], PLOT_FORMAT_GERBER, layer_info[2]) 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: if gen_job_file == True:
jobfile_writer.AddGbrFile( layer_info[1], os.path.basename(pctl.GetPlotFileName()) ); 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 lyrname = 'inner%s' % innerlyr
pctl.OpenPlotfile(lyrname, PLOT_FORMAT_GERBER, "inner") pctl.OpenPlotfile(lyrname, PLOT_FORMAT_GERBER, "inner")
print( "plot %s" % getFName( pctl.GetPlotFileName() ) ) print( "plot %s" % fromUTF8Text( pctl.GetPlotFileName() ) )
if pctl.PlotLayer() == False: if pctl.PlotLayer() == False:
print( "plot error" ) print( "plot error" )
@ -144,12 +144,12 @@ drlwriter.SetFormat( metricFmt )
genDrl = True genDrl = True
genMap = 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 ); drlwriter.CreateDrillandMapFilesSet( pctl.GetPlotDirName(), genDrl, genMap );
# One can create a text file to report drill statistics # One can create a text file to report drill statistics
rptfn = pctl.GetPlotDirName() + 'drill_report.rpt' rptfn = pctl.GetPlotDirName() + 'drill_report.rpt'
print( 'report: %s' % getFName( rptfn ) ) print( 'report: %s' % fromUTF8Text( rptfn ) )
drlwriter.GenDrillReportFile( rptfn ); drlwriter.GenDrillReportFile( rptfn );
if gen_job_file == True: 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.dirname(pctl.GetPlotFileName()) + '/' + os.path.basename(filename)
job_fn=os.path.splitext(job_fn)[0] + '.gbrjob' 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 ) jobfile_writer.CreateJobFile( job_fn )

View File

@ -1,51 +1,58 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function from __future__ import print_function
import sys
from pcbnew import * from pcbnew import *
size_0_6mm = wxSizeMM(0.6,0.6) # A helper function to convert a UTF8 string coming from Kicad for python2 or python3
size_1_0mm = wxSizeMM(1.0,1.0) def fromUTF8Text( aText ):
if sys.version_info < (3, 0):
return aText.encode()
else:
return aText
# create a blank board def GenerateBoard():
pcb = BOARD() 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 # create a new footprint, it's parent is our previously created pcb
module = FOOTPRINT(pcb) footprint = FOOTPRINT(pcb)
module.SetReference("M1") # give it a reference name footprint.SetReference("M1") # give it a reference name
module.Reference().SetPos0(wxPointMM(6,-2)) footprint.Reference().SetPos0(wxPointMM(6,-2))
module.Reference().SetDrawCoord() footprint.Reference().SetDrawCoord()
pcb.Add(module) # add it to our pcb pcb.Add(footprint) # add it to our pcb
m_pos = wxPointMM(50,50) m_pos = wxPointMM(50,50)
module.SetPosition(m_pos) footprint.SetPosition(m_pos)
# create a pad array and add it to the module # create a pad array and add it to the footprint
n = 1 n = 1
for y in range (0,10): for y in range (0,5):
for x in range (0,10): for x in range (0,5):
pad = PAD(module) pad = PAD(footprint)
pad.SetDrillSize(size_0_6mm) pad.SetDrillSize(size_0_6mm)
pad.SetSize(size_1_0mm) pad.SetSize(size_1_0mm)
pt = wxPointMM(1.27*x,1.27*y) pt = wxPointMM(1.27*x,1.27*y)
pad.SetPos0(pt); pad.SetPos0(pt);
pad.SetDrawCoord() pad.SetDrawCoord()
pad.SetName(str(n)) pad.SetName(str(n))
module.Add(pad) footprint.Add(pad)
n+=1 n+=1
# save the PCB to disk # save the PCB to disk
pcb.Save("my2.kicad_pcb") 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():
for p in m.Pads(): print('pad ', fromUTF8Text( p.GetName() ), ToMM(p.GetPosition()), ToMM(p.GetOffset()))
print('pad ', p.GetName(), p.GetPosition(), p.GetOffset())
GenerateBoard()
# pcb.GetDesignSettings() ReadBoard()

View File

@ -7,7 +7,10 @@ filename=sys.argv[1]
pcb = LoadBoard(filename) pcb = LoadBoard(filename)
for module in pcb.GetFootprints(): 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.Value().SetVisible(False) # set Value as Hidden
module.Reference().SetVisible(True) # set Reference as Visible module.Reference().SetVisible(True) # set Reference as Visible

View File

@ -5,6 +5,13 @@ from __future__ import print_function
import sys import sys
from pcbnew import * 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] filename=sys.argv[1]
pcb = LoadBoard(filename) pcb = LoadBoard(filename)
@ -14,7 +21,7 @@ FromUnits = FromMM
#ToUnits=ToMils #ToUnits=ToMils
#FromUnits=FromMils #FromUnits=FromMils
print("LISTING VIAS:") print("List vias:")
for item in pcb.GetTracks(): for item in pcb.GetTracks():
if type(item) is VIA: if type(item) is VIA:
@ -36,21 +43,21 @@ for item in pcb.GetTracks():
print("Unknown type %s" % type(item)) print("Unknown type %s" % type(item))
print("") print("")
print("LIST DRAWINGS:") print("List drawings:")
for item in pcb.GetDrawings(): for item in pcb.GetDrawings():
if type(item) is TEXTE_PCB: if type(item) is PCB_TEXT:
print("* Text: '%s' at %s" % (item.GetText(), item.GetPosition())) print("* Text: '%s' at %s" % ( fromUTF8Text( item.GetText() ), ToUnits(item.GetPosition()) ) )
elif type(item) is DRAWSEGMENT: elif type(item) is PCB_SHAPE:
print("* Drawing: %s" % item.GetShapeStr()) # dir(item) print( "* Drawing: %s" % fromUTF8Text( item.GetShapeStr() ) )
else: else:
print(type(item)) print("* Drawing type: %s" % type(item))
print("") print("")
print("LIST MODULES:") print("List footprints:")
for module in pcb.GetFootprints(): for footprint in pcb.Footprints():
print("* Module: %s at %s" % (module.GetReference(), ToUnits(module.GetPosition()))) print("* Module: %s at %s" % ( fromUTF8Text( footprint.GetReference() ), ToUnits(footprint.GetPosition())))
print("") print("")
print("Nets cnt: ", pcb.GetNetCount()) print("Nets cnt: ", pcb.GetNetCount())
@ -58,11 +65,8 @@ print("track w cnt:", len(pcb.GetTrackWidthList()))
print("via s cnt:", len(pcb.GetViasDimensionsList())) print("via s cnt:", len(pcb.GetViasDimensionsList()))
print("") print("")
print("LIST ZONES:", pcb.GetAreaCount()) print("List zones:", pcb.GetAreaCount())
for idx in range(0, pcb.GetAreaCount()): for idx in range(0, pcb.GetAreaCount()):
zone=pcb.GetArea(idx) zone=pcb.GetArea(idx)
print("zone:", idx, "priority:", zone.GetPriority(), "netname", zone.GetNetname()) print("zone:", idx, "priority:", zone.GetPriority(), "netname", fromUTF8Text( zone.GetNetname() ) )
print("")
print("NetClasses:", pcb.GetNetClasses().GetCount())