Skip to main content
greless
Inspiring
December 17, 2019
Answered

how to draw a shape Path

  • December 17, 2019
  • 1 reply
  • 1459 views

hi everybody,

    I used the following code to draw the path

 

DrawShape([100, 100], [100, 250], [200, 200], [200, 100]);

function DrawShape() {
    var doc = app.activeDocument;
    var y = arguments.length;
    var i = 0; 
    var lineArray = [];

    for (i = 0; i < y; i++) {
        lineArray[i] = new PathPointInfo;
        lineArray[i].kind = PointKind.CORNERPOINT;
        lineArray[i].anchor = arguments[i];
        lineArray[i].leftDirection = lineArray[i].anchor;
        lineArray[i].rightDirection = lineArray[i].anchor;
    }

    var lineSubPathArray = new SubPathInfo();
    lineSubPathArray.closed = true;
    lineSubPathArray.operation = ShapeOperation.SHAPEADD;
    lineSubPathArray.entireSubPath = lineArray;

    var myPathItem = doc.pathItems.add("myPath", [lineSubPathArray]);
    var mySolidColor= new SolidColor();
    mySolidColor.rgb.hexValue="ff0000";
    myPathItem.fillPath(mySolidColor,ColorBlendMode.NORMAL,100,true,0,true,true);
     myPathItem.strokePath(ToolType.SHARPEN);
    myPathItem.remove();
}

The results are as follows,

 

but ,i want to getting a shape layer,How do I ?(use PathItem,PathPoint,PathPointInfo......)

 

 

This topic has been closed for replies.
Correct answer greless

I think it's better to fill it as a solid color adjustment layer

 DrawShape([100, 100], [100, 200], [200, 200], [200, 100]);
    
    function DrawShape() 
    {
     try {
        var doc = app.activeDocument;
        var y = arguments.length;
        var i = 0;
        var lineArray = [];

        for (i = 0; i < y; i++) {
            lineArray[i] = new PathPointInfo;
            lineArray[i].kind = PointKind.CORNERPOINT;
            lineArray[i].anchor = arguments[i];
            lineArray[i].leftDirection = lineArray[i].anchor;
           lineArray[i].rightDirection = lineArray[i].anchor;
        }
    
        var lineSubPathArray = new SubPathInfo();
        lineSubPathArray.closed = true;
        lineSubPathArray.operation = ShapeOperation.SHAPEADD;
        lineSubPathArray.entireSubPath = lineArray;
        var myPathItem = doc.pathItems.add("myPath", [lineSubPathArray]);
      
         myPathItem.strokePath(ToolType.PENCIL);
         MakeSolidColor(255,0,0);
         myPathItem.remove();
      //   SetAlignEdges();
     
    }catch (e) { if (e.number!=8007) { alert("Line: "+e.line+"\n\n"+e,"Bug!",true); throw(e); } }
    
}
///////////////////////////////////////////////////////////////////////////////
 function MakeSolidColor(red,green,blue)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putClass(stringIDToTypeID("contentLayer"));
        d.putReference(stringIDToTypeID("null"), r);
        var d1 = new ActionDescriptor();
        var d2 = new ActionDescriptor();
        var d3 = new ActionDescriptor();
        d3.putDouble(stringIDToTypeID("red"), red);
        d3.putDouble(stringIDToTypeID("green"), green);
        d3.putDouble(stringIDToTypeID("blue"), blue);
        d2.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d3);
        d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("solidColorLayer"), d2);
        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("contentLayer"), d1);
        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
        }
    catch (e) { if (e.number!=8007) { alert("Line: "+e.line+"\n\n"+e,"Bug!",true); throw(e); } }
    }

 

1 reply

Chuck Uebele
Community Expert
Community Expert
December 17, 2019

Try this:

var mySolidColor= new SolidColor();
mySolidColor.rgb.hexValue="ff0000";

DrawShape([100, 100], [100, 250], [200, 200], [200, 100]);


