|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import datetime |
| 4 | + |
| 5 | +MAX_EAGLE_PARTS = 1000 |
| 6 | + |
| 7 | +print"" # print a new line |
| 8 | + |
| 9 | +# C:\github\SparkFun-Eagle-Libraries C:\github\SparkFun-KiCad-Libraries\Conversion\TestLibs SparkFun-Capacitors |
| 10 | +numArgs = len(sys.argv) |
| 11 | +if numArgs < 4: |
| 12 | + #Print help |
| 13 | + print "Not enough arguments" |
| 14 | + print "Requires: <source path> <dest path> <mod name>" |
| 15 | + print "Example: Python file.py C:\github\SparkFun-KiCad-Libraries\SparkFun.pretty C:\github\SparkFun-KiCad-Libraries\Export.pretty SFE_LOGO_FLAME_.2" |
| 16 | + exit() |
| 17 | + |
| 18 | +sourcePath = sys.argv[1] |
| 19 | +destPath = sys.argv[2] |
| 20 | +commonName = sys.argv[3] |
| 21 | + |
| 22 | +modLinePath = sourcePath + '\\' + commonName + '.kicad_mod' |
| 23 | +modPolyPath = destPath + '\\' + commonName + '.kicad_mod' |
| 24 | +logPath = destPath + '\\scriptOutputLog.txt' |
| 25 | + |
| 26 | +print 'Decoded paths:' |
| 27 | +print modLinePath |
| 28 | +print modPolyPath |
| 29 | +print logPath |
| 30 | + |
| 31 | +# Open a log file, or create a new one |
| 32 | +if os.path.isfile(logPath) == True: |
| 33 | + statinfo = os.stat(logPath) |
| 34 | + logFile = open(logPath, 'a') |
| 35 | + logFile.write('\n' + str(datetime.datetime.now())) |
| 36 | + logFile.write(" Appending log file\n") |
| 37 | +else: |
| 38 | + print "creating log file" |
| 39 | + logFile = open(logPath, "w+") |
| 40 | + logFile.write('\n' + str(datetime.datetime.now())) |
| 41 | + logFile.write(" Created log file\n") |
| 42 | + |
| 43 | +logFile.write( "modLinePath: " + modLinePath + '\n' ) |
| 44 | +logFile.write( "modPolyPath: " + modPolyPath + '\n' ) |
| 45 | +logFile.write( "logPath: " + logPath + '\n' ) |
| 46 | + |
| 47 | +# Open the export mod style file |
| 48 | +if os.path.isfile(modLinePath) == True: |
| 49 | + statinfo = os.stat(modLinePath) |
| 50 | + modLineFile = open(modLinePath, "r") |
| 51 | + # Read the KiCad Library to memeory |
| 52 | + kicadLineModMemory = modLineFile.read() |
| 53 | + # ..Now kicadLibMemory is a string that is the input file |
| 54 | + modLineFile.close() |
| 55 | +else: |
| 56 | + print "Invalid file name" |
| 57 | + |
| 58 | + |
| 59 | +#Create the output file memory |
| 60 | +kicadPolyModMemory = ''#start with blank file |
| 61 | + |
| 62 | + |
| 63 | +print "" |
| 64 | + |
| 65 | + |
| 66 | +logFile.write( "Starting to parse input mod file\n" ) |
| 67 | + |
| 68 | + |
| 69 | +#First, seek out initial "fp_line" text -- copy all lines before the line with it. |
| 70 | +locVar = kicadLineModMemory.find("fp_line", 0, len(kicadLineModMemory)) - 3 |
| 71 | +kicadPolyModMemory = kicadPolyModMemory + kicadLineModMemory[:locVar] |
| 72 | + |
| 73 | +#Second, capture the layer real quick for later |
| 74 | +layerDefStart = kicadLineModMemory.find("(layer", locVar, len(kicadLineModMemory)) |
| 75 | +layerDefEnd = kicadLineModMemory.find(")", layerDefStart, len(kicadLineModMemory)) + 1 |
| 76 | + |
| 77 | +#Now we want to do the following type format |
| 78 | + # (fp_poly (pts |
| 79 | + # (xy 0.982653 -2.516805) |
| 80 | + # (xy 1.161515 -2.508866) |
| 81 | + |
| 82 | +polyStart = '' |
| 83 | +workingOnPoly = 0 |
| 84 | + |
| 85 | +#But an input line looks like: |
| 86 | + # (fp_line (start 4.6482 -6.53796) (end 4.6482 -6.51764) (layer F.SilkS) (width 0.00762)) |
| 87 | +#... and all we need is the first point from it. |
| 88 | +#Find "(fp_line (start " statments until there are none |
| 89 | +exitCondition = 0 |
| 90 | +while exitCondition == 0: |
| 91 | + locVar = kicadLineModMemory.find("(fp_line (start ", locVar, len(kicadLineModMemory)) |
| 92 | + if locVar == -1: |
| 93 | + exitCondition = 1 |
| 94 | + else: |
| 95 | + # move to data |
| 96 | + print( str(locVar) ) |
| 97 | + point1DataStart = locVar + 16 |
| 98 | + point1DataEnd = kicadLineModMemory.find(")", point1DataStart, len(kicadLineModMemory)) |
| 99 | + locVar = kicadLineModMemory.find("(end ", locVar, len(kicadLineModMemory)) |
| 100 | + point2DataStart = locVar + 5 |
| 101 | + point2DataEnd = kicadLineModMemory.find(")", point2DataStart, len(kicadLineModMemory)) |
| 102 | + #Create the point in file |
| 103 | + if workingOnPoly == 0: #also memorize this beginning point |
| 104 | + kicadPolyModMemory = kicadPolyModMemory + "(fp_poly (pts\n" |
| 105 | + kicadPolyModMemory = kicadPolyModMemory + " (xy " + kicadLineModMemory[point1DataStart:point1DataEnd] + ")\n" |
| 106 | + workingOnPoly = 1 |
| 107 | + polyStart = kicadLineModMemory[point1DataStart:point1DataEnd] |
| 108 | + else: |
| 109 | + #already working on poly, see if this is the end |
| 110 | + kicadPolyModMemory = kicadPolyModMemory + " (xy " + kicadLineModMemory[point1DataStart:point1DataEnd] + ")\n" |
| 111 | + #print polyStart |
| 112 | + #print kicadLineModMemory[point2DataStart:point2DataEnd] |
| 113 | + if polyStart == kicadLineModMemory[point2DataStart:point2DataEnd]: |
| 114 | + #Got a loop |
| 115 | + workingOnPoly = 0 |
| 116 | + #Print close parenthesis |
| 117 | + kicadPolyModMemory = kicadPolyModMemory + ") " + kicadLineModMemory[layerDefStart:layerDefEnd] + " (width 0.01))\n" |
| 118 | + |
| 119 | + |
| 120 | +# Now finish the file |
| 121 | +# (xy 15.279895 -0.93621)) (layer F.SilkS) (width 0.01)) |
| 122 | +#) |
| 123 | +if workingOnPoly == 1: |
| 124 | +#didn't finish proper |
| 125 | + kicadPolyModMemory = kicadPolyModMemory + ") " + kicadLineModMemory[layerDefStart:layerDefEnd] + " (width 0.01))\n" |
| 126 | +kicadPolyModMemory = kicadPolyModMemory + ")\n" |
| 127 | + |
| 128 | +#print "" |
| 129 | +#print "Number of parts inspected: ", partsInspected |
| 130 | +#print "Number of valid parts found: ", validParts |
| 131 | +#print "Number of parts skipped: ", skippedParts |
| 132 | +#print "Number not found in KiCad libs: ", notFoundInKicadLib |
| 133 | +#print "Number of DCM entries created: ", dcmRecordsCreated |
| 134 | +#print "Number PROD_ID fields created: ", prodIDFieldsAdded |
| 135 | +#print "Default PROD_ID used: ", defaultProdIDsCreated |
| 136 | +#print "" |
| 137 | +# |
| 138 | +#logFile.write( "Number of parts inspected: " + str(partsInspected) + '\n' ) |
| 139 | +#logFile.write( "Number of valid parts found: " + str(validParts) + '\n' ) |
| 140 | +#logFile.write( "Number of parts skipped: " + str(skippedParts) + '\n' ) |
| 141 | +#logFile.write( "Number not found in KiCad libs: " + str(notFoundInKicadLib) + '\n' ) |
| 142 | +#logFile.write( "Number of DCM entries created: " + str(dcmRecordsCreated) + '\n' ) |
| 143 | +#logFile.write( "Number PROD_ID fields created: " + str(prodIDFieldsAdded) + '\n' ) |
| 144 | +#logFile.write( "Default PROD_ID used: " + str(defaultProdIDsCreated) + '\n' ) |
| 145 | + |
| 146 | +#eagleLbrFile.close() |
| 147 | + |
| 148 | +#logFile.write(kicadLibMemory) # Enable this line to write memory to logfile |
| 149 | +logFile.write(str(datetime.datetime.now()) + ' ' ) |
| 150 | +logFile.write("Closing log file\n\n\n\n") |
| 151 | +logFile.close() |
| 152 | + |
| 153 | +# Write out the kicadPolyModMemory |
| 154 | +if os.path.isfile(modPolyPath) == True: |
| 155 | + statinfo = os.stat(modPolyPath) |
| 156 | + modPolyFile = open(modPolyPath, "w+") |
| 157 | + # Read the KiCad Library to memeory |
| 158 | +else: |
| 159 | + print "creating poly .kicad_mod file" |
| 160 | + modPolyFile = open(modPolyPath, "w+") |
| 161 | +modPolyFile.write(kicadPolyModMemory) # Enable this line to write memory to OUTPUT |
| 162 | +modPolyFile.close() |
| 163 | + |
| 164 | + |
| 165 | +print "File IO complete" |
0 commit comments