public void GetFabricAdjustmentSettings()
        {
            try
              {
              Utilities FabUtils= new Utilities();
              string sLSABearingTolerance=
              FabUtils.ReadFromRegistry(RegistryHive.CurrentUser, "Software\\ESRI\\Desktop10.1\\ArcMap\\Cadastral", "LSABearingTolerance",false);

              string sLSAListingDetailLevel=
              FabUtils.ReadFromRegistry(RegistryHive.CurrentUser,
              "Software\\ESRI\\Desktop10.1\\ArcMap\\Cadastral", "LSAListingDetailLevel", false);

              dlgAdjustmentSettings pAdjustmentSettingsDialog = new dlgAdjustmentSettings();

              //Display the dialog
              DialogResult pDialogResult = pAdjustmentSettingsDialog.ShowDialog();

              if (pDialogResult != DialogResult.OK)
            return;

              }
              catch(Exception ex)
              {
            MessageBox.Show(ex.Message);
              }
        }
        protected void AddTraverseInfoToGrid(IGSPlan Plan, DataGridView TraverseGrid, List<IVector3D> Traverse, 
      List<IVector3D> AdjustedTraverse, List<string> FromToList)
        {
            ICadastralUnitConversion pCadUnitConverter = new CadastralUnitConversionClass();
              double dMetersPerUnit = pCadUnitConverter.ConvertDouble(1, Plan.DistanceUnits, esriCadastralDistanceUnits.esriCDUMeter);
              IAngularConverter pAngConv = new AngularConverterClass();
              Utilities Utils = new Utilities();

              List<string> lstAdjustedCourses = new List<string>();
              List<string> lstResiduals = new List<string>();

              foreach (IVector3D vect in AdjustedTraverse)
              {
            pAngConv.SetAngle(vect.Azimuth, esriDirectionType.esriDTNorthAzimuth, esriDirectionUnits.esriDURadians);

            int iPrec = 7;
            if (Plan.AngleUnits == esriDirectionUnits.esriDUDegreesMinutesSeconds)
              iPrec = 0;
            string sBearing = pAngConv.GetString(Plan.DirectionFormat, Plan.AngleUnits, iPrec);
            string sAdjusted = Utils.FormatDirectionDashesToDegMinSecSymbols(sBearing) + ", " + (vect.Magnitude / dMetersPerUnit).ToString("0.000");

            lstAdjustedCourses.Add(sAdjusted);

              }

              int i = 0;

              foreach (IVector3D vect in Traverse)
              {
            pAngConv.SetAngle(vect.Azimuth, esriDirectionType.esriDTNorthAzimuth, esriDirectionUnits.esriDURadians);

            int iPrec = 7;
            if(Plan.AngleUnits == esriDirectionUnits.esriDUDegreesMinutesSeconds)
              iPrec=0;
            string sBearing = pAngConv.GetString(Plan.DirectionFormat, Plan.AngleUnits, iPrec);
            string sDescription = Utils.FormatDirectionDashesToDegMinSecSymbols(sBearing) + ", " + (vect.Magnitude/dMetersPerUnit).ToString("0.000");

            string sDistResidual= ((vect.Magnitude-AdjustedTraverse[i].Magnitude)/dMetersPerUnit).ToString("0.000");

            //get the angle difference between the vectors
            IVector3D vec1 = new Vector3DClass();
            vec1.PolarSet(vect.Azimuth, 0, 1);

            IVector3D vec2 = new Vector3DClass();
            vec2.PolarSet(AdjustedTraverse[i].Azimuth, 0, 1);

            double dAngleDifference = Math.Acos(vec1.DotProduct(vec2));
            pAngConv.SetAngle(dAngleDifference, esriDirectionType.esriDTPolar, esriDirectionUnits.esriDURadians);
            string sAngResidual = pAngConv.GetString(esriDirectionType.esriDTPolar, Plan.AngleUnits, iPrec);
            sAngResidual = Utils.FormatDirectionDashesToDegMinSecSymbols(sAngResidual);

            TraverseGrid.Rows.Add(FromToList[i], sDescription, lstAdjustedCourses[i], sAngResidual + ", " + sDistResidual);
            i++;
              }
        }
    public AdjustmentDockWindow(object hook)
    {
      InitializeComponent();
      this.Hook = hook;
      s_TxtToler = txtMainDistResReport;
      s_BtnRun = btnRun;
      s_BtnSettings=btnSettings;
      s_ChkUseLinePoints = chkUseLinePoints;
      s_LblDistUnits=lblUnits1;
      //read from registry
      Utilities FabUTILS = new Utilities();
      string sVersion = FabUTILS.GetDesktopVersionFromRegistry();
      string sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
     "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
     "LSADistanceToleranceReport", true);
      if (sVal.Trim() != "")
        this.txtMainDistResReport.Text = sVal;
      
      SetEnabled(false);

      s_LblDistUnits.Text="";
      FabUTILS=null;
    }
    internal static void SetEnabled(bool enabled)
    {
      s_enabled = enabled;
        Utilities FabUTILS = new Utilities();
        string sVersion = FabUTILS.GetDesktopVersionFromRegistry();
      // if the dockable window was never displayed, text box could be null
      if (s_TxtToler == null)
        return;
      s_TxtToler.Enabled= enabled;

      if(!enabled)
        s_TxtToler.Text="";
      else
      {
        string sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSADistanceToleranceReport", true);
        if (sVal.Trim() != "")
          s_TxtToler.Text= sVal;
      }

      if (s_BtnRun == null)
        return;
      s_BtnRun.Enabled = enabled;

      s_BtnSettings.Enabled = enabled;

      s_ChkUseLinePoints.Enabled=enabled;

      s_LblDistUnits.Visible=enabled;

      IActiveView pActView = ArcMap.Document.ActiveView;
      if (pActView!=null)
        s_LblDistUnits.Text = FabUTILS.UnitNameFromSpatialReference(pActView.FocusMap.SpatialReference);
      FabUTILS = null;

    }
    private void LoadValuesFromRegistry()
    {
      Utilities FabUTILS = new Utilities();
      try
      {
        string sVal ="";
        string sVersion = FabUTILS.GetDesktopVersionFromRegistry();
        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSADistanceToleranceReport", false);
        if (sVal.Trim() != "")
          m_dDistToleranceReport = Convert.ToDouble(sVal);
        

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSABearingToleranceReport", false);
        if (sVal.Trim() != "")
          m_dBearingToleranceReport = Convert.ToDouble(sVal); 

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSALinePointsOffsetToleranceReport", false);
        if (sVal.Trim() != "")
          m_dLinePtsOffsetToleranceReport = Convert.ToDouble(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSAClosePointsToleranceReport", false);
        if (sVal.Trim() != "")
          m_dClosePointsToleranceReport = Convert.ToDouble(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSANumberRepeatCountIteration", false);
        if (sVal.Trim() != "")
          m_iRepeatCount = Convert.ToInt32(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSAConvergenceValue", false);
        if (sVal.Trim() != "")
          m_dConvergenceValue = Convert.ToDouble(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
        "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
        "LSADivergenceValue", false);
        if (sVal.Trim() != "")
          m_dDivergenceValue = Convert.ToDouble(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSAReportType", false);
        if (sVal.Trim() != "")
          m_iReportType = Convert.ToInt32(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSABrowseFilePath", false);
        if (sVal.Trim() != "")
          m_sBrowseFilePath = sVal;

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSAChkBendLinesOn", false);
        if (sVal.Trim() != "")
          m_bBendLines = ((sVal.ToUpper().Trim()) == "TRUE");

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSABendLinesTolerance", false);
        if (sVal.Trim() != "")
          m_dBendLinesTolerance = Convert.ToDouble(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSAChkIncludeDependentLines", false);
        if (sVal.Trim() != "")
          m_bIncludeDependentLines = ((sVal.ToUpper().Trim()) == "TRUE");

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSAChkSnapLinePointsToLines", false);
        if (sVal.Trim() != "")
          m_bSnapLinePointsToLines = ((sVal.ToUpper().Trim()) == "TRUE");

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSASnapLinePtsToLinesTolerance", false);
        if (sVal.Trim() != "")
          m_dSnapLinePointTolerance = Convert.ToDouble(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSAChkStraightenRoadFrontages", false);
        if (sVal.Trim() != "")
          m_bStraightenRoadFrontages = ((sVal.ToUpper().Trim()) == "TRUE");

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSAStraightenRoadFrontagesOffset", false);
        if (sVal.Trim() != "")
          m_dStraightRoadOffsetTolerance = Convert.ToDouble(sVal);

        sVal = FabUTILS.ReadFromRegistry(RegistryHive.CurrentUser,
       "Software\\ESRI\\Desktop" + sVersion + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
       "LSAStraightenRoadFrontagesAngle", false);
        if (sVal.Trim() != "")
          m_dStraightRoadAngleTolerance = Convert.ToDouble(sVal);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "Fabric Adjustment Settings");
      }
      finally
      {
        FabUTILS = null;
      }
    }
    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;

    }
        protected override void OnStartup()
        {
            //AdjustmentDockWindow pDock=

              Utilities UTIL = new Utilities();
              s_extension = this;

              try
              {
            string sDesktopVers = UTIL.GetDesktopVersionFromRegistry();
            if (sDesktopVers.Trim() == "")
              sDesktopVers = "Desktop10.0";
            else
              sDesktopVers = "Desktop" + sDesktopVers;

            string sValues = UTIL.ReadFromRegistry(RegistryHive.CurrentUser,
            "Software\\ESRI\\" + sDesktopVers + "\\ArcMap\\Cadastral\\AddIn.ParcelEditHelper",
              "Options", false);

            string[] Values = sValues.Split(',');
            string sVal = Values[0];
            string sFieldName = Values[1];
            bool bUseFieldRecord = false;

            if (sVal.Trim() != "")
              bUseFieldRecord = ((sVal.ToUpper().Trim()) == "1");

            s_extension.FieldName = sFieldName;
            s_extension.RecordToField = bUseFieldRecord;

              }
              catch
              {}

              m_editEvents = ArcMap.Editor as IEditEvents_Event;
              m_editEvents.OnStartEditing += new IEditEvents_OnStartEditingEventHandler(m_editEvents_OnStartEditing);
              m_editEvents.OnStopEditing += new IEditEvents_OnStopEditingEventHandler(m_editEvents_OnStopEditing);
        }
        protected override void OnClick()
        {
            ICadastralEditor pCadEd = (ICadastralEditor)ArcMap.Application.FindExtensionByName("esriCadastralUI.CadastralEditorExtension");
              IParcelEditManager pParcEditorMan = (IParcelEditManager)pCadEd;

              ICadastralPacketManager pCadPacketMan = (ICadastralPacketManager)pCadEd;
              //bool bStartedWithPacketOpen = pCadPacketMan.PacketOpen;

              if (pParcEditorMan == null)
            return;

              IEditor pEd = (IEditor)ArcMap.Application.FindExtensionByName("esri object editor");
              if (pEd.EditState == esriEditState.esriStateNotEditing)
              {
            MessageBox.Show("Please start editing and try again.");
            return;
              }

              IParcelConstruction pConstr = pParcEditorMan.ParcelConstruction;
              IParcelConstruction4 pConstr4 = pConstr as IParcelConstruction4;
              ICadastralPoints pCadastralPts = pConstr4 as ICadastralPoints;
              ICadastralEditorSettings2 pCadastralEditorSettings2 = pCadEd as ICadastralEditorSettings2;

              ICadastralFixedPoints pFixedPoints = pCadastralPts as ICadastralFixedPoints;
              IPointCalculation pPointCalc = new PointCalculationClass();

              if (pConstr == null)
            return;

              IGSLine pParcelLine = null;
              IMetricUnitConverter pMetricUnitConv = (IMetricUnitConverter)pCadEd;
              IGSPoint pStartPoint = null;
              List<int> lstPointIds = new List<int>();

              List<IVector3D> Traverse = new List<IVector3D>();

              //get rotation here
              IParcelConstructionData pConstrData = pConstr4.ConstructionData;
              IConstructionParentParcels pConstructionParentParcels = pConstrData as IConstructionParentParcels;

              ICadastralUndoRedo pCadUndoRedo = pConstr as ICadastralUndoRedo;
              try
              {
            int iParcelID = -1;
            if (pConstructionParentParcels.ParentParcelCount>0)
              pConstructionParentParcels.GetParentParcel(0, ref iParcelID);

            ICadastralParcel pCadaParcel = pCadPacketMan.JobPacket as ICadastralParcel;

            IGSParcel pGSParcel = null;

            if (pCadaParcel != null)
              pGSParcel = pCadaParcel.GetParcel(iParcelID);
            //if in measurement view then rotation is 0
            double TheRotation = 0;

            if (pGSParcel == null)
              pGSParcel = pConstr.Parcel;

            if (!pCadastralEditorSettings2.MeasurementView)
              TheRotation = pGSParcel.Rotation;//radians

            if (TheRotation == 123456789)
              TheRotation = 0;

            pPointCalc.Rotation = TheRotation;
            IGSPoint pClosingPoint = null;

            #region simple method as fall-back
            bool bUseSimpleStackSelection = false;
            if (bUseSimpleStackSelection)
            {
              //bool bLineSelectionSequence = false;
              //IGSLine pLastSelectedGSLineInGrid = null;
              //for (int i = 0; i < pConstr.LineCount; i++)
              //{
              //  if (pConstr.GetLineSelection(i))
              //  {
              //    if (pConstr.GetLine(i, ref pParcelLine))
              //    {
              //      if (!bLineSelectionSequence) //first line
              //      {
              //        pStartPoint = pCadastralPts.GetPoint(pParcelLine.FromPoint);
              //        pToPoint = pCadastralPts.GetPoint(pParcelLine.ToPoint);
              //      }

              //      pPointCalc.AddLine(pParcelLine);

              //      pLastSelectedGSLineInGrid = pParcelLine;
              //      pToPoint = pCadastralPts.GetPoint(pParcelLine.ToPoint);
              //      lstPointIds.Add(pToPoint.Id);

              //    }
              //    bLineSelectionSequence = true;

              //    double dBear = pParcelLine.Bearing; //Azimuth of IVector3D is north azimuth radians zero degrees north
              //    double dDist = pParcelLine.Distance;
              //    IVector3D vec = new Vector3DClass();
              //    vec.PolarSet(dBear, 0, dDist); ////Azimuth of IVector3D is north azimuth radians zero degrees north
              //    Traverse.Add(vec);
              //  }
              //  else
              //  {
              //    if (bLineSelectionSequence && pConstr.GetLine(i, ref pParcelLine) && HasLineSelectionAfter(pConstr, i))
              //    //if there was a prior selection and this line is a complete line, and there is no later selection
              //    {
              //      MessageBox.Show("Please select a continuous set of lines for closure.");
              //      return;
              //    }
              //  }
              //}
              //pClosingPoint = pCadastralPts.GetPoint(pLastSelectedGSLineInGrid.ToPoint);
            }
            else
            #endregion

            {//build a forward star for the selected lines
              IEnumCELines pCELines = new EnumCELinesClass();
              IEnumGSLines pEnumGSLines = (IEnumGSLines)pCELines;
              ILongArray pLongArray = new LongArrayClass();
              int iFirstToNode = -1;
              for (int i = 0; i < pConstr.LineCount; i++)
              {
            if (pConstr.GetLineSelection(i))
            {
              if (pConstr.GetLine(i, ref pParcelLine))
              {
                if (iFirstToNode < 0)
                  iFirstToNode = pParcelLine.ToPoint;
                pLongArray.Add(i);
                pCELines.Add(pParcelLine);
              }
            }
              }

              if (pCELines.Count==0)
              {
            MessageBox.Show("No lines selected. Please select a continuous set of lines for closure." + Environment.NewLine +
              "Line selection should not have branches.", "Traverse");
            return;
              }

              IParcelLineFunctions3 ParcelLineFx = new ParcelFunctionsClass();
              IGSForwardStar pFwdStar = ParcelLineFx.CreateForwardStar(pEnumGSLines);
              //forward star object is now created for all the selected lines,
              //need to first re-sequence the lines, and test for branching and discontinuity

              int iBranches = 0; int iTracedLines = 0;
              int iLoops = 0; int iTerminals = 0;
              List<int> LineIDList = new List<int>();
              List<int> FromList = new List<int>();
              List<int> ToList = new List<int>();
              List<string> FromToLine = new List<string>();

              bool bTraceSucceeded = TraceLines(ref pFwdStar, iFirstToNode, ref iBranches, ref iTracedLines,
            ref iLoops, ref iTerminals, ref FromToLine, ref FromList, ref ToList, 0);

              if (iBranches > 0)
              {
            MessageBox.Show("Please select a continuous set of lines for closure." + Environment.NewLine +
              "Line selection should not have branches.", "Traverse");
            return;
              }

              if (iTracedLines < pLongArray.Count)
              {
            MessageBox.Show("Please select a continuous set of lines for closure." + Environment.NewLine +
              "Selected Lines should be connected in a single sequence without branches.", "Traverse");
            return;
              }

              //if it's a single loop check to see if the sequence needs to be reversed
              //CW or CCW based on bearings
              if (iLoops == 1)
              {
            bool bIsReversed = false;
            foreach (int i in FromList)
            {
              if (i < 0)
                bIsReversed = true;
              else
              {
                bIsReversed = false;
                break;
              }
            }
            if (bIsReversed)
            {//all courses are running reversed, so reverse the whole sequence
              FromToLine.Clear();
              FromList.Reverse();
              ToList.Reverse();
              int iNewFrom = -ToList[ToList.Count-1];
              int iNewTo = -FromList[ToList.Count-1];
              string sNewFromTo = iNewFrom.ToString() + "," + iNewTo.ToString();
              FromToLine.Add(sNewFromTo);
              for (int i =1; i < ToList.Count ;i++)
              {
                iNewFrom = -ToList[i-1];
                iNewTo = -FromList[i-1];
                sNewFromTo = iNewFrom.ToString() + "," + iNewTo.ToString();
                FromToLine.Add(sNewFromTo);
              }
            }
              }

              LineIDList.Clear();
              FromList.Clear();
              ToList.Clear();
              pLongArray.RemoveAll();

              pCadUndoRedo.StartUndoRedoSession("Adjust Traverse");

              if (iLoops == 0)
              {
            //re-sequence using TraceLines function based on either end point, because the order of
            //selected construction lines in grid don't control start or end point
            FromToLine.Clear();
            int iTerminus = -1;
            iTracedLines = 0;
            iBranches = 0;
            iLoops = 0; iTerminals = 0;
            FindTerminusForSequence(ref pFwdStar, iFirstToNode, ref iTerminus, 0);
            if (iTerminus == -1)
            {
              pCadUndoRedo.WriteUndoRedoSession(false);
              return;
            }
            TraceLines(ref pFwdStar, iTerminus, ref iBranches, ref iTracedLines, ref iLoops, ref iTerminals,
              ref FromToLine, ref FromList, ref ToList, 0);
              }

              List<IVector3D> SequencedTraverse = new List<IVector3D>();
              IGSLine pGSLineInPath = null;
              foreach (string s in FromToLine)
              {
            string[] sFromTo = s.Split(',');
            int iFrom = Convert.ToInt32(sFromTo[0]);
            int iTo = Convert.ToInt32(sFromTo[1]);

            bool bReversed = pFwdStar.GetLine(iFrom, iTo, ref pGSLineInPath);
            if (bReversed)
            {
              IGSLine pGSLine180 = new GSLineClass();
              pGSLine180.FromPoint = pGSLineInPath.ToPoint;
              pGSLine180.ToPoint = pGSLineInPath.FromPoint;
              pGSLine180.Bearing = pGSLineInPath.Bearing + Math.PI;
              pGSLine180.Distance = pGSLineInPath.Distance;

              IVector3D vec180 = new Vector3DClass();
              vec180.PolarSet(pGSLine180.Bearing, 0, pGSLine180.Distance); //Azimuth of IVector3D is north azimuth radians zero degrees north
              Traverse.Add(vec180);
              lstPointIds.Add(pGSLine180.ToPoint);
              pPointCalc.AddLine(pGSLine180);
            }
            else
            {
              double dBear = pGSLineInPath.Bearing;
              double dDist = pGSLineInPath.Distance;
              IVector3D vec = new Vector3DClass();
              vec.PolarSet(dBear, 0, dDist); //Azimuth of IVector3D is north azimuth radians zero degrees north
              Traverse.Add(vec);
              lstPointIds.Add(pGSLineInPath.ToPoint);
              pPointCalc.AddLine(pGSLineInPath);
            }

            if (pStartPoint == null)
            {
              if (bReversed)
                pStartPoint = pCadastralPts.GetPoint(pGSLineInPath.ToPoint);
              else
                pStartPoint = pCadastralPts.GetPoint(pGSLineInPath.FromPoint);
            }

            if (bReversed)
              pClosingPoint = pCadastralPts.GetPoint(pGSLineInPath.FromPoint);
            else
              pClosingPoint = pCadastralPts.GetPoint(pGSLineInPath.ToPoint);
              }
            }

            if (pStartPoint == null)
            {
              pCadUndoRedo.WriteUndoRedoSession(false);
              return;
            }
            IPoint pStart = new PointClass();
            pStart.X = pStartPoint.X;
            pStart.Y = pStartPoint.Y;

            string sAdjustMethod = "Compass";
            esriParcelAdjustmentType eAdjMethod = esriParcelAdjustmentType.esriParcelAdjustmentCompass;

            if (pCadastralEditorSettings2.ParcelAdjustment == esriParcelAdjustmentType.esriParcelAdjustmentNone ||
              pCadastralEditorSettings2.ParcelAdjustment == esriParcelAdjustmentType.esriParcelAdjustmentCompass)
              eAdjMethod = esriParcelAdjustmentType.esriParcelAdjustmentCompass;
            else if (pCadastralEditorSettings2.ParcelAdjustment == esriParcelAdjustmentType.esriParcelAdjustmentCrandall)
            {
              sAdjustMethod = "Crandall";
              eAdjMethod = pCadastralEditorSettings2.ParcelAdjustment;
            }
            else if (pCadastralEditorSettings2.ParcelAdjustment == esriParcelAdjustmentType.esriParcelAdjustmentTransit)
            {
              sAdjustMethod = "Transit";
              eAdjMethod = pCadastralEditorSettings2.ParcelAdjustment;
            }

            pPointCalc.CalculatePoints(eAdjMethod, pStartPoint.Id, pStartPoint, pClosingPoint.Id, pClosingPoint, true);
            ITraverseClosure pClose = pPointCalc.Closure;
            List<string> lstCoursesFromTo = new List<string>();
            List<IVector3D> AdjustedTraverse = new List<IVector3D>();
            double dAdjustedPointX = 0; double dAdjustedPointY = 0;
            double dPreviousPointX = 0; double dPreviousPointY = 0;

            for (int i = 0; i < pClose.CourseCount; i++)
            {
              IGSPoint pPt = pCadastralPts.GetPoint(lstPointIds[i]);
              dAdjustedPointY = pPointCalc.GetCalculatedPoint(lstPointIds[i], ref dAdjustedPointX);
              string sFromTo = "";
              IVector3D pAdjustedLine = new Vector3DClass();
              if (i == 0)
              {
            sFromTo = pStartPoint.Id.ToString() + "-" + lstPointIds[i].ToString();
            pAdjustedLine.SetComponents(dAdjustedPointX - pStartPoint.X, dAdjustedPointY - pStartPoint.Y, 0);
              }
              else
              {
            sFromTo = lstPointIds[i - 1].ToString() + "-" + lstPointIds[i].ToString();
            pAdjustedLine.SetComponents(dAdjustedPointX - dPreviousPointX, dAdjustedPointY - dPreviousPointY, 0);
              }
              lstCoursesFromTo.Add(sFromTo);

              IVector3D Z_Axis = new Vector3DClass();
              Z_Axis.SetComponents(0, 0, 100);

              pAdjustedLine.Rotate(TheRotation, Z_Axis);
              AdjustedTraverse.Add(pAdjustedLine);

              dPreviousPointX = dAdjustedPointX;
              dPreviousPointY = dAdjustedPointY;

              pPt.X = dAdjustedPointX;
              pPt.Y = dAdjustedPointY;

              if (!pCadastralEditorSettings2.MeasurementView)
            pFixedPoints.SetFixedPoint(lstPointIds[i], true);
            }

            double dMisclosureDistance = pClose.MisclosureDistance; double dMisclosureBearing = pClose.MisclosureDirection;
            IVector MiscloseVector = new Vector3DClass();

            IEditProperties2 pEdProps = pEd as IEditProperties2;

            IAngularConverter pAngConv = new AngularConverterClass();
            pAngConv.SetAngle(dMisclosureBearing, esriDirectionType.esriDTNorthAzimuth, esriDirectionUnits.esriDURadians);
            //int iPrec = 7;
            //if (pConstr.Parcel.Plan.AngleUnits == esriDirectionUnits.esriDUDegreesMinutesSeconds)
            //  iPrec = 0;

            string sMiscloseBearing = pAngConv.GetString(pEdProps.DirectionType, pEdProps.DirectionUnits, pEdProps.AngularUnitPrecision);

            Utilities UTIL = new Utilities();
            string sRatio = "High Accuracy";

            if (pClose.RelativeErrorRatio < 10000)
              sRatio = "1:" + pClose.RelativeErrorRatio.ToString("0");

            if (dMisclosureDistance >= 0.001)
              sMiscloseBearing = UTIL.FormatDirectionDashesToDegMinSecSymbols(sMiscloseBearing);
            else
              sMiscloseBearing = "----";

            ICadastralUnitConversion pCadUnitConverter = new CadastralUnitConversionClass();
            double dMetersPerUnit = pCadUnitConverter.ConvertDouble(1, pConstr.Parcel.Plan.DistanceUnits, esriCadastralDistanceUnits.esriCDUMeter);

            string sReport = "Closure:" + Environment.NewLine +
              "        error:  " + sRatio + Environment.NewLine +
              "        distance:  " + (dMisclosureDistance / dMetersPerUnit).ToString("0.000") + Environment.NewLine +
              "        bearing:  " + sMiscloseBearing + Environment.NewLine +
              "        xdist:  " + (pClose.MisclosureX / dMetersPerUnit).ToString("0.000") + Environment.NewLine +
              "        ydist:  " + (pClose.MisclosureY / dMetersPerUnit).ToString("0.000") + Environment.NewLine +
              "        courses: " + (pClose.CourseCount) + Environment.NewLine +
              Environment.NewLine + "Adjustment:" + Environment.NewLine +
              "        method: " + sAdjustMethod;

            dlgTraverseResults dlgTraverseResults = new dlgTraverseResults();
            AddTraverseInfoToGrid(pConstr.Parcel.Plan, dlgTraverseResults.dataGridView1, Traverse, AdjustedTraverse, lstCoursesFromTo);
            dlgTraverseResults.txtMiscloseReport.Text = sReport;
            DialogResult dRes = dlgTraverseResults.ShowDialog();
            if (dRes == DialogResult.Cancel)
            {
              //since we cancelled, set the points back
              foreach (int i in lstPointIds)
            pFixedPoints.SetFixedPoint(i, false);
              pCadUndoRedo.WriteUndoRedoSession(false);
            }
            else
              pCadUndoRedo.WriteUndoRedoSession(true);
              }
              catch (Exception ex)
              {
            MessageBox.Show(ex.Message + Environment.NewLine + "Line number:" + ex.LineNumber().ToString()
              + " in " + ex.TargetSite.Name, "Traverse");
            pCadUndoRedo.WriteUndoRedoSession(false);
              }
        }
        protected override void OnClick()
        {
            FileType ft = FileType.COGOToolbarTraverse; //start out assuming it is a cogo traverse file
              m_count = 1;
              ICadastralEditor pCadEd = (ICadastralEditor)ArcMap.Application.FindExtensionByName("esriCadastralUI.CadastralEditorExtension");
              ICadastralFabric pCadFabric = pCadEd.CadastralFabric;
              ICadastralExtensionManager2 pCadExtMan = (ICadastralExtensionManager2)pCadEd;
              IParcelEditManager pParcEditorMan = (IParcelEditManager)pCadEd;
              IParcelConstruction pTrav = pParcEditorMan.ParcelConstruction;

              if (!(pParcEditorMan.InTraverseEditMode) && !(pParcEditorMan.InConstructionEditMode))
              {//if this is not a construction or a new parcel, then get out.
            MessageBox.Show("Please create a new parcel or new construction first, and then try again.");
            return;
              }

              //Make sure the lines grid is selected
              Utilities UTILS = new Utilities();
              UTILS.SelectCadastralPropertyPage((ICadastralExtensionManager)pCadExtMan, "lines");

              IParcelConstruction3 pTrav3 = (IParcelConstruction3)pTrav;
              IGSParcel pParcel = null;
              try
              {
            pParcel = pTrav.Parcel;
              }
              catch (COMException error)
              {
            MessageBox.Show(error.Message.ToString());
            return;
              }
              //go get a traverse file
              // Display .Net dialog for File selection.
              OpenFileDialog openFileDialog = new OpenFileDialog();
              // Set File Filter
              openFileDialog.Filter = "Traverse file (*.txt)|*.txt|Comma-delimited(*.csv)|*.csv|All Files|*.*";
              // Disable multi-select
              openFileDialog.Multiselect = false;
              // Don't need to Show Help
              openFileDialog.ShowHelp = false;
              // Set Dialog Title
              openFileDialog.Title = "Load file";
              openFileDialog.FilterIndex = 2;
              // Display Open File Dialog
              if (openFileDialog.ShowDialog() != DialogResult.OK)
              {
            openFileDialog = null;
            return;
              }

              TextReader tr = null;
              try
              {
            tr = new StreamReader(openFileDialog.FileName);
              }
              catch (Exception ex)
              {
            MessageBox.Show(ex.Message);
            return;
              }

              string sCourse = "";
              int iCount = 0;
              string[] sFileLine = new string[0]; //define as dynamic array
              //initialize direction units and format
              esriDirectionType enumDirectionType = esriDirectionType.esriDTQuadrantBearing;
              esriDirectionUnits enumDirectionUnits = esriDirectionUnits.esriDUDegreesMinutesSeconds;

              //initialize start and end points
              IPoint StartPoint = new PointClass();
              IPoint EndPoint = new PointClass();
              bool IsLoopTraverse = false;

              //fill the array with the lines from the file
              while (sCourse != null)
              {
            sCourse = tr.ReadLine();
            try
            {
              if (sCourse.Trim().Length >= 1) //test for empty lines
              {
            RedimPreserveString(ref sFileLine, 1);
            sFileLine[iCount] = sCourse;
              }
              iCount++;
              sCourse = sCourse.ToLower();
              if (sCourse.Contains("dt"))
              {
            if (sCourse.Contains("qb"))
              enumDirectionType = esriDirectionType.esriDTQuadrantBearing;
            else if (sCourse.Contains("na"))
              enumDirectionType = esriDirectionType.esriDTNorthAzimuth;
            else if (sCourse.Contains("sa"))
              enumDirectionType = esriDirectionType.esriDTSouthAzimuth;
            else if (sCourse.Contains("p"))
              enumDirectionType = esriDirectionType.esriDTPolar;
              }
              if (sCourse.Contains("du"))
              {
            if (sCourse.Contains("dms"))
              enumDirectionUnits = esriDirectionUnits.esriDUDegreesMinutesSeconds;
            else if (sCourse.Contains("dd"))
              enumDirectionUnits = esriDirectionUnits.esriDUDecimalDegrees;
            else if (sCourse.Contains("g"))
              enumDirectionUnits = esriDirectionUnits.esriDUGons;
            else if (sCourse.Contains("r"))
              enumDirectionUnits = esriDirectionUnits.esriDURadians;
              }
              if (sCourse.Contains("sp"))
              {//start point
            string[] XY = sCourse.Split(' ');
            double x = Convert.ToDouble(XY[1]);
            double y = Convert.ToDouble(XY[2]);
            StartPoint.PutCoords(x, y);
              }

              if (sCourse.Contains("ep"))
              {//end point
            string[] XY = sCourse.Split(' ');
            double x = Convert.ToDouble(XY[1]);
            double y = Convert.ToDouble(XY[2]);
            EndPoint.PutCoords(x, y);
              }

              if (sCourse.Contains("tometricfactor"))
              {//this handles the comma-separated file case
            string[] sScaleFactor = sCourse.Split(',');
            m_dScaleFactor = Convert.ToDouble(sScaleFactor[1]);
              }
            }
            catch { }
              }
              tr.Close(); //close the file and release resources
              string sFileExt = System.IO.Path.GetExtension(openFileDialog.FileName.TrimEnd());
              if ((sFileExt.ToLower() == ".csv") && (sFileLine[0].Contains(",")))
              {//if it's a comma-delimited file
            ft = FileType.CommaDelimited;
              }

              //Test for loop traverse
              if (!(EndPoint.IsEmpty))
              {
            if (EndPoint.Compare(StartPoint) == 0)
              IsLoopTraverse = true;
            else
              IsLoopTraverse = false;
              }

              //get highest point id number in grid, and get the to point id on the last line.
              IGSLine pParcelLine = null;
              int iFirstToNode = -1;
              int iLastToNode = -1;
              int iHighestPointID = -1;
              for (int i = 0; i < pTrav.LineCount; i++)
              {
            if (pTrav.GetLine(i, ref pParcelLine))
            {
              if (iFirstToNode < 0)
            iFirstToNode = pParcelLine.ToPoint;
              iLastToNode = pParcelLine.ToPoint;
              iHighestPointID = iHighestPointID < pParcelLine.ToPoint ? pParcelLine.ToPoint : iHighestPointID;
              iHighestPointID = iHighestPointID < pParcelLine.FromPoint ? pParcelLine.FromPoint : iHighestPointID;
            }
              }

              ICadastralUndoRedo pCadUndoRedo = pTrav as ICadastralUndoRedo;
              pCadUndoRedo.StartUndoRedoSession("Load Lines From File");
              try
              {
            IGSLine pLine = null;
            int iLinecount = iCount - 1;
            ISegment pExitTangent = null;
            for (iCount = 0; iCount <= iLinecount; iCount++)
            {
              if (ft == FileType.COGOToolbarTraverse)
              {
            {
              pLine = null;

              ICircularArc pCircArc;//need to use this in the test to handle curves greater than 180
              pLine = CreateGSLine(sFileLine[iCount], enumDirectionUnits, enumDirectionType,
                      pExitTangent, out pExitTangent, out pCircArc);
              //exit tangent from the previous course is the new entry tangent for the next course

              if (pLine != null)
              {
                if (pCircArc == null)
                {// straight line
                  //if this is the last course then set the to point to 1
                  if ((iCount == iLinecount) && IsLoopTraverse)
                    pLine.ToPoint = 1;
                  pTrav.InsertGridRow(-1, pLine);
                }
                else
                {//some post-processing needed to figure out if a 180 curve needs to be split
                  if (Math.Abs(pCircArc.CentralAngle) < (Math.PI - 0.000001))//some tolerance for being close to 180
                  { //this curve is OK
                    //if this is the last course then set the to point to 1
                    if ((iCount == iLinecount) && IsLoopTraverse)
                      pLine.ToPoint = 1;
                    pTrav.InsertGridRow(-1, pLine);
                  }
                  else
                  {//curve is greater than or equal to 180, special treatment for GSE needed to split curve into 2 parts
                    //first decrement the count
                    m_count -= 1;
                    ISegment pFullSegment = (ISegment)pCircArc; ISegment pFirstHalf; ISegment pSecondHalf;
                    pFullSegment.SplitAtDistance(0.5, true, out pFirstHalf, out pSecondHalf);
                    IConstructCircularArc2 pCircArcConstr1 = new CircularArcClass();
                    ICircularArc pCircArc1 = (ICircularArc)pCircArcConstr1;
                    pCircArcConstr1.ConstructEndPointsRadius(pFirstHalf.FromPoint,
                            pFirstHalf.ToPoint, pCircArc.IsCounterClockwise, pCircArc.Radius, true);
                    ILine2 pTangentLine = new LineClass();
                    pCircArc1.QueryTangent(esriSegmentExtension.esriExtendTangentAtTo, 0, false, 100, pTangentLine);
                    string sTangentBearing = PolarRadians_2_DirectionString(pTangentLine.Angle,
                            enumDirectionType, enumDirectionUnits);
                    sTangentBearing = sTangentBearing.Replace(" ", "");
                    string sHalfDelta = Radians_2_Angle(Math.Abs(pCircArc1.CentralAngle), enumDirectionUnits);
                    string sSide = pCircArc.IsCounterClockwise ? " L " : " R ";

                    ISegment EntryTangent = (ISegment)pTangentLine;

                    //construct the string for the first piece
                    // looks similar to this: NC R 500 D 181-59-59 T N59-59-59W L
                    string sFirstCurve = "NC R " + Convert.ToString(pCircArc.Radius)
                          + " D " + sHalfDelta + " T " + sTangentBearing + sSide;

                    IGSLine pLineFirstCurve = CreateGSLine(sFirstCurve, enumDirectionUnits, enumDirectionType, pExitTangent,
                            out pExitTangent, out pCircArc);
                    pTrav.InsertGridRow(-1, pLineFirstCurve);

                    ICircularArc pCircArc2 = (ICircularArc)pCircArcConstr1;
                    pCircArcConstr1.ConstructEndPointsRadius(pSecondHalf.FromPoint,
                            pSecondHalf.ToPoint, pCircArc.IsCounterClockwise, pCircArc.Radius, true);
                    pCircArc2.QueryTangent(esriSegmentExtension.esriExtendTangentAtTo, 0, false, 100, pTangentLine);
                    sTangentBearing = PolarRadians_2_DirectionString(pTangentLine.Angle, enumDirectionType, enumDirectionUnits);
                    sTangentBearing = sTangentBearing.Replace(" ", "");
                    //construct the string for the second piece
                    // looks similar to this: NC R 500 D 181-59-59 T N59-59-59W L
                    string sSecondCurve = "NC R " + Convert.ToString(pCircArc.Radius)
                          + " D " + sHalfDelta + " T " + sTangentBearing + sSide;

                    IGSLine pLineSecondCurve = CreateGSLine(sSecondCurve, enumDirectionUnits, enumDirectionType, pExitTangent,
                            out pExitTangent, out pCircArc);
                    //if this is the last course then set the to point to 1
                    if ((iCount == iLinecount) && IsLoopTraverse)
                      pLine.ToPoint = 1;
                    pTrav.InsertGridRow(-1, pLineSecondCurve);
                  }
                }
              }
            }
              }
              else//this is comma-separated version of the grid, so do the following
              {
            pLine = null;
            //apply a point id number offset if there are existing lines in the grid.
            if (iHighestPointID > -1 && iCount >= 3)
            {
              string[] sTraverseCourse = sFileLine[iCount].Split(',');
              if (iCount == 3)
              {
                sTraverseCourse[0] = iLastToNode.ToString();
                sTraverseCourse[5] = (Convert.ToInt32(sTraverseCourse[5]) + iHighestPointID).ToString();
              }
              else if (iCount > 3)
              {
                sTraverseCourse[0] = (Convert.ToInt32(sTraverseCourse[0]) + iHighestPointID).ToString();
                sTraverseCourse[5] = (Convert.ToInt32(sTraverseCourse[5]) + iHighestPointID).ToString();
              }

              sFileLine[iCount] = sTraverseCourse[0];
              int iAttCount = sTraverseCourse.GetLength(0);
              for (int j = 1; j < iAttCount; j++)
                sFileLine[iCount] += "," + sTraverseCourse[j];
            }

            pLine = CreateGSLineFromCommaSeparatedString(sFileLine[iCount], enumDirectionUnits, enumDirectionType);

            if (pLine != null)
              pTrav.InsertGridRow(-1, pLine);
              }
            }
            pTrav3.UpdateGridFromGSLines(false);
            IParcelConstruction2 pConstr2 = (IParcelConstruction2)pTrav3; //hidden interface
            pConstr2.RecalculatePoints(); //explicit recalculate needed on a construction
            pParcel.Modified();
            pParcEditorMan.Refresh();
            pCadUndoRedo.WriteUndoRedoSession(true);
            sFileLine = null;
            openFileDialog = null;
              }
              catch(Exception ex)
              {
            MessageBox.Show(ex.Message,"Load Lines From File");
            pCadUndoRedo.WriteUndoRedoSession(false);
              }
        }
        private IGSLine CreateGSLineFromCommaSeparatedString(String inCourse, esriDirectionUnits inDirectionUnits,
            esriDirectionType inDirectionType)
        {
            string[] sCourse = inCourse.Split(',');
              int iUpperBnd = sCourse.GetUpperBound(0);
              double dToMeterUnitConversion = m_dScaleFactor;
              if (m_dScaleFactor == -1)
              {//if the scale factor wasn't found in the file for some reason
            Utilities UTILS = new Utilities();
            dToMeterUnitConversion = UTILS.ToMeterUnitConversion();
              }
              if (iUpperBnd <= 4) { return null; }
              IGSLine pLine = new GSLineClass();
              string sLineCat = "";
              string sLineUserType = "";
              string sAccCat = "";
              string sFromPt = (string)sCourse.GetValue(0);//from point
              //string sTemplate = (string)sCourse.GetValue(1);//line template
              string sDirection = (string)sCourse.GetValue(1);//direction
              string sDistance = (string)sCourse.GetValue(2);//distance
              string sRadius = (string)sCourse.GetValue(3);//radius
              string sChord = (string)sCourse.GetValue(4);//chord
              string sToPt = (string)sCourse.GetValue(5);//to point
              if (iUpperBnd >= 6)
            sLineCat = (string)sCourse.GetValue(6); //line category
              if (iUpperBnd >= 7)
            sLineUserType = (string)sCourse.GetValue(7); //line user type
              if (iUpperBnd >= 8)
            sAccCat = (string)sCourse.GetValue(8); //accuracy category

              int iFromPt = -1; //from point
              int iToPt = -1; //to point
              int iLineCat = -1;//line category
              int iLineUserType = -1;//line user type
              int iAccCat = -1;//accuracy
              double dDistance = -123456789;//distance
              double dChord = -123456789;//chord
              double dRadius = -123456789;//radius

              try
              {
            iFromPt = Convert.ToInt32(sFromPt); //from point
            iToPt = Convert.ToInt32(sToPt); //to point
            if (sLineCat.Trim().Length > 0)
              iLineCat = Convert.ToInt32(sLineCat); //line category
            if (sLineUserType.Trim().Length > 0)
              iLineUserType = Convert.ToInt32(sLineUserType); //line user type
            if (sAccCat.Trim().Length > 0)
              iAccCat = Convert.ToInt32(sAccCat); //accuracy
            if (sDistance.Trim().Length > 0)
              dDistance = Convert.ToDouble(sDistance); //distance
            if (sChord.Trim().Length > 0)
              dChord = Convert.ToDouble(sChord); //chord
            if (sRadius.Trim().Length > 0)
              dRadius = Convert.ToDouble(sRadius); //radius
              }
              catch
              {
            return null;
              }
              double dBear = DirectionString_2_NorthAzimuth(sDirection, inDirectionType, inDirectionUnits);
              pLine.Bearing = dBear;
              if (dDistance != -123456789)
            pLine.Distance = dDistance * dToMeterUnitConversion;
              if (dChord != -123456789)
            pLine.Distance = dChord * dToMeterUnitConversion;
              if (dRadius != -123456789)
            pLine.Radius = dRadius * dToMeterUnitConversion;
              pLine.FromPoint = iFromPt;
              pLine.ToPoint = iToPt;
              if (iAccCat > -1)
            pLine.Accuracy = iAccCat;
              if (iLineUserType > -1)
            pLine.LineType = iLineUserType;
              if (iLineCat > -1)
            pLine.Category = (esriCadastralLineCategory)iLineCat;
              return pLine;
        }
        private IGSLine CreateGSLine(String inCourse, esriDirectionUnits inDirectionUnits,
            esriDirectionType inDirectionType, ISegment EntryTangent,
            out ISegment ExitTangent, out ICircularArc outCircularArc)
        {
            //
              string[] sCourse = inCourse.Split(' ');
              Utilities UTILS = new Utilities();
              double dToMeterUnitConversion = UTILS.ToMeterUnitConversion();
              // ISegment ExitTangent = null;
              string item = (string)sCourse.GetValue(0);
              if (item.ToLower() == "dd")
              {//Direction distance -- straight line course
            IGSLine pLine = new GSLineClass();
            double dBear = DirectionString_2_NorthAzimuth((string)sCourse.GetValue(1), inDirectionType, inDirectionUnits);
            pLine.Bearing = dBear;
            pLine.Distance = Convert.ToDouble(sCourse.GetValue(2)) * dToMeterUnitConversion;
            pLine.FromPoint = m_count;
            m_count += 1;
            pLine.ToPoint = m_count;
            //Now define the tangent exit segment
            //in case it's needed for the next course (TC tangent curve, or Angle deflection)
            double dPolar = DirectionString_2_PolarRadians((string)sCourse.GetValue(1),
                inDirectionType, inDirectionUnits);
            IPoint pFromPt = new PointClass();
            pFromPt.PutCoords(1000, 1000);
            IConstructPoint2 pToPt = new PointClass();
            pToPt.ConstructAngleDistance(pFromPt, dPolar, 100);
            ILine ExitTangentLine = new LineClass();
            ExitTangentLine.PutCoords(pFromPt, (IPoint)pToPt);
            ExitTangent = (ISegment)ExitTangentLine;
            outCircularArc = null;
            Marshal.ReleaseComObject(pFromPt);
            Marshal.ReleaseComObject(pToPt);
            return pLine;
              }
              if (item.ToLower() == "ad")
              {//Angle deflection distance
            IGSLine pLine = new GSLineClass();
            double dDeflAngle = Angle_2_Radians((string)sCourse.GetValue(1), inDirectionUnits);
            //now need to take the previous tangent segment, reverse its orientation and
            //add +ve clockwise to get the bearing
            ILine calcLine = (ILine)EntryTangent;
            double dBear = PolarRAD_2_SouthAzimuthRAD(calcLine.Angle) + dDeflAngle;
            pLine.Bearing = dBear;
            pLine.Distance = Convert.ToDouble(sCourse.GetValue(2)) * dToMeterUnitConversion;
            pLine.FromPoint = m_count;
            m_count += 1;
            pLine.ToPoint = m_count;
            //Now define the tangent exit segment
            //in case it's needed for the next course (TC tangent curve, or Angle deflection)
            IPoint pFromPt = new PointClass();
            pFromPt.PutCoords(1000, 1000);
            IConstructPoint2 pToPt = new PointClass();
            double dPolar = NorthAzimuthRAD_2_PolarRAD(dBear);
            pToPt.ConstructAngleDistance(pFromPt, dPolar, 100);
            ILine ExitTangentLine = new LineClass();
            ExitTangentLine.PutCoords(pFromPt, (IPoint)pToPt);
            ExitTangent = (ISegment)ExitTangentLine;
            outCircularArc = null;
            Marshal.ReleaseComObject(pFromPt);
            Marshal.ReleaseComObject(pToPt);

            return pLine;
              }
              else if ((item.ToLower() == "nc") || (item.ToLower() == "tc"))
              {
            double dChordlength;
            double dChordBearing;
            ICircularArc pArc = ConstructCurveFromString(inCourse, EntryTangent,
                inDirectionType, inDirectionUnits, out dChordlength, out dChordBearing);
            try
            {
              IGSLine pLine = new GSLineClass();
              pLine.Bearing = PolarRAD_2_NorthAzimuthRAD(dChordBearing);
              pLine.Radius = pArc.Radius * dToMeterUnitConversion;//convert to meters
              if (pArc.IsCounterClockwise) { pLine.Radius = pLine.Radius * -1; }
              pLine.Distance = dChordlength * dToMeterUnitConversion; //convert to meters
              pLine.FromPoint = m_count;
              m_count += 1;
              pLine.ToPoint = m_count;
              ILine pTangentLine = new LineClass();
              pArc.QueryTangent(esriSegmentExtension.esriExtendTangentAtTo, 1, true, 100, pTangentLine);
              //pass the exit tangent back out for use as next entry tangent
              ExitTangent = (ISegment)pTangentLine;
              outCircularArc = pArc;
              return pLine;
            }
            catch { }
              }
              outCircularArc = null;
              ExitTangent = null;
              return null;
        }