【问题标题】:How to convert an ellipse to a polyline in Objectarx C#如何在 Objectarx C# 中将椭圆转换为折线
【发布时间】:2021-10-09 20:05:57
【问题描述】:

目前我有一个创建椭圆的脚本,我正在尝试添加调整线条粗细的功能。由于据我所知,您不能正常使用椭圆来执行此操作,因此我尝试使用 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);

【问题讨论】:

  • 向我们展示一些可以作为起点的示例代码
  • 添加示例代码

标签: c# autocad autodesk objectarx


【解决方案1】:

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

var polyline = ellipse.ToPolyline();

【讨论】:

    【解决方案2】:

    我在不同的上下文中遇到了同样的问题。 这是我手动将椭圆转换为折线的代码: (对不起德国cmets) 最初的任务是将 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;
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2021-11-19
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多