向圖層中添加線段和向圖層中添加點是一樣的,其本質都是向圖層中添加一個圖元,只是屬於不同類型的圖元。並且點和線段是可以在一個圖存上共存的。代碼示例如下:
複製內容到剪貼板
代碼:
/// <summary>
/// 向圖層中添加線段
/// 2008年8月6日
/// <param name="tempLayerTableName">表名</param>
/// <param name="tempLayerName">圖層名</param>
/// <param name="startPoint">線段起點座標</param>
/// <param name="endPoint">線段終點座標</param>
/// </summary>
public static void AddPointToLayer(string tempLayerTableName, string tempLayerName, DPoint startPoint, DPoint endPoint)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
//獲取圖層和表
FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[tempLayerName];
MapInfo.Data.Table tblTemp = MapInfo.Engine.Session.Current.Catalog.GetTable(tempLayerTableName);
//創建線圖元及其樣式
FeatureGeometry pgLine = MultiCurve.CreateLine(workLayer.CoordSys, startPoint, endPoint);
MapInfo.Styles.SimpleLineStyle slsLine = new
MapInfo.Styles.SimpleLineStyle(new LineWidth(3, LineWidthUnit.Pixel),
2, System.Drawing.Color.OrangeRed);
MapInfo.Styles.CompositeStyle csLine = new MapInfo.Styles.CompositeStyle(slsLine);
MapInfo.Data.Feature ptLine = new MapInfo.Data.Feature(tblTemp.TableInfo.Columns);
ptPoint.Geometry = pgLine;
ptPoint.Style = csLine;
//將線圖元加入圖層
workLayer.Table.InsertFeature(ptLine);
複製內容到剪貼板
代碼:
/// <summary>
/// 向圖層中添加線段
/// 2008年8月6日
/// <param name="tempLayerTableName">表名</param>
/// <param name="tempLayerName">圖層名</param>
/// <param name="startPoint">線段起點座標</param>
/// <param name="endPoint">線段終點座標</param>
/// </summary>
public static void AddPointToLayer(string tempLayerTableName, string tempLayerName, DPoint startPoint, DPoint endPoint)
{
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
//獲取圖層和表
FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[tempLayerName];
MapInfo.Data.Table tblTemp = MapInfo.Engine.Session.Current.Catalog.GetTable(tempLayerTableName);
//創建線圖元及其樣式
FeatureGeometry pgLine = MultiCurve.CreateLine(workLayer.CoordSys, startPoint, endPoint);
MapInfo.Styles.SimpleLineStyle slsLine = new
MapInfo.Styles.SimpleLineStyle(new LineWidth(3, LineWidthUnit.Pixel),
2, System.Drawing.Color.OrangeRed);
MapInfo.Styles.CompositeStyle csLine = new MapInfo.Styles.CompositeStyle(slsLine);
MapInfo.Data.Feature ptLine = new MapInfo.Data.Feature(tblTemp.TableInfo.Columns);
ptPoint.Geometry = pgLine;
ptPoint.Style = csLine;
//將線圖元加入圖層
workLayer.Table.InsertFeature(ptLine);