
There’s a lot of ways to burn up your time when designing PCBs, but renaming components can be one of the most frustrating. [Joe Pinzone] wrote in with his solution to the problem. Instead of hunting for each part on the schematic to change them one at a time, he makes a list of the substitutions and then uses a script to make all the changes in the XML files. He didn’t publish a post about his work, but you’ll find the source code he wrote embedded after the break.
The straw that finally broke the camel’s back was a project that included about two hundred components which didn’t seem to have a naming order that made any sense with the actual values of the components. The script is written in C++ (for Windows but [Joe] says this should be easily ported to other systems as well). To use it he creates a CSV file with the current component names in the first column. He then goes through and types what he wants for the new name in the second column. This CSV, along with the BRD and SCH files are then given as inputs for the script (through selecting them all and dragging to the script or as CLI arguments) and it automatically makes the changes.
Of course this is only possible because Cadsoft transitioned to using XML files in Eagle 6.
/* Title: EagleCAD XML Batch Component Renamer Author: Joe Pinzone, Newbury Ohio License: Free ("beer"), with acknowledgement of original author Date: January, 2013 Description: * Designed to use a CSV to perform batch renaming of EagleCAD schematic/board components * Works only on new Eagle XML format schematic and board files To generate CSV file with original component name list: * Simply drag ONLY your SCH file or BRD onto the EXE. partsList.csv is automatically generated from that file. To perform batch renaming: * Generate a CSV with original part names in first column, and replacement names in the second * Click and drag your SCH, BRD, and CSV file onto the EXE (use multi-select) * ^Alternatively, enter in the file paths as arguments from the command prompt Notes: * If the entry in the second column of the CSV row (replacement name) is blank, that entry will be skipped, and the component will retain it's original name * To prevent accidental replacement of identical character strings within the XML file, the program looks for the specific XML tags that correspond to component names. Edit the list in the "Handy things you can modify" section if Eagle changes things, or if you want to play around. * You can only load ONE schematic and ONE board file at a time. * You can only have 1000 entries in the CSV file. All others are ignored. However, you can increase this by setting the value of #define MAX_CSV_ROWS in the source file. I didn't want to take up too much memory unnecessarily, and 1000 is plenty for most people. * Live long and prosper*/#include #include #include #include using namespace std;// Handy things you can modify ----------------------------------------------------------------------------------- /* Path and file name to use when generating a name list */ #define MAX_CSV_ROWS 1000 // change this to allow more entries in the CSV find/replace file if necessary. // The program fails nicely, though - it just ignores entries after row 1000. char* partsListGenFile = "./partsList.csv"; // the directory and name of the generated parts list CSV file (if no find/replace CSV file is specified) /* This is the list of XML entries/tags that contain component names after them (from both SCH and BRD Eagle XML files) This list is used to make sure that ONLY component names are modified, and coincidental string matches elsewhere in the XML aren't accidentally changed. It happened once to me. It wasn't fun. True story. Add/remove tags to this list if Eagle changes their standard, or you decide to adapt this for some other purpose. The value of 'numTags' should match the number of elements in the 'xmlTags' array below - make sure they match if you change something! */ int numTags = 5; string xmlTags[] = { "part name", \ "instance part", \ "pinref part", \ "element name", \ "contactref element" \ }; /* When generating the CSV list, we only want one copy of each component name. The
XML entries in the schematic files, or the XML entries in the board files only contain one entry per part, so we use those as the lookup strings when generating the CSV. */ string schemXMLTag_part = "part name"; string boardXMLTag_part = "element name";// (Global) Variable Declarations ------------------------------------------------------------------------------- bool schemFound = false; // used by parseArguments, marks if schematic file was found in the files loaded bool boardFound = false; // used by parseArguments, marks if board file was found in the files loaded bool listFound = false; // used by parseArguments, marks if find/replace csv file was found in the files loaded char* path_schem; // the file path (full or relative) of the schematic file char* path_board; // the file path (full or relative) of the board file char* path_list ; // the file path (full or relative) of the csv find/replace list string list[1000][2]; // The storage array for the find/replace list. FIND string is element 0, REPLACE string is element 1 int listLength = 0; // number of populated entries in the list array above (set by the loadList function's return value)// Function Prototypes ------------------------------------------------------------------------------------------- int parseArguments(int argc, char* args[]); // loads the program arguments (file paths of schematic/board/csv files) int processFile(char* XMLFilePath); // assuming that 'list' is populated, goes line-by-line through specified file and replaces component names string processLine(string original); // used by processFile to process an individual line of text string processPart(string original); // used by processLine to change the part name based on the find/replace list int loadList(char* listFilePath); // parses/loads the list file into memory (into the 'list' array) void generateList(char* XMLPath, char* outPath, string xmlReferenceTag); // generates a new CSV component name list from the specified SCH or BRD file// Program --------------------------------------------------------------------------------------------------------int main(int argc, char* args[]){ cout