简体   繁体   English

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

[英]How to convert an ellipse to a polyline in Objectarx C#

Currently I have a script that creates an ellipse, and Im trying to add the ability to adjust line thickness.目前我有一个创建椭圆的脚本,我正在尝试添加调整线条粗细的功能。 Since from my knowledge you cannot do this normally with an ellipse, Im trying to convert it to a Polyline using the PELLIPSE variable.由于据我所知,您通常无法使用椭圆来执行此操作,因此我尝试使用 PELLIPSE 变量将其转换为折线。 However, even with the variable setting, it doesn't seem to convert over.但是,即使使用变量设置,它似乎也没有转换过来。 Is there an easier way of doing this, or a potential fix to this issue?有没有更简单的方法来做到这一点,或者有可能解决这个问题?

Current Ellipse code:当前椭圆代码:

 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 variable setting: PELLIPSE 变量设置:

Application.SetSystemVariable("PELLIPSE", 1);

The PELLIPSE system variable acts only on the native ELLIPSE command. PELLIPSE 系统变量仅作用于本机 ELLIPSE 命令。 You could use it in combination with the Editor.Command() method but only for closed ellipses.您可以将它与 Editor.Command() 方法结合使用,但仅适用于闭合椭圆。 Alternatively, you can use the GeometryExtensions library which provides a ToPolyline() extension method for the Ellipse type.或者,您可以使用GeometryExtensions 库,它为 Ellipse 类型提供了 ToPolyline() 扩展方法。

var polyline = ellipse.ToPolyline();

I had the same problem in a different context.我在不同的上下文中遇到了同样的问题。 This is my code to manually converting an ellipse to a polyline: (sorry for german comments) The original task is to convert DWG to a Protobuf-structure, so the code is more complicate than needed.这是我手动将椭圆转换为多段线的代码:(对不起,德语注释)最初的任务是将 DWG 转换为 Protobuf 结构,因此代码比需要的更复杂。 Please ignore all "Protobuf..." elements.请忽略所有“Protobuf...”元素。 I hope it still explain's the algorithm.我希望它仍然解释了算法。

            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;
                }
            }

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

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