/// <summary> 在不改变单行文字定位的情况下,修改其对齐方式。默认为默认的左对齐 </summary> /// <param name="txt"></param> /// <param name="horiMode">水平对齐方式</param> /// <param name="vertMode">竖直对齐方式</param> public static void SetAlignment(this DBText txt, TextVerticalMode vertMode = TextVerticalMode.TextBase, TextHorizontalMode horiMode = TextHorizontalMode.TextLeft) { var originalPosition = txt.Position; // 1. 计算出变形后的 AlignmentPoint。 // 注:除了“布满”与“对齐”这两种对正方式外,其他的对齐方式在互相转换时,AlignmentPoint 是不变的,其计算方式如下面代码; // 而“布满”与“对齐”这两种对正方式,会改变单行文字的宽度,所以在切换时,AlignmentPoint 会略有变化。 Point3d alignPt = txt.IsDefaultAlignment ? originalPosition : txt.AlignmentPoint; // 2. 设置对齐后,其 AlignmentPoint 会被自动设置到 {0,0,0},而且其 Position 属性被屏蔽,设置其值无效。 txt.HorizontalMode = horiMode; txt.VerticalMode = vertMode; // 3. 调整文字定位 if (txt.IsDefaultAlignment) { // AlignmentPoint 始终为 {0,0,0},此时通过设置 Position来控制其定位 txt.Position = originalPosition; } else { // Position 属性无效,此时通过设置 AlignmentPoint 来控制其定位 // 注意设置对齐后的 txt.AlignmentPoint 也可能是无效的{0,0,0},所以要用前面计算得到的 对齐点 txt.AlignmentPoint = originalPosition.Add(txt.Position.GetVectorTo(alignPt)); } }
public void CreateMenu() { // 获取当前文档和数据库 Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; List <BlockData> lst = new List <BlockData>(); string blocktype = "INSERT"; SelectionFilter selFtrBlock = blocktype.GetSingleTypeFilter(); SelectionSet ss = doc.GetSelectionSet("请选择图框", selFtrBlock); if (ss != null) { foreach (SelectedObject obj in ss) { BlockData data = db.GetBlockData(obj.ObjectId); if (data.ProjectName != null) { lst.Add(data); } } if (lst.Count > 0) { var orderlst = lst.OrderBy(s => s.DrawingNumber).ToList(); Point3d?inp = ed.GetPointOnScreen("\n共找到" + lst.Count.ToString() + "个图框, 请点击图纸目录左上角点: "); if (inp != null) { Point3d p = (Point3d)inp; double h = 3.5, r = 0, f = 0.75; string t = "SMEDI"; TextHorizontalMode thmode = TextHorizontalMode.TextLeft; TextVerticalMode tvmode = TextVerticalMode.TextBottom; db.AddTextToModeSpace("1", p + new Vector3d(29.78, -69.09, 0), h, r, f, t, thmode, tvmode); db.AddTextToModeSpace((orderlst[0].DrawingNumber.Substring(0, orderlst[0].DrawingNumber.Length - 2) + "00"), p + new Vector3d(41.48, -69.09, 0), h, r, f, t, thmode, tvmode); db.AddTextToModeSpace("图纸目录", p + new Vector3d(78.36, -69.09, 0), h, r, f, t, thmode, tvmode); for (int i = 0; i < orderlst.Count(); i++) { db.AddTextToModeSpace((i + 2).ToString(), p + new Vector3d(29.78, -69.09 - 8 * (i + 1), 0), h, r, f, t, thmode, tvmode); db.AddTextToModeSpace(orderlst[i].DrawingNumber, p + new Vector3d(41.48, -69.09 - 8 * (i + 1), 0), h, r, f, t, thmode, tvmode); db.AddTextToModeSpace(orderlst[i].DrawingName, p + new Vector3d(78.36, -69.09 - 8 * (i + 1), 0), h, r, f, t, thmode, tvmode); } ed.WriteMessage("\n生成完成! "); } } } }
/// <summary> /// 标注对齐方式 /// </summary> /// <param name="alignment"></param> /// <param name="textHorizontalMode"></param> /// <param name="textVerticalMode"></param> private static void GetLabelAlignment(int alignment, ref TextHorizontalMode textHorizontalMode, ref TextVerticalMode textVerticalMode) { if (alignment == 1 || alignment == 5 || alignment == 9) { textHorizontalMode = TextHorizontalMode.TextLeft; } else if (alignment == 0 || alignment == 4 || alignment == 8) { textHorizontalMode = TextHorizontalMode.TextMid; } else if (alignment == 2 || alignment == 6 || alignment == 10) { textHorizontalMode = TextHorizontalMode.TextRight; } if (alignment == 4 || alignment == 5 || alignment == 6) { textVerticalMode = TextVerticalMode.TextTop; } else if (alignment == 0 || alignment == 1 || alignment == 2) { textVerticalMode = TextVerticalMode.TextVerticalMid; } else if (alignment == 8 || alignment == 9 || alignment == 10) { textVerticalMode = TextVerticalMode.TextBottom; } }
/// <summary> /// 添加文字 /// </summary> /// <param name="db"></param> /// <param name="point0">文字起始点</param> /// <param name="height0">文字高度</param> /// <param name="str0">文字内容</param> /// <returns>图形的ObjectId</returns> public static ObjectId AddTextToModeSpace(this Database db, string content, Point3d position, double height = 3.5, double rotation = 0, double widthfactor = 0.7, string textstylename = "SMEDI", TextHorizontalMode thmode = TextHorizontalMode.TextCenter, TextVerticalMode tvmode = TextVerticalMode.TextVerticalMid) { DBText text = new DBText(); text.TextString = content; text.Position = position; text.Height = height; text.Rotation = rotation; text.WidthFactor = widthfactor; text.TextStyleId = db.GetTextStyleId(textstylename); text.HorizontalMode = thmode; text.VerticalMode = tvmode; text.AlignmentPoint = text.Position; return(db.AddEntityToModeSpace(text)); }
//-------------------------------------------------------------------------- // // ���ܣ�������Զ�������� // // ���ߣ� // // ���ڣ�200708 // // ��ʷ�� //-------------------------------------------------------------------------- private void SetAttributeDefinition(string tag, Point3d position, double height, Autodesk.AutoCAD.Colors.Color color, TextVerticalMode vertMode, TextHorizontalMode horzMode, string expressionString, Point3d alignmentPoint) { using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()) { //����ע������ ObjectId idTagText; idTagText = m_annotations[m_templateName].CreateAnnotationText(); // AttributeDefinition tagText = null; tagText = trans.GetObject(idTagText, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite) as AttributeDefinition; if (null == tagText) { MessageBox.Show("����ע������ʧ�ܡ�"); return; } // ����Ƿ���ע������ if (!m_annotations.IsAnnotationText(tagText)) { MessageBox.Show(string.Format("�����ж�{0} Ϊע������", tag)); return; } tagText.Position = position; tagText.Tag = tag; tagText.Height = height; tagText.Color = color; tagText.VerticalMode = vertMode; tagText.HorizontalMode = horzMode; tagText.AlignmentPoint = alignmentPoint; m_annotations.SetExpressionString(tagText, AnnotationExpressionFields.AttributeDefinitionAnnotationString, expressionString); // For OD ":Column_Name@Table_Name" // ʹ�� Annotation.TextLayer���ò�. m_annotations.SetExpressionString(tagText, AnnotationExpressionFields.AttributeDefinitionTextLayer, "Layer1"); string newTemplateName = null; newTemplateName = textbTemplateName.Text; // ���ÿ鶨�����ɫ Autodesk.AutoCAD.Colors.Color redClr = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.None, 1); m_annotations[newTemplateName].Color = redClr; //����ע����� trans.Commit(); } }
public static AttributeDefinition CreateAttribute(Database db, Point3d pt, string tag, string prompt, string text, double textHeight, double rotation, double widthFactor, TextHorizontalMode horizontalMode, TextVerticalMode verticalMode, ObjectId textStyleId, ObjectId layerId) { using (CurrentDB curr = new CurrentDB(db)) { AttributeDefinition attdef = new AttributeDefinition(); attdef.SetDatabaseDefaults(db); attdef.Tag = tag; attdef.Prompt = prompt; attdef.TextString = text; attdef.Position = pt; attdef.Height = textHeight; attdef.Rotation = rotation; attdef.WidthFactor = widthFactor; if (horizontalMode == TextHorizontalMode.TextLeft) { if (verticalMode == TextVerticalMode.TextTop) { attdef.Justify = AttachmentPoint.TopLeft; } else if (verticalMode == TextVerticalMode.TextBase) { attdef.Justify = AttachmentPoint.BaseLeft; } else if (verticalMode == TextVerticalMode.TextBottom) { attdef.Justify = AttachmentPoint.BottomLeft; } else { attdef.Justify = AttachmentPoint.MiddleLeft; } } else if (horizontalMode == TextHorizontalMode.TextRight) { if (verticalMode == TextVerticalMode.TextTop) { attdef.Justify = AttachmentPoint.TopRight; } else if (verticalMode == TextVerticalMode.TextBase) { attdef.Justify = AttachmentPoint.BaseRight; } else if (verticalMode == TextVerticalMode.TextBottom) { attdef.Justify = AttachmentPoint.BottomRight; } else { attdef.Justify = AttachmentPoint.MiddleRight; } } else if (horizontalMode == TextHorizontalMode.TextMid || horizontalMode == TextHorizontalMode.TextCenter) { if (verticalMode == TextVerticalMode.TextTop) { attdef.Justify = AttachmentPoint.TopCenter; } else if (verticalMode == TextVerticalMode.TextBase) { attdef.Justify = AttachmentPoint.BaseCenter; } else if (verticalMode == TextVerticalMode.TextBottom) { attdef.Justify = AttachmentPoint.BottomCenter; } else { attdef.Justify = AttachmentPoint.MiddleCenter; } } else if (horizontalMode == TextHorizontalMode.TextAlign) { if (verticalMode == TextVerticalMode.TextTop) { attdef.Justify = AttachmentPoint.TopAlign; } else if (verticalMode == TextVerticalMode.TextBase) { attdef.Justify = AttachmentPoint.BaseAlign; } else if (verticalMode == TextVerticalMode.TextBottom) { attdef.Justify = AttachmentPoint.BottomAlign; } else { attdef.Justify = AttachmentPoint.MiddleAlign; } } else if (horizontalMode == TextHorizontalMode.TextFit) { if (verticalMode == TextVerticalMode.TextTop) { attdef.Justify = AttachmentPoint.TopFit; } else if (verticalMode == TextVerticalMode.TextBase) { attdef.Justify = AttachmentPoint.BaseFit; } else if (verticalMode == TextVerticalMode.TextBottom) { attdef.Justify = AttachmentPoint.BottomFit; } else { attdef.Justify = AttachmentPoint.MiddleFit; } } if (horizontalMode != TextHorizontalMode.TextLeft || verticalMode != TextVerticalMode.TextBase) { attdef.AlignmentPoint = pt; attdef.AdjustAlignment(db); } if (!textStyleId.IsNull) { attdef.TextStyleId = textStyleId; } if (!layerId.IsNull) { attdef.LayerId = layerId; } return(attdef); } }
public static DBText CreateText(Database db, Point3d pt, string text, double textHeight, double rotation, double widthFactor, TextHorizontalMode horizontalMode, TextVerticalMode verticalMode) { return(CreateText(db, pt, text, textHeight, rotation, widthFactor, horizontalMode, verticalMode, ObjectId.Null, ObjectId.Null)); }
public static DBText CreateText(Database db, Point3d pt, string text, double textHeight, double rotation, double widthFactor, TextHorizontalMode horizontalMode, TextVerticalMode verticalMode, ObjectId textStyleId, ObjectId layerId) { using (CurrentDB curr = new CurrentDB(db)) { DBText dbtext = new DBText(); dbtext.SetDatabaseDefaults(db); dbtext.TextString = text; dbtext.Position = pt; dbtext.Height = textHeight; dbtext.Rotation = rotation; dbtext.WidthFactor = widthFactor; if (horizontalMode == TextHorizontalMode.TextLeft) { if (verticalMode == TextVerticalMode.TextTop) { dbtext.Justify = AttachmentPoint.TopLeft; } else if (verticalMode == TextVerticalMode.TextBase) { dbtext.Justify = AttachmentPoint.BaseLeft; } else if (verticalMode == TextVerticalMode.TextBottom) { dbtext.Justify = AttachmentPoint.BottomLeft; } else { dbtext.Justify = AttachmentPoint.MiddleLeft; } } else if (horizontalMode == TextHorizontalMode.TextRight) { if (verticalMode == TextVerticalMode.TextTop) { dbtext.Justify = AttachmentPoint.TopRight; } else if (verticalMode == TextVerticalMode.TextBase) { dbtext.Justify = AttachmentPoint.BaseRight; } else if (verticalMode == TextVerticalMode.TextBottom) { dbtext.Justify = AttachmentPoint.BottomRight; } else { dbtext.Justify = AttachmentPoint.MiddleRight; } } else if (horizontalMode == TextHorizontalMode.TextMid || horizontalMode == TextHorizontalMode.TextCenter) { if (verticalMode == TextVerticalMode.TextTop) { dbtext.Justify = AttachmentPoint.TopCenter; } else if (verticalMode == TextVerticalMode.TextBase) { dbtext.Justify = AttachmentPoint.BaseCenter; } else if (verticalMode == TextVerticalMode.TextBottom) { dbtext.Justify = AttachmentPoint.BottomCenter; } else { dbtext.Justify = AttachmentPoint.MiddleCenter; } } else if (horizontalMode == TextHorizontalMode.TextAlign) { if (verticalMode == TextVerticalMode.TextTop) { dbtext.Justify = AttachmentPoint.TopAlign; } else if (verticalMode == TextVerticalMode.TextBase) { dbtext.Justify = AttachmentPoint.BaseAlign; } else if (verticalMode == TextVerticalMode.TextBottom) { dbtext.Justify = AttachmentPoint.BottomAlign; } else { dbtext.Justify = AttachmentPoint.MiddleAlign; } } else if (horizontalMode == TextHorizontalMode.TextFit) { if (verticalMode == TextVerticalMode.TextTop) { dbtext.Justify = AttachmentPoint.TopFit; } else if (verticalMode == TextVerticalMode.TextBase) { dbtext.Justify = AttachmentPoint.BaseFit; } else if (verticalMode == TextVerticalMode.TextBottom) { dbtext.Justify = AttachmentPoint.BottomFit; } else { dbtext.Justify = AttachmentPoint.MiddleFit; } } if (horizontalMode != TextHorizontalMode.TextLeft || verticalMode != TextVerticalMode.TextBase) { dbtext.AlignmentPoint = pt; dbtext.AdjustAlignment(db); } if (!textStyleId.IsNull) { dbtext.TextStyleId = textStyleId; } if (!layerId.IsNull) { dbtext.LayerId = layerId; } return(dbtext); } }