import mariadb import pyodbc import math 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, 8.2, 9.1] 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, 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, 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, 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, 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, 9.76] def number_to_unit_string(n, unit): # print(f"Nunmber: {n} with unit {unit}") if n != 0: exponent = math.floor(math.log10(n) / 3) else: exponent = 0 exponent = min(3, exponent) exponent = max(-5, exponent) if exponent <= -5: suffix = 'f' elif exponent == -4: suffix = 'p' elif exponent == -3: suffix = 'n' elif exponent == -2: suffix = 'u' elif exponent == -1: suffix = 'm' elif exponent == 0: suffix = unit elif exponent == 1: suffix = 'k' elif exponent == 2: suffix = 'M' elif exponent >= 3: suffix = 'G' adjusted_number = n / (1000 ** exponent) adjusted_number_as_string = f"{adjusted_number:g}" if '.' not in adjusted_number_as_string: string_to_return = adjusted_number_as_string + suffix else: string_to_return = adjusted_number_as_string.replace(".", suffix) return string_to_return def number_to_resistance_string(n): return number_to_unit_string(n, 'R') def number_to_capacitance_string(n): return number_to_unit_string(n, 'F') def number_to_inductance_string(n): return number_to_unit_string(n, 'H') def round_to_significant(x, num): round_to = -int(math.floor(math.log10(abs(x)))) + num - 1 result = round(x, round_to) # print(f"Rounding {x}, with {round_to}, resulting in {result}") return result def expand_series(series, lowest=1, highest=10): lowest_decade = math.floor(math.log10(lowest)) highest_decade = math.ceil(math.log10(highest)) values = [] for decade in range(lowest_decade, highest_decade + 1): values_to_add = [x * 10 ** decade for x in series if x * 10 ** decade >= lowest and x * 10 ** decade <= highest] values.extend([round_to_significant(x, 3) for x in values_to_add]) return values resistor_packages = {"01005": {"power": 0.03, "height": 0.15, "length": 0.4, "width": 0.2, "footprint": "R01005", "symbol": "resistor_2pin"}, "0201": {"power": 0.05, "height": 0.3, "length": 0.6, "width": 0.3, "footprint": "R0201", "symbol": "resistor_2pin"}, "0402": {"power": 0.062, "height": 0.4, "length": 1.0, "width": 0.5, "footprint": "R0402", "symbol": "resistor_2pin"}, "0603": {"power": 0.1, "height": 0.5, "length": 1.6, "width": 0.85, "footprint": "R0603", "symbol": "resistor_2pin"}, "0805": {"power": 0.125, "height": 0.5, "length": 2.0, "width": 1.2, "footprint": "R0805", "symbol": "resistor_2pin"}, "1206": {"power": 0.25, "height": 0.6, "length": 3.2, "width": 1.6, "footprint": "R1206", "symbol": "resistor_2pin"}, "1210": {"power": 0.5, "height": 0.6, "length": 3.2, "width": 2.5, "footprint": "R1210", "symbol": "resistor_2pin"}, "1812": {"power": 1.0, "height": 0.6, "length": 3.2, "width": 4.6, "footprint": "R1812", "symbol": "resistor_2pin"}, "2010": {"power": 0.75, "height": 0.6, "length": 5.0, "width": 2.5, "footprint": "R1210", "symbol": "resistor_2pin"}, "2512": {"power": 1.0, "height": 0.6, "length": 6.3, "width": 3.2, "footprint": "R2512", "symbol": "resistor_2pin"} } def add_resistors(db_cursor, values, tolerance, package): for value in values: description = f"Resistor, {number_to_resistance_string(value)}, {tolerance * 100}%, SMD, {package}, 100ppm" # print(f"Adding {description}") query_insert_internal_part = f""" INSERT INTO internal_parts (part_description, part_group_id) VALUES ('{description}', 'chip_resistor');""" # print(query_insert_internal_part) db_cursor.execute(query_insert_internal_part) # print(f"Warnings: {cur.warnings}") new_part_number = db_cursor.lastrowid query_insert_resistor_info = f""" INSERT INTO group_details_chip_resistor (internal_pn, resistance, resistance_tolerance_upper, resistance_tolerance_lower, `power`, resistor_type, temperature_max, temperature_min, temperature_coefficient_upper, temperature_coefficient_lower, dimension_height, dimension_length, dimension_width, component_package, component_symbol) VALUES ({new_part_number}, {value}, {tolerance / 100}, -{tolerance / 100}, {resistor_packages[package]["power"]}, 'Thick Film', 155, -55, 100, -100, {resistor_packages[package]["height"]}, {resistor_packages[package]["length"]}, {resistor_packages[package]["width"]}, '{resistor_packages[package]["footprint"]}', '{resistor_packages[package]["symbol"]}');""" # print(query_insert_resistor_info) db_cursor.execute(query_insert_resistor_info) # print(f"Warnings: {cur.warnings}") return def main(): try: conn = mariadb.connect( user="jegatroncomponents_admin", password="librarian", host="localhost", port=3306, database="jegatroncomponents" ) except mariadb.Error as e: print(f"Error connecting to MariaDB Platform: {e}") exit(1) db_curr = conn.cursor() for package in resistor_packages: print(f"Adding {package} E24 series") add_resistors(db_curr, [0] + expand_series(e24, 0.1, 10000000), 0.05, package) print(f"Adding {package} E96 series") add_resistors(db_curr, [0] + expand_series(e96, 0.1, 10000000), 0.01, package) """ db_curr.execute("SELECT * from internal_parts;") for k in db_curr: print("Man!") print(k)""" conn.commit() db_curr.close() conn.close() return try: cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};" "Server=db.altiumlibrary.com;" "Database=altium_library;" "Trusted_Connection=no;" "UID=alib_OOMDUPOV4YG1;" "PWD=e071szxdIu6SnrC6") except Exception as e: print(f"Could not connect to celectial library") print(e) exit(1) print("Dude!") SQL_QUERY = """ SELECT TOP 5 * from z_CapacitorsCeramic; """ SQL_QUERY = """ SELECT * FROM "z_ResistorsSurfaceMount" WHERE Resistance is null; """ # cur = cnxn.cursor() # cur.execute(SQL_QUERY) """ for k in cur: print("Man!") print(k) """ if __name__ == '__main__': main()