Tutorial: Eagle PCB ULP to write Script that Changes all the Devices in a library to have Value = ON

You select a device in an opened library for edit by using this syntax:  edit deviceset

Value is set to an active editable value with the syntax:  value on

Thus on a meta level this script is going to do the following things for each part in the library:

  • write the open device command to script file using syntax:  edit deviceset
  • write the turn the device value to script file using syntax: value on
  • repeat until all the parts in the library have been cycled through

This script will build upon the script that lists the devicesets in a library.


//—— Get the ULP Path ——//

string ULP_Path;
char bkslash = '/';
int pos = strrchr(argv[0], bkslash);

  if (pos >= 0) 
    { 
      ULP_Path = strsub(argv[0], 0, pos + 1);
    }

output(ULP_Path + "TurnValueOnAllParts.scr", "wt"){library(L) { L.devicesets(D) { printf("edit " +  D.name + ".dev" + "\n"); 
                                                                                  printf("value on \n");
                               
                               }}}


Note that the script cycles through devicesets and not devices. 

ULP tutorial that writes all the Reference Designators Values and an example Attribute to a text file

For my BOM ULP I want to use attribute fields that you can see here.  To do this I need to know how to read an attribute.  The code snippet below does that.  The test attribute name is FUDGE and the test value is VANILLA.   To read attributes you need to know what they are called as you will see below.


dlgMessageBox("Do you want to count the parts?","Yes","No","Maybe");                       //opens a dialog box: no matter what you answer it will count the parts.   It is a joke.


//—— Get the ULP Path ——//

string ULP_Path;
char bkslash = '/';
int pos = strrchr(argv[0], bkslash);

  if (pos >= 0) 
    { 
      ULP_Path = strsub(argv[0], 0, pos + 1);
    }
    

//—— This is the loop that gets the parts and writes to file —–//

int cnt=0;

output(ULP_Path + "ListOfParts.txt", "wt")
{  
if (schematic) schematic(S)                                                                //This nested set of loops cycle through all the parts and increment the count each pass through
  {
    S.parts(P)
      {
        P.instances(E)                                                             
          { 
            printf(E.name + "     " + E.value + "     " + P.attribute[“FUDGE”] + "\n");
            cnt++;
          }
      }
  }
 } 
  

string result;
sprintf(result, "The parts count is %d", cnt);
dlgMessageBox(result, "+Yes", "No","Maybe");                                               // display the parts count in a dlgMessageBox
  
exit(0); 

Eagle PCB ULP User Language Program Tutorial that writes all the reference designators to a file

Now it is time to build on the previous examples.  This example does the following:

  • Get the path of the User Language Program (ULP)
  • It loops through all the parts
  • As it loops it appends the part name to a file

dlgMessageBox("Do you want to count the parts?","Yes","No","Maybe");                       //opens a dialog box: no matter what you answer it will count the parts.  


//—— Get the ULP Path ——//

string ULP_Path;
char bkslash = '/';
int pos = strrchr(argv[0], bkslash);

  if (pos >= 0) 
    { 
      ULP_Path = strsub(argv[0], 0, pos + 1);
    }

    

//—— This is the loop that gets the parts and writes to file —–//

string FileLine;
int cnt=0;

output(ULP_Path + "ListOfParts.txt", "at")
{  
if (schematic) schematic(S)                                                                //This nested set of loops cycle through all the parts and increment the count each pass through
  {
    S.parts(P)
      {
        P.instances(E)                                                             
          {
            printf(FileLine += E.name +"\n");
            cnt++;
          }
      }
  }
 } 
  

string result;
sprintf(result, "The parts count is %d", cnt);
dlgMessageBox(result, "+Yes", "No","Maybe");                                               // display the parts count in a dlgMessageBox
  
exit(0); 


The result will look like this: ListOfParts.txt

Simple Eagle PCB ULP User Language Program Tutorial to show the path of the ULP

If you want to alter anything on the schematic with a User Language Program you will need to output to a command line script file.   The most common thing to do is write it to the same directory that the ULP is in.  To do that you are going to need to know the path of the ULP.  Below is a program that gives you the path of the ULP.


string ULP_Path;
char bkslash = '/';
int pos = strrchr(argv[0], bkslash);

  if (pos >= 0) 
    { 
      ULP_Path = strsub(argv[0], 0, pos + 1);
    }
    
dlgMessageBox(ULP_Path,"aroo","aree");                       //opens a dialog box with the path in the notification area

exit(0);


Notes for Writing an Eagle PCB Change of Value User Language Program

