Untitled

 avatar
unknown
plain_text
2 years ago
5.9 kB
3
Indexable
var canvas = page.getWidget("PalletDrawing");
var lastString = "";
var lastSizeArray = [];
var lastState = -1;
var timer = 0;
var show = true;
var checkCursorTimer = null;
var lastStateMachine = -1;
var lastListonSize = -1;
var lastEspesorDisco = -1;

function sizesToArray(_size_string, _liston_size, _espesor_disco)
{
    var _total_size = 0;
    var _ret = []; 
    var _comas = _size_string.split(",");
    for (i=0; i<_comas.length; i++)
    {
        var _reps = _comas[i].split("x");
        var _value;
        if (_reps.length > 1)
        {
            var _num = parseInt(_reps[0]);
            _value = parseInt(_reps[1]) + _espesor_disco;
            for (k=0; k<_num; k++)
            {
                _ret.push(_value);
                _total_size += _value;
            }
        }
        else
        {
            _value = parseInt(_comas[i]) + _espesor_disco;
            _ret.push(_value);
            _total_size += _value;
        }
    }
    //add restante...
    _value = _liston_size - _total_size;
    _ret.push(_value);
    return _ret;
}

function scaleArray(_array, _scale)
{
    var _ret = [];
    for (i=0; i<_array.length; i++)
    {
        _ret.push(_array[i] * _scale);
    }
    return _ret;
}

function checkUpdate()
{
    var _state = new State();
    var _size_string = project.getTag("recipe_formula_corte", _state, 0); 
    var _liston_size = project.getTag("recipe_largo_barra", _state, 0);    
    var _espesor_disco = parseFloat(project.getTag("recipe_e_disco", _state, 0)); 
    var _update = false;
    
    if (lastString === "" || lastString.localeCompare(_size_string) !== 0)
    {
        lastString = (' ' + _size_string).slice(1);
        _update = true;
    }
    if (lastListonSize !== _liston_size)
    {
        lastListonSize = _liston_size;
        _update = true;
    }
    if (lastEspesorDisco !== _espesor_disco)
    {
        lastEspesorDisco = _espesor_disco;
        _update = true;
    }
    
    return _update;
}

function PalletDrawing_onDraw(me, eventInfo)
{
    var _ctx = canvas.context2d;
    
    if (_ctx === null)
        return;

    var _state = new State();

    var MAX_SIZE = project.getTag("ini_max_size", _state, 0);
    
    var _size_string = project.getTag("recipe_formula_corte", _state, 0); 
    var _liston_size = project.getTag("recipe_largo_barra", _state, 0);    
    var _espesor_disco = parseFloat(project.getTag("recipe_e_disco", _state, 0)); 
    var _cur_trozo = project.getTag("recipe_current_trozo", _state, 0); 
    var _size_array = [];
    
    if (checkUpdate())
    {
        _size_array = sizesToArray(_size_string, _liston_size, _espesor_disco);
        project.setTag("recipe_data", scaleArray(_size_array, 10), -1, true);
        project.setTag("recipe_num_data", _size_array.length);
        lastSizeArray = _size_array;
       
       // project.showMessage(lastString2 + " <> " + _size_string);
    }
    else
    {
        _size_array = lastSizeArray;
    }
    
    _ctx.font = "bold 15px MS Shell Dlg";

    var _x = 0;
    var _y = 30;
    var _w = 0;
    var _h = 50;
    var _cur_size = 0;
    var _total_w = 0;
    var _total_size = 0;
    
    for (i=0; i<_size_array.length-1; i++)
    {
        if ((i % 2) == 0)
            _ctx.fillStyle ="green";
        else
            _ctx.fillStyle ="blue";
        
        _cur_size = _size_array[i];
        _w = (_cur_size / MAX_SIZE) * canvas.width;
        
        _ctx.fillRect(_x, 0, _w, _h);
        
        _ctx.fillStyle ="yellow";
        _ctx.textAlign = "center";
        _ctx.fillText(parseFloat((_cur_size - _espesor_disco).toFixed(2)), _x + _w/2, _y);
        
        if (_cur_trozo === i && show)
        {
            _ctx.beginPath();
            _ctx.lineWidth = "2";
            _ctx.strokeStyle = "yellow";
            _ctx.rect(_x+2, 2, _w-4, _h-4);
            _ctx.stroke();            
        }
        
        _x += _w;
        _total_w += _w;
        _total_size += _cur_size;
    }
    
    //Sobrante del listone...
    if (_size_array.length)
    {
        _cur_size = _size_array[i];
        _w = (_cur_size / MAX_SIZE) * canvas.width;
        //_w = ((_liston_size - _total_size) / MAX_SIZE) * canvas.width;
        _ctx.fillStyle = "red";
        _ctx.fillRect(_x, 0, _w, _h);
        _ctx.fillStyle ="yellow";
        _ctx.textAlign = "center";
        //_ctx.fillText(parseFloat((_liston_size - _total_size).toFixed(2)), _x + _w/2, _y);
        _ctx.fillText(parseFloat(_cur_size.toFixed(2)), _x + _w/2, _y);
        _total_size += _cur_size;//(_liston_size - _total_size);
    }

    //El restante...
    _x += _w;
    _w = ((MAX_SIZE - _total_size) / MAX_SIZE) * canvas.width;
    _ctx.fillStyle = "gray";
    _ctx.fillRect(_x, 0, _w, _h);
    _ctx.fillStyle ="yellow";
    _ctx.textAlign = "center";
    _ctx.fillText(parseFloat((MAX_SIZE - _total_size).toFixed(2)), _x + _w/2, _y);

    //canvas.update();
}

function Automatico_onActivate(me, eventInfo)
{
    page.setInterval("OnUpdate()", 1000);
    //project.showMessage("hola");
}

function OnCheckCursor()
{
    if ((Date.now() - timer) >= 250)
    {
        timer = Date.now();
        show = !show;
        canvas.update();
    }
}

function OnUpdate()
{
    var _state = new State();
    var _state_machine = project.getTag("hmi_estado_maquina", _state, 0);

    //project.showMessage(_state_machine);
    
    if (lastStateMachine !== _state_machine)
    {
        //project.showMessage(_state_machine + " = " + lastStateMachine);
        
        if (_state_machine === 1) //1=ENMARCHA
        {
            timer = Date.now();
            checkCursorTimer = page.setInterval("OnCheckCursor()", 200);
        }
        else
        {
            page.clearInterval(checkCursorTimer);
        }
        lastStateMachine = _state_machine;
    }
    
    if (checkUpdate())
    {
        canvas.update();        
    }
}
Editor is loading...
Leave a Comment