Created
December 29, 2015 16:50
-
-
Save elijahparker/ec156cba31e40ddccb37 to your computer and use it in GitHub Desktop.
fix imported schematic grid alignment issues in Upverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fixUpverterFile(filename) { | |
try { | |
var upverterDoc = JSON.parse(fs.readFileSync(filename).toString()); | |
var grid = 10; | |
var changes = fixUpverterSchematicGrid(upverterDoc, grid); | |
var newName = filename.replace(".upv", "") + "-fixed.upv"; | |
return { | |
success: changes !== false, | |
newName: newName, | |
fileContents: JSON.stringify(upverterDoc) | |
} | |
} catch (e) { | |
return { | |
success: false, | |
} | |
} | |
} | |
function toNearest(num, div) { | |
return Math.round(num / div) * div; | |
} | |
function moveToGrid(obj, grid) { | |
var change = false; | |
if (obj.x && obj.x % grid != 0) { | |
obj.x = toNearest(obj.x, grid); | |
change = true; | |
} | |
if (obj.y && obj.y % grid != 0) { | |
obj.y = toNearest(obj.y, grid); | |
change = true; | |
} | |
return change; | |
} | |
function fixUpverterSchematicGrid(upverterObject, grid) { | |
var doc = upverterObject; | |
if (doc && doc.component_instances) { | |
var changes = 0; | |
for (var i = 0; i < doc.component_instances.length; i++) { | |
if (doc.component_instances[i].symbol_attributes && doc.component_instances[i].symbol_attributes.length > 0) { | |
for (var j = 0; j < doc.component_instances[i].symbol_attributes.length; j++) { | |
if (moveToGrid(doc.component_instances[i].symbol_attributes[j], grid)) changes++; | |
} | |
} | |
} | |
for (var i = 0; i < doc.nets.length; i++) { | |
if (doc.nets[i].points && doc.nets[i].points.length > 0) { | |
for (var j = 0; j < doc.nets[i].points.length; j++) { | |
if (moveToGrid(doc.nets[i].points[j], grid)) changes++; | |
} | |
} | |
} | |
return changes; | |
} else { | |
console.log("invalid file"); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment