Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Center object / group / vector in a circle and then center in the artboard.

Contributor ,
Jan 04, 2023 Jan 04, 2023

Hi community, Happy new year! Hope you're all doing good.

 

Can anyone help me with a script (or some threads where I can find information) to do the following.

 

Let's say I have selected an object / group / vector (let's call it "design"), width and heigth will be variable, sometimes it will be wider sometimes and the way around in other circumstanses. I would like to have an script that will resize and center that design in the middle of a circle / elipse of 18.8mm x 18.8mm , the design will not exceed that area, and after that, the circle should be deleted (because we will not need it anymore) and then center the new resized design in an artboard of 1 inch x 1 inch. Not sure what's teh easiest way if to create a new document or to just resize the actual artboard, i'll rather prefer to resize the actual artboard.

 

I will appreciat your help! Picture for reference. Please note that the "design" will increase it's size or reduce it's size in order to fit, and I am only showing how I think it will be, I did not show the artboard resized.

 

AntonioPachecoHN_0-1672854167438.png

 

TOPICS
Draw and design , Feature request , Scripting , Tools , Type
837
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jan 04, 2023 Jan 04, 2023

The circle is unnecessary and is for demonstration only.  To remove it, uncomment the last line. 

// select item
var r = 18.8 * 2.83465;
var AB = 1 * 72;
var doc = app.activeDocument;
var x = doc.width / 2, y = -doc.height / 2;
var top = y + r / 2, left = x - r / 2;
var item = doc.selection[0];
item.position = [
    (doc.width / 2) - (item.width / 2), 
    (-doc.height / 2) + (item.height / 2)
];
var circle = doc.pathItems.ellipse(top, left, r, r);
circle.move(item, ElementPlacement.PLACEAFTER);
...
Translate
Adobe
Contributor ,
Jan 04, 2023 Jan 04, 2023

I have a script that reduces proportionally according the width, maybe this can help but I have not figured out the other stuff. here it is hope it helps for somebody.

 

var doc = app.activeDocument;
var objeto = doc.selection[0];
var dup = objeto.duplicate();
var WidthInicial = objeto.width;
var WidthFinal = WidthInicial - 14.4;
var ScaleFactor = (WidthFinal / WidthInicial) * 100;
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 04, 2023 Jan 04, 2023

Got the following code so far, it makes your object 18.8mm wide or height, but still does not fit inside the circle of 18.8mm, you can give it a try.

 

var doc = app.activeDocument;
var objeto = doc.selection[0];
var anchoObjmm = (objeto.width / 72) * 25.4;
var altoObjmm = (objeto.height / 72) * 25.4;

if(anchoObjmm > altoObjmm){
alert("El objeto es mas ancho que alto");
var dup = objeto.duplicate();
var WidthInicial = anchoObjmm;
alert(anchoObjmm);
var diferencia = 18.8 - anchoObjmm;
alert(diferencia);
if(diferencia < 0){
var WidthFinal = WidthInicial + diferencia;
alert(WidthFinal);
}
else{
var WidthFinal = WidthInicial - diferencia;
alert(WidthFinal);
}
var ScaleFactor = (WidthFinal / WidthInicial) * 100;
alert(ScaleFactor);
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();
}
else{
alert("El objeto es mas alto que ancho");
var dup = objeto.duplicate();
var altoInicial = altoObjmm;
var diferencia = 18.8 - altoObjmm;
if(diferencia < 0){
var altoFinal = altoInicial + diferencia;
}
else{
var altoFinal = altoInicial - diferencia;
}
var ScaleFactor = (altoFinal / altoInicial) * 100;
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 04, 2023 Jan 04, 2023

I just try trying to make a "resize" using a scale value from the area of the circle, which is pi * r2.

 

var doc = app.activeDocument;
var objeto = doc.selection[0];
var anchoObjmm = (objeto.width / 72) * 25.4;
var altoObjmm = (objeto.height / 72) * 25.4;
var areaObjmm2 = anchoObjmm * altoObjmm;
var ScaleFactor = (areaObjmm2 / 277.6) * 100;
var dup = objeto.duplicate();
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;
objeto.remove();

 

still does nto fit inside the circle

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 04, 2023 Jan 04, 2023

The circle is unnecessary and is for demonstration only.  To remove it, uncomment the last line. 

// select item
var r = 18.8 * 2.83465;
var AB = 1 * 72;
var doc = app.activeDocument;
var x = doc.width / 2, y = -doc.height / 2;
var top = y + r / 2, left = x - r / 2;
var item = doc.selection[0];
item.position = [
    (doc.width / 2) - (item.width / 2), 
    (-doc.height / 2) + (item.height / 2)
];
var circle = doc.pathItems.ellipse(top, left, r, r);
circle.move(item, ElementPlacement.PLACEAFTER);
circle.filled = false;
var m = item.height / item.width;
width = Math.sqrt(Math.pow(r / 2, 2) / (Math.pow(m, 2) + 1)) * 2;
height = width * m;
item.resize(100 * (width / item.width), 100 * (height / item.height));
doc.artboards[0].artboardRect = [x - AB / 2, y + AB / 2, x + AB / 2, y - AB / 2];
// circle.remove();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 04, 2023 Jan 04, 2023

Since there is a lot of logos, with different sizes and shapes, this is the closest script to what I was looking for! it is not completely filling all the shape of the 18.8mm circle but the script is good! I hope mines were useful for somebody as well. thanks @femkeblanco 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 15, 2023 Mar 15, 2023

Hi community! Can I reopen this thread again or make a new one? I would like a modification, not sure how to do it.

 

the script from @femkeblanco :

 

// select item
var r = 19 * 2.83465;
var AB = 1 * 72;
var doc = app.activeDocument;
var x = doc.width / 2, y = -doc.height / 2;
var top = y + r / 2, left = x - r / 2;
var item = doc.selection[0];
item.position = [
(doc.width / 2) - (item.width / 2),
(-doc.height / 2) + (item.height / 2)
];
var circle = doc.pathItems.ellipse(top, left, r, r);
circle.move(item, ElementPlacement.PLACEAFTER);
circle.filled = false;
var m = item.height / item.width;
width = Math.sqrt(Math.pow(r / 2, 2) / (Math.pow(m, 2) + 1)) * 2;
height = width * m;
item.resize(100 * (width / item.width), 100 * (height / item.height));
doc.artboards[0].artboardRect = [x - AB / 2, y + AB / 2, x + AB / 2, y - AB / 2];
app.executeMenuCommand("fitin");
// circle.remove();

 

Now the circle needs to be 19mm instead of 18.8 and the artboard needs to be 22mm instead of 1in (25.4mm).

 

I was able to change 18.8mm to 19mm really easy but not sure how to reduce the artboard to 22mm. I would appreciate some help. Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2023 Mar 15, 2023

You'll need to adjust the third line that calculates the artboard size. Illustrator uses points as the unit of measurement for almost all distances (1 inch equals 72 points). You can read more about that in the documentation here. So just convert 22 mm to points.

 

var AB = 22 * 2.834645;

 

dad x 2. designer. maker. fun haver. ‍
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 15, 2023 Mar 15, 2023
LATEST

I cant like the comments anymore but big like for you, thanks!

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