private void ApplyAdjustmentSettings(ICadastralAdjustment pCadAdj, ICadastralAdjustment3 pCadAdjEx)
        {
            pCadAdj.DistanceTolerance = m_dDistToleranceReport;

            pCadAdj.BearingTolerance     = m_dBearingToleranceReport;
            pCadAdj.ClosePointsTolerance = m_dClosePointsToleranceReport;

            pCadAdj.LinePointsTolerance = m_dLinePtsOffsetToleranceReport;

            if (m_iReportType == 0)
            {
                pCadAdj.ListingDetailLevel = enumGSLSAListingDetailLevel.enumGSAdjustmentSimple;
            }

            if (m_iReportType == 1)
            {
                pCadAdj.ListingDetailLevel = enumGSLSAListingDetailLevel.enumGSAdjustmentStandard;
            }

            if (m_iReportType == 2)
            {
                pCadAdj.ListingDetailLevel = enumGSLSAListingDetailLevel.enumGSAdjustmentExtended;
            }

            //Main Line point Check-control
            pCadAdjEx.IncludeLinePoint = chkUseLinePoints.Checked;

            if (chkUseLinePoints.Checked)
            {
                pCadAdj.ForceLinePoints          = m_bSnapLinePointsToLines;
                pCadAdj.ForceLinePointsTolerance = m_dSnapLinePointTolerance;

                pCadAdjEx.FlexLinesOutOfLinePointTolerance = m_bBendLines;
                pCadAdjEx.FlexLinePointTolerance           = m_dBendLinesTolerance;
            }
            else
            {
                pCadAdj.ForceLinePoints                    = false;
                pCadAdj.ForceLinePointsTolerance           = m_dSnapLinePointTolerance;
                pCadAdjEx.FlexLinesOutOfLinePointTolerance = false;
                pCadAdjEx.FlexLinePointTolerance           = m_dBendLinesTolerance;
            }
            pCadAdj.ForceStraights = m_bStraightenRoadFrontages;
            pCadAdj.ForceStraightsAngleTolerance = m_dStraightRoadAngleTolerance;
            pCadAdj.ForceStraightsTolerance      = m_dStraightRoadOffsetTolerance;

            pCadAdj.IncludeEasements = m_bIncludeDependentLines;
        }
    private bool RunAdjustment(ICadastralPacketManager pCadastralPacketManager,
      ICadastralAdjustment pCadAdj, ICadastralAdjustment3 pCadAdjEx, int Iteration, out double MaxShift, out string Summary)
    {
      esriCadastralDistanceUnits eDistUnits =esriCadastralDistanceUnits.esriCDUUSSurveyFoot;
      esriDirectionType eDirectType  =esriDirectionType.esriDTNorthAzimuth;
      esriCadastralAreaUnits eAreaUnits = esriCadastralAreaUnits.esriCAUAcre;

      ICadastralPlan pCadPlan = (ICadastralPlan)pCadAdj;
      IEnumGSPlans pGSPlans =pCadPlan.GetPlans(enumGSPlansType.enumGSPlansWithParcels);
      pGSPlans.Reset();
      IGSPlan pGSPlan=pGSPlans.Next();

      while (pGSPlan!=null)
      {
        eDistUnits = pGSPlan.DistanceUnits;
        eDirectType = pGSPlan.DirectionFormat;
        eAreaUnits = esriCadastralAreaUnits.esriCAUAcre;
        break; //just use the first one
      }

      double dMaxShift=-999;
      string sSummary="";
      MaxShift=dMaxShift;
      try
      {
        //get the units from the map plan
                
        sSummary=
        pCadAdj.PerformAdjustment(false,m_sBrowseFilePath,null,eDistUnits, eDirectType,eAreaUnits);

        double dMaxEast=-999;
        double dMaxNorth=-999;
        int iPt=-999;
        pCadAdjEx.GetMaxShiftData(ref iPt, ref dMaxEast, ref dMaxNorth);

        double dDistEast=-999;
        double dDistNorth=-999;

        IMetricUnitConverter pUnitConversion = (IMetricUnitConverter)pCadastralPacketManager;
        pUnitConversion.ConvertDistance(esriCadastralUnitConversionType.esriCUCFromMetric, 
        dMaxEast, ref dDistEast);
        pUnitConversion.ConvertDistance(esriCadastralUnitConversionType.esriCUCFromMetric, 
        dMaxNorth, ref dDistNorth);

        dMaxShift = Math.Sqrt((dDistEast * dDistEast) + (dDistNorth * dDistNorth));

        return true;
      }
      catch(Exception ex)
      {
        MessageBox.Show(ex.Message);
        return false;
      }

      finally
      {
        MaxShift = dMaxShift;
        Summary=sSummary;
      }
    }
    private void ApplyAdjustmentSettings(ICadastralAdjustment pCadAdj,ICadastralAdjustment3 pCadAdjEx)
    {

      pCadAdj.DistanceTolerance=m_dDistToleranceReport;

      pCadAdj.BearingTolerance=m_dBearingToleranceReport;
      pCadAdj.ClosePointsTolerance=m_dClosePointsToleranceReport;

      pCadAdj.LinePointsTolerance = m_dLinePtsOffsetToleranceReport;

      if (m_iReportType == 0)
        pCadAdj.ListingDetailLevel = enumGSLSAListingDetailLevel.enumGSAdjustmentSimple;

      if (m_iReportType==1)
        pCadAdj.ListingDetailLevel = enumGSLSAListingDetailLevel.enumGSAdjustmentStandard;

      if (m_iReportType == 2)
        pCadAdj.ListingDetailLevel = enumGSLSAListingDetailLevel.enumGSAdjustmentExtended;

      //Main Line point Check-control
      pCadAdjEx.IncludeLinePoint = chkUseLinePoints.Checked;

      if (chkUseLinePoints.Checked)
      {
        pCadAdj.ForceLinePoints=m_bSnapLinePointsToLines;
        pCadAdj.ForceLinePointsTolerance=m_dSnapLinePointTolerance;

        pCadAdjEx.FlexLinesOutOfLinePointTolerance = m_bBendLines;
        pCadAdjEx.FlexLinePointTolerance = m_dBendLinesTolerance;
      }
      else
      {
        pCadAdj.ForceLinePoints = false;
        pCadAdj.ForceLinePointsTolerance = m_dSnapLinePointTolerance;
        pCadAdjEx.FlexLinesOutOfLinePointTolerance = false;
        pCadAdjEx.FlexLinePointTolerance = m_dBendLinesTolerance;
      }
      pCadAdj.ForceStraights=m_bStraightenRoadFrontages;
      pCadAdj.ForceStraightsAngleTolerance=m_dStraightRoadAngleTolerance;
      pCadAdj.ForceStraightsTolerance = m_dStraightRoadOffsetTolerance;
        
      pCadAdj.IncludeEasements=m_bIncludeDependentLines;
    }
        private bool RunAdjustment(ICadastralPacketManager pCadastralPacketManager,
                                   ICadastralAdjustment pCadAdj, ICadastralAdjustment3 pCadAdjEx, int Iteration, out double MaxShift, out string Summary)
        {
            esriCadastralDistanceUnits eDistUnits  = esriCadastralDistanceUnits.esriCDUUSSurveyFoot;
            esriDirectionType          eDirectType = esriDirectionType.esriDTNorthAzimuth;
            esriCadastralAreaUnits     eAreaUnits  = esriCadastralAreaUnits.esriCAUAcre;

            ICadastralPlan pCadPlan = (ICadastralPlan)pCadAdj;
            IEnumGSPlans   pGSPlans = pCadPlan.GetPlans(enumGSPlansType.enumGSPlansWithParcels);

            pGSPlans.Reset();
            IGSPlan pGSPlan = pGSPlans.Next();

            while (pGSPlan != null)
            {
                eDistUnits  = pGSPlan.DistanceUnits;
                eDirectType = pGSPlan.DirectionFormat;
                eAreaUnits  = esriCadastralAreaUnits.esriCAUAcre;
                break; //just use the first one
            }

            double dMaxShift = -999;
            string sSummary  = "";

            MaxShift = dMaxShift;
            try
            {
                //get the units from the map plan

                sSummary =
                    pCadAdj.PerformAdjustment(false, m_sBrowseFilePath, null, eDistUnits, eDirectType, eAreaUnits);

                double dMaxEast  = -999;
                double dMaxNorth = -999;
                int    iPt       = -999;
                pCadAdjEx.GetMaxShiftData(ref iPt, ref dMaxEast, ref dMaxNorth);

                double dDistEast  = -999;
                double dDistNorth = -999;

                IMetricUnitConverter pUnitConversion = (IMetricUnitConverter)pCadastralPacketManager;
                pUnitConversion.ConvertDistance(esriCadastralUnitConversionType.esriCUCFromMetric,
                                                dMaxEast, ref dDistEast);
                pUnitConversion.ConvertDistance(esriCadastralUnitConversionType.esriCUCFromMetric,
                                                dMaxNorth, ref dDistNorth);

                dMaxShift = Math.Sqrt((dDistEast * dDistEast) + (dDistNorth * dDistNorth));

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }

            finally
            {
                MaxShift = dMaxShift;
                Summary  = sSummary;
            }
        }
        private void btnRun_Click(object sender, EventArgs e)
        {
            double dConvTol     = 0.003;
            int    iRepeatCount = 2;
            double dMaxShift    = 0;

            double dVal = 0;

            if (Double.TryParse(this.txtMainDistResReport.Text, out dVal))
            {
                //write to registry
                Utilities FabUTILS = new Utilities();
                string    sVersion = FabUTILS.GetDesktopVersionFromRegistry();
                FabUTILS.WriteToRegistry(RegistryHive.CurrentUser,
                                         "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
                                         "LSADistanceToleranceReport", this.txtMainDistResReport.Text, true);
                FabUTILS = null;
            }

            bool   bAdjResult = true;
            string sSummary   = "";

            #region Setup Adjustment & Verify that there's enough information

            ICadastralPacketManager pCadastralPacketManager = null;
            ICadastralAdjustment    pCadAdj     = null;
            ICadastralAdjustment3   pCadAdjEx   = null;
            ICadastralMapEdit       pCadMapEdit = null;

            LoadValuesFromRegistry();

            UID pUID = new UIDClass();
            pUID.Value = "{114D685F-99B7-4B63-B09F-6D1A41A4DDC1}";
            ICadastralEditor pCadEd     = (ICadastralEditor)ArcMap.Application.FindExtensionByCLSID(pUID);
            ICadastralFabric pCadFabric = pCadEd.CadastralFabric;

            pCadastralPacketManager = (ICadastralPacketManager)pCadEd;
            bool open = pCadastralPacketManager.PacketOpen;

            ISelectionSet     pBeforeSS        = null;
            IFeatureSelection pParcelSelection = null;

            if (!open)
            {
                ICadastralSelection pSelection = (ICadastralSelection)pCadEd;
                IEnumGSParcels      pParcels   = pSelection.SelectedParcels;
                IEnumCEParcels      pCEParcels = (IEnumCEParcels)pParcels;
                if (pCEParcels != null)
                {
                    long count = pCEParcels.Count;
                    if (count == 0)
                    {
                        MessageBox.Show("There are no parcels selected to adjust." + Environment.NewLine
                                        + "Please select parcels and try again.", "Fabric Adjustment");
                        return;
                    }
                }

                ICadastralFabricLayer pCFLayer = pCadEd.CadastralFabricLayer;
                if (pCFLayer != null)
                {
                    IFeatureLayer pParcelLayer = pCFLayer.get_CadastralSubLayer(esriCadastralFabricRenderer.esriCFRParcels);
                    pParcelSelection = (IFeatureSelection)pParcelLayer;
                    if (pParcelSelection != null)
                    {
                        pBeforeSS = pParcelSelection.SelectionSet;
                    }
                }
            }

            pCadMapEdit = (ICadastralMapEdit)pCadEd;
            pCadMapEdit.StartMapEdit(esriMapEditType.esriMEParcelSelection, "Fabric Adjustment", false);

            ICadastralPacket pCadastralPacket = pCadastralPacketManager.JobPacket;
            int cpCount = 0;

            ICadastralControlPoints pCPts = (ICadastralControlPoints)pCadastralPacket;
            if (pCPts != null)
            {
                IGeometry            pGeom    = null;
                IEnumGSControlPoints pEnumCPs = pCPts.GetControlPoints(pGeom);
                pEnumCPs.Reset();
                IGSControlPoint pCP;
                if (pEnumCPs != null)
                {
                    pCP = pEnumCPs.Next();
                    while ((pCP != null) && (cpCount < 2))
                    {
                        if (pCP.Active)
                        {
                            cpCount++;
                        }
                    }
                }
            }

            if (cpCount < 2)
            {
                MessageBox.Show("Please make sure that at least 2 control points are" +
                                Environment.NewLine + "attached to the selected parcels.", "Fabric Adjustment");
                return;
            }
            pCadAdj   = (ICadastralAdjustment)pCadastralPacket;
            pCadAdjEx = (ICadastralAdjustment3)pCadastralPacket;
            ApplyAdjustmentSettings(pCadAdj, pCadAdjEx);

            #endregion

            double dHighestMaxShift = 0;

            //// Change display text depending on count
            //string itemText = count > 1 ? "items" : "item";

            for (int i = 1; i <= iRepeatCount; i++)
            {
                if (!RunAdjustment(pCadastralPacketManager, pCadAdj, pCadAdjEx, i, out dMaxShift, out sSummary))
                {
                    bAdjResult = false;
                    break;
                }
                if (dHighestMaxShift > dMaxShift)
                {
                    dHighestMaxShift = dMaxShift;
                }

                pCadAdj.AcceptAdjustment();
                if (dMaxShift < dConvTol)
                {
                    break;
                }
            }

            if (bAdjResult)
            {
                lblAdjResult.Text = "Adjustment Complete";
            }
            else
            {
                lblAdjResult.Text = "Adjustment Failed";
            }

            lblAdjResult.Visible = true;

            dlgAdjustmentResults AdjResults = new dlgAdjustmentResults();
            AdjResults.txtReport.Text = sSummary;

            //Display the dialog
            DialogResult pDialogResult = AdjResults.ShowDialog();
            if (pDialogResult != DialogResult.OK)
            {
                AdjResults = null;
                pCadMapEdit.StopMapEdit(false);
            }
            else
            {
                pCadMapEdit.StopMapEdit(true);
            }

            pParcelSelection.SelectionSet = pBeforeSS;

            Utilities FabUTILS2 = new Utilities();
            FabUTILS2.RefreshFabricLayers(ArcMap.Document.FocusMap, pCadFabric);
            FabUTILS2 = null;
        }