Skip to content

Instantly share code, notes, and snippets.

@arturo182
Created September 13, 2019 14:25
Show Gist options
  • Save arturo182/ca53a32b07213122960c1f209e347759 to your computer and use it in GitHub Desktop.
Save arturo182/ca53a32b07213122960c1f209e347759 to your computer and use it in GitHub Desktop.
// Results above this price will not be listed
var maxPrice = 50;
// Configurations that would result in more than this qty of boards will not get listed
var maxBoardQty = 500;
// Maximum number of panels to check for
var maxPanelQty = 50;
// Maximum number of boards in a panel, X and Y
var maxPanelSize = 10;
// Should the results be sorted by price per PCB, if false, they're sorted by total qty
var sortByPrice = true;
function addJob(qty, panel, x, y)
{
jobs.push({
'qty': qty,
'panel': panel,
'x': x,
'y': y
});
}
function startJob(job)
{
if (!job)
return;
//console.log("Starting " + JSON.stringify(job));
console.log(`Progress: ${Math.round((1 - (jobs.length / numJobs)) * 100)}%`);
if (job['panel']) {
if (form.Panel == "No")
enablePanelBtn.click();
form.sestencilCountX = job['x'];
form.sestencilCountY = job['y'];
form.edgeRails = 1;
} else if (form.Panel == "Yes") {
disablePanelBtn.click();
}
// Set to 0 and when the value changes we know we got the response
pcbNode.countPrice.shoppingItemDetail.customerOrderDetail.stencilCounts = 0;
pcbNode.chose(job['qty']);
pcbNode.smtRequirements('changeAchieveDate');
setTimeout(processJobs, 250, false);
}
function processJobs(isFirst)
{
if (jobs.length == 0) {
console.log("No more jobs.");
return;
}
var job = jobs[0];
if (isFirst) {
startJob(job);
return;
}
var orderDetail = pcbNode.countPrice.shoppingItemDetail.customerOrderDetail;
var hasPanel = job['panel'];
var xMatch = orderDetail.sestencilCountX == job['x'] || (!orderDetail.sestencilCountX && !job['x']);
var yMatch = orderDetail.sestencilCountY == job['y'] || (!orderDetail.sestencilCountY && !job['y']);
var qtyMatch = orderDetail.stencilCounts == job['qty'];
if (!qtyMatch || (hasPanel && (!xMatch || !yMatch))) {
//console.log("Still loading");
setTimeout(processJobs, 100, false, false);
} else {
//console.log("job: " + JSON.stringify(job) + ", price: " + pcbNode.totalPrice);
results.push({
'job': job,
'totalPrice': pcbNode.totalPrice
});
jobs = jobs.slice(1);
if (jobs.length > 0) {
startJob(jobs[0]);
} else {
processResults();
}
}
}
function processResults()
{
results.forEach(function(result)
{
var job = result['job'];
var totalPrice = result['totalPrice'];
var totalQty = job['qty'];
if (job['panel'])
totalQty *= (job['x'] * job['y']);
result['totalQty'] = totalQty;
result['pricePerPanel'] = totalPrice / job['qty'];
result['pricePerBoard'] = totalPrice / totalQty;
//console.log(JSON.stringify(result));
});
results.sort(function(a, b)
{
var field = sortByPrice ? 'pricePerBoard' : 'totalQty';
if (a[field] < b[field])
return -1;
if (a[field] > b[field])
return 1;
return 0;
});
results = results.filter(function(result)
{
return result['totalPrice'] < maxPrice;
});
console.log("Done! Here's the top 5 configurations:");
results.slice(0, 5).forEach(function(result, idx) {
var job = result['job'];
console.log(`${idx + 1}. PCB Qty: ${job['qty']}, Panel X: ${job['x']} Y: ${job['y']}, Total PCBs: ${result['totalQty']}, Total Price: ${result['totalPrice']}, Price per PCB: ${result['pricePerBoard'].toFixed(3)}`);
});
}
function findSiblingWithAttribute(node, attr)
{
for (var i = 0; i < 100; ++i) {
node = node.$$nextSibling;
if (attr in node)
break;
}
return node;
}
var jobs = [];
var results = [];
var numJobs = 0;
var app = angular.element(document);
var scope = app.data('$scope');
var node = scope.$$childHead;
var pcbNode = findSiblingWithAttribute(node, 'encapsulationForm');
var form = pcbNode.form;
var enablePanelBtn = angular.element("button[ng-disabled='form.disablePannel']")[0];
var disablePanelBtn = angular.element("button[ng-class='{\'cur\':form.Panel==\'No\'}']")[0];
var boardWidth = parseInt(form.stencilWidth);
var boardHeight = parseInt(form.stencilLength);
// Generate job list
form.selectqtylist.forEach(function(qty)
{
if (qty > maxPanelQty)
return;
addJob(qty, false, 0, 0);
for (var x = 1; x < maxPanelSize; ++x) {
for (var y = 1; y < maxPanelSize; ++y) {
var boardQty = qty * x * y;
if (boardQty > maxBoardQty)
continue;
var addTenX = 0;
var addTenY = 0;
if (x < y) {
addTenX = 10
} else {
addTenY = 10;
}
// Panel size must be at least 70x70, rails are 10mm
if (((x * boardWidth) + addTenX) < 70 || ((y * boardHeight) + addTenY) < 70)
continue;
addJob(qty, true, x, y);
}
}
});
numJobs = jobs.length;
//console.log(jobs);
setTimeout(processJobs, 0, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment