/*

routenet.ulp  version 1.0 (2005-11-02)

Copyright (C) 2005  Johannes Frank, joe@cerberon.net

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#usage "<b>Route airwires of a given net</b>\n"
       "<p>"
       "This ULP automatically routes all airwires of the given net "
       "creating straight connections between pads.<br>"
       "Since Eagle does not (yet) snap to pads this can be useful when "
       "routing off-grid components."
       "<p>"
       "Usage: RUN routenet [ <i>net</i> ]"
       "<p>"
       "<author>Author: joe@cerberon.net</author>"

string s, cmd = "", net_name = argv[1];
int dlg_result, signal_selected = 0, signal_count = 0, wire_count = 0;
numeric string board_signals[];

if(board)
{
    board(B)
    {
        if(net_name == "")
        {
            B.signals(S)
                board_signals[signal_count++] = S.name;
            //sort(signal_count, board_signals);

            dlg_result = dlgDialog("Select net to route")
            {
                dlgVBoxLayout
                {
                    dlgListBox(board_signals, signal_selected) dlgAccept();
                    dlgHBoxLayout
                    {
                        dlgStretch(1);
                        dlgPushButton("+OK") dlgAccept();
                        dlgPushButton("-Cancel") dlgReject();
                    }
                }
            };
            if(!dlg_result)
                exit(EXIT_SUCCESS);

            net_name = board_signals[signal_selected];
        }

        cmd = "GRID MM;";

        B.signals(S)
            if(S.name == net_name)
                S.wires(W)
                    if(W.layer == LAYER_UNROUTED)
                    {
                        sprintf(s, " ROUTE (%f %f) (%f %f);",
                                u2mm(W.x1), u2mm(W.y1),
                                u2mm(W.x2), u2mm(W.y2));
                        cmd += s;
                        wire_count++;
                    }

        if(!wire_count)
        {
            dlgMessageBox(":Net not found or no airwires within that net.");
            exit(EXIT_FAILURE);
        }

        cmd += " GRID LAST;";
        exit(cmd);
    }
}
else
{
    dlgMessageBox("Start this ULP in a board!");
    exit(EXIT_SUCCESS);
}

//EOF
