How to modify this script After running, the circle will automatically group. Thank you very much

Explorer ,
Oct 29, 2020

Copied

// Circumcircle

// draws circumcircles for each selcted path

// Method:
// The major reason why I wrote this script is to draw a "circumcircle"
// for a star.
// So the method to find the center and the radius for the circumcircle
// is rather incertitude.
// 1. find out a perpendicular bisector for the line connecting
// 1st anchor and 2nd anchor
// 2. do the same for 1st anchor and 3rd anchor
// (if the path is a triangle, 2nd anchor and 3rd anchor)
// 3. find out intersection point of 1 and 2.
// for the center of the circumcircle to draw
// 4. find out the length of the line connecting the center and 1st
// anchor, then do the same for the center and 2nd anchor.
// define longer one as the radius of the circumcircle to draw.

// JavaScript Script for Adobe Illustrator CS3
// Tested with Adobe Illustrator CS3 13.0.3, Windows XP SP2 (Japanese version).
// This script provided "as is" without warranty of any kind.
// Free to use and distribute.

// http://park12.wakwak.com/~shp/lc/et/en_aics_script.html

// 2005-09-01 release on the web (with Japanese comments)
// 2009-05-23 some refinements

var ver10 = (version.indexOf('10') == 0);

main();
function main(){
var sp = [];
getPathItemsInSelection(2, sp);
if(sp.length < 1) return;

activateEditableLayer(sp[0]);

var col = getGray(); // strokeColor

var p, arr1, arr2, mp, o, j, r, r1, rIdx, pi;
var err_fail_to_find_center = 0;

for(var i = 0; i < sp.length; i++){
p = sp[i].pathPoints;

// find out the center of the circle to draw
arr1 = perpendicularBisector(p[0].anchor, p[2].anchor);

if(p.length==3){ // in case triangle
arr2 = perpendicularBisector(p[1].anchor, p[2].anchor);
} else {
arr2 = perpendicularBisector(p[1].anchor, p[3].anchor);
}

o = intersection(arr1, arr2);
if(o.length < 1){
err_fail_to_find_center = 1;
continue;
}

// find out the radius of the circle to draw
r = dist(p[0].anchor, o);
r1 = dist(p[1].anchor, o);
if(r >= r1){
rIdx = 0;
} else {
rIdx = 1;
r = r1;
}

// do not draw if the radius is larger than the artboard
with(activeDocument){
if(r==0 || r>Math.max(width, height)){
continue;
}
}

// draw a circle
pi = activeDocument.activeLayer.pathItems.ellipse(o[1] + r, o[0] - r, r * 2, r * 2);
with(pi){
filled = false;
stroked = true;
strokeColor = sp[i].stroked ? sp[i].strokeColor : col;
strokeWidth = sp[i].strokeWidth || 1;
}
}

if(err_fail_to_find_center == 1)

}

// ------------------------------------------------
function perpendicularBisector(p1, p2){
var mp = getMidPnt(p1, p2);
var arr = defline([ mp[0] - (p1[1] - mp[1]), mp[1] + (p1[0] - mp[0]) ],
[ mp[0] - (p2[1] - mp[1]), mp[1] + (p2[0] - mp[0]) ]);
return arr;
}

