<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: eInvalidInput at Region.CreateFromCurves in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/einvalidinput-at-region-createfromcurves/m-p/7842210#M27100</link>
    <description>&lt;P&gt;Sorry for taking me too long to accept the answer, very helpful as usual. I just didn't have time to give it a try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks once again Gile!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Very much appreciated it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Mar 2018 20:16:52 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-03-09T20:16:52Z</dc:date>
    <item>
      <title>eInvalidInput at Region.CreateFromCurves</title>
      <link>https://forums.autodesk.com/t5/net-forum/einvalidinput-at-region-createfromcurves/m-p/7812932#M27098</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have migrating tom ARX code into .NET from a plugin I'm still not familiar, which convert drawings to SVG.&amp;nbsp;This function gets the polyline centroid and is using some ideas from the link&amp;nbsp;&lt;A title="link" href="https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-9CD22AE5-8F66-4925-A155-95852BAFD565-htm.html" target="_blank"&gt;https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-9CD22AE5-8F66-4925-A155-95852BAFD565-htm.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once in a while, especially when I'm trying to convert all layers I see this eInvalidInput exception. (An unhandled exception of type 'Autodesk.AutoCAD.Runtime.Exception' occurred in AcdbMgd.dll)&lt;/P&gt;&lt;P&gt;Not sure&amp;nbsp;why this my be happening.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FYI I'm using this plugin with Autocad 2013 and .NET 4.5.2&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any&amp;nbsp;help will be much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;public static SPointF GetPolyCentroid(this Polyline pline)&lt;BR /&gt;{&lt;BR /&gt;DBObjectCollection plineCollection = new DBObjectCollection();&lt;BR /&gt;DBObjectCollection regionCollection = new DBObjectCollection();&lt;BR /&gt;plineCollection.Add(pline);&lt;/P&gt;&lt;P&gt;regionCollection = Region.CreateFromCurves(plineCollection);&amp;lt;----Here the exception occurs&lt;BR /&gt;Region region = regionCollection[0] as Region;&lt;BR /&gt;Point3d centroid;&lt;BR /&gt;using (region)&lt;BR /&gt;{&lt;BR /&gt;//TODO&lt;BR /&gt;//&lt;A href="https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-9CD22AE5-8F66-4925-A155-95852BAFD565-htm.html" target="_blank"&gt;https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-9CD22AE5-8F66-4925-A155-95852BAFD565-htm.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;using (Solid3d solid = new Solid3d())&lt;BR /&gt;{&lt;BR /&gt;solid.Extrude(region, 2.0, 0.0);&lt;BR /&gt;Point3d solidCentroid = solid.MassProperties.Centroid;&lt;BR /&gt;centroid = solidCentroid.TransformBy(Matrix3d.Displacement(region.Normal.Negate()));&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (centroid != null)&lt;BR /&gt;return new SPointF(centroid.X, centroid.Y);&lt;BR /&gt;else return null;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Tue, 27 Feb 2018 16:10:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/einvalidinput-at-region-createfromcurves/m-p/7812932#M27098</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-27T16:10:57Z</dc:date>
    </item>
    <item>
      <title>Re: eInvalidInput at Region.CreateFromCurves</title>
      <link>https://forums.autodesk.com/t5/net-forum/einvalidinput-at-region-createfromcurves/m-p/7813133#M27099</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not know why you get this error, but there's probably a problem with the polyline as self intersection.&lt;/P&gt;