function DrawShape() {
    var doc = app.activeDocument;
    var y = arguments.length;
    var i = 0; 
    var lineArray = [];

    for (i = 0; i < y; i++) {
        lineArray[i] = new PathPointInfo;
        lineArray[i].kind = PointKind.CORNERPOINT;
        lineArray[i].anchor = arguments[i];
        lineArray[i].leftDirection = lineArray[i].anchor;
        lineArray[i].rightDirection = lineArray[i].anchor;
    }

    var lineSubPathArray = new SubPathInfo();
    lineSubPathArray.closed = true;
    lineSubPathArray.operation = ShapeOperation.SHAPEADD;
    lineSubPathArray.entireSubPath = lineArray;

    var myPathItem = doc.pathItems.add("myPath", [lineSubPathArray]);

    //myPathItem.fillPath(mySolidColor,ColorBlendMode.NORMAL,100,true,0,true,true);
    //myPathItem.strokePath(ToolType.SHARPEN);
    makeShape ()
    myPathItem.remove();
}

function makeShape(){
    var idMk = charIDToTypeID( "Mk  " );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idcontentLayer = stringIDToTypeID( "contentLayer" );
            ref1.putClass( idcontentLayer );
        desc2.putReference( idnull, ref1 );
        var idUsng = charIDToTypeID( "Usng" );
            var desc3 = new ActionDescriptor();
            var idType = charIDToTypeID( "Type" );
                var desc4 = new ActionDescriptor();
                var idClr = charIDToTypeID( "Clr " );
                    var desc5 = new ActionDescriptor();
                    var idRd = charIDToTypeID( "Rd  " );
                    desc5.putDouble( idRd, mySolidColor.rgb.red );
                    var idGrn = charIDToTypeID( "Grn " );
                    desc5.putDouble( idGrn, mySolidColor.rgb.green );
                    var idBl = charIDToTypeID( "Bl  " );
                    desc5.putDouble( idBl, mySolidColor.rgb.blue );
                var idRGBC = charIDToTypeID( "RGBC" );
                desc4.putObject( idClr, idRGBC, desc5 );
            var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
            desc3.putObject( idType, idsolidColorLayer, desc4 );
            var idShp = charIDToTypeID( "Shp " );
                var desc6 = new ActionDescriptor();
                var idTrgp = charIDToTypeID( "Trgp" );
                var idPthK = charIDToTypeID( "PthK" );
                var idTrgp = charIDToTypeID( "Trgp" );
                desc6.putEnumerated( idTrgp, idPthK, idTrgp );
            var idpathClass = stringIDToTypeID( "pathClass" );
            desc3.putObject( idShp, idpathClass, desc6 );
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
                var desc7 = new ActionDescriptor();
                var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
                desc7.putInteger( idstrokeStyleVersion, 2 );
                var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
                desc7.putBoolean( idstrokeEnabled, false );
                var idfillEnabled = stringIDToTypeID( "fillEnabled" );
                desc7.putBoolean( idfillEnabled, true );
                var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc7.putUnitDouble( idstrokeStyleLineWidth, idPxl, 1.000000 );
                var idstrokeStyleLineDashOffset = stringIDToTypeID( "strokeStyleLineDashOffset" );
                var idPnt = charIDToTypeID( "#Pnt" );
                desc7.putUnitDouble( idstrokeStyleLineDashOffset, idPnt, 0.000000 );
                var idstrokeStyleMiterLimit = stringIDToTypeID( "strokeStyleMiterLimit" );
                desc7.putDouble( idstrokeStyleMiterLimit, 100.000000 );
                var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" );
                var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" );
                var idstrokeStyleButtCap = stringIDToTypeID( "strokeStyleButtCap" );
                desc7.putEnumerated( idstrokeStyleLineCapType, idstrokeStyleLineCapType, idstrokeStyleButtCap );
                var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
                var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
                var idstrokeStyleMiterJoin = stringIDToTypeID( "strokeStyleMiterJoin" );
                desc7.putEnumerated( idstrokeStyleLineJoinType, idstrokeStyleLineJoinType, idstrokeStyleMiterJoin );
                var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
                var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
                var idstrokeStyleAlignInside = stringIDToTypeID( "strokeStyleAlignInside" );
                desc7.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, idstrokeStyleAlignInside );
                var idstrokeStyleScaleLock = stringIDToTypeID( "strokeStyleScaleLock" );
                desc7.putBoolean( idstrokeStyleScaleLock, false );
                var idstrokeStyleStrokeAdjust = stringIDToTypeID( "strokeStyleStrokeAdjust" );
                desc7.putBoolean( idstrokeStyleStrokeAdjust, false );
                var idstrokeStyleLineDashSet = stringIDToTypeID( "strokeStyleLineDashSet" );
                    var list1 = new ActionList();
                desc7.putList( idstrokeStyleLineDashSet, list1 );
                var idstrokeStyleBlendMode = stringIDToTypeID( "strokeStyleBlendMode" );
                var idBlnM = charIDToTypeID( "BlnM" );
                var idNrml = charIDToTypeID( "Nrml" );
                desc7.putEnumerated( idstrokeStyleBlendMode, idBlnM, idNrml );
                var idstrokeStyleOpacity = stringIDToTypeID( "strokeStyleOpacity" );
                var idPrc = charIDToTypeID( "#Prc" );
                desc7.putUnitDouble( idstrokeStyleOpacity, idPrc, 100.000000 );
                var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" );
                    var desc8 = new ActionDescriptor();
                    var idClr = charIDToTypeID( "Clr " );
                        var desc9 = new ActionDescriptor();
                        var idRd = charIDToTypeID( "Rd  " );
                        desc9.putDouble( idRd, mySolidColor.rgb.red );
                        var idGrn = charIDToTypeID( "Grn " );
                        desc9.putDouble( idGrn, mySolidColor.rgb.green );
                        var idBl = charIDToTypeID( "Bl  " );
                        desc9.putDouble( idBl, mySolidColor.rgb.blue );
                    var idRGBC = charIDToTypeID( "RGBC" );
                    desc8.putObject( idClr, idRGBC, desc9 );
                var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
                desc7.putObject( idstrokeStyleContent, idsolidColorLayer, desc8 );
                var idstrokeStyleResolution = stringIDToTypeID( "strokeStyleResolution" );
                desc7.putDouble( idstrokeStyleResolution, 72.000000 );
            var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
            desc3.putObject( idstrokeStyle, idstrokeStyle, desc7 );
        var idcontentLayer = stringIDToTypeID( "contentLayer" );
        desc2.putObject( idUsng, idcontentLayer, desc3 );
        var idLyrI = charIDToTypeID( "LyrI" );
        desc2.putInteger( idLyrI, 3 );
    executeAction( idMk, desc2, DialogModes.NO );
    }
