如何在 Objectarx C# 中将椭圆转换为折线

How to convert an ellipse to a polyline in Objectarx C#

目前我有一个创建椭圆的脚本,我正在尝试添加调整线条粗细的功能。因为据我所知你不能用椭圆正常地做到这一点,我试图使用 PELLIPSE 变量将它转换为多段线。但是,即使使用变量设置,它似乎也没有转换过来。有没有更简单的方法来执行此操作,或者有可能解决此问题?

当前椭圆代码:

 public override Entity getAcObj()
        {
            return
                new Ellipse(
                        new Point3d(Markup.XCoord, Markup.YCoord, 0),
                        Vector3d.ZAxis,
                        new Vector3d((Markup.Locations[0].XCoord - Markup.Locations[1].XCoord) / 2, -(Markup.Locations[2].YCoord - Markup.Locations[3].YCoord) / 2, 0),
                        (Math.Abs(Markup.Locations[0].YCoord - Markup.Locations[3].YCoord) / 2)
                            / (Math.Abs(Markup.Locations[0].XCoord - Markup.Locations[1].XCoord) / 2),
                        0,
                        360 * Math.Atan(1.0) / 45.0
                    )
                {
                    Layer = Layer
                };
        }

PELLIPSE 变量设置:

Application.SetSystemVariable("PELLIPSE", 1);

PELLIPSE 系统变量仅作用于本机 ELLIPSE 命令。您可以将它与 Editor.Command() 方法结合使用,但仅适用于闭合椭圆。 或者,您可以使用 GeometryExtensions library,它为 Ellipse 类型提供 ToPolyline() 扩展方法。

var polyline = ellipse.ToPolyline();

我在不同的上下文中遇到了同样的问题。 这是我手动将椭圆转换为折线的代码: (抱歉德国评论) 最初的任务是将 DWG 转换为 Protobuf 结构,因此代码比需要的更复杂。请忽略所有“Protobuf ...”元素。我希望它仍然能解释算法。

            Ellipse ellipse = entity as Ellipse;

            // vorsichtshalber unsinnige Ellipsen ignorieren (auch wenn man diese interaktiv gar nicht erzeugen kann)
            if (ellipse.StartAngle == ellipse.EndAngle) { return 0; }

            // ...wir machen eine Polylinie daraus mHv.:
            // Ellipse = (a x cos(alpha), b x sin(alpha)), a = Hauptachse(nvektor), b = Nebenachse(nvektor), 0 <= alpha <= 2 x PI
            ProtobufLayer protolayer = this.GetOrCreateProtobufLayer(newLayer, newLayerId, 1);
            ProtobufEntity protoentity = GDBProtobufHelper.DefaultEntityPolyline(false);
            double angle, angleSum = 0, angleStep = Math.PI / 18;
            bool stop = false;

            // Punkte (auf der Ellipse) einzeln hinzufügen 
            angle = ellipse.StartAngle;
            while (true)
            {
                // (alle Winkelangaben/-funktionen sind in Bogenmass)
                Vector3d vector = ellipse.MajorAxis * Math.Cos(angle) + ellipse.MinorAxis * Math.Sin(angle);
                protoentity.EntityPolyline.Points.Add(new ProtobufPolyPoint()
                {
                    Bulge = 0,
                    Point = new ProtobufRealPoint()
                    {
                        X = ellipse.Center.X + vector.X,
                        Y = ellipse.Center.Y + vector.Y
                    }
                });
                if (stop) { break; }

                // nächsten Winkel berechnen
                angle += angleStep;
                if (angle >= 2 * Math.PI) { angle -= 2 * Math.PI; }
                angleSum += angleStep;

                // sind wir - beim nächsten Mal, der aktuelle neue Punkt muss ja noch dazu! - fertig?
                // (Fallunterscheidung, falls Start > Ende, ist es etwas komplzierter)
                if ((ellipse.StartAngle < ellipse.EndAngle) && (angleSum >= ellipse.EndAngle - ellipse.StartAngle))
                {
                    angle = ellipse.EndAngle;
                    stop = true;
                }
                if ((ellipse.StartAngle > ellipse.EndAngle) && (angleSum >= 2 * Math.PI + ellipse.EndAngle - ellipse.StartAngle))
                {
                    angle = ellipse.EndAngle;
                    stop = true;
                }
            }