public static IFeatureLayer FromXElement(XElement ele) { if (ele == null) { return(null); } string name = ele.Attribute("name").Value; bool visible = bool.Parse(ele.Attribute("visible").Value); bool enableDisplayLevel = true; if (ele.Attribute("enabledisplayLevel") != null) { enableDisplayLevel = bool.Parse(ele.Attribute("enabledisplayLevel").Value); } ScaleRange displayScaleRange = ScaleRange.FromXElement(ele.Element("DisplayScaleRange")); LabelDef labelDef = AgileMap.Core.LabelDef.FromXElement(ele.Element("LabelDef")); IFeatureRenderer renderer = PersistObject.ReflectObjFromXElement(ele.Element("Renderer")) as IFeatureRenderer; RotateFieldDef rotateFieldDef = RotateFieldDef.FromXElement(ele.Element("RotateField")); IFeatureClass fetclass = AgileMap.Core.FeatureClass.FromXElement(ele.Element("FeatureClass")); if (fetclass == null) { return(null); } fetclass.Name = name; IFeatureLayer lyr = new FeatureLayer(name, fetclass, renderer, labelDef); lyr.RotateFieldDef = rotateFieldDef; lyr.DisplayScaleRange = displayScaleRange; lyr.EnabledDisplayLevel = enableDisplayLevel; (lyr as ILayerDrawable).Visible = visible; // if (ele.Attribute("twostepflag") != null) { string twoStepflagstring = ele.Attribute("twostepflag").Value; lyr.IsTwoStepDraw = bool.Parse(twoStepflagstring); } // return(lyr); }
public void Draw(Matrix emptyRotateMatrix, Graphics g, LabelDef labelDef, ISymbol currentSymbol, Feature fet, QuickTransformArgs tranArgs) { string text = fet.GetFieldValue(labelDef.Fieldname); if (string.IsNullOrEmpty(text) || labelDef.InvalidValue.Equals(text)) { return; } Matrix oldMatrix = g.Transform; LabelLocation[] locs = GetLabelLocations(fet, labelDef); if (locs == null || locs.Length == 0) { return; } foreach (LabelLocation loc in locs) { //标注点在可视区域外 if (!_environment.ExtentOfProjectionCoord.IsContains(loc.Location)) { continue; } // pointF.X = (float)(loc.Location.X * tranArgs.kLon + tranArgs.bLon); pointF.Y = (float)(loc.Location.Y * tranArgs.kLat + tranArgs.bLat); #region debug //符号中心点 //(_environment.ConflictorForLabel as PixelConflictor).DrawTestRectangleF(new PointF(pointF.X - 1, pointF.Y - 1), new SizeF(2, 2), Color.Blue); #endregion PointF oldPt = pointF; SizeF fontSize = g.MeasureString(text, labelDef.LabelFont); //计算标注矩形区域 SizeF labelRect = SizeF.Empty; if (labelDef.AutoToNewline && text.Length > labelDef.CharcountPerLine) { int pos = (int)Math.Ceiling(text.Length / 2f); labelRect = g.MeasureString(text.Substring(0, pos), labelDef.LabelFont); labelRect.Height *= 2; labelRect.Height = (float)Math.Ceiling(labelRect.Height); } else { labelRect = g.MeasureString(text, labelDef.LabelFont); } //应用冲突检测 masLabelPosition currentpos = labelDef.MasLabelRuler; if (_conflictor != null && _conflictor.Enabled) { bool isConflicted = true; PointF originalPt = pointF; #region debug //if (Array.IndexOf(fet.FieldValues, "东芝笔记本电脑技术服务中心") >= 0 || Array.IndexOf(fet.FieldValues, "甘托克") >= 0) // Console.WriteLine(""); //画出8个候选位置 //foreach (masLabelPosition pos in Enum.GetValues(typeof(masLabelPosition))) //{ // switch (pos) // { // case masLabelPosition.LeftCenter: // case masLabelPosition.RightCenter: // case masLabelPosition.UpCenter: // case masLabelPosition.BottomCenter: // pointF = originalPt; // GetLabelLocation(pos, g, ref pointF, labelRect, currentSymbol); // (_environment.ConflictorForLabel as PixelConflictor).DrawTestRectangleF(pointF, labelRect, Color.Blue); // break; // } //} #endregion foreach (masLabelPosition pos in Enum.GetValues(typeof(masLabelPosition))) { pointF = originalPt; GetLabelLocation(pos, g, ref pointF, labelRect, currentSymbol); if (!_conflictor.IsConflicted(pointF, labelRect)) { isConflicted = false; currentpos = pos; break; } } if (isConflicted) { return; } } else { //应用对齐方式 GetLabelLocation(labelDef.MasLabelRuler, g, ref pointF, fontSize, currentSymbol); } // if (loc.Angle != 0 && loc.Angle != 360)//一般情况下是沿线标注 { emptyRotateMatrix.Reset(); emptyRotateMatrix.RotateAt(loc.Angle, oldPt); g.Transform = emptyRotateMatrix; if (_conflictor != null) { _conflictor.HoldPosition(pointF.X, pointF.Y, labelRect, emptyRotateMatrix); } } //背景符号 if (labelDef.ContainerSymbol != null) { SizeF backSize = labelDef.ContainerSymbol.Draw(g, pointF, fontSize); if (backSize.Width - fontSize.Width > 0) { pointF.X += (backSize.Width - fontSize.Width) / 2f; } if (backSize.Height - fontSize.Height > 0) { pointF.Y += (backSize.Height - fontSize.Height) / 2f; } } //写标注 DrawString(text, g, pointF, labelRect, labelDef); // if (loc.Angle != 0) { g.Transform = oldMatrix; } } }
private void DrawString(string text, Graphics g, PointF pointF, SizeF labelRect, LabelDef labelDef) { RectangleF rect = new RectangleF(pointF.X, pointF.Y, labelRect.Width, labelRect.Height); if (labelDef.Angle != 0) { Matrix oldMatrix = g.Transform; try { Matrix matrix = g.Transform.Clone(); Matrix newMatrix = new Matrix(); newMatrix.RotateAt(labelDef.Angle, new PointF(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2)); newMatrix.Multiply(matrix, MatrixOrder.Append); g.Transform = newMatrix; DrawString(rect, text, g, pointF, labelRect, labelDef); matrix.Dispose(); } finally { g.Transform.Dispose(); g.Transform = oldMatrix; } } else { DrawString(rect, text, g, pointF, labelRect, labelDef); } }
private void DrawString(RectangleF rect, string text, Graphics g, PointF pointF, SizeF labelRect, LabelDef labelDef) { if (labelDef.MaskBrush != null) { rect.Location = new PointF(pointF.X + 1, pointF.Y + 1); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); rect.Location = new PointF(pointF.X + 1, pointF.Y - 1); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); rect.Location = new PointF(pointF.X + 1, pointF.Y); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); rect.Location = new PointF(pointF.X - 1, pointF.Y + 1); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); rect.Location = new PointF(pointF.X - 1, pointF.Y - 1); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); rect.Location = new PointF(pointF.X - 1, pointF.Y); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); rect.Location = new PointF(pointF.X, pointF.Y + 1); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); rect.Location = new PointF(pointF.X, pointF.Y - 1); g.DrawString(text, labelDef.LabelFont, labelDef.MaskBrush, rect, StaticTextStringFormat); } rect.Location = new PointF(pointF.X, pointF.Y); g.DrawString(text, labelDef.LabelFont, labelDef.LabelBrush, rect, StaticTextStringFormat); }
//<LabelDef enabled="False" field="ADMIN_NAME" fields="FIPS_ADMIN,GMI_ADMIN,ADMIN_NAME,FIPS_CNTRY,GMI_CNTRY,CNTRY_NAME,POP_ADMIN,TYPE_ENG,TYPE_LOC,SQKM,SQMI,COLOR_MAP" ruler="Center" invalidvalue=""> // <Font font="Microsoft Sans Serif,9,Regular" color="255,0,0,0" maskcolor="0,0,0,0" /> // <DisplayScaleRange enabled="False" minscale="-1" maxscale="1" /> //</LabelDef> public static LabelDef FromXElement(XElement ele) { if (ele == null) { return(null); } enumLabelSource source = enumLabelSource.Label; if (ele.Attribute("source") != null) { string sourceString = ele.Attribute("source").Value; foreach (enumLabelSource s in Enum.GetValues(typeof(enumLabelSource))) { if (s.ToString() == sourceString) { source = s; break; } } } Font font = FontHelper.StringToFont(ele.Element("Font").Attribute("font").Value); ScaleRange range = ScaleRange.FromXElement(ele.Element("DisplayScaleRange")); Color color = ColorHelper.StringToColor(ele.Element("Font").Attribute("color").Value); Color maskcolor = ColorHelper.StringToColor(ele.Element("Font").Attribute("maskcolor").Value); string field = ele.Attribute("field").Value; string fields = ele.Attribute("fields").Value; bool enabled = bool.Parse(ele.Attribute("enabled").Value); bool autotonewline = false; if (ele.Attribute("autotonewline") != null) { autotonewline = bool.Parse(ele.Attribute("autotonewline").Value); } int charcount = 6; if (ele.Attribute("charcountperline") != null) { charcount = int.Parse(ele.Attribute("charcountperline").Value); } masLabelPosition ruler = masLabelPosition.Center; string rulerstring = ele.Attribute("ruler").Value; foreach (masLabelPosition r in Enum.GetValues(typeof(masLabelPosition))) { if (r.ToString() == rulerstring) { ruler = r; break; } } string invalidvalue = ele.Attribute("invalidvalue").Value; // IContainerSymbol csym = null; if (ele.Element("ContainerSymbol") != null) { csym = PersistObject.ReflectObjFromXElement(ele.Element("ContainerSymbol")) as IContainerSymbol; } // LabelDef def = new LabelDef(field, fields.Trim() != string.Empty ? fields.Split(',') : null); def.LabelFont = font; def.DisplayScaleRange = range; def.EnableLabeling = enabled; def.ForeColor = color; def.MaskColor = maskcolor; def.MasLabelRuler = ruler; def.InvalidValue = invalidvalue; def.LabelSource = source; def.ContainerSymbol = csym; def.AutoToNewline = autotonewline; def.CharcountPerLine = charcount; return(def); }