greless
grelessAuthorCorrect answer
Inspiring
December 18, 2019

I think it's better to fill it as a solid color adjustment layer

 DrawShape([100, 100], [100, 200], [200, 200], [200, 100]);
    
    function DrawShape() 
    {
     try {
        var doc = app.activeDocument;
        var y = arguments.length;
        var i = 0;
        var lineArray = [];

        for (i = 0; i < y; i++) {
            lineArray[i] = new PathPointInfo;
            lineArray[i].kind = PointKind.CORNERPOINT;
            lineArray[i].anchor = arguments[i];
            lineArray[i].leftDirection = lineArray[i].anchor;
           lineArray[i].rightDirection = lineArray[i].anchor;
        }
    
        var lineSubPathArray = new SubPathInfo();
        lineSubPathArray.closed = true;
        lineSubPathArray.operation = ShapeOperation.SHAPEADD;
        lineSubPathArray.entireSubPath = lineArray;
        var myPathItem = doc.pathItems.add("myPath", [lineSubPathArray]);
      
         myPathItem.strokePath(ToolType.PENCIL);
         MakeSolidColor(255,0,0);
         myPathItem.remove();
      //   SetAlignEdges();
     
    }catch (e) { if (e.number!=8007) { alert("Line: "+e.line+"\n\n"+e,"Bug!",true); throw(e); } }
    
}
///////////////////////////////////////////////////////////////////////////////
 function MakeSolidColor(red,green,blue)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putClass(stringIDToTypeID("contentLayer"));
        d.putReference(stringIDToTypeID("null"), r);
        var d1 = new ActionDescriptor();
        var d2 = new ActionDescriptor();
        var d3 = new ActionDescriptor();
        d3.putDouble(stringIDToTypeID("red"), red);
        d3.putDouble(stringIDToTypeID("green"), green);
        d3.putDouble(stringIDToTypeID("blue"), blue);
        d2.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d3);
        d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("solidColorLayer"), d2);
        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("contentLayer"), d1);
        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
        }
    catch (e) { if (e.number!=8007) { alert("Line: "+e.line+"\n\n"+e,"Bug!",true); throw(e); } }
    }

 

Chuck Uebele
Community Expert
Community Expert
December 18, 2019

That works too.