简体   繁体   中英

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. 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:

Application.SetSystemVariable("PELLIPSE", 1);

The PELLIPSE system variable acts only on the native ELLIPSE command. You could use it in combination with the Editor.Command() method but only for closed ellipses. Alternatively, you can use the GeometryExtensions library which provides a ToPolyline() extension method for the Ellipse type.

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. Please ignore all "Protobuf..." elements. 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;
                }
            }

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