private void modelDrawer1_Paint(object sender, PaintEventArgs e) { SoilTunnelGeometry stg = new SoilTunnelGeometry(20, new float[] { 3, 4, 5 }, 2, 3, 6); Graphics g = e.Graphics; modelDrawer1.DrawSoilTunnelModel(stg, g); }
public override StationGeometry GetStationGeometry() { SoilTunnelGeometry stg = null; if (Tunnel != null) { stg = new SoilTunnelGeometry( soilWidth: (float)Tunnel.Radius * 6.0f, soilHeight: SoilLayers.Select(r => r.Top - r.Bottom).ToArray(), overlyingSoilHeight: MethodProperty.OverLayingSoilHeight, tunnelRadius: (float)Tunnel.Radius, tunnelSegmentNum: Tunnel.SegmentNum); } return(stg); }
public void DrawSoilTunnelModel(SoilTunnelGeometry stg, Graphics g = null) { g = g ?? CreateGraphics(); g.Clear(BackColor); //构造图形平移和缩放需要的参数 float soilHeight = stg.SoilHeight.Sum();; double rx, ry, r; if (this.Width != 0 && this.Height != 0 && stg.SoilWidth > 0) { rx = (float)this.Width / stg.SoilWidth; ry = (float)this.Height / soilHeight; r = Math.Min(rx, ry); // r 值越小,则模型缩得越多 } else { r = 10; } modelScale = (float)(r * 0.8); float translatex = (this.Width - stg.SoilWidth * modelScale) / 2; float translatey = (this.Height - soilHeight * modelScale) / 2; //图形平移 g.TranslateTransform(translatex, translatey); //图形缩放 g.ScaleTransform(modelScale, modelScale); //由 stg 得到后面画圆形隧道需要的参数, tunnelWidth,tunnelHeight,segmentNum,radius int segmentNum = stg.TunnelSegmentNum; float radius = stg.TunnelRadius; float tunnelWidth = 2 * radius; float tunnelHeight = 2 * radius; #region --- 画土层 Pen soilPen = new Pen(Color.Black, 1); var colors = sdUtils.ClassicalColorsExpand(stg.SoilHeight.Length); for (int i = 0; i < stg.SoilHeight.Length; i++) { float[] SoilHeighti = new float[i]; Array.Copy(stg.SoilHeight, 0, SoilHeighti, 0, i); float soilHeighti = SoilHeighti.Sum(); try { SolidBrush soil = new SolidBrush(colors[i]); g.FillRectangle(soil, 0, soilHeighti, stg.SoilWidth, stg.SoilHeight[i]); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region --- 画结构 Pen structurePen1 = new Pen(Color.Black, 8f / modelScale); Pen structurePen2 = new Pen(Color.White, 4f / modelScale); SolidBrush structure = new SolidBrush(Color.White); g.FillEllipse(structure, stg.SoilWidth / 2 - radius, stg.OverlyingSoilHeight, tunnelWidth, tunnelHeight); //SolidBrush structure1 = new SolidBrush(Color.Black); //g.FillEllipse(structure1, stg.SoilWidth / 2 - radius - 2f / modelScale, stg.OverlyingSoilHeight - 2f / modelScale, tunnelWidth + 4f / modelScale, tunnelHeight + 4f / modelScale); //g.FillEllipse(structure, stg.SoilWidth / 2 - radius + 2f / modelScale, stg.OverlyingSoilHeight + 2f / modelScale, tunnelWidth - 4f / modelScale, tunnelHeight - 4f / modelScale); double[] Angles = new double[segmentNum + 1]; if (segmentNum != 0) { double _angle = 360 / segmentNum; for (int i = 0; i < segmentNum + 1; i++) { double _angleI = _angle * (i + 0.5); Angles[i] = _angleI; } PointF point1 = new PointF(stg.SoilWidth / 2, (stg.OverlyingSoilHeight + radius)); for (int i = 0; i < segmentNum; i++) { g.DrawArc(structurePen1, stg.SoilWidth / 2 - radius, stg.OverlyingSoilHeight, tunnelWidth, tunnelHeight, (float)Angles[i], (float)_angle); float a = (float)(stg.SoilWidth / 2 + (radius + 4f / modelScale) * Math.Cos(Angles[i] * Math.PI / 180)); float b = (float)((stg.OverlyingSoilHeight + radius) + (radius + 4f / modelScale) * Math.Sin(Angles[i] * Math.PI / 180)); PointF point2 = new PointF(a, b); g.DrawLine(structurePen2, point1, point2); } } #endregion }