7 Replies Latest reply on Nov 21, 2017 4:39 AM by OMOTI

    Selecting by stroke color?

    BradB73

      Forgive me, this is my first attempt at truly working with any sort of scripting and it has been an interesting week. I've been trying to work up something for some of my production guys that will be a script for Illustrator that will work with 'plotter' type equipment. 

       

      What I am wanting to do is to take the flattened .ai file (Layer 1) that has been output and remove any clipping masks, then create the four necesary layers for our cutters that they need.  From there, separate the stroke colors into the respective layers. For example, the strokes colored with the 'Cut swatch' would go into a newly created 'knife' layer, Crease will go into 'Crease layer', etc.

       

      So far, I have everything working else working, but cannot figure out how to select the paths by their swatch color and move them into the layers I have created.  Everything I have tried from reading online (or stealing other peoples) doesn't seem to be working and I am afraid I don't know enough to figure out what I need to do. 

        • 1. Re: Selecting by stroke color?
          williamadowling Level 4
          function container()
          {
              var docRef = app.activeDocument;
              var layers = docRef.layersj
              var myPaths = docRef.pathItems;
              var knifeLayer = layers["knife"];
              var creaseLayer = layers["crease"];
          
          
              var pathLen = myPaths.length;
              var myPath;
              for(var x=pathLen-1;x>=0;x--)
              {
                  myPath = myPaths[x];
                  if(myPath.stroked && myPath.strokeColor.spot)
                  {
                      if(myPath.strokeColor.spot.name === "Cut swatch")
                      {
                          myPath.moveToBeginning(knifeLayer);
                      }
                      else if(myPath.strokeColor.spot.name === "Crease")
                      {
                          myPath.moveToBeginning(creaseLayer);
                      }
                  }
              }
          }
          container();
          
          • 2. Re: Selecting by stroke color?
            BradB73 Level 1

            Awesome!  Thanks for the help William.  That has me on the right path (no pun intended).

             

            Got one question though. After running the script I still have just a few random paths on Layer 1. I looked at the paths and they are all showing as <Path> and aren't part of any group or Compound Path.  I checked their color and they share the same colors as the ones that the script moved successfully and also have the same stroke attributes as far as I can tell.  It doesn't seem to be limited to Crease or Cut (seems to be a little of both) or the file - I have tried it with a couple files now and get the same results - the file .  I got no errors when running the script. Any thoughts?

            • 3. Re: Selecting by stroke color?
              williamadowling Level 4

              can you share the file so I can do some debugging?

              • 4. Re: Selecting by stroke color?
                BradB73 Level 1

                Yes, I put the script along with a couple working files I have been using to test in a dropbox folder.  Here is a link:

                 

                https://www.dropbox.com/sh/x9idapvf5s1983g/AAAH5wBVuji2aZDI2mPC4tUma?dl=0

                 

                One thing I had to change was the swatch color.  Per their workflow I had to change the names.  You will see that when you look at it.  However, it found the swatches in the files and ran with no problems.  Only thing was what I mentioned.

                 

                Thank you again for looking at it.

                • 5. Re: Selecting by stroke color?
                  OMOTI Level 2

                  Hi, BradB73.

                   

                  You may be Kongsberg and ArtiosCAD user.

                  I made the process of ungrouping and dismantling compoundItems simpler.

                  However, the selected state can not be kept.

                  You can use it by customizing the presets in the main function.

                  Try this:

                   

                  #target illustrator
                  
                  (function () {
                     
                      function dismantleCompoundPathItem() {
                          var doc = app.activeDocument;
                          app.executeMenuCommand("selectall");
                          app.executeMenuCommand("noCompoundPath");
                      }
                     
                      function ungroup() {
                          var doc = app.activeDocument;
                          app.executeMenuCommand("selectall");
                          app.executeMenuCommand("releaseMask");
                          app.executeMenuCommand("ungroup");
                      }
                     
                      function classify(pathItem, preset) {
                          var i,
                              max;
                          for (i = 0, max = preset.length; i < max; i += 1) {
                              if (pathItem.strokeColor.spot.name === preset[i].spotName) {
                                  pathItem.moveToBeginning(app.activeDocument.layers[preset[i].layerName]);
                              }
                          }
                      }
                     
                      function main() {
                          var preset = [
                                  {
                                      spotName: "Camera",
                                      layerName: "iCut"
                                  },
                                  {
                                      spotName: "[Registration]",
                                      layerName: "Registration Marks"
                                  },
                                  {
                                      spotName: "Artios Cut",
                                      layerName: "Knife"
                                  },
                                  {
                                      spotName: "Artios Crease",
                                      layerName: "Crease"
                                  }
                              ],
                              doc,
                              layer,
                              pathItem,
                              i,
                              max;
                         
                          // VALIDATE DOCUMENT
                          if (app.documents.length === 0) {
                              return;
                          }
                         
                          doc = app.activeDocument;
                          // DISMANTLE COMPOUNDPATHITEMS
                          if (0 < doc.compoundPathItems.length) {
                              dismantleCompoundPathItem();
                          }
                         
                          // CLEAR CLIPPING PATH AND UNGROUP
                          while (0 < doc.groupItems.length) {
                              ungroup();
                          }
                         
                          // CREATE LAYERS FOR CAD TABLES
                          for (i = 0, max = preset.length; i < max; i += 1) {
                              layer = doc.layers.add();
                              layer.name = preset[i].layerName;
                          }
                         
                          // MOVE THE STROKES TO NEW CAD LAYERS
                          for (i = 0, max = doc.pathItems.length; i < max; i += 1) {
                              pathItem = doc.pathItems[i];
                              if (pathItem.stroked && pathItem.strokeColor.spot) {
                                  classify(pathItem, preset);
                              }
                          }
                         
                          doc.selection = null;
                      }
                     
                      main();
                  
                  }());
                  
                  • 6. Re: Selecting by stroke color?
                    BradB73 Level 1

                    Thank you sir!  Yes you are correct, we use ArtiosCAD + Kongsberg as well as the Esko Automation Engine.  The Pilot Workflow is what we have setup to generate our cut files from the art separating the print, production art, and the CAD but it ends up generating a flattened mess for the cutting table with the art embedded.  This script works beautifully.  Thanks again.

                     

                    Can you explain briefly what you fixed?  I may end up adding another color in the future (line type) and I would like to know for my own knowledge.

                    • 7. Re: Selecting by stroke color?
                      OMOTI Level 2

                      You have a wonderful system. I would like to try operating the Esko Automation Engine.

                       

                      ~ about JavaScript ~

                      I think it is a problem that variables with the same name are scattered. If you keep in mind that simplifying the scope of variables and changing the properties of Illustrator DOM objects every time you manipulate, it will work with the same previous approach (your 'CURRENT.jsx').

                       

                      ~ my code ~

                      // DISMANTLE COMPOUNDPATHITEMS

                      Since it is a hard work, I changed it to a menu command.

                       

                      // CLEAR CLIPPING PATH AND UNGROUP

                      Continue ungrouping while group objects exist in the document.

                      Since it is a hard work, I changed it to a menu command.

                       

                      ~ adding new line type (Vibra Knife) and removing line type (Knife)~

                      Change preset in the main function.

                       

                      var preset = [

                              {

                                  spotName: "Camera",

                                  layerName: "iCut"

                              },

                              {

                                  spotName: "[Registration]",

                                  layerName: "Registration Marks"

                              },

                              {

                                  spotName: "Artios Crease",

                                  layerName: "Crease"

                              },

                              {

                                  spotName: "Artios Vibra",

                                  layerName: "Vibra Knife"

                              }

                          ],

                       

                      Before executing the script, keep all of the objects editable by unlock and visible.