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 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);
        }