private ISegmentCollection createSegments(Cell cell, BoostVoronoi bv, ArcConstructionMethods method, ISpatialReference spatialReference) { List <Cell> cells = bv.Cells; List <Edge> edges = bv.Edges; List <Vertex> vertices = bv.Vertices; IPoint previousEndPoint = null; ISegmentCollection segmentCollection = new PolygonClass() { SpatialReference = spatialReference }; // As per boost documentation, edges are returned in counter clockwise (CCW) rotation. // voronoi_edge_type* next() Returns the pointer to the CCW next edge within the corresponding Voronoi cell. Edges not necessarily share a common vertex (e.g. infinite edges). for (int i = cell.EdgesIndex.Count - 1; i >= 0; i--) { Edge edge = edges[cell.EdgesIndex[i]]; //If the vertex index equals -1, it means the edge is infinite. It is impossible to print the coordinates. if (!edge.IsFinite && edge.End < 0) { // this is the ending portion of a pair of infinite edges, file the previous edge with Start >= 0 Edge previous = null; for (int k = i + 1; k < cell.EdgesIndex.Count; k++) { previous = edges[cell.EdgesIndex[k]]; if (previous.End >= 0) { break; } previous = null; } if (previous == null) { for (int k = 0; k < i; k++) { previous = edges[cell.EdgesIndex[k]]; if (previous.End >= 0) { break; } previous = null; } } if (previous == null) { throw new Exception("No outbound infinite edge could be found"); } //Add a straight line segment Vertex start = vertices[previous.End]; IPoint FromPoint = new PointClass() { X = start.X, Y = start.Y, SpatialReference = spatialReference }; Vertex end = vertices[edge.Start]; IPoint ToPoint = new PointClass() { X = end.X, Y = end.Y, SpatialReference = spatialReference }; segmentCollection.AddSegment(new LineClass() { FromPoint = FromPoint, ToPoint = ToPoint, SpatialReference = spatialReference }); previousEndPoint = ToPoint; } else if (edge.IsFinite) { Vertex start = vertices[edge.End]; IPoint FromPoint = new PointClass() { X = start.X, Y = start.Y, SpatialReference = spatialReference }; if (previousEndPoint != null) { if ((Math.Abs(previousEndPoint.X - FromPoint.X) > 0.05 || Math.Abs(previousEndPoint.X - FromPoint.X) > 0.05)) { throw new Exception("Significant change between last end point and current start point"); } else { FromPoint = previousEndPoint; } } Vertex end = vertices[edge.Start]; IPoint ToPoint = new PointClass() { X = end.X, Y = end.Y, SpatialReference = spatialReference }; if (method == ArcConstructionMethods.Straight || edge.IsLinear) { segmentCollection.AddSegment(new LineClass() { FromPoint = FromPoint, ToPoint = ToPoint, SpatialReference = spatialReference }); previousEndPoint = ToPoint; } else { // We need three points, use start, end, mid-point between focus and directrix Cell twinCell = cells[edges[edge.Twin].Cell]; VPoint pointSite; VSegment lineSite; if (cell.ContainsPoint && twinCell.ContainsSegment) { pointSite = bv.RetrieveInputPoint(cell); lineSite = bv.RetrieveInputSegment(twinCell); } else if (cell.ContainsSegment && twinCell.ContainsPoint) { pointSite = bv.RetrieveInputPoint(twinCell); lineSite = bv.RetrieveInputSegment(cell); } else { throw new Exception("Invalid edge, curves should only be present between a point and a line"); } double scaleFactor = Convert.ToDouble(bv.ScaleFactor); IPoint aoPointSite = new Point() { X = Convert.ToDouble(pointSite.X) / scaleFactor, Y = Convert.ToDouble(pointSite.Y) / scaleFactor, SpatialReference = spatialReference }; ISegment aoLineSite = new LineClass() { FromPoint = new PointClass() { X = Convert.ToDouble(lineSite.Start.X) / scaleFactor, Y = Convert.ToDouble(lineSite.Start.Y) / scaleFactor, SpatialReference = spatialReference }, ToPoint = new PointClass() { X = Convert.ToDouble(lineSite.End.X) / scaleFactor, Y = Convert.ToDouble(lineSite.End.Y) / scaleFactor, SpatialReference = spatialReference }, SpatialReference = spatialReference }; if (method == ArcConstructionMethods.Approximate) { List <Vertex> sampledVerticed = null; try { sampledVerticed = bv.SampleCurvedEdge(edge, aoLineSite.Length / 10); } catch (FocusOnDirectixException e) { //Log any exception here is required sampledVerticed = new List <Vertex>() { start, end }; } catch (UnsolvableVertexException e) { sampledVerticed = new List <Vertex>() { start, end }; } sampledVerticed.Reverse(); List <IPoint> discretizedEdge = sampledVerticed.Select( p => new Point() { X = p.X, Y = p.Y } ).ToList <IPoint>(); IPoint prev = discretizedEdge[0]; foreach (IPoint v in discretizedEdge.Skip(1)) { segmentCollection.AddSegment(new LineClass() { FromPoint = new Point() { X = prev.X, Y = prev.Y, SpatialReference = spatialReference }, ToPoint = new Point() { X = v.X, Y = v.Y, SpatialReference = spatialReference }, SpatialReference = spatialReference }); prev = v; } previousEndPoint = discretizedEdge.Last(); } else if (method == ArcConstructionMethods.Circular) { IPoint nearPoint = ((IProximityOperator)aoLineSite).ReturnNearestPoint(aoPointSite, esriSegmentExtension.esriNoExtension); IPoint midpoint = new PointClass() { X = (nearPoint.X + aoPointSite.X) / 2, Y = (nearPoint.Y + aoPointSite.Y) / 2, SpatialReference = spatialReference }; IConstructCircularArc constArc = new CircularArcClass() { SpatialReference = spatialReference }; constArc.ConstructThreePoints(FromPoint, midpoint, ToPoint, false); ICircularArc arc = (ICircularArc)constArc; if (!arc.IsMinor) { constArc = new CircularArcClass() { SpatialReference = spatialReference }; constArc.ConstructEndPointsRadius(FromPoint, ToPoint, !arc.IsCounterClockwise, arc.Radius, true); arc = (ICircularArc)constArc; } segmentCollection.AddSegment((ISegment)arc); previousEndPoint = arc.ToPoint; } else if (method == ArcConstructionMethods.Ellipse) { IPoint nearPoint = ((IProximityOperator)aoLineSite).ReturnNearestPoint(aoPointSite, esriSegmentExtension.esriExtendTangents); nearPoint.SpatialReference = spatialReference; ILine lineToFocus = new LineClass() { FromPoint = nearPoint, ToPoint = aoPointSite, SpatialReference = spatialReference }; ILine semiMajor = new LineClass() { SpatialReference = spatialReference }; lineToFocus.QueryTangent(esriSegmentExtension.esriExtendTangentAtTo, 1, true, 100 * lineToFocus.Length, semiMajor); IPoint center = new PointClass() { X = (semiMajor.FromPoint.X + semiMajor.ToPoint.X) / 2, Y = (semiMajor.FromPoint.Y + semiMajor.ToPoint.Y) / 2, SpatialReference = spatialReference }; double minor_length = Math.Sqrt( Math.Pow(distance(semiMajor.FromPoint, ToPoint) + distance(semiMajor.ToPoint, ToPoint), 2) - Math.Pow(semiMajor.Length, 2)); IEllipticArc arc = new EllipticArcClass() { SpatialReference = spatialReference }; double rotation = lineToFocus.Angle; double from = GetAngle(center, FromPoint); arc.PutCoords(false, center, FromPoint, ToPoint, rotation, minor_length / semiMajor.Length, esriArcOrientation.esriArcMinor); segmentCollection.AddSegment((ISegment)arc); previousEndPoint = arc.ToPoint; } } } } return(segmentCollection); }
private ISegmentCollection createSegments(Cell cell,BoostVoronoi bv,ArcConstructionMethods method,ISpatialReference spatialReference) { List<Cell> cells = bv.Cells; List<Edge> edges = bv.Edges; List<Vertex> vertices = bv.Vertices; IPoint previousEndPoint = null; ISegmentCollection segmentCollection = new PolygonClass() { SpatialReference = spatialReference }; // As per boost documentation, edges are returned in counter clockwise (CCW) rotation. // voronoi_edge_type* next() Returns the pointer to the CCW next edge within the corresponding Voronoi cell. Edges not necessarily share a common vertex (e.g. infinite edges). for (int i = cell.EdgesIndex.Count - 1; i >= 0; i--) { Edge edge = edges[cell.EdgesIndex[i]]; //If the vertex index equals -1, it means the edge is infinite. It is impossible to print the coordinates. if (!edge.IsFinite && edge.End < 0) { // this is the ending portion of a pair of infinite edges, file the previous edge with Start >= 0 Edge previous = null; for (int k = i + 1; k < cell.EdgesIndex.Count; k++) { previous = edges[cell.EdgesIndex[k]]; if (previous.End >= 0) break; previous = null; } if (previous == null) { for (int k = 0; k < i; k++) { previous = edges[cell.EdgesIndex[k]]; if (previous.End >= 0) break; previous = null; } } if (previous == null) throw new Exception("No outbound infinite edge could be found"); //Add a straight line segment Vertex start = vertices[previous.End]; IPoint FromPoint = new PointClass() { X = start.X, Y = start.Y, SpatialReference = spatialReference }; Vertex end = vertices[edge.Start]; IPoint ToPoint = new PointClass() { X = end.X, Y = end.Y, SpatialReference = spatialReference }; segmentCollection.AddSegment(new LineClass() { FromPoint = FromPoint, ToPoint = ToPoint, SpatialReference = spatialReference }); previousEndPoint = ToPoint; } else if (edge.IsFinite) { Vertex start = vertices[edge.End]; IPoint FromPoint = new PointClass() { X = start.X, Y = start.Y, SpatialReference = spatialReference }; if (previousEndPoint != null) { if ((Math.Abs(previousEndPoint.X - FromPoint.X) > 0.05 || Math.Abs(previousEndPoint.X - FromPoint.X) > 0.05)) throw new Exception("Significant change between last end point and current start point"); else FromPoint = previousEndPoint; } Vertex end = vertices[edge.Start]; IPoint ToPoint = new PointClass() { X = end.X, Y = end.Y, SpatialReference = spatialReference }; if (method == ArcConstructionMethods.Straight || edge.IsLinear) { segmentCollection.AddSegment(new LineClass() { FromPoint = FromPoint, ToPoint = ToPoint, SpatialReference = spatialReference }); previousEndPoint = ToPoint; } else { // We need three points, use start, end, mid-point between focus and directrix Cell twinCell = cells[edges[edge.Twin].Cell]; VPoint pointSite; VSegment lineSite; if (cell.ContainsPoint && twinCell.ContainsSegment) { pointSite = bv.RetrieveInputPoint(cell); lineSite = bv.RetrieveInputSegment(twinCell); } else if (cell.ContainsSegment && twinCell.ContainsPoint) { pointSite = bv.RetrieveInputPoint(twinCell); lineSite = bv.RetrieveInputSegment(cell); } else { throw new Exception("Invalid edge, curves should only be present between a point and a line"); } double scaleFactor = Convert.ToDouble(bv.ScaleFactor); IPoint aoPointSite = new Point() { X = Convert.ToDouble(pointSite.X) / scaleFactor, Y = Convert.ToDouble(pointSite.Y) / scaleFactor, SpatialReference = spatialReference }; ISegment aoLineSite = new LineClass() { FromPoint = new PointClass() { X = Convert.ToDouble(lineSite.Start.X) / scaleFactor, Y = Convert.ToDouble(lineSite.Start.Y) / scaleFactor, SpatialReference = spatialReference }, ToPoint = new PointClass() { X = Convert.ToDouble(lineSite.End.X) / scaleFactor, Y = Convert.ToDouble(lineSite.End.Y) / scaleFactor, SpatialReference = spatialReference }, SpatialReference = spatialReference }; if (method == ArcConstructionMethods.Approximate) { List<Vertex> sampledVerticed = null; try { sampledVerticed = bv.SampleCurvedEdge(edge, aoLineSite.Length / 10); } catch (FocusOnDirectixException e) { //Log any exception here is required sampledVerticed = new List<Vertex>() { start, end }; } catch (UnsolvableVertexException e) { sampledVerticed = new List<Vertex>() { start, end }; } sampledVerticed.Reverse(); List<IPoint> discretizedEdge = sampledVerticed.Select( p => new Point() { X = p.X, Y = p.Y } ).ToList<IPoint>(); IPoint prev = discretizedEdge[0]; foreach (IPoint v in discretizedEdge.Skip(1)) { segmentCollection.AddSegment(new LineClass() { FromPoint = new Point() { X = prev.X, Y = prev.Y, SpatialReference = spatialReference }, ToPoint = new Point() { X = v.X, Y = v.Y, SpatialReference = spatialReference }, SpatialReference = spatialReference }); prev = v; } previousEndPoint = discretizedEdge.Last(); } else if (method == ArcConstructionMethods.Circular) { IPoint nearPoint = ((IProximityOperator)aoLineSite).ReturnNearestPoint(aoPointSite, esriSegmentExtension.esriNoExtension); IPoint midpoint = new PointClass() { X = (nearPoint.X + aoPointSite.X) / 2, Y = (nearPoint.Y + aoPointSite.Y) / 2, SpatialReference = spatialReference }; IConstructCircularArc constArc = new CircularArcClass() { SpatialReference = spatialReference }; constArc.ConstructThreePoints(FromPoint, midpoint, ToPoint, false); ICircularArc arc = (ICircularArc)constArc; if (!arc.IsMinor) { constArc = new CircularArcClass() { SpatialReference = spatialReference }; constArc.ConstructEndPointsRadius(FromPoint, ToPoint, !arc.IsCounterClockwise, arc.Radius, true); arc = (ICircularArc)constArc; } segmentCollection.AddSegment((ISegment)arc); previousEndPoint = arc.ToPoint; } else if (method == ArcConstructionMethods.Ellipse) { IPoint nearPoint = ((IProximityOperator)aoLineSite).ReturnNearestPoint(aoPointSite, esriSegmentExtension.esriExtendTangents); nearPoint.SpatialReference = spatialReference; ILine lineToFocus = new LineClass() { FromPoint = nearPoint, ToPoint = aoPointSite, SpatialReference = spatialReference }; ILine semiMajor = new LineClass() { SpatialReference = spatialReference }; lineToFocus.QueryTangent(esriSegmentExtension.esriExtendTangentAtTo, 1, true, 100 * lineToFocus.Length, semiMajor); IPoint center = new PointClass() { X = (semiMajor.FromPoint.X + semiMajor.ToPoint.X) / 2, Y = (semiMajor.FromPoint.Y + semiMajor.ToPoint.Y) / 2, SpatialReference = spatialReference }; double minor_length = Math.Sqrt( Math.Pow(distance(semiMajor.FromPoint, ToPoint) + distance(semiMajor.ToPoint, ToPoint), 2) - Math.Pow(semiMajor.Length, 2)); IEllipticArc arc = new EllipticArcClass() { SpatialReference = spatialReference }; double rotation = lineToFocus.Angle; double from = GetAngle(center, FromPoint); arc.PutCoords(false, center, FromPoint, ToPoint, rotation, minor_length / semiMajor.Length, esriArcOrientation.esriArcMinor); segmentCollection.AddSegment((ISegment)arc); previousEndPoint = arc.ToPoint; } } } } return segmentCollection; }
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); } }
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); } }