简体   繁体   English

在Illustrator中使用C#自动化绘制一条线

[英]Draw a line using C# automation in Illustrator

I'm tring to draw a line form C# in Illustrator with the folowing code. 我正在尝试使用以下代码在Illustrator中绘制线形C#。 No matter what point coordinates are sent it always draw the same line from 0,0 to 10,-10. 无论发送什么点坐标,它始终会在0,0到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#. 此代码是从Adobe VB文档转换为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. 在VB版本中为newPoint.anchor = [x1,y1]。 How to translate it in C# properly ? 如何在C#中正确翻译? Documentation state that newPoint.Anchor is a Variant Array of 2 doubles. 文档指出newPoint.Anchor是2个double的Variant Array。

Spotted it ! 发现了!

To set a variant array in C# the solution is 要在C#中设置变量数组,解决方案是

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

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

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