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

Script to Number Vectors

New Here ,
Dec 17, 2022 Dec 17, 2022

Copy link to clipboard

Copied

Hello! I am wondering if anybody can help me with creating a script for Illustrator?

 

I have attached an example file to show the layout of the layers.

Link to file

 

Each of the same coloured vectors are grouped together, and there are 24 groups, so 24 different colours.

I need the script to label each vector, so all of the vectors in Layer 1 will be labelled 1, all of the vectors in Layer 2 will be labeled 2, etc. 

 

I would like the labels to vary in size, so if the vector is large the number will be larger, and smaller vectors with smaller numbers so the number fits inside the vector. I would like it to not label vectors that would be too small to see by eye if the file was printed onto A3 paper. 

 

It would also be great if all the numbers go in particular layers, so all of the number 1 labels go into the Numbers 1 layer, all of the number 2 labels go into the Numbers 2 layer, etc.

 

Help would be very much appreciated!!

 

I have seen similar scripts on here but I can't make them work for my particular layer layout.

TOPICS
Scripting

Views

385

Translate

Translate

Report

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
Adobe
Guide ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

You may not get a satisfactory answer, but your best bet is with this script.

 

It won't work as is because your document has too many paths, but you could have success running it one layer at a time. Also, you'll need further tweeks to the script for your purpose. I suggest contacting the author and seeing if they're willing to help.

Votes

Translate

Translate

Report

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 ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

quote

You may not get a satisfactory answer, but your best bet is with this script. …

 


By femkeblanco

 

Hi @femkeblanco 

good finding. If you are interested - this was the topic that led to the script you linked:

Adobe Illustrator Forum | Script. Insert text number in the middle of visible bounds of the each obj...

 

 



Votes

Translate

Translate

Report

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 ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

Hi @eloisejo, as @femkeblanco alludes, getting the numbers "inside" the vectors is not a trivial undertaking. In fact what you are asking isn't as straightforward as you may think. However, I thought it might be helpful to do a "basic" script as a starting point. I have written the following, but I haven't made any strong effort to position the numbers (I just center them in the path item's bounds) and my scheme for deciding point size is probably not what you want. But at least you can see what's going on, and perhaps make adjustments yourself?

Here's my script:

 

/**
 * Quick and dirty paint by numbers script.
 * by m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/script-to-number-vectors/m-p/13428479
 * Note: requires rigidly structured document (see sample file posted)
 */
(function () {

    var doc = app.activeDocument,
        groups = doc.groupItems,
        layerNameMatcher = /Layer (\d+)/;

    // iterate over each group item
    for (var i = 0; i < groups.length; i++) {

        var group = groups[i];

        if (
            group.parent.typename != 'Layer'
            || !layerNameMatcher.test(group.parent.name)
        )
            continue;

        var num = Number(group.parent.name.match(layerNameMatcher)[1]);

        // iterate over each group's path items
        for (var j = 0; j < group.pageItems.length; j++)
            drawNumber(doc, num, group.pageItems[j].geometricBounds);

    }


    function drawNumber(doc, num, bounds) {
        // calculate size for number
        var s = getTextSizeForBounds(bounds);
        if (s == -1)
            // this path item is too small!
            return;
        // make the textFrame
        var textFrame = doc.textFrames.pointText([0, 0]);
        textFrame.contents = String(num);
        textFrame.textRange.size = s;
        // move to numbers layer
        textFrame.move(doc.layers['Numbers'].layers['Numbers ' + num], ElementPlacement.PLACEATEND);
        // position in center of path item bounds
        var c1 = centerOfBounds(bounds),
            c2 = centerOfBounds(textFrame.geometricBounds),
            d = differenceBetweenPoints(c1, c2);
        textFrame.translate(d[0], d[1]);
    };

    function getTextSizeForBounds(bounds) {
        // minimum of width and height
        var s = Math.min((bounds[2] - bounds[0], -(bounds[3] - bounds[1])));
        // bail out if too small
        if (s < 5)
            return -1;
        // very rough attempt to fit in desired range
        s = Math.pow(s, 2) * 0.1;
        //minimum pt size
        s = Math.max(4, s);
        // maximum pt size
        s = Math.min(s, 15);
        return s;
    }

    function centerOfBounds(bounds) {
        var l = bounds[0],
            t = bounds[1],
            r = bounds[2],
            b = bounds[3];
        return [
            l + ((r - l) / 2),
            t + ((b - t) / 2)
        ];
    };

    function differenceBetweenPoints(p1, p2) {
        return [-(p2[0] - p1[0]), -(p2[1] - p1[1])];
    };

})();

 

 

Unfortunately the result can be quite messy:

Screenshot 2022-12-19 at 09.04.06.pngexpand image

You change the "5" in the line "if (s < 5)" to adjust the cut-off threshold for drawing a number.

- Mark

 

Edit 2022-12-19: fixed bug that ignored CompoundPathItems!

Votes

Translate

Translate

Report

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 ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

LATEST

Hi again @eloisejo, I've been working on something that might help with your challenge and I've had another crack at the script, incorporating a new "findVisualCenter" function. Have a look at these screenshots, and also the attached Illustrator file and see what you think... assuming you are still interested!

- Mark

 

Edit: forgot to say... the script took 1 hour and 10 minutes to process your sample file!

 

Screenshot 2023-01-19 at 18.28.39.pngexpand imageScreenshot 2023-01-19 at 18.28.55.pngexpand image

Votes

Translate

Translate

Report

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