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
+43 -36
View File
@@ -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()
@@ -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
+19 -15
View File
@@ -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() ) )