Calculations with Switch private data

Post Reply
User avatar
JimmyHartington
Advanced member
Posts: 310
Joined: Tue Mar 22, 2011 7:38 am

Calculations with Switch private data

Post by JimmyHartington »

Hi

In a flow I needed to calculate how many times a step and repeat imposition could be repeated on 965 mm.

I could not get my math to work with Switch calculations, so I decided to write a script to calculate and return a new private data key with the number I needed.

But it took me way to long because I think I messed up declaring my variables as numbers/floats.

The code below works, but is probably not the best code.

What is the best way to read a private data key and have the script intepret it as a number (which can contain decimals)?

Code: Select all

async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {

    let ImposeRoll: string = await job.getPrivateData("ImposeRoll");
    let BleedRoll: string = await job.getPrivateData("BleedRoll");
    let Width: string = await job.getPrivateData("Width");
    let ImposeRollPrintOnCutterFloor: number = 0
    let ImposeRollPrintOnCutter: number = 0
    let MarksRollPrint: number = 0

    ImposeRollPrintOnCutterFloor = await Math.floor((965 / (Number(ImposeRoll) * (Number(BleedRoll) + Number(Width)))));
    ImposeRollPrintOnCutter = await ImposeRollPrintOnCutterFloor * Number(ImposeRoll);
    MarksRollPrint = await Number(ImposeRollPrintOnCutter) - 1;

    await job.setPrivateData("ImposeRollPrintOnCutter", ImposeRollPrintOnCutter);
    await job.setPrivateData("MarksRollPrint", MarksRollPrint);

    await job.log(LogLevel.Info, "ImposeRoll: " + ImposeRoll);
    await job.log(LogLevel.Info, "BleedRoll: " + BleedRoll);
    await job.log(LogLevel.Info, "Width: " + Width);
    await job.log(LogLevel.Info, "ImposeRollPrintOnCutterFloor: " + ImposeRollPrintOnCutterFloor);
    await job.log(LogLevel.Info, "ImposeRollPrintOnCutter: " + ImposeRollPrintOnCutter);
    await job.log(LogLevel.Info, "MarksRollPrint: " + MarksRollPrint);

    await job.sendToSingle();
}
laurentd
Member
Posts: 142
Joined: Wed Mar 13, 2019 2:06 pm

Re: Calculations with Switch private data

Post by laurentd »

Hi Jimmy,

let Width: number = parseFloat( await job.getPrivateData("Width") );

And in case Width is a property:
let Width: number = parseFloat(await flowElement.getPropertyStringValue("Width") as string);
Laurent De Wilde, Solution Architect @ Enfocus
User avatar
JimmyHartington
Advanced member
Posts: 310
Joined: Tue Mar 22, 2011 7:38 am

Re: Calculations with Switch private data

Post by JimmyHartington »

I have tried to clean up the code:

Code: Select all

    let ImposeRoll: number = parseFloat(await job.getPrivateData("ImposeRoll"));
    let BleedRoll: number = parseFloat(await job.getPrivateData("BleedRoll"));
    let Width: number = parseFloat(await job.getPrivateData("Width"));

    let ImposeRollPrintOnCutterFloor = await Math.floor((965 / ImposeRoll * BleedRoll + Width));
    let ImposeRollPrintOnCutter = await ImposeRollPrintOnCutterFloor * ImposeRoll;
    let MarksRollPrint = await ImposeRollPrintOnCutter - 1;

    await job.setPrivateData("ImposeRollPrintOnCutter", ImposeRollPrintOnCutter);
    await job.setPrivateData("MarksRollPrint", MarksRollPrint);

    await job.log(LogLevel.Info, "ImposeRoll: " + ImposeRoll);
    await job.log(LogLevel.Info, "BleedRoll: " + BleedRoll);
    await job.log(LogLevel.Info, "Width: " + Width);
    await job.log(LogLevel.Info, "ImposeRollPrintOnCutterFloor: " + ImposeRollPrintOnCutterFloor);
    await job.log(LogLevel.Info, "ImposeRollPrintOnCutter: " + ImposeRollPrintOnCutter);
    await job.log(LogLevel.Info, "MarksRollPrint: " + MarksRollPrint);

    await job.sendToSingle();
But does not get the correct values as I expect
Image
laurentd
Member
Posts: 142
Joined: Wed Mar 13, 2019 2:06 pm

Re: Calculations with Switch private data

Post by laurentd »

priority of mathematical operators!
try this: let ImposeRollPrintOnCutterFloor = await Math.floor( 965 / ( ImposeRoll * ( BleedRoll + Width )));
Laurent De Wilde, Solution Architect @ Enfocus
User avatar
JimmyHartington
Advanced member
Posts: 310
Joined: Tue Mar 22, 2011 7:38 am

Re: Calculations with Switch private data

Post by JimmyHartington »

Perfect. Thank you very much.
I had the correct parentheses in an earlier version, but probably messed it up trying to wrap it in Number() as in the code I posted first.

Image
User avatar
JimmyHartington
Advanced member
Posts: 310
Joined: Tue Mar 22, 2011 7:38 am

Re: Calculations with Switch private data

Post by JimmyHartington »

For completeness here is my finished script

Code: Select all

async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {

    // Use parseFloat() set the variable as a number, so math works
    
    let ImposeRoll: number = parseFloat(await job.getPrivateData("ImposeRoll"));
    let BleedRoll: number = parseFloat(await job.getPrivateData("BleedRoll"));
    let Width: number = parseFloat(await job.getPrivateData("Width"));

    // Remember to use correct order of operations when doing math
    let ImposeRollPrintOnCutterFloor = await Math.floor( 965 / ( ImposeRoll * ( BleedRoll + Width )));
    
    let ImposeRollPrintOnCutter = await ImposeRollPrintOnCutterFloor * ImposeRoll;
    let MarksRollPrint = await ImposeRollPrintOnCutter - 1;

    await job.setPrivateData("ImposeRollPrintOnCutter", ImposeRollPrintOnCutter);
    await job.setPrivateData("MarksRollPrint", MarksRollPrint);

    await job.log(LogLevel.Info, "ImposeRoll: " + ImposeRoll);
    await job.log(LogLevel.Info, "BleedRoll: " + BleedRoll);
    await job.log(LogLevel.Info, "Width: " + Width);
    await job.log(LogLevel.Info, "ImposeRollPrintOnCutterFloor: " + ImposeRollPrintOnCutterFloor);
    await job.log(LogLevel.Info, "ImposeRollPrintOnCutter: " + ImposeRollPrintOnCutter);
    await job.log(LogLevel.Info, "MarksRollPrint: " + MarksRollPrint);

    await job.sendToSingle();
}
Post Reply