// ------------------------------------------------
function getMidPnt(p1, p2){
return [ (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2 ];
}

// ------------------------------------------------
function dist(arr1, arr2) {
return Math.sqrt(Math.pow(arr1[0] - arr2[0], 2)
+ Math.pow(arr1[1] - arr2[1], 2));
}

// -----------------------------------------------
function defline(p1, p2){
var a = p1[1] - p2[1];
var b = p1[0] - p2[0];
return [a, -b, b * p1[1] - a * p1[0]];
}

// -----------------------------------------------
function intersection(p, q){
var d = p[0] * q[1] - p[1] * q[0];
if(d == 0) return [];
return [ (q[2] * p[1] - p[2] * q[1]) / d,
(p[2] * q[0] - q[2] * p[0]) / d ];
}

// -----------------------------------------------
function getGray(){
var col = new GrayColor();
col.gray = 100;
if(ver10){
var col2 = new Color();
col2.gray = col;
return col2;
}
return col;
}
// ------------------------------------------------
// extract PathItems from the selection which length of PathPoints
// is greater than "n"
function getPathItemsInSelection(n, pathes){
if(documents.length < 1) return;

var s = activeDocument.selection;

if (!(s instanceof Array) || s.length < 1) return;

extractPathes(s, n, pathes);
}

// --------------------------------------
// extract PathItems from "s" (Array of PageItems -- ex. selection),
// and put them into an Array "pathes". If "pp_length_limit" is specified,
// this function extracts PathItems which PathPoints length is greater
// than this number.
function extractPathes(s, pp_length_limit, pathes){
for(var i = 0; i < s.length; i++){
if(s[i].typename == "PathItem"
&& !s[i].guides && !s[i].clipping){
if(pp_length_limit
&& s[i].pathPoints.length <= pp_length_limit){
continue;
}
pathes.push(s[i]);

} else if(s[i].typename == "GroupItem"){
// search for PathItems in GroupItem, recursively
extractPathes(s[i].pageItems, pp_length_limit, pathes);

} else if(s[i].typename == "CompoundPathItem"){
// searches for pathitems in CompoundPathItem, recursively
// ( ### Grouped PathItems in CompoundPathItem are ignored ### )
extractPathes(s[i].pathItems, pp_length_limit , pathes);
}
}
}

// ----------------------------------------------
function activateEditableLayer(pi){
var lay = activeDocument.activeLayer;
if(lay.locked || !lay.visible) activeDocument.activeLayer = pi.layer;
}

Most Valuable Participant
| Most Valuable Participant

TOPICS
Scripting

Views

41

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to modify this script After running, the circle will automatically group. Thank you very much

Explorer ,
Oct 29, 2020

Copied

// Circumcircle

// draws circumcircles for each selcted path

// Method:
// The major reason why I wrote this script is to draw a "circumcircle"
// for a star.
// So the method to find the center and the radius for the circumcircle
// is rather incertitude.
// 1. find out a perpendicular bisector for the line connecting
// 1st anchor and 2nd anchor
// 2. do the same for 1st anchor and 3rd anchor
// (if the path is a triangle, 2nd anchor and 3rd anchor)
// 3. find out intersection point of 1 and 2.
// for the center of the circumcircle to draw
// 4. find out the length of the line connecting the center and 1st
// anchor, then do the same for the center and 2nd anchor.
// define longer one as the radius of the circumcircle to draw.

// JavaScript Script for Adobe Illustrator CS3
// Tested with Adobe Illustrator CS3 13.0.3, Windows XP SP2 (Japanese version).
// This script provided "as is" without warranty of any kind.
// Free to use and distribute.

// http://park12.wakwak.com/~shp/lc/et/en_aics_script.html

// 2005-09-01 release on the web (with Japanese comments)
// 2009-05-23 some refinements

var ver10 = (version.indexOf('10') == 0);

main();
function main(){
var sp = [];
getPathItemsInSelection(2, sp);
if(sp.length < 1) return;

activateEditableLayer(sp[0]);

var col = getGray(); // strokeColor

var p, arr1, arr2, mp, o, j, r, r1, rIdx, pi;
var err_fail_to_find_center = 0;

for(var i = 0; i < sp.length; i++){
p = sp[i].pathPoints;

// find out the center of the circle to draw
arr1 = perpendicularBisector(p[0].anchor, p[2].anchor);

if(p.length==3){ // in case triangle
arr2 = perpendicularBisector(p[1].anchor, p[2].anchor);
} else {
arr2 = perpendicularBisector(p[1].anchor, p[3].anchor);
}

o = intersection(arr1, arr2);
if(o.length < 1){
err_fail_to_find_center = 1;
continue;
}

// find out the radius of the circle to draw
r = dist(p[0].anchor, o);
r1 = dist(p[1].anchor, o);
if(r >= r1){
rIdx = 0;
} else {
rIdx = 1;
r = r1;
}

// do not draw if the radius is larger than the artboard
with(activeDocument){
if(r==0 || r>Math.max(width, height)){
continue;
}
}

// draw a circle
pi = activeDocument.activeLayer.pathItems.ellipse(o[1] + r, o[0] - r, r * 2, r * 2);
with(pi){
filled = false;
stroked = true;
strokeColor = sp[i].stroked ? sp[i].strokeColor : col;
strokeWidth = sp[i].strokeWidth || 1;
}
}

if(err_fail_to_find_center == 1)

}

// ------------------------------------------------
function perpendicularBisector(p1, p2){
var mp = getMidPnt(p1, p2);
var arr = defline([ mp[0] - (p1[1] - mp[1]), mp[1] + (p1[0] - mp[0]) ],
[ mp[0] - (p2[1] - mp[1]), mp[1] + (p2[0] - mp[0]) ]);
return arr;
}

// ------------------------------------------------
function getMidPnt(p1, p2){
return [ (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2 ];
}

// ------------------------------------------------
function dist(arr1, arr2) {
return Math.sqrt(Math.pow(arr1[0] - arr2[0], 2)
+ Math.pow(arr1[1] - arr2[1], 2));
}

// -----------------------------------------------
function defline(p1, p2){
var a = p1[1] - p2[1];
var b = p1[0] - p2[0];
return [a, -b, b * p1[1] - a * p1[0]];
}

// -----------------------------------------------
function intersection(p, q){
var d = p[0] * q[1] - p[1] * q[0];
if(d == 0) return [];
return [ (q[2] * p[1] - p[2] * q[1]) / d,
(p[2] * q[0] - q[2] * p[0]) / d ];
}

// -----------------------------------------------
function getGray(){
var col = new GrayColor();
col.gray = 100;
if(ver10){
var col2 = new Color();
col2.gray = col;
return col2;
}
return col;
}
// ------------------------------------------------
// extract PathItems from the selection which length of PathPoints
// is greater than "n"
function getPathItemsInSelection(n, pathes){
if(documents.length < 1) return;

var s = activeDocument.selection;

if (!(s instanceof Array) || s.length < 1) return;

extractPathes(s, n, pathes);
}

// --------------------------------------
// extract PathItems from "s" (Array of PageItems -- ex. selection),
// and put them into an Array "pathes". If "pp_length_limit" is specified,
// this function extracts PathItems which PathPoints length is greater
// than this number.
function extractPathes(s, pp_length_limit, pathes){
for(var i = 0; i < s.length; i++){
if(s[i].typename == "PathItem"
&& !s[i].guides && !s[i].clipping){
if(pp_length_limit
&& s[i].pathPoints.length <= pp_length_limit){
continue;
}
pathes.push(s[i]);

} else if(s[i].typename == "GroupItem"){
// search for PathItems in GroupItem, recursively
extractPathes(s[i].pageItems, pp_length_limit, pathes);

} else if(s[i].typename == "CompoundPathItem"){
// searches for pathitems in CompoundPathItem, recursively
// ( ### Grouped PathItems in CompoundPathItem are ignored ### )
extractPathes(s[i].pathItems, pp_length_limit , pathes);
}
}
}

// ----------------------------------------------
function activateEditableLayer(pi){
var lay = activeDocument.activeLayer;
if(lay.locked || !lay.visible) activeDocument.activeLayer = pi.layer;
}

Most Valuable Participant
| Most Valuable Participant

TOPICS
Scripting

Views

42

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 29, 2020 0
3 Replies 3
Nov 01, 2020

Copied

Hi,

Could you explain more what exacty you want to acheive here. After running the script, I get circle around the selected item, which is not group with the selected item.

Please explain more so that we can guide you in a better way.

Best regards

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 01, 2020 0
Most Valuable Participant ,
Nov 01, 2020

Copied

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 01, 2020 1
LATEST
Explorer ,
Nov 02, 2020

Copied

The problem has been solved, I don’t know how to add another one, sorry

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 02, 2020 0
Resources
Learning Resources for Illustrator
What's new and fixed in Illustrator
Fonts and Typography in Illustrator