简体   繁体   中英

Draw a line using C# automation in Illustrator

I'm tring to draw a line form C# in Illustrator with the folowing code. No matter what point coordinates are sent it always draw the same line from 0,0 to 10,-10.

        // x1,y1 x2,y2 are point coordinates
        // doc is the active document

        var myLine = Doc.PathItems.Add();
        myLine.Left = Math.Min(x1, x2);
        myLine.Top = Math.Min(y1, y2);

        //set stroked to true so we can see the path
        myLine.Stroked = true;

        var newPoint = myLine.PathPoints.Add();
        newPoint.Anchor[0]=x1;
        newPoint.Anchor[1]=y1;
        //giving the direction points the same value as the 
        //anchor point creates a straight line segment
        newPoint.LeftDirection = newPoint.Anchor;
        newPoint.RightDirection = newPoint.Anchor;
        newPoint.PointType = AiPointType.aiCorner;

        var newPoint1= myLine.PathPoints.Add();
        newPoint1.Anchor[0] = x2;
        newPoint1.Anchor[1] = y2;
        newPoint1.LeftDirection = newPoint1.Anchor;
        newPoint1.RightDirection = newPoint1.Anchor;
        newPoint1.PointType = AiPointType.aiCorner;

This code is from the Adobe VB doc converted to C#.

Edit : I guess the problem comes from

        newPoint.Anchor[0]=x1;
        newPoint.Anchor[1]=y1;

It was newPoint.anchor=[x1,y1] in VB version. How to translate it in C# properly ? Documentation state that newPoint.Anchor is a Variant Array of 2 doubles.

Spotted it !

To set a variant array in C# the solution is

newPoint.Anchor = new object[] { x1,y1 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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