diff --git a/footprint_conv.cc b/footprint_conv.cc index bf6b7ec..2f2f6b0 100644 --- a/footprint_conv.cc +++ b/footprint_conv.cc @@ -1,4 +1,11 @@ +/* + * Lookup "Ultra Librarian" in your favorite search engine. + * + * AFAIK, TI has footprints in format compatible with this software for all its chips. + */ + #include +#include #include #include #include @@ -20,9 +27,9 @@ int print_pads(const pugi::xml_document& doc, const std::string& footprint_name, using namespace pugi; using namespace std; - xpath_node_set pins = doc.select_nodes((string("//footprint[@name='") + xpath_node_set pads = doc.select_nodes((string("//footprint[@name='") + footprint_name + "']/pcbpin").c_str()); - for (auto &pin : pins) + for (auto &pin : pads) { map ul_kicad_shape = { {"round", "C"}, @@ -60,7 +67,7 @@ int print_pads(const pugi::xml_document& doc, const std::string& footprint_name, out << "$EndPAD" << endl; } - return pins.size(); + return pads.size(); } int print_lines(const pugi::xml_document& doc, const std::string& footprint_name, std::ostream& out) @@ -164,17 +171,110 @@ void print_library(const pugi::xml_document& doc, std::ostream& out) out << "$EndLIBRARY" << endl; } +void print_pins(const pugi::xml_document& doc, const std::string& pin_name, std::ostream& out) +{ + using namespace pugi; + using namespace std; + + xpath_node_set pins = doc.select_nodes((string("//symbol[@name='") + + pin_name + "']/sympin").c_str()); + + for (auto &pin : pins) + { + const int number = pin.node().attribute("number").as_int(); + const double posx = pin.node().attribute("originx").as_double(); + const double posy = pin.node().attribute("originy").as_double(); + const int rotation = pin.node().attribute("rotation").as_int(); + const double length = pin.node().attribute("length").as_double(); + const string flipped = pin.node().attribute("flipped").value(); + + xpath_node pindes_node = doc.select_single_node((string("//symbol[@name='") + + pin_name + "']/sympin[@number='" + + to_string(number) + "']/text[@name='pindes']").c_str()); + + xpath_node pinname_node = doc.select_single_node((string("//symbol[@name='") + + pin_name + "']/sympin[@number='" + + to_string(number) + "']/text[@name='pinname']").c_str()); + + const string pinname = pinname_node.node().attribute("data").value(); + const string pindes = pindes_node.node().attribute("data").value(); + + map kicad_rotation = { + {270, 'U'}, + {180, 'R'}, + {90, 'D'}, + {0, 'L'} + }; + + /* + * X TO 1 - 200 0.150 R 40 40 1 1 P + */ + out << "X " << pinname << " " << number << " " << posx << " " << posy << " " + << length << " " << kicad_rotation[rotation] << " 40 40 0 0 U" << endl; + } +} + +int print_symbols(const pugi::xml_document& doc, std::ostream& out) +{ + using namespace pugi; + using namespace std; + + xpath_node_set symbols = doc.select_nodes("//symbol"); + + unsigned int part_number = 0; + for (auto &pin : symbols) + { + string symbol_name = pin.node().attribute("name").value(); + + /* + * DEF DIODE D 0 40 Y NR 1 0 NR + */ + out << "DEF " << symbol_name << " U 0 50 Y Y " << ++part_number + << "L N" << endl; + + /* + * F0 “D” 0.100 50 H V L CNN + * F1 “DIODE” 0 -100 50 H V L CIB + */ + out << "F0 \"U\" 0 -100 50 H V L CNN" << endl + << "F1 \"" + string(symbol_name) + "\" 0 -200 50 H V L CIB" << endl; + + out << "DRAW" << endl; + print_pins(doc,symbol_name,out); + out << "ENDDRAW" << endl + << "ENDDEF" << endl; + } + + return symbols.size(); +} + +void print_module(const pugi::xml_document& doc, std::ostream& out) +{ + using namespace std; + using namespace pugi; + + out << "EESchema-LIBRARY Version 2.0 24/1/1997-18:9:6" << endl; + print_symbols(doc,out); +} + int main(int argc, char* argv[]) { using namespace std; using namespace pugi; - if (argc != 2) + if (argc != 4) return 1; xml_document doc; doc.load_file(argv[1]); - print_library(doc,cout); + + ofstream libfile(argv[2]); + print_library(doc,libfile); + libfile.close(); + + ofstream symfile(argv[3]); + print_module(doc,symfile); + symfile.close(); return 0; }