&lt;P&gt;Did you tray to create a region from this polyline in the AutoCAD with the REGION command?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, getting the centroid of a polyline by creating a region and a solid 3D is not very efficient.&lt;/P&gt;
&lt;P&gt;You can compute it with some geometry calculations as shown in the following methods (you can also find a similar method in the&amp;nbsp;&lt;STRONG&gt;&lt;A href="http://www.theswamp.org/index.php?topic=31865.msg373672#msg373672" target="_blank"&gt;GeometryExtensions&lt;/A&gt; &lt;/STRONG&gt;library)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        private Point3d GetCentroid(Polyline pl)
        {
            Point2d p0 = pl.GetPoint2dAt(0);
            Point2d cen = new Point2d(0.0, 0.0);
            double area = 0.0;
            double bulge = pl.GetBulgeAt(0);
            int last = pl.NumberOfVertices - 1;
            double tmpArea;
            Point2d tmpPoint;

            if (bulge != 0.0)
            {
                double[] datas = GetArcGeom(pl, bulge, 0, 1);
                area = datas[0];
                cen = new Point2d(datas[1], datas[2]) * datas[0];
            }
            for (int i = 1; i &amp;lt; last; i++)
            {
                tmpArea = TriangleAlgebricArea(p0, pl.GetPoint2dAt(i), pl.GetPoint2dAt(i + 1));
                tmpPoint = TriangleCentroid(p0, pl.GetPoint2dAt(i), pl.GetPoint2dAt(i + 1));
                cen += (tmpPoint * tmpArea).GetAsVector();
                area += tmpArea;
                bulge = pl.GetBulgeAt(i);
                if (bulge != 0.0)
                {
                    double[] datas = GetArcGeom(pl, bulge, i, i + 1);
                    area += datas[0];
                    cen += new Vector2d(datas[1], datas[2]) * datas[0];
                }
            }
            bulge = pl.GetBulgeAt(last);
            if (bulge != 0.0)
            {
                double[] datas = GetArcGeom(pl, bulge, last, 0);
                area += datas[0];
                cen += new Vector2d(datas[1], datas[2]) * datas[0];
            }
            cen = cen.DivideBy(area);
            Point3d result = new Point3d(cen.X, cen.Y, pl.Elevation);
            return result.TransformBy(Matrix3d.PlaneToWorld(pl.Normal));
        }

        private double[] GetArcGeom(Polyline pl, double bulge, int index1, int index2)
        {
            CircularArc2d arc = (pl.GetArcSegment2dAt(index1));
            double arcRadius = arc.Radius;
            Point2d arcCenter = arc.Center;
            double arcAngle = 4.0 * Math.Atan(bulge);
            double tmpArea = ArcAlgebricArea(arcRadius, arcAngle);
            Point2d tmpPoint = ArcCentroid(pl.GetPoint2dAt(index1), pl.GetPoint2dAt(index2), arcCenter, tmpArea);
            return new double[3] { tmpArea, tmpPoint.X, tmpPoint.Y };
        }

        private Point2d TriangleCentroid(Point2d p0, Point2d p1, Point2d p2)
        {
            return (p0 + p1.GetAsVector() + p2.GetAsVector()) / 3.0;
        }

        private double TriangleAlgebricArea(Point2d p0, Point2d p1, Point2d p2)
        {
            return (((p1.X - p0.X) * (p2.Y - p0.Y)) - ((p2.X - p0.X) * (p1.Y - p0.Y))) / 2.0;
        }

        private Point2d ArcCentroid(Point2d start, Point2d end, Point2d cen, double tmpArea)
        {
            double chord = start.GetDistanceTo(end);
            double angle = (end - start).Angle;
            return Polar2d(cen, angle - (Math.PI / 2.0), (chord * chord * chord) / (12.0 * tmpArea));
        }

        private double ArcAlgebricArea(double rad, double ang)
        {
            return rad * rad * (ang - Math.Sin(ang)) / 2.0;
        }

        private Point2d Polar2d(Point2d org, double angle, double distance)
        {
            return org + new Vector2d(distance * Math.Cos(angle), distance * Math.Sin(angle));
        }&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 17:17:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/einvalidinput-at-region-createfromcurves/m-p/7813133#M27099</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-02-27T17:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: eInvalidInput at Region.CreateFromCurves</title>
      <link>https://forums.autodesk.com/t5/net-forum/einvalidinput-at-region-createfromcurves/m-p/7842210#M27100</link>
      <description>&lt;P&gt;Sorry for taking me too long to accept the answer, very helpful as usual. I just didn't have time to give it a try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks once again Gile!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Very much appreciated it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 20:16:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/einvalidinput-at-region-createfromcurves/m-p/7842210#M27100</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-03-09T20:16:52Z</dc:date>
    </item>
  </channel>
</rss>