Script functions required

  • Need the script syntax to change a part value.  The command line to issue to change a part value is: value refdes value.  And example would be to change R5's value to 100 you would issue the command:  value r5 100
  • Need to add attribute fields to all parts.  Example: Attribute r5 Fudge  adds the fudge attribute. Pops up a window prompting for value of this attribute.  ATTRIBUTE r4  fudge 'vanilla'  is the correct syntax to include the value.
  • Need to run a script on libraries to turn on all value fields.  The command line to issue is: value on  while the device is up in the librarie editor.

Requirements for Ideal Bill of Materials

  • Need to turn on the value field for all parts.  This will hold the basic resistance, capacitance, inductance values or integrated circuit part number.

BOM Fields

  • Item Number: Line item number
  • Quantity
  • Schematic Value: This is the simple short value shown in the schematic. Short values avoid clutter on the schematic
  • Vendor Part Number: Attribute field:  This will hold the catalog part number.  Example Mouser part number 
  • Manufacturers Part Number:  Attribute field: Example: Hittite part number
  • Internal Part Number: Attribute field:  Example: The company part number that covers all 0.1 uF capacitors
  • Description:  Attribute field: Description with specifications:  Example: CAP CER 4.7UF 35V 10% X5R 1210
  • Reference Designators: Grouped by value.  
  • Package: Need this to ease confirmation of correct purchase: Example: 0603

….. more

A simple Eagle PCB ULP User Language Program Tutorial to Count the Number of Parts on a Board

This program demonstrates how you access the Schematic object with an Eagle PCB User Language Program – ULP.


dlgMessageBox("Do you want to count the parts?","Yes","No","Maybe");                       //opens a dialog box: no matter what you answer it will count the parts.  

int cnt=0;
if (schematic) schematic(S)                                                                //This nested set of loops cycle through all the parts and increment the count each pass through
  {
    S.parts(P)
      {
        P.instances(E)                                                             
          {
            cnt++;
          }
      }
  }

string result;
sprintf(result, "The parts count is %d", cnt);
dlgMessageBox(result, "+Yes", "No","Maybe");                                               // display the parts count in a dlgMessageBox
  
exit(0); 

How an Eagle PCB ULP User Language Program Can Alter a Schematic

I have used a ULP ( User Language Program ) called Smash.ulp to smash all the parts on the schematic many times.  ULP's by themselves are said to be unable to modify a schematic.  Smash.ulp generates a script file named Smash.scr.  Inside of Smash.scr you will see something like the following:


grid mil;
smash (2700 1400);
smash (3800 1400);
smash (500 6900);
smash (1400 7000);
smash (8500 6400);
smash (3100 6800);
smash (3400 6800);
smash (3700 6800);
smash (4000 6800);


The integers shown are in units of mils which is called out at the top of the list.  I verified indeed that these coordinates correspond to parts origins on the schematic which I ran Smash.ulp on.  So what Smash.ulp does is the following:

  1. create a list of smash commands for each component with their respective coordinates
  2. At the exit of Smash.ulp there is a statement that calls the script file named Smash.scr. That line is shown below:

exit("; SCR '" + ulp_path + "smash_all.scr';\n");

An Ultra simple Eagle PCB ULP User Language Program Tutorial to write to a file

I like simple examples for purposes of learning a programming language.  Something that does not clutter everything up leaving me with a million doubts.  The following Eagle ULP tutorial file writes an ASCII string to two different files.  The first file write demonstrate the default location of file writing and the second shows how to specify where you want the file written.  Notice the backslash character needs escaped because it is the escape character!  

 


output("fudge.txt", "wt"){ printf("Directly printed to file with output statement\n");}           //w= Write new file / t= Text mode
                                                                                                  //This did not work when located at bottom of file. Must not have been executed.  
                                                                                                  //It writes to the Eagle program directory! 
output("C:\\Users\\freemonsandlewould\\Documents\\Schematics\\Reader\\ULP\\farge.txt", "wt")
      { printf("Directly printed to file with output statement\n");}          
                                                                                                  //The backslash character must be escaped with another backslash
                                                                                                  //When you specify the entire path then it goes where you plan that it should
      
exit (0);   // Must have this or it errors out


A simple Eagle PCB ULP User Language Program to Open a file dialog and write to the file specified by it

This ULP ( User Language Program ) opens a file dialog automatically using the schematic name to form a file name in the dialog opened.  If you want you can change the name in the open dialog box.  It is a fairly simple program without all the extra stuff you see in this mind boggling script files.  I checked and was able to cut and paste the following into NotePad++ without getting html characters.