main.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import mariadb
  2. import pyodbc
  3. import math
  4. e24 = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5,
  5. 8.2, 9.1]
  6. e96 = [1.00, 1.02, 1.05, 1.07, 1.10, 1.13, 1.15, 1.18, 1.21, 1.24, 1.27, 1.30, 1.33, 1.37, 1.40, 1.43, 1.47, 1.50, 1.54,
  7. 1.58, 1.62, 1.65, 1.69, 1.74, 1.78, 1.82, 1.87, 1.91, 1.96, 2.00, 2.05, 2.10, 2.15, 2.21, 2.26, 2.32, 2.37, 2.43,
  8. 2.49, 2.55, 2.61, 2.67, 2.74, 2.80, 2.87, 2.94, 3.01, 3.09, 3.16, 3.24, 3.32, 3.40, 3.48, 3.57, 3.65, 3.74, 3.83,
  9. 3.92, 4.02, 4.12, 4.22, 4.32, 4.42, 4.53, 4.64, 4.75, 4.87, 4.99, 5.11, 5.23, 5.36, 5.49, 5.62, 5.76, 5.90, 6.04,
  10. 6.19, 6.34, 6.49, 6.65, 6.81, 6.98, 7.15, 7.32, 7.50, 7.68, 7.87, 8.06, 8.25, 8.45, 8.66, 8.87, 9.09, 9.31, 9.53,
  11. 9.76]
  12. def number_to_unit_string(n, unit):
  13. # print(f"Nunmber: {n} with unit {unit}")
  14. if n != 0:
  15. exponent = math.floor(math.log10(n) / 3)
  16. else:
  17. exponent = 0
  18. exponent = min(3, exponent)
  19. exponent = max(-5, exponent)
  20. if exponent <= -5:
  21. suffix = 'f'
  22. elif exponent == -4:
  23. suffix = 'p'
  24. elif exponent == -3:
  25. suffix = 'n'
  26. elif exponent == -2:
  27. suffix = 'u'
  28. elif exponent == -1:
  29. suffix = 'm'
  30. elif exponent == 0:
  31. suffix = unit
  32. elif exponent == 1:
  33. suffix = 'k'
  34. elif exponent == 2:
  35. suffix = 'M'
  36. elif exponent >= 3:
  37. suffix = 'G'
  38. adjusted_number = n / (1000 ** exponent)
  39. adjusted_number_as_string = f"{adjusted_number:g}"
  40. if '.' not in adjusted_number_as_string:
  41. string_to_return = adjusted_number_as_string + suffix
  42. else:
  43. string_to_return = adjusted_number_as_string.replace(".", suffix)
  44. return string_to_return
  45. def number_to_resistance_string(n):
  46. return number_to_unit_string(n, 'R')
  47. def number_to_capacitance_string(n):
  48. return number_to_unit_string(n, 'F')
  49. def number_to_inductance_string(n):
  50. return number_to_unit_string(n, 'H')
  51. def round_to_significant(x, num):
  52. round_to = -int(math.floor(math.log10(abs(x)))) + num - 1
  53. result = round(x, round_to)
  54. # print(f"Rounding {x}, with {round_to}, resulting in {result}")
  55. return result
  56. def expand_series(series, lowest=1, highest=10):
  57. lowest_decade = math.floor(math.log10(lowest))
  58. highest_decade = math.ceil(math.log10(highest))
  59. values = []
  60. for decade in range(lowest_decade, highest_decade + 1):
  61. values_to_add = [x * 10 ** decade for x in series if x * 10 ** decade >= lowest and x * 10 ** decade <= highest]
  62. values.extend([round_to_significant(x, 3) for x in values_to_add])
  63. return values
  64. resistor_packages = {"01005": {"power": 0.03, "height": 0.15, "length": 0.4, "width": 0.2, "footprint": "R01005",
  65. "symbol": "resistor_2pin"},
  66. "0201": {"power": 0.05, "height": 0.3, "length": 0.6, "width": 0.3, "footprint": "R0201",
  67. "symbol": "resistor_2pin"},
  68. "0402": {"power": 0.062, "height": 0.4, "length": 1.0, "width": 0.5, "footprint": "R0402",
  69. "symbol": "resistor_2pin"},
  70. "0603": {"power": 0.1, "height": 0.5, "length": 1.6, "width": 0.85, "footprint": "R0603",
  71. "symbol": "resistor_2pin"},
  72. "0805": {"power": 0.125, "height": 0.5, "length": 2.0, "width": 1.2, "footprint": "R0805",
  73. "symbol": "resistor_2pin"},
  74. "1206": {"power": 0.25, "height": 0.6, "length": 3.2, "width": 1.6, "footprint": "R1206",
  75. "symbol": "resistor_2pin"},
  76. "1210": {"power": 0.5, "height": 0.6, "length": 3.2, "width": 2.5, "footprint": "R1210",
  77. "symbol": "resistor_2pin"},
  78. "1812": {"power": 1.0, "height": 0.6, "length": 3.2, "width": 4.6, "footprint": "R1812",
  79. "symbol": "resistor_2pin"},
  80. "2010": {"power": 0.75, "height": 0.6, "length": 5.0, "width": 2.5, "footprint": "R1210",
  81. "symbol": "resistor_2pin"},
  82. "2512": {"power": 1.0, "height": 0.6, "length": 6.3, "width": 3.2, "footprint": "R2512",
  83. "symbol": "resistor_2pin"}
  84. }
  85. def add_resistors(db_cursor, values, tolerance, package):
  86. for value in values:
  87. description = f"Resistor, {number_to_resistance_string(value)}, {tolerance * 100}%, SMD, {package}, 100ppm"
  88. # print(f"Adding {description}")
  89. query_insert_internal_part = f"""
  90. INSERT INTO internal_parts (part_description, part_group_id)
  91. VALUES
  92. ('{description}', 'chip_resistor');"""
  93. # print(query_insert_internal_part)
  94. db_cursor.execute(query_insert_internal_part)
  95. # print(f"Warnings: {cur.warnings}")
  96. new_part_number = db_cursor.lastrowid
  97. query_insert_resistor_info = f"""
  98. INSERT INTO group_details_chip_resistor (internal_pn, resistance, resistance_tolerance_upper,
  99. resistance_tolerance_lower, `power`, resistor_type, temperature_max, temperature_min,
  100. temperature_coefficient_upper, temperature_coefficient_lower,
  101. dimension_height, dimension_length, dimension_width,
  102. component_package, component_symbol)
  103. VALUES
  104. ({new_part_number}, {value}, {tolerance / 100}, -{tolerance / 100}, {resistor_packages[package]["power"]}, 'Thick Film',
  105. 155, -55, 100, -100,
  106. {resistor_packages[package]["height"]}, {resistor_packages[package]["length"]}, {resistor_packages[package]["width"]},
  107. '{resistor_packages[package]["footprint"]}', '{resistor_packages[package]["symbol"]}');"""
  108. # print(query_insert_resistor_info)
  109. db_cursor.execute(query_insert_resistor_info)
  110. # print(f"Warnings: {cur.warnings}")
  111. return
  112. def main():
  113. try:
  114. conn = mariadb.connect(
  115. user="jegatroncomponents_admin",
  116. password="librarian",
  117. host="localhost",
  118. port=3306,
  119. database="jegatroncomponents"
  120. )
  121. except mariadb.Error as e:
  122. print(f"Error connecting to MariaDB Platform: {e}")
  123. exit(1)
  124. db_curr = conn.cursor()
  125. for package in resistor_packages:
  126. print(f"Adding {package} E24 series")
  127. add_resistors(db_curr, [0] + expand_series(e24, 0.1, 10000000), 0.05, package)
  128. print(f"Adding {package} E96 series")
  129. add_resistors(db_curr, [0] + expand_series(e96, 0.1, 10000000), 0.01, package)
  130. """
  131. db_curr.execute("SELECT * from internal_parts;")
  132. for k in db_curr:
  133. print("Man!")
  134. print(k)"""
  135. conn.commit()
  136. db_curr.close()
  137. conn.close()
  138. return
  139. try:
  140. cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
  141. "Server=db.altiumlibrary.com;"
  142. "Database=altium_library;"
  143. "Trusted_Connection=no;"
  144. "UID=alib_OOMDUPOV4YG1;"
  145. "PWD=e071szxdIu6SnrC6")
  146. except Exception as e:
  147. print(f"Could not connect to celectial library")
  148. print(e)
  149. exit(1)
  150. print("Dude!")
  151. SQL_QUERY = """
  152. SELECT
  153. TOP 5
  154. *
  155. from z_CapacitorsCeramic;
  156. """
  157. SQL_QUERY = """
  158. SELECT *
  159. FROM "z_ResistorsSurfaceMount"
  160. WHERE Resistance is null;
  161. """
  162. # cur = cnxn.cursor()
  163. # cur.execute(SQL_QUERY)
  164. """
  165. for k in cur:
  166. print("Man!")
  167. print(k)
  168. """
  169. if __name__ == '__main__':
  170. main()