Adobe Illustrator CS3〜CC2014編 選択した図形の中心線を描く/四辺のグリッドを描く

今回は選択した図形の中心線を描くスクリプトです。線を描くだけでなく、ガイドにすることもできます。また、もうひとつ、選択した図形の四辺をベースにしたガイドを描くスクリプトです。複数の図形が選択されている場合は、選択されている全ての図形に対して処理されます。

最初は選択した図形の中心線を描くスクリプトです。ガイドでなく単純な線にしたい場合は、以下のスクリプト内の「pObj.guides= true;」の行を削除してください。

// 選択された図形の中心線を描く
(function(){
var offset = 10;
if (app.documents.length < 1){ return; }
var selObj = activeDocument.selection;
for(var i=0; i<selObj.length; i++){
var rect = selObj[i].geometricBounds;
$.writeln(rect);
var x1 = rect[0];
var y1 = rect[1];
var x2 = rect[2];
var y2 = rect[3];
var cx = x1 + (x2-x1)/2;
var cy = y1 + (y2 - y1)/2;
drawLine(cx, y1+offset, cx, y2-offset);
drawLine(x1-offset, cy, x2+offset, cy);
}
// 線を描画
function drawLine(sx,sy,ex,ey){
var pObj = app.activeDocument.pathItems.add();
pObj.setEntirePath([[sx, sy],[ex, ey]]);
pObj.filled = false; // 塗りなし
pObj.stroked = true; // 線あり
pObj.strokeWidth = 1; // 線幅1ポイント
pObj.strokeColor = setColor(255,0,0); // 線の色を指定(青色)
pObj.guides= true;
}
// カラーを設定
function setColor(r, g, b){
var tmpColor = new RGBColor();
tmpColor.red = r;
tmpColor.green = g;
tmpColor.blue = b;
return tmpColor;
}
})();

次のスクリプトは選択肢図形の四辺をベースにガイドを描きます。

// 選択された図形の辺のガイドを描く
(function(){
if (app.documents.length < 1){ return; }
var w = app.activeDocument.width; // 横幅
var h = app.activeDocument.height; // 縦幅
var selObj = activeDocument.selection;
for(var i=0; i<selObj.length; i++){
var rect = selObj[i].geometricBounds;
$.writeln(rect);
var x1 = rect[0];
var y1 = rect[1];
var x2 = rect[2];
var y2 = rect[3];
drawLine(0, y1, w, y1);
drawLine(0, y2, w, y2);
drawLine(x1, 0, x1, -h);
drawLine(x2, 0, x2, -h);
}
// 線を描画
function drawLine(sx,sy,ex,ey){
var pObj = app.activeDocument.pathItems.add();
pObj.setEntirePath([[sx, sy],[ex, ey]]);
pObj.filled = false; // 塗りなし
pObj.stroked = true; // 線あり
pObj.strokeWidth = 1; // 線幅1ポイント
pObj.strokeColor = setColor(255,0,0); // 線の色を指定(青色)
pObj.guides = true;
}
// カラーを設定
function setColor(r, g, b){
var tmpColor = new RGBColor();
tmpColor.red = r;
tmpColor.green = g;
tmpColor.blue = b;
return tmpColor;
}
})();

[サンプルをダウンロード]