|
// Adobe Illustrator Script |
|
|
|
// 選択パスのアンカーポイントの座標値を、選択パスを含む範囲が |
|
// -1 <= x <= 1, -1 <= y <= 1 の範囲になるように換算して |
|
// ファイルに出力する。 |
|
|
|
// Schwarz-Christoffel mapping の入力データ用で外周が正方形の場合 true |
|
var MODE_SQUARE = true; |
|
// MODE_SQUARE 用の定数 |
|
var Ke = 1.8540746773013719; |
|
// 出力座標値は 0 <= x <= -2*Ke, -Ke < y < Ke の範囲になる |
|
|
|
function main(){ |
|
if(app.documents.length < 1) return; |
|
var sel = app.activeDocument.selection; |
|
if(sel.length < 1) return; |
|
|
|
alert("getCoords\rMODE_SQUARE=" + MODE_SQUARE.toString()); |
|
|
|
var points = []; |
|
for(var i = 0; i < sel.length; i++){ |
|
// グループ、複合パス、効果が適用されているオブジェクトは対象外 |
|
if(sel[i].typename == "PathItem"){ |
|
getCoords(sel[i], points); |
|
} |
|
} |
|
if(points.length < 1){ |
|
alert("選択オブジェクトから座標を取得できませんでした。"); |
|
return; |
|
} |
|
|
|
var rect = getRectSpec(points); |
|
|
|
var k = MODE_SQUARE ? Ke : 1.0; |
|
var rate = k * 2.0 / Math.max(rect.height, rect.width); |
|
if(MODE_SQUARE) rect.center[0] = rect.right; |
|
|
|
// output |
|
var output_lines = []; |
|
for(var i = 0; i < points.length; i++){ |
|
var p = points[i]; |
|
|
|
if(p.length < 1){ |
|
// 空の配列はパスごとの区切り。空行として出力 |
|
output_lines.push("\n"); |
|
} else { |
|
output_lines.push( |
|
(rate * (p[0] - rect.center[0])).toFixed(8) |
|
+ " " |
|
+ (rate * (p[1] - rect.center[1])).toFixed(8) + "\n"); |
|
} |
|
} |
|
|
|
writeToFile(output_lines.join(""), "txt"); |
|
} |
|
|
|
// ---------------------------------------------- |
|
// dat: 出力用データ。直接 File.write される |
|
// ext: ファイル選択ダイアログのフィルタ用拡張子。例:"txt" |
|
function writeToFile(dat, ext){ |
|
var afile = File.saveDialog("save to a file",ext + " File:*." + ext); |
|
if(!afile){ // cancel |
|
return false; |
|
} |
|
if(!afile.open("w")){ |
|
alert("failed to open a file"); |
|
return false; |
|
} |
|
afile.write(dat); |
|
afile.close(); |
|
alert("saved"); |
|
return true; |
|
} |
|
|
|
// ---------------------------------------------- |
|
// 座標値のリストを元に、全体を囲む矩形範囲の情報を取得する |
|
// points: 座標値 [x, y] の配列 |
|
// 戻り値: rect { left, right, top, bottom, center:[x, y], width, height } |
|
function getRectSpec(points){ |
|
var x = points[0][0]; |
|
var y = points[0][1]; |
|
var rect = {left:x, right:x, top:y, bottom:y, center:[x,y], |
|
width:0, height:0}; |
|
|
|
for(var i = 1; i < points.length; i++){ |
|
var p = points[i]; |
|
if(p.length < 1) continue; // パスごとの区切り |
|
|
|
x = p[0]; |
|
y = p[1]; |
|
|
|
if(rect.left > x) rect.left = x; |
|
if(rect.right < x) rect.right = x; |
|
if(rect.top < y) rect.top = y; |
|
if(rect.bottom > y) rect.bottom = y; |
|
} |
|
rect.center = [(rect.left + rect.right)/2, (rect.top + rect.bottom)/2]; |
|
rect.width = rect.right - rect.left; |
|
rect.height = rect.top - rect.bottom; |
|
return rect; |
|
} |
|
// ---------------------------------------------- |
|
// item: PathItem |
|
// points: Array |
|
function getCoords(item, points){ |
|
var pts = item.pathPoints; |
|
for(var i = 0; i < pts.length; i++){ |
|
points.push(pts[i].anchor); |
|
} |
|
points.push([]); // パスごとの区切り。空行として出力 |
|
} |
|
main() |