简体   繁体   English

检查选择是否包含图形

[英]Check if the selection contains graphics

I'm trying to write a script where part of it is to find out if there is an image in the objects selected, or the objects are empty shapes.我正在尝试编写一个脚本,其中的一部分是找出所选对象中是否有图像,或者对象是否为空形状。

I need to check if the files of the selection are all images, all shapes or are mixed.我需要检查选择的文件是否都是图像、所有形状或混合的。

I've put together this code with alerts (the final code will not be with alerts, it's for understanding purposes):我将这段代码与警报放在一起(最终代码不会带有警报,这是为了理解目的):

    if (app.selection.length == 0) {
        alert ("Nothing selected");
        }
    else {
        for (var i = 0; i < app.selection.length; i++) {
            if ((app.selection[i].graphics.length == 0) && (app.selection[i].graphics.length == 1)) {
                alert ("The files are mixed");
                }
            else if (app.selection[i].graphics.length == 1) {
                alert ("The files are images");
                }
            else if (app.selection[i].graphics.length == 0) {
                alert ("The files are shapes");
                }
            }
        }

Obviously the code is not working as intended (it works for the images and the shapes, while I already know the mixed part it's not written correctly), since my searches have been inconclusive.显然代码没有按预期工作(它适用于图像和形状,而我已经知道混合部分它没有正确编写),因为我的搜索没有定论。

What I'm looking for is: if nothing is selected (do something), else if the files are shapes and images (do something only to the shapes), else if there are only images (do something), else if there are only shapes (do something).我正在寻找的是:如果没有选择任何东西(做某事),否则如果文件是形状和图像(只对形状做某事),否则如果只有图像(做某事),否则如果只有形状(做某事)。

Can someone give me an idea on how to do it?有人可以告诉我该怎么做吗?

Basically the algo can be something like this:基本上算法可以是这样的:

var sel = app.selection;

if (sel.length == 0) alert('Nothing selected');

else {

    var images = sel.pop().graphics.length > 0;
    var mixed = false;

    while (sel.length) {
        if ((sel.pop().graphics.length > 0) != images) {
            mixed = true;
            break;
        }
    }

    if (mixed) alert('Mixed');
    else {
        if (images) alert('Images');
        else alert('Shapes');
    }
}

But it doesn't handle groups.但它不处理组。

Update更新

If you just want to get shapes (and omit images) from the selection it can be done this way:如果您只想从选择中获取形状(并省略图像),可以通过以下方式完成:

var sel = app.selection;
if (sel.length == 0) { alert('Nothing selected'); exit() }

var shapes = [];
for (var i=0; i<sel.length; i++)
    if (sel[i].graphics.length == 0) shapes.push(sel[i]);

if (shapes.length > 0) do_something(shapes);

// ----------------------------------------------------------------------
function do_something(shapes) {
    alert('Here we go...');
    app.selection = shapes;

    // do stuff...
}

Or a short variant (just to deselect images):或者一个简短的变体(只是为了取消选择图像):

var sel = app.selection;
for (var i=0; i<sel.length; i++)
    if (sel[i].graphics.length > 0) sel[i].select(SelectionOptions.REMOVE_FROM);

Update 2更新 2

Here is the code that does as follows:这是执行以下操作的代码:

  • if nothing selected > creates a white rectangle,如果没有选择 > 创建一个白色矩形,
  • if only images selected > creates a white rectangle,如果仅选择图像 > 创建一个白色矩形,
  • if only shapes selected > colors the shapes white,如果只选择形状 > colors 形状为白色,
  • if the selection is mixed > selects only the shapes and colors white.如果选择是混合的 > 仅选择形状和 colors 白色。
var sel = app.selection;

if (sel.length == 0) fn_nothing();
else {
    var images = sel.pop().graphics.length > 0;
    var mixed = false;
    while (sel.length) {
        if ((sel.pop().graphics.length > 0) != images) {
            mixed = true;
            break;
        }
    }
    if (mixed) fn_mixed();
    else {
        if (images) fn_images();
        else fn_shapes();
    }
}

function fn_nothing() {
    alert('Nothing');
    create_white_rectangle();
}

function fn_images() {
    alert('Images');
    create_white_rectangle();
}

function fn_shapes() {
    alert('Shapes');
    color_shapes_white();
}

function fn_mixed() {
    alert('Mixed');
    select_shapes();
    color_shapes_white();
}

function select_shapes() {
    var sel = app.selection;
    while (sel.length) {
        var item = sel.pop();
        if (item.graphics.length > 0)
        item.select(SelectionOptions.REMOVE_FROM)
    }
    alert('Select shapes');
}

function color_shapes_white() {
    alert('Color shapes white')
}

function create_white_rectangle() {
    alert('Create a white rectangle');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM