Last active
November 7, 2023 14:37
-
-
Save fathidevs/a427c940c3199c1f663e2eb1b7047b3b to your computer and use it in GitHub Desktop.
screen builder
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
// use active document | |
var doc = app.activeDocument; | |
// initiate dialog | |
var dialog = new Window("dialog", "Enter details "); | |
dialog.orientaions = "column"; | |
// function to build text box | |
function editTextGroup(group, msg, defaultText, limit) { | |
group.add("statictext", undefined, msg); | |
var value = group.add("edittext", undefined, defaultText); | |
value.characters = limit; | |
return value; | |
} | |
// get surface dimesions | |
var surfaceGroup = dialog.add("group"); | |
var surfaceWidth = editTextGroup(surfaceGroup, "surface width: ", "105", 6); | |
var surfaceHeight = editTextGroup(surfaceGroup, "surface height: ", "25", 6); | |
// get cabinet dimensions | |
var cabinetGroup = dialog.add("group"); | |
var cabinetWidth = editTextGroup(cabinetGroup, "cabinet width: ", "10", 6); | |
var cabinetHeight = editTextGroup(cabinetGroup, "cabinet height: ", "10", 6); | |
// add buttons | |
var buttonsGroup = dialog.add("group"); | |
var cancelButton = buttonsGroup.add("button", undefined, "cancel"); | |
var submitButton = buttonsGroup.add("button", undefined, "submit"); | |
// buttons callback | |
cancelButton.onClick = function () { | |
dialog.close(); | |
} | |
submitButton.onClick = function () { | |
// validate inputs | |
if (!surfaceWidth || !surfaceHeight) { | |
alert("wrong inputs"); | |
dialog.close(); | |
} | |
drawClading(mmToPoints(surfaceWidth.text), mmToPoints(surfaceHeight.text)); | |
drawCabinet(mmToPoints(cabinetWidth.text), mmToPoints(cabinetHeight.text), mmToPoints(surfaceWidth.text), mmToPoints(surfaceHeight.text)); | |
dialog.close(); | |
} | |
// Fuction to center object in document | |
function centerInDocument(width, height) { | |
// center horizontal | |
var docWidth = doc.width / 2; | |
var objWidth = width / 2; | |
// center vertical | |
var docHeight = doc.height / 2; | |
var objHeight = height / 2; | |
return { | |
"top": objHeight - docHeight, | |
"left": docWidth - objWidth | |
}; | |
} | |
// Function to convert millimeters to points | |
function mmToPoints(mm) { | |
return mm * 2.83465; // 1 millimeter = 2.83465 points | |
} | |
function color(c, m, y, k) { | |
var color = new CMYKColor(); | |
color.cyan = c || 0; | |
color.magenta = m || 0; | |
color.yellow = y || 0; | |
color.black = k || 0; | |
return color; | |
} | |
function drawClading(width, height) { | |
var points = centerInDocument(width, height); | |
var clading = doc.pathItems.rectangle(points['top'], points['left'], width, height); | |
clading.strokeWidth = 1; | |
clading.fillColor = color(100, 100, 100, 100); | |
clading.strokeColor = new NoColor(); | |
} | |
function drawScreen(width, height) { | |
var points = centerInDocument(width, height); | |
var screen = doc.pathItems.rectangle(points['top'], points['left'], width, height); | |
screen.fillColor = color(); | |
screen.strokeColor = new NoColor(); | |
} | |
function drawCabinet(cabinetWidth, cabinetHeight, surfaceWidth, surfaceHeight) { | |
var col = Math.floor(surfaceWidth / cabinetWidth); | |
var row = Math.floor(surfaceHeight / cabinetHeight); | |
var screenWidth = cabinetWidth * col; | |
var screenHeight = cabinetHeight * row; | |
var screenStartingPoints = centerInDocument(screenWidth, screenHeight); | |
var pointerX = screenStartingPoints['left']; | |
var pointerY = screenStartingPoints['top']; | |
for (var i = 0; i < col * row; i++) { | |
var cabinet = doc.pathItems.rectangle(pointerY, pointerX, cabinetWidth, cabinetHeight); | |
cabinet.fillColor = color(14, 11, 11, 0); | |
cabinet.strokeColor = color(100, 100, 100, 100); | |
cabinet.strokeWidth = .2; | |
pointerX += cabinetWidth; | |
if (pointerX - screenStartingPoints['left'] + cabinetWidth > surfaceWidth) { | |
pointerX = screenStartingPoints['left']; | |
pointerY -= cabinetHeight; | |
} | |
} | |
var surfaceStartingPoints = centerInDocument(surfaceWidth, surfaceHeight); | |
var spointerX = surfaceStartingPoints['left']; | |
var spointerY = surfaceStartingPoints['top']; | |
drawDimensionDetails(spointerX, spointerY, surfaceWidth+spointerX, spointerY); | |
} | |
function drawDimensionDetails(startX, startY, endX, endY){ | |
var spacer = 15; | |
drawLine(startX, startY, endX, endY, 0, spacer); // horizontal top | |
drawLine(endX, startY+spacer-10, endX, endY+spacer+10, 0, 0); // vertical right top | |
drawLine(startX, startY+spacer-10, startX, endY+spacer+10, 0, 0); // vertical left top | |
setText((surfaceWidth.text)+" mm",endX/2, startY+(spacer*2)); // text top | |
} | |
function drawLine(startX, startY, endX, endY, positionX, positionY) { | |
var line = doc.pathItems.add(); | |
line.setEntirePath([[startX+positionX, startY+positionY], [endX+positionX, endY+positionY]]); | |
line.filled = false; | |
line.stroked = true; | |
line.strokeColor = color(20, 100,100, 20); | |
line.strokeWidth = .5; | |
} | |
function setText(content, x, y) { | |
var aproxWidth = mmToPoints(20.0); | |
var txt = doc.textFrames.add(); | |
txt.left = x+(aproxWidth); | |
txt.top = y; | |
txt.textRange.characterAttributes.fillColor = color(20, 100,100, 20); | |
txt.contents = content; | |
} | |
dialog.show(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment