Skip to main content
May 20, 2016
Answered

Marking distance with path automately

  • May 20, 2016
  • 2 replies
  • 1009 views

The blue line is a path with some anchors, then I stroke it with blue brush.

What I want is marking distance lables automately like the red lables with script.

When i supply 3 parameters, the min value ,the max value and lable numbers, and select some anchor, I run the script so that the lables can be painted in a new layer.

Thanks all.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

1. Distances between anchors drawn by Pen Tool can be calculated after i choose two anchors?

I already stated that selected PathPoints can not be assessed directly via Script.

I recommend you abandon that approach, but if you insist you can employ a work-around (copy, create new Path, paste in AM code) and evaluate that newly created Path.

I choose pathpoints by mouse not via script.

                      pic1.select all points 

            pic2.select 3points of line by Direct Selection Tool

How to calculate the choosen part of line in canvas via script?

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. How to make lables along the line? Maybe Text Tool ?

One can create Type Layers via AM or DOM code.

Could you give me a demo script ( AM or DOM code ) please? I have never heard it before. Also, you could send it to my email: 279157364@qq.com


I already pointed out a work-around for evaluating the selected PathPoints:

Use ScriptingListener.plugin to record copy, create new path, paste.

The resulting Path contains only the originally selected PathPoints.

As for determining the points and type layers:

// only for open paths without curves!

// only for cornerpoints!

// add numbers along an open path that consists of straight segments;

// 2016, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var res = myDocument.resolution;

if (myDocument.pathItems.length > 0) {

// check for selected path;

var thePath = selectedPath2015 ();

if (thePath == undefined) {thePath = myDocument.pathItems[0]};

var theStart = 100;

var theEnd = 600;

var theSteps = 5;

// collect path information;

var thePath = collectPathInfoFromDesc2012 (myDocument, thePath);

////////////////////////////////////

for (var m = 0; m < thePath.length; m++) {

var theSubPath = thePath;

// determine the length;

var theLength = 0;

for (var o = 0; o < theSubPath.length - 3; o++) {

var thisLength = getDistance (theSubPath[0], theSubPath[o+1][0]);

theLength = theLength + thisLength;

};

////////////////////////////////////

var thisDist = 0;

var theTexts = new Array;

var targetDist = (theLength/theSteps);

var count = 0;

var check = false;

$.writeln("length: "+theLength+"\n"+targetDist);

var thisPoint = theSubPath[count][0];

var nextPoint = theSubPath[count+1][0];

var points = [thisPoint];

var theSubtract = 0;

////////////////////////////////////

while (check == false) {

thisDist = getDistance (thisPoint, nextPoint);

switch (true) {

// if longer;

case thisDist > (targetDist - theSubtract):

var factor = ((targetDist - theSubtract)/thisDist);

var width = nextPoint[0] - thisPoint[0];

var height = nextPoint[1] - thisPoint[1];

var newPoint = [Number(thisPoint[0])+width*factor, Number(thisPoint[1])+height*factor];

$.writeln (points.length +"___\nadd\n"+newPoint+"\n"+nextPoint+"\n");

thisPoint = newPoint;

theSubtract = 0;

points.push(newPoint);

break;

// if exact hit;

case thisDist == (targetDist - theSubtract):

if (points.length < theSteps-1) {

  points.push(nextPoint);

  count++;

  thisPoint = nextPoint;

  nextPoint = theSubPath[count+1][0];

  } else {check = true};

break;

// if shorter;

case thisDist < (targetDist - theSubtract):

theSubtract = theSubtract + thisDist;

count++;

thisPoint = nextPoint;

nextPoint = theSubPath[count+1][0];

break;

default:

break;

};

if (points.length == theSteps) {check = true};

};

// add last point;

points.push(theSubPath[theSubPath.length -3][0]);

// create type layers;

for (var x = 0; x < points.length; x++) {

theTexts.push(createTextLayer (theStart+(theEnd - theStart)/theSteps*x, points, res))

};

////////////////////////////////////

}

////////////////////////////////////

}

};

////////////////////////////////////

////// collect path info from actiondescriptor, smooth added //////

function collectPathInfoFromDesc2012 (myDocument, thePath) {

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

// based of functions from xbytor’s stdlib;

var ref = new ActionReference();

for (var l = 0; l < myDocument.pathItems.length; l++) {

  var thisPath = myDocument.pathItems;

  if (thisPath == thePath && thisPath.kind == PathKind.WORKPATH) {

  ref.putProperty(cTID("Path"), cTID("WrPt"));

  };

  if (thisPath == thePath && thisPath.kind != PathKind.WORKPATH && thisPath.kind != PathKind.VECTORMASK) {

  ref.putIndex(cTID("Path"), l + 1);

  };

  if (thisPath == thePath && thisPath.kind == PathKind.VECTORMASK) {

        var idPath = charIDToTypeID( "Path" );

        var idPath = charIDToTypeID( "Path" );

        var idvectorMask = stringIDToTypeID( "vectorMask" );

        ref.putEnumerated( idPath, idPath, idvectorMask );

  };

  };

var desc = app.executeActionGet(ref);

var pname = desc.getString(cTID('PthN'));

// create new array;

var theArray = new Array;

var pathComponents = desc.getObjectValue(cTID("PthC")).getList(sTID('pathComponents'));

// for subpathitems;

for (var m = 0; m < pathComponents.count; m++) {

  var listKey = pathComponents.getObjectValue(m).getList(sTID("subpathListKey"));

  var operation1 = pathComponents.getObjectValue(m).getEnumerationValue(sTID("shapeOperation"));

  switch (operation1) {

  case 1097098272:

  var operation = 1097098272 //cTID('Add ');

  break;

  case 1398961266:

  var operation = 1398961266 //cTID('Sbtr');

  break;

  case 1231975538:

  var operation = 1231975538 //cTID('Intr');

  break;

  default:

// case 1102:

  var operation = sTID('xor') //ShapeOperation.SHAPEXOR;

  break;

  };

// for subpathitem’s count;

  for (var n = 0; n < listKey.count; n++) {

  theArray.push(new Array);

  var points = listKey.getObjectValue(n).getList(sTID('points'));

  try {var closed = listKey.getObjectValue(n).getBoolean(sTID("closedSubpath"))}

  catch (e) {var closed = false};

// for subpathitem’s segment’s number of points;

  for (var o = 0; o < points.count; o++) {

  var anchorObj = points.getObjectValue(o).getObjectValue(sTID("anchor"));

  var anchor = [anchorObj.getUnitDoubleValue(sTID('horizontal')), anchorObj.getUnitDoubleValue(sTID('vertical'))];

  var thisPoint = [anchor];

  try {

  var left = points.getObjectValue(o).getObjectValue(cTID("Fwd "));

  var leftDirection = [left.getUnitDoubleValue(sTID('horizontal')), left.getUnitDoubleValue(sTID('vertical'))];

  thisPoint.push(leftDirection)

  }

  catch (e) {

  thisPoint.push(anchor)

  };

  try {

  var right = points.getObjectValue(o).getObjectValue(cTID("Bwd "));

  var rightDirection = [right.getUnitDoubleValue(sTID('horizontal')), right.getUnitDoubleValue(sTID('vertical'))];

  thisPoint.push(rightDirection)

  }

  catch (e) {

  thisPoint.push(anchor)

  };

  try {

  var smoothOr = points.getObjectValue(o).getBoolean(cTID("Smoo"));

  thisPoint.push(smoothOr)

  }

  catch (e) {thisPoint.push(false)};

  theArray[theArray.length - 1].push(thisPoint);

  };

  theArray[theArray.length - 1].push(closed);

  theArray[theArray.length - 1].push(operation);

  };

  };

// by xbytor, thanks to him;

function cTID (s) { return cTID || cTID = app.charIDToTypeID(s); };

function sTID (s) { return sTID || sTID = app.stringIDToTypeID(s); };

// reset;

app.preferences.rulerUnits = originalRulerUnits;

return theArray;

};

////// create a path from collectPathInfoFromDesc2012-array //////

function createPath2015(theArray, thePathsName) {

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

// thanks to xbytor;

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Path'), cTID('WrPt'));

    desc1.putReference(sTID('null'), ref1);

    var list1 = new ActionList();

for (var m = 0; m < theArray.length; m++) {

  var thisSubPath = theArray;

    var desc2 = new ActionDescriptor();

    desc2.putEnumerated(sTID('shapeOperation'), sTID('shapeOperation'), thisSubPath[thisSubPath.length - 1]);

    var list2 = new ActionList();

    var desc3 = new ActionDescriptor();

    desc3.putBoolean(cTID('Clsp'), thisSubPath[thisSubPath.length - 2]);

    var list3 = new ActionList();

for (var n = 0; n < thisSubPath.length - 2; n++) {

  var thisPoint = thisSubPath;

    var desc4 = new ActionDescriptor();

    var desc5 = new ActionDescriptor();

    desc5.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[0][0]);

    desc5.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[0][1]);

    desc4.putObject(cTID('Anch'), cTID('Pnt '), desc5);

    var desc6 = new ActionDescriptor();

    desc6.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[1][0]);

    desc6.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[1][1]);

    desc4.putObject(cTID('Fwd '), cTID('Pnt '), desc6);

    var desc7 = new ActionDescriptor();

    desc7.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[2][0]);

    desc7.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[2][1]);

    desc4.putObject(cTID('Bwd '), cTID('Pnt '), desc7);

    desc4.putBoolean(cTID('Smoo'), thisPoint[3]);

    list3.putObject(cTID('Pthp'), desc4);

  };

    desc3.putList(cTID('Pts '), list3);

    list2.putObject(cTID('Sbpl'), desc3);

    desc2.putList(cTID('SbpL'), list2);

    list1.putObject(cTID('PaCm'), desc2);

  };

    desc1.putList(cTID('T   '), list1);

    executeAction(cTID('setd'), desc1, DialogModes.NO);

// name work path;

var check = false;

var x = activeDocument.pathItems.length - 1;

while (check == false) {

if (activeDocument.pathItems.kind == PathKind.WORKPATH) {

app.activeDocument.pathItems.name = thePathsName;

var myPathItem = app.activeDocument.pathItems;

check = true

};

x--

};

app.preferences.rulerUnits = originalRulerUnits;

return myPathItem

};

////// determine selected path, updated 09.2015 //////

function selectedPath2015 () {

try {

var ref = new ActionReference();

ref.putProperty (stringIDToTypeID("property"), stringIDToTypeID("targetPathIndex"));

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var docDesc = executeActionGet(ref);

return app.activeDocument.pathItems[docDesc.getInteger(stringIDToTypeID("targetPathIndex"))]

}

catch (e) {return undefined}

};

////// get a distance between two points //////

function getDistance (pointOne, pointTwo) {

// calculate the triangle sides;

  var width = pointTwo[0] - pointOne[0];

  var height = pointTwo[1] - pointOne[1];

  var sideC = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));

  return sideC

  };

////// add text layer //////

function createTextLayer (thisText, thePoint, res) {

var docRef = activeDocument;

var myLayerRef = docRef.artLayers.add();

myLayerRef.kind = LayerKind.TEXT;

myLayerRef.name = thisText;

var myTextRef = myLayerRef.textItem;

myTextRef.kind = TextType.POINTTEXT;

myTextRef.size = 12 * 72 / res;

myTextRef.font = "Arial";

//Set text colour in RGB values

var newColor = new SolidColor();

newColor.rgb.red = 0;

newColor.rgb.green = 0;

newColor.rgb.blue = 0;

myTextRef.color = newColor;

// set the position;

myTextRef.position = [thePoint[0] * 72 / res, thePoint[1] * 72 / res];

myLayerRef.blendMode = BlendMode.NORMAL;

myLayerRef.opacity = 100;

myTextRef.useAutoLeading = false;

myTextRef.leading = 0;

myTextRef.contents = thisText;

return docRef.activeLayer

};

2 replies

c.pfaffenbichler
Community Expert
Community Expert
May 23, 2016

For determining distances on a curved Path the only practical solution I have found is a brute force approach – calculating a high number of additional point on the curve and adding up the distances of those.

If you ever want to look into that you could check out:

script for distributing smart object instances along path for testing

June 1, 2016

Thanks c.p for your script.

I modify the script for labels that are vertical to line.

But some lables are strange, they are inverse to our sight. Maybe I can make it better in some day.

June 1, 2016

Here is the script. Please try it.

// only for open paths without curves!

// only for cornerpoints!

// add numbers along an open path that consists of straight segments;

// 2016, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

    var myDocument = app.activeDocument;

    var res = myDocument.resolution;

    var myTextRefSize=12;

    if (myDocument.pathItems.length > 0) {

        // check for selected path;

        var thePath = selectedPath2015 ();

        if (thePath == undefined) {thePath = myDocument.pathItems[0]};

        var theStart = 100;

        var theEnd =1100;

        var CharacterNumbers="1100".length+1;//get length of string "theEnd"

        var theSteps =10;

        // collect path information;

        var thePath = collectPathInfoFromDesc2012 (myDocument, thePath);

        var RotateAngle=new Array;

        ////////////////////////////////////

        for (var m = 0; m < thePath.length; m++) {

            var theSubPath = thePath;

            // determine the length;

            var theLength = 0;

            for (var o = 0; o < theSubPath.length - 3; o++) {

$.writeln("theSubPath.length= "+theSubPath.length+"\n");

                var thisLength = getDistance (theSubPath[0], theSubPath[o+1][0]);

                theLength = theLength + thisLength;

            };

            ////////////////////////////////////

            var thisDist = 0;

            var theTexts = new Array;

            //var RotateAngle=new Array;

            var targetDist = (theLength/theSteps);

            var count = 0;

            var check = false;

$.writeln("length= "+theLength+"\n"+"targetDist="+targetDist);

            var thisPoint = theSubPath[count][0];

            var nextPoint = theSubPath[count+1][0];

            var currentRotateAngle_0=CalculateSlope (thisPoint, nextPoint);//calculate the rotating angle;

            var points = [thisPoint];  //points is a queue.

            var theSubtract = 0;

            ////////////////////////////////////s

            while (check == false) {

                thisDist = getDistance (thisPoint, nextPoint);

$.writeln("thisDist: "+thisDist);

                switch (true) {

                    // if longer;

                    case thisDist > (targetDist - theSubtract):

                        var factor = ((targetDist - theSubtract)/thisDist);                      

                        var width = nextPoint[0] - thisPoint[0];

                        var height = nextPoint[1] - thisPoint[1];

                        var newPoint = [Number(thisPoint[0])+width*factor, Number(thisPoint[1])+height*factor];

                        var currentRotateAngle=CalculateSlope (thisPoint, nextPoint);//calculate the rotating angle;

                        var number=Number(points.length);

                        RotateAngle[number]=currentRotateAngle;

                        if (number==1){

                            RotateAngle[0]=currentRotateAngle_0;                           

                        }

  $.writeln (points.length +"___\nadd\n"+newPoint+"\n"+nextPoint+"\n");

                        thisPoint = newPoint;

                        theSubtract = 0;

                        points.push(newPoint);

                    break;

                    // if exact hit;

                    case thisDist == (targetDist - theSubtract):

                        if (points.length < theSteps-1) {

                            points.push(nextPoint);

                            count++;

                            thisPoint = nextPoint;

                            nextPoint = theSubPath[count+1][0];

                        } else {check = true};

                    break;

                    // if shorter;

                    case thisDist < (targetDist - theSubtract):

                        theSubtract = theSubtract + thisDist;

                        count++;

                        thisPoint = nextPoint;

                        nextPoint = theSubPath[count+1][0];

                    break;

                    default:

                    break;

                };

                if (points.length == theSteps) {

                    RotateAngle[theSteps]=CalculateSlope (theSubPath[theSubPath.length -3][0], theSubPath[theSubPath.length -4][0]);

                    check = true};

$.writeln ("RotateAngle:"+RotateAngle+"\n");

            };

            // add last point;

            points.push(theSubPath[theSubPath.length -3][0]);

            // create type layers;

            for (var x = 0; x < points.length; x++) {

                 // $.writeln("points.length="+points.length);

                 var offset=new Array;

                 offset=DetermineOffset (CharacterNumbers, myTextRefSize, RotateAngle);

                 var offset_x=Number(offset[0]);

                 var offset_y=Number(offset[1]);

                theTexts.push(createTextLayer ("-"+(theStart+(theEnd - theStart )/theSteps*x), myTextRefSize, points, res, offset_x, offset_y ))

                //theTexts.push(createTextLayer ("-"+(theStart+(theEnd - theStart )/theSteps*x), myTextRefSize, points, res, 0, 0 ))

                RotateFixedAngle (RotateAngle);

            };

        ////////////////////////////////////

        }

    ////////////////////////////////////

    }

};

////////////////////////////////////

////// collect path info from actiondescriptor, smooth added //////

function collectPathInfoFromDesc2012 (myDocument, thePath) {

    var originalRulerUnits = app.preferences.rulerUnits;

    app.preferences.rulerUnits = Units.POINTS;

    // based of functions from xbytor’s stdlib;

    var ref = new ActionReference();

    for (var l = 0; l < myDocument.pathItems.length; l++) {

        var thisPath = myDocument.pathItems;

        if (thisPath == thePath && thisPath.kind == PathKind.WORKPATH) {

            ref.putProperty(cTID("Path"), cTID("WrPt"));

        };

        if (thisPath == thePath && thisPath.kind != PathKind.WORKPATH && thisPath.kind != PathKind.VECTORMASK) {

            ref.putIndex(cTID("Path"), l + 1);

        };

        if (thisPath == thePath && thisPath.kind == PathKind.VECTORMASK) {

            var idPath = charIDToTypeID( "Path" );

            var idPath = charIDToTypeID( "Path" );

            var idvectorMask = stringIDToTypeID( "vectorMask" );

            ref.putEnumerated( idPath, idPath, idvectorMask );

        };

    };

    var desc = app.executeActionGet(ref);

    var pname = desc.getString(cTID('PthN'));

    // create new array;

    var theArray = new Array;

    var pathComponents = desc.getObjectValue(cTID("PthC")).getList(sTID('pathComponents'));

    // for subpathitems;

    for (var m = 0; m < pathComponents.count; m++) {

        var listKey = pathComponents.getObjectValue(m).getList(sTID("subpathListKey"));

        var operation1 = pathComponents.getObjectValue(m).getEnumerationValue(sTID("shapeOperation"));

        switch (operation1) {

            case 1097098272:

            var operation = 1097098272 //cTID('Add ');

            break;

            case 1398961266:

            var operation = 1398961266 //cTID('Sbtr');

            break;

            case 1231975538:

            var operation = 1231975538 //cTID('Intr');

            break;

            default:

            // case 1102:

            var operation = sTID('xor') //ShapeOperation.SHAPEXOR;

            break;

        };

        // for subpathitem’s count;

        for (var n = 0; n < listKey.count; n++) {

            theArray.push(new Array);

            var points = listKey.getObjectValue(n).getList(sTID('points'));

            try {var closed = listKey.getObjectValue(n).getBoolean(sTID("closedSubpath"))}

            catch (e) {var closed = false};

            // for subpathitem’s segment’s number of points;

            for (var o = 0; o < points.count; o++) {

                var anchorObj = points.getObjectValue(o).getObjectValue(sTID("anchor"));

                var anchor = [anchorObj.getUnitDoubleValue(sTID('horizontal')), anchorObj.getUnitDoubleValue(sTID('vertical'))];

                var thisPoint = [anchor];

                try {

                    var left = points.getObjectValue(o).getObjectValue(cTID("Fwd "));

                    var leftDirection = [left.getUnitDoubleValue(sTID('horizontal')), left.getUnitDoubleValue(sTID('vertical'))];

                    thisPoint.push(leftDirection)

                }

                catch (e) {

                    thisPoint.push(anchor)

                };

                try {

                    var right = points.getObjectValue(o).getObjectValue(cTID("Bwd "));

                    var rightDirection = [right.getUnitDoubleValue(sTID('horizontal')), right.getUnitDoubleValue(sTID('vertical'))];

                    thisPoint.push(rightDirection)

                }

                catch (e) {

                    thisPoint.push(anchor)

                };

                try {

                    var smoothOr = points.getObjectValue(o).getBoolean(cTID("Smoo"));

                    thisPoint.push(smoothOr)

                }

                catch (e) {thisPoint.push(false)};

                    theArray[theArray.length - 1].push(thisPoint);

            };

            theArray[theArray.length - 1].push(closed);

            theArray[theArray.length - 1].push(operation);

        };

    };

    // by xbytor, thanks to him;

    function cTID (s) { return cTID || cTID = app.charIDToTypeID(s); };

    function sTID (s) { return sTID || sTID = app.stringIDToTypeID(s); };

    // reset;

    app.preferences.rulerUnits = originalRulerUnits;

    return theArray;

};

////// create a path from collectPathInfoFromDesc2012-array //////

function createPath2015(theArray, thePathsName) {

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

// thanks to xbytor;

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Path'), cTID('WrPt'));

    desc1.putReference(sTID('null'), ref1);

    var list1 = new ActionList();

for (var m = 0; m < theArray.length; m++) {

  var thisSubPath = theArray;

    var desc2 = new ActionDescriptor();

    desc2.putEnumerated(sTID('shapeOperation'), sTID('shapeOperation'), thisSubPath[thisSubPath.length - 1]);

    var list2 = new ActionList();

    var desc3 = new ActionDescriptor();

    desc3.putBoolean(cTID('Clsp'), thisSubPath[thisSubPath.length - 2]);

    var list3 = new ActionList();

for (var n = 0; n < thisSubPath.length - 2; n++) {

  var thisPoint = thisSubPath;

    var desc4 = new ActionDescriptor();

    var desc5 = new ActionDescriptor();

    desc5.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[0][0]);

    desc5.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[0][1]);

    desc4.putObject(cTID('Anch'), cTID('Pnt '), desc5);

    var desc6 = new ActionDescriptor();

    desc6.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[1][0]);

    desc6.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[1][1]);

    desc4.putObject(cTID('Fwd '), cTID('Pnt '), desc6);

    var desc7 = new ActionDescriptor();

    desc7.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[2][0]);

    desc7.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[2][1]);

    desc4.putObject(cTID('Bwd '), cTID('Pnt '), desc7);

    desc4.putBoolean(cTID('Smoo'), thisPoint[3]);

    list3.putObject(cTID('Pthp'), desc4);

  };

    desc3.putList(cTID('Pts '), list3);

    list2.putObject(cTID('Sbpl'), desc3);

    desc2.putList(cTID('SbpL'), list2);

    list1.putObject(cTID('PaCm'), desc2);

  };

    desc1.putList(cTID('T   '), list1);

    executeAction(cTID('setd'), desc1, DialogModes.NO);

// name work path;

var check = false;

var x = activeDocument.pathItems.length - 1;

while (check == false) {

    if (activeDocument.pathItems.kind == PathKind.WORKPATH) {

        app.activeDocument.pathItems.name = thePathsName;

        var myPathItem = app.activeDocument.pathItems;

        check = true

    };

x--

};

app.preferences.rulerUnits = originalRulerUnits;

return myPathItem

};

////// determine selected path, updated 09.2015 //////

function selectedPath2015 () {

    try {

        var ref = new ActionReference();

            ref.putProperty (stringIDToTypeID("property"), stringIDToTypeID("targetPathIndex"));

            ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

        var docDesc = executeActionGet(ref);

        return app.activeDocument.pathItems[docDesc.getInteger(stringIDToTypeID("targetPathIndex"))]

    }

    catch (e) {return undefined}

};

////// get a distance between two points //////

function getDistance (pointOne, pointTwo) {

// calculate the triangle sides;

  var width = pointTwo[0] - pointOne[0];

  var height = pointTwo[1] - pointOne[1];

  var sideC = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));

  return sideC

};

////// add text layer //////

function createTextLayer (thisText,myTextRefSize, thePoint, res,offset_x,offset_y) {

    var docRef = activeDocument;

    var myLayerRef = docRef.artLayers.add();

    myLayerRef.kind = LayerKind.TEXT;

    myLayerRef.name = thisText;

    var myTextRef = myLayerRef.textItem;

    myTextRef.kind = TextType.POINTTEXT;

        myTextRef.direction=Direction.VERTICAL;

   

    myTextRef.size = myTextRefSize * 72 / res;

    myTextRef.font = "Arial";

    //Set text colour in RGB values

    var newColor = new SolidColor();

    newColor.rgb.red = 0;

    newColor.rgb.green = 0;

    newColor.rgb.blue = 0;

    myTextRef.color = newColor;

    //Set number of characters in string to variable n.

    // set the position;

   //offset_x=Number(offset_x);

   //offset_y=Number(offset_y);

    //myTextRef.position = [((thePoint[0]) +myTextRefSize)* 72 / res, (thePoint[1] +5)* 72 / res];

    myTextRef.position = [(thePoint[0]-offset_x)* 72 / res, (thePoint[1] +offset_y)* 72 / res];

    myLayerRef.blendMode = BlendMode.NORMAL;

    myLayerRef.opacity = 100;

    myTextRef.useAutoLeading = false;

    myTextRef.leading = 0;

    myTextRef.contents = thisText;

    return docRef.activeLayer

};

// =======================================================

//角度转成弧度  

function angle2rad(angle){

    var rad;

    rad=angle*(Math.PI)/180;

    return rad;

    }  

   //弧度转成角度

function rad2angle(rad) {

        var angle;

        angle=rad*180/(Math.PI);

        return angle;

       

};

//计算两点间直线的斜率

function CalculateSlope(FirstPoint, SecondPoint){

SlopeAngle_rad=Math.atan((FirstPoint[1]-SecondPoint[1])/(FirstPoint[0]-SecondPoint[0]));

SlopeAngle_angle_1=rad2angle(SlopeAngle_rad);

$.writeln("SlopeAngle_angle= "+SlopeAngle_angle_1+"\n");

return SlopeAngle_angle_1

}

//偏移函数       

