private static bool ZeroElevation(FeatureLine fl)
        {
            try
            {
                using (var tr = Active.StartTransaction())
                {
                    Point3dCollection pointsOnFL =
                        fl.GetPoints(FeatureLinePointType.AllPoints);

                    for (int i = 0; i < pointsOnFL.Count; i++)
                    {
                        if (Math.Abs(pointsOnFL[i].Z) < 0.01)
                        {
                            return(true);
                        }
                    }

                    tr.Commit();
                }
            }
            catch (Exception ex)

            {
                MessengerManager.MessengerManager.LogException(ex);
            }

            return(false);
        }
        private static void ApplyElevationCorrection(FeatureLine fl, double mean, double flMaxElevation)
        {
            try
            {
                if (mean <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(mean));
                }


                fl.UpgradeOpen();
                COMS.MessengerManager.AddLog("Start ApplyElevationCorrection");

                using (var tr = Active.StartTransaction())
                {
                    Point3dCollection pointsOnFL =
                        fl.GetPoints(FeatureLinePointType.AllPoints);

                    for (int i = 0; i < pointsOnFL.Count; i++)
                    {
                        Point3d p = pointsOnFL[i];


                        var diff = Math.Abs(flMaxElevation - p.Z);
                        if (diff > MaxDiff)
                        {
                            try
                            {
                                fl.SetPointElevation(i, mean);
                                COMS.MessengerManager.AddLog
                                    ("Changed Feature Line: " +
                                    fl.Name + " Old Elevation=" +
                                    p.Z + " New Elevation=" +
                                    mean);
                            }
                            catch (Exception ex)
                            {
                                COMS.MessengerManager.LogException(ex);
                            }
                        }
                    }

                    tr.Commit();
                }

                COMS.MessengerManager.AddLog("End ApplyElevationCorrection");
            }
            catch (Exception ex)

            {
                MessengerManager.MessengerManager.LogException(ex);
            }
        }
        public static void CheckElevFromFeatureLine(Acaddb.ObjectId featureId)
        {
            var mean = 0.0;

            using (Acaddb.Transaction tr = Active.StartTransaction())
            {
                var feature =
                    (Autodesk.Civil.DatabaseServices.FeatureLine)featureId.GetObject(Acaddb.OpenMode.ForRead);

                if (feature == null)
                {
                    return;
                }
                if (feature != null)
                {
                    SendMessage(feature);

                    try
                    {
                        mean = GetAverageElev(feature);
                    }
                    catch
                    {
                    }

                    if (Math.Abs(mean) <= 0.01)
                    {
                        goto Skip;
                    }

                    if (ZeroElevation(feature))
                    {
                        COMS.MessengerManager.AddLog("Starting Checking Zero Elevation > 20 Points");
                        if (feature.GetPoints(FeatureLinePointType.AllPoints).Cast <Point3d>().Count(p => Math.Abs(p.Z) < 0.001) < 20)
                        {
                            COMS.MessengerManager.AddLog("Starting Correcting Zero Elevation > 20 Points");
                            ApplyElevationCorrection(feature, mean, feature.MaxElevation);
                            COMS.MessengerManager.AddLog("Ending Correcting Zero Elevation > 20 Points");
                        }
                        COMS.MessengerManager.AddLog("Ending Checking Zero Elevation > 20 Points");
                    }

                    SendMessage(feature);
                }

Skip:

                tr.Commit();
            }

            COMS.MessengerManager.AddLog("End CheckElevFromFeatureLine");
        }
        public static void RecurseElevFromSurface()
        {
            try
            {
                var surfaceId = Acaddb.ObjectId.Null;
                var siteId    = Acaddb.ObjectId.Null;
                //var ElevDiff = 0.0;
                var mean = 0.0;

                var featurelines = GetFeatureLines();

                COMS.MessengerManager.AddLog("Start RecurseElevFromSurface");


                using (Application.DocumentManager.MdiActiveDocument.LockDocument())
                {
                    foreach (var Id in featurelines)
                    {
                        using (var tr = Active.StartTransaction())

                        {
                            var fId = Id.GetObject(Acaddb.OpenMode.ForRead) as FeatureLine;

                            if (fId != null)
                            {
                                try
                                {
                                    mean = GetAverageElev(fId);
                                }
                                catch
                                {
                                }

                                if (mean <= 0)
                                {
                                    goto Skip;
                                }

                                if (ZeroElevation(fId))
                                {
                                    ApplyElevationCorrection(fId, mean, fId.MaxElevation);
                                }

                                if (fId.Layer.Contains("S-WATER"))
                                {
                                    //CorrectWaterFeaturelines(fId, mean);
                                }

                                SendMessage(fId);
                            }

Skip:

                            tr.Commit();
                        }
                    }

                    COMS.MessengerManager.AddLog(" End RecurseElevFromSurface");
                }
            }
            catch (Exception ex)
            {
                MessengerManager.MessengerManager.LogException(ex);
            }
        }
        /// <summary>
        /// Corrects the water featurelines.
        /// Has been omitted until further notice due to 25 swings in elevation for wier and structures
        /// </summary>
        /// <param name="fl">The fl.</param>
        /// <param name="mean">The mean.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public static void CorrectWaterFeaturelines(FeatureLine fl, double mean)
        {
            try
            {
                if (mean <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(mean));
                }

                fl.UpgradeOpen();
                if (fl.Layer.Contains("S-WATER"))
                {
                    using (var tr = Active.StartTransaction())
                    {
                        Point3dCollection pointsOnFL =
                            fl.GetPoints(FeatureLinePointType.AllPoints);
                        COMS.MessengerManager.AddLog("Start CorrectWaterFeaturelines");

                        for (int i = 0; i < pointsOnFL.Count; i++)
                        {
                            Point3d p    = pointsOnFL[i];
                            var     last = pointsOnFL.Count - 1;
                            var     diff = Math.Abs(p.Z - mean);

                            if ((diff > MaxWater) && (Math.Abs(mean) > 0.01))
                            {
                                COMS.MessengerManager.AddLog("S-Changed to Mean = " + mean);
                                try
                                {
                                    if (i != last)
                                    {
                                        fl.SetPointElevation(i, mean);
                                    }
                                }
                                catch (System.ArgumentOutOfRangeException)
                                {
                                }

                                catch (Exception ex)
                                {
                                    MessengerManager.MessengerManager.LogException(ex);
                                    MessengerManager.MessengerManager.AddLog("Failure = " + i + "and Mean = " + mean);
                                }

                                COMS.MessengerManager.AddLog("E-Changed to Mean = " + mean);
                            }
                        }

                        tr.Commit();
                    }
                }

                COMS.MessengerManager.AddLog("End CorrectWaterFeaturelines");
            }

            catch (Exception ex)

            {
                MessengerManager.MessengerManager.LogException(ex);
            }
        }
        public static void CheckElevFromFeatureLine(FeatureLine fl)
        {
            try
            {
                var mean = 0.0;

                if (fl == null)
                {
                    throw new ArgumentNullException(nameof(fl));
                }


                COMS.MessengerManager.AddLog("Start CheckElevFromFeatureLine");

                using (Application.DocumentManager.MdiActiveDocument.LockDocument())
                {
                    using (var tr = Active.StartTransaction())

                    {
                        var feature = (FeatureLine)fl.ObjectId.GetObject(Acaddb.OpenMode.ForRead);

                        if (!feature.IsWriteEnabled)
                        {
                            feature.UpgradeOpen();
                        }


                        if (feature != null)
                        {
                            SendMessage(feature);

                            try
                            {
                                mean = GetAverageElev(feature);
                            }
                            catch
                            {
                            }

                            if (Math.Abs(mean) <= 0.01)
                            {
                                goto Skip;
                            }

                            if (ZeroElevation(feature))
                            {
                                ApplyElevationCorrection(feature, mean, feature.MaxElevation);
                            }

                            if (feature.Layer.Contains("S-WATER"))
                            {
                                // CorrectWaterFeaturelines(fl, mean);
                            }

                            SendMessage(feature);
                        }

Skip:

                        tr.Commit();
                    }

                    COMS.MessengerManager.AddLog("End CheckElevFromFeatureLine");
                }
            }
            catch (Exception ex)
            {
                MessengerManager.MessengerManager.LogException(ex);
            }
        }