function DetermineOffset(n, myTextRefSize, SlopeAngle_angle){

    var Offset_x;

    var Offset_y;

    var Offset=new Array;

  

    //直线与x轴正向的夹角在0-90之间,offset方向为右上方

    if (SlopeAngle_angle>=0&&SlopeAngle_angle<90){

        SlopeAngle_angle=angle2rad (SlopeAngle_angle);

        Offset_x=(n+1)*myTextRefSize*Math.sin(SlopeAngle_angle)/4;

        Offset_y=(-1)*((n+1)*myTextRefSize*(1-Math.sin(Math.PI/2-SlopeAngle_angle))/4);

        };

   

    else if (SlopeAngle_angle<0){

        SlopeAngle_angle=(-1)*SlopeAngle_angle;

        SlopeAngle_angle=angle2rad (SlopeAngle_angle);

        Offset_x=(-1)*(n+1)*myTextRefSize*Math.sin(SlopeAngle_angle)/4;

        Offset_y=(-1)*((n+1)*myTextRefSize*(1-Math.sin(Math.PI/2-SlopeAngle_angle))/4);

        }

    else{

        //Alert("线位是否有问题?按默认情况进行标注");

        SlopeAngle_angle=angle2rad (SlopeAngle_angle);

        Offset_x=(n+1)*myTextRefSize*Math.sin(SlopeAngle_angle)/4;

        Offset_y=(-1)*((n+1)*myTextRefSize*(1-Math.sin(Math.PI/2-SlopeAngle_angle))/4);       

        }

     Offset=[Offset_x, Offset_y];

    return  Offset;

}

//对text文本进行角度旋转

function RotateFixedAngle(FixedAngle){

    var idTrnf = charIDToTypeID( "Trnf" );

    var desc3 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref2 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref2.putEnumerated( idLyr, idOrdn, idTrgt );

    desc3.putReference( idnull, ref2 );

    var idFTcs = charIDToTypeID( "FTcs" );

    var idQCSt = charIDToTypeID( "QCSt" );

    var idQcsa = charIDToTypeID( "Qcsa" );

    desc3.putEnumerated( idFTcs, idQCSt, idQcsa );

    var idOfst = charIDToTypeID( "Ofst" );

        var desc4 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc4.putUnitDouble( idHrzn, idPxl, -0.000000 );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc4.putUnitDouble( idVrtc, idPxl, -0.000000 );

    var idOfst = charIDToTypeID( "Ofst" );

    desc3.putObject( idOfst, idOfst, desc4 );

    var idAngl = charIDToTypeID( "Angl" );

    var idAng = charIDToTypeID( "#Ang" );

    desc3.putUnitDouble( idAngl, idAng, FixedAngle );

    var idIntr = charIDToTypeID( "Intr" );

    var idIntp = charIDToTypeID( "Intp" );

    var idBcbc = charIDToTypeID( "Bcbc" );

    desc3.putEnumerated( idIntr, idIntp, idBcbc );

executeAction( idTrnf, desc3, DialogModes.NO );

}

c.pfaffenbichler
Community Expert
Community Expert
May 20, 2016

Selected PathPoints cannot be directly evaluated via Script, selected Paths can be – why would you need to select one anchor?

Are all the PathPoints CornerPoints without left and right bezier handles (or bezier handles of length 0)?

When i supply 3 parameters, the min value ,the max value and lable numbers

How do the values relate to the numbers?

What should the numbers indicate exactly (the length of the path, the length over the x-axis, …)?

Please explain the process in more detail.

May 21, 2016

1. Selecting the pathpoints: Firstly choosing some pathpoints with Path selection tools( shortcut key: a), not with script. Then I want to run the script to create number lables.

2. Direction of line: Anchors are created from right to left. The whole line is a stright line from the whole sight.  Order of lables are the same with anchors' order-- from right to left( small number is always at the right of line, and big number is at the left one).

3. 3 parameters. The 3 parameters can be understood as the scale of line. When I give 3 parameters, the scale is certain. For example [0 10000 10 ], the step is (p2-p1)/p3=1000, the labels are [0 1000 2000 ... 10000]. In my future work, the p3 may be step, so ,the third one can be replaced by step. 3 paras can be [0 10000 1000 ] with 10+1=11 lables or [0 100 20] with 5+1=6 lables.

4.Number indication: the number of lables indicates exactly the length of the path, not x-axis.

c.pfaffenbichler
Community Expert
Community Expert
May 21, 2016

I still don’t get it.

Please post screenshots illustrating the steps of the intended process (and what the path/s are like originally, what is getting selected and how that determines the final result).