惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
F
Full Disclosure
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
N
News and Events Feed by Topic
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
B
Blog
GbyAI
GbyAI
C
Check Point Blog
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
O
OpenAI News
V
V2EX
人人都是产品经理
人人都是产品经理
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Security @ Cisco Blogs
C
Cisco Blogs
Security Latest
Security Latest
S
Security Affairs
V
Visual Studio Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
博客园_首页
U
Unit 42
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
L
LINUX DO - 热门话题
H
Hacker News: Front Page

博客园 - 我才是银古

第16章:常见问题、排错与最佳实践 第15章:扩展生态、MCAD 与外部集成 第12章:实战案例:机械结构与 3D 打印零件 第14章:构建、测试、调试与贡献流程 第13章:OpenSCAD 源码架构与核心执行流程 第11章:预览、渲染、网格精度与性能优化 第09章:列表推导、递归与算法建模 第08章:参数化零件库与复用设计 第10章:导入导出、命令行与自动化 第06章:CSG 布尔建模方法 第07章:二维图形、拉伸、旋转与投影 第05章:基础几何、坐标系与变换 第04章:参数、变量、函数、模块与作用域 OpenSCAD 教程目录 第03章:OpenSCAD 语言基础 第02章:安装、环境配置与开发工作流 第01章:OpenSCAD 项目全景与学习路线 第02章:源码获取、编译与开发环境配置 第01章:OCCT项目全景与学习路线 第18章:二次开发实战与综合案例 第18章:综合实战案例 第17章:数据交换与协同 第16章:源码架构与二次开发 第15章:插件与自定义工作台开发 第14章:Python脚本宏与自动化 第13章:FEM仿真分析 第12章:CAM数控加工 第11章:SurfaceMesh与逆向工程 第10章:Draft二维绘图与BIM建筑 第09章:工程图TechDraw 第07章:参数化表达式与Spreadsheet 第08章:装配设计Assembly 第06章:Part工作台与几何内核 第05章:PartDesign实体特征建模 第04章:草图Sketcher约束建模 第02章:安装版本与工作环境配置 第03章:界面工作台与基础操作 第01章:项目全景与学习路线 第十二章:插件开发、研究功能与最佳实践 第十章:定时任务与自动化(Cron) 第七章:技能、记忆与自学习闭环 第八章:MCP 集成与上下文文件 第六章:工具系统与终端后端 第五章:模型供应商与配置体系 Hermes Agent 教程目录 第十一章:语音、视觉、浏览器与子代理协作 第四章:CLI/TUI 与会话管理 第十二章:学习路线、实战方案与最佳实践 第十一章:源码结构、开发调试与插件开发 第十章:自动化、远程访问、日志与排障 第九章:Control UI、节点、Canvas 与语音能力 第七章:工具、技能、插件与能力扩展 第八章:安全模型、访问控制与沙箱实践 第六章:Agent 工作区、会话与多智能体路由 第五章:多通道消息接入与聊天平台配置 第四章:配置体系、模型接入与认证管理 第三章:Gateway 架构、协议与运行机制 第二章:安装、环境准备与快速上手 第一章:OpenClaw 项目概览与核心定位 oh-my-openagent 教程目录 09-命令模型回退与配置参考 10-实战案例最佳实践与故障排除 05-工作模式-Ultrawork-Prometheus-Atlas 08-Hooks与MCP系统 06-Category与Skill系统 07-核心工具链 04-智能体全景详解 03-安装与环境配置 02-整体架构与多模型编排机制 01-项目简介与核心理念 01-项目概览与学习路线 02-安装部署与工具适配 03-Skill机制与using-superpowers 05-TDD系统化调试与完成前验证 04-需求澄清方案设计与计划编写 07-并行智能体子智能体与Git-Worktree 第六章:代码审查、反馈处理与分支收尾 08-中国特色Skills与本土团队落地 09-MCP构建工作流执行与自定义Skill 第23章:FreeCAD-Python-API Clipper2 C# 源码解读教程 第19章:PolyTree 多边形树结构 第20章:实际应用与最佳实践 第18章:Minkowski 和与差 第17章:RectClip 矩形裁剪优化 第16章:ClipperOffset 偏移类详解 第15章:填充规则详解 第14章:布尔运算执行流程 第13章:ClipperD 浮点裁剪类 第11章:OutRec 与 OutPt 输出结构 第9章:Active 活动边结构 第10章:Vertex 顶点与 LocalMinima 局部极小值 第12章:Clipper64 裁剪类详解 第7章:高精度运算与128位整数 第8章:ClipperBase 基类详解 第5章:枚举类型与常量定义 第6章:InternalClipper 内部工具类 第2章:核心数据结构 - Point64、PointD 第3章:路径与多边形表示 - Path64、PathD、Paths64、PathsD 第4章:矩形边界 - Rect64、RectD
标注与文本系统
我才是银古 · 2026-06-20 · via 博客园 - 我才是银古

第十八章:标注与文本系统

18.1 标注系统概述

18.1.1 标注的重要性

标注是工程图纸中不可或缺的组成部分,用于标明尺寸、角度、半径等几何信息。LightCAD提供了完整的二维和三维标注系统。

18.1.2 标注类型

二维标注(LightCAD.Core/Elements/Dim/)

标注类型 说明
LcDimLinear 线性标注(水平、垂直、对齐)
LcDimAngular 角度标注
LcDimRadial 半径标注
LcDimDiameter 直径标注
LcDimOrdinate 坐标标注
LcDimArcLength 弧长标注

三维标注(LightCAD.Core/Elements/Dim3/)

标注类型 说明
LcDim3dLinear 三维线性标注
LcDim3dAngular 三维角度标注
LcDim3dRadial 三维半径标注
LcDim3dElevation 标高标注

18.2 标注基类

18.2.1 标注数据结构

public abstract class LcDimension : LcEntity, IUpdateObject
{
    /// <summary>
    /// 标注样式
    /// </summary>
    public DimStyle Style { get; set; }

    /// <summary>
    /// 标注文本(null表示使用测量值)
    /// </summary>
    public string OverrideText { get; set; }

    /// <summary>
    /// 文本位置
    /// </summary>
    public Point2d TextPosition { get; set; }

    /// <summary>
    /// 文本旋转角度
    /// </summary>
    public double TextRotation { get; set; }

    /// <summary>
    /// 测量值
    /// </summary>
    public abstract double MeasuredValue { get; }

    /// <summary>
    /// 获取显示文本
    /// </summary>
    public string DisplayText
    {
        get
        {
            if (!string.IsNullOrEmpty(OverrideText))
                return OverrideText;

            return FormatMeasuredValue();
        }
    }

    /// <summary>
    /// 格式化测量值
    /// </summary>
    protected string FormatMeasuredValue()
    {
        var value = MeasuredValue;
        var precision = Style?.Precision ?? 2;
        var prefix = Style?.Prefix ?? "";
        var suffix = Style?.Suffix ?? "";

        return $"{prefix}{value.ToString($"F{precision}")}{suffix}";
    }

    public override string TypeName => "Dimension";
}

18.2.2 标注样式

public class DimStyle
{
    /// <summary>
    /// 样式名称
    /// </summary>
    public string Name { get; set; }

    // 尺寸线设置
    public LcColor DimensionLineColor { get; set; } = LcColor.ByBlock;
    public double DimensionLineWeight { get; set; } = 0.25;
    public double DimensionLineExtension { get; set; } = 2;

    // 延伸线设置
    public LcColor ExtensionLineColor { get; set; } = LcColor.ByBlock;
    public double ExtensionLineOffset { get; set; } = 2;
    public double ExtensionLineExtension { get; set; } = 2;
    public bool SuppressExtLine1 { get; set; } = false;
    public bool SuppressExtLine2 { get; set; } = false;

    // 箭头设置
    public ArrowType ArrowType { get; set; } = ArrowType.ClosedFilled;
    public double ArrowSize { get; set; } = 3;

    // 文本设置
    public string TextFont { get; set; } = "SimSun";
    public double TextHeight { get; set; } = 3.5;
    public LcColor TextColor { get; set; } = LcColor.ByBlock;
    public TextAlignment TextAlignment { get; set; } = TextAlignment.Center;
    public double TextGap { get; set; } = 1;
    public int Precision { get; set; } = 2;
    public string Prefix { get; set; } = "";
    public string Suffix { get; set; } = "";

    // 比例设置
    public double ScaleFactor { get; set; } = 1.0;

    // 公差设置
    public bool ShowTolerance { get; set; } = false;
    public double ToleranceUpper { get; set; } = 0;
    public double ToleranceLower { get; set; } = 0;
}

public enum ArrowType
{
    ClosedFilled,    // 实心箭头
    ClosedEmpty,     // 空心箭头
    Open,            // 开放箭头
    Dot,             // 圆点
    Slash,           // 斜线
    None             // 无
}

public enum TextAlignment
{
    Center,          // 居中
    Above,           // 上方
    Outside          // 外侧
}

18.3 线性标注(LcDimLinear)

18.3.1 数据结构

public class LcDimLinear : LcDimension
{
    /// <summary>
    /// 第一个定义点(被标注对象的端点1)
    /// </summary>
    public Point2d DefPoint1 { get; set; }

    /// <summary>
    /// 第二个定义点(被标注对象的端点2)
    /// </summary>
    public Point2d DefPoint2 { get; set; }

    /// <summary>
    /// 尺寸线位置
    /// </summary>
    public Point2d DimLinePoint { get; set; }

    /// <summary>
    /// 标注方向
    /// </summary>
    public DimLinearType LinearType { get; set; } = DimLinearType.Aligned;

    /// <summary>
    /// 强制角度(用于旋转标注)
    /// </summary>
    public double? ForceAngle { get; set; }

    /// <summary>
    /// 测量值
    /// </summary>
    public override double MeasuredValue
    {
        get
        {
            switch (LinearType)
            {
                case DimLinearType.Horizontal:
                    return Math.Abs(DefPoint2.X - DefPoint1.X);
                case DimLinearType.Vertical:
                    return Math.Abs(DefPoint2.Y - DefPoint1.Y);
                case DimLinearType.Aligned:
                    return DefPoint1.DistanceTo(DefPoint2);
                case DimLinearType.Rotated:
                    if (ForceAngle.HasValue)
                    {
                        var dir = new Vector2d(
                            Math.Cos(ForceAngle.Value),
                            Math.Sin(ForceAngle.Value));
                        var diff = DefPoint2 - DefPoint1;
                        return Math.Abs(diff.Dot(dir));
                    }
                    return DefPoint1.DistanceTo(DefPoint2);
                default:
                    return DefPoint1.DistanceTo(DefPoint2);
            }
        }
    }

    /// <summary>
    /// 获取标注的几何图形(用于渲染)
    /// </summary>
    public DimGeometry GetGeometry()
    {
        var geometry = new DimGeometry();
        var style = Style ?? DimStyle.Default;

        // 计算尺寸线方向
        Vector2d dimDirection;
        switch (LinearType)
        {
            case DimLinearType.Horizontal:
                dimDirection = Vector2d.XAxis;
                break;
            case DimLinearType.Vertical:
                dimDirection = Vector2d.YAxis;
                break;
            default:
                dimDirection = (DefPoint2 - DefPoint1).Normalize();
                break;
        }

        var perpDirection = dimDirection.Perpendicular();

        // 将定义点投影到尺寸线上
        var dimLineY = (DimLinePoint - DefPoint1).Dot(perpDirection);
        var ext1End = DefPoint1 + perpDirection * dimLineY;
        var ext2End = DefPoint2 + perpDirection * dimLineY;

        // 延伸线
        var ext1Start = DefPoint1 + perpDirection *
            Math.Sign(dimLineY) * style.ExtensionLineOffset;
        var ext2Start = DefPoint2 + perpDirection *
            Math.Sign(dimLineY) * style.ExtensionLineOffset;

        geometry.ExtensionLines.Add((ext1Start,
            ext1End + perpDirection * Math.Sign(dimLineY) *
            style.ExtensionLineExtension));
        geometry.ExtensionLines.Add((ext2Start,
            ext2End + perpDirection * Math.Sign(dimLineY) *
            style.ExtensionLineExtension));

        // 尺寸线
        geometry.DimensionLine = (ext1End, ext2End);

        // 箭头
        geometry.Arrow1Position = ext1End;
        geometry.Arrow2Position = ext2End;
        geometry.ArrowDirection = dimDirection;

        // 文本位置
        geometry.TextPosition = ext1End.MidPoint(ext2End);
        geometry.Text = DisplayText;

        return geometry;
    }
}

public enum DimLinearType
{
    Horizontal,  // 水平标注
    Vertical,    // 垂直标注
    Aligned,     // 对齐标注
    Rotated      // 旋转标注
}

18.4 角度标注

public class LcDimAngular : LcDimension
{
    public Point2d Vertex { get; set; }
    public Point2d Point1 { get; set; }
    public Point2d Point2 { get; set; }
    public Point2d ArcPoint { get; set; }

    public override double MeasuredValue
    {
        get
        {
            var dir1 = (Point1 - Vertex).Normalize();
            var dir2 = (Point2 - Vertex).Normalize();
            var angle = Math.Acos(dir1.Dot(dir2));
            return AngleUtils.RadToDeg(angle);
        }
    }

    protected override string FormatMeasuredValue()
    {
        return $"{MeasuredValue.ToString($"F{Style?.Precision ?? 1}")}°";
    }
}

18.5 半径和直径标注

public class LcDimRadial : LcDimension
{
    public Point2d Center { get; set; }
    public Point2d ChordPoint { get; set; }

    public override double MeasuredValue =>
        Center.DistanceTo(ChordPoint);

    protected override string FormatMeasuredValue()
    {
        var value = MeasuredValue;
        return $"R{value.ToString($"F{Style?.Precision ?? 2}")}";
    }
}

public class LcDimDiameter : LcDimension
{
    public Point2d Center { get; set; }
    public Point2d ChordPoint { get; set; }

    public override double MeasuredValue =>
        Center.DistanceTo(ChordPoint) * 2;

    protected override string FormatMeasuredValue()
    {
        var value = MeasuredValue;
        return $"⌀{value.ToString($"F{Style?.Precision ?? 2}")}";
    }
}

18.6 文本系统

18.6.1 文本实体

public class LcText : LcEntity, IUpdateObject
{
    /// <summary>
    /// 文本内容
    /// </summary>
    public string Content { get; set; }

    /// <summary>
    /// 插入点
    /// </summary>
    public Point2d InsertionPoint { get; set; }

    /// <summary>
    /// 文字高度
    /// </summary>
    public double Height { get; set; } = 3.5;

    /// <summary>
    /// 旋转角度
    /// </summary>
    public double Rotation { get; set; } = 0;

    /// <summary>
    /// 宽度因子
    /// </summary>
    public double WidthFactor { get; set; } = 1.0;

    /// <summary>
    /// 倾斜角度
    /// </summary>
    public double ObliqueAngle { get; set; } = 0;

    /// <summary>
    /// 字体名称
    /// </summary>
    public string FontName { get; set; } = "SimSun";

    /// <summary>
    /// 水平对齐方式
    /// </summary>
    public TextHorizontalAlignment HorizontalAlignment { get; set; }
        = TextHorizontalAlignment.Left;

    /// <summary>
    /// 垂直对齐方式
    /// </summary>
    public TextVerticalAlignment VerticalAlignment { get; set; }
        = TextVerticalAlignment.Baseline;

    /// <summary>
    /// 是否为多行文本
    /// </summary>
    public bool IsMultiline { get; set; } = false;

    /// <summary>
    /// 多行文本宽度
    /// </summary>
    public double TextWidth { get; set; }

    public override string TypeName => "Text";

    public override BoundingBox2d Bounds
    {
        get
        {
            var textWidth = CalculateTextWidth();
            var textHeight = Height;

            // 考虑旋转
            if (Math.Abs(Rotation) < 1e-10)
            {
                return new BoundingBox2d
                {
                    Min = InsertionPoint,
                    Max = new Point2d(
                        InsertionPoint.X + textWidth,
                        InsertionPoint.Y + textHeight)
                };
            }
            else
            {
                // 计算旋转后的包围盒
                var corners = GetRotatedCorners(textWidth, textHeight);
                return BoundingBox2d.FromPoints(corners);
            }
        }
    }

    /// <summary>
    /// 计算文本宽度
    /// </summary>
    private double CalculateTextWidth()
    {
        var font = FontManager.GetFont(FontName);
        if (font == null) return Content.Length * Height * 0.6;

        double width = 0;
        foreach (var ch in Content)
        {
            var charWidth = font.GetCharWidth(ch);
            width += charWidth * Height * WidthFactor;
        }
        return width;
    }

    /// <summary>
    /// 获取文本轮廓(用于渲染)
    /// </summary>
    public List<List<Point2d>> GetOutlines()
    {
        var font = FontManager.GetFont(FontName);
        var outlines = new List<List<Point2d>>();
        var currentX = 0.0;

        foreach (var ch in Content)
        {
            var charOutline = font.GetCharOutline(ch);
            if (charOutline != null)
            {
                var transformed = charOutline.Select(p =>
                {
                    var scaled = new Point2d(
                        p.X * Height * WidthFactor + currentX,
                        p.Y * Height);

                    // 应用旋转
                    if (Math.Abs(Rotation) > 1e-10)
                    {
                        var rotated = scaled.ToVector().Rotate(Rotation);
                        scaled = new Point2d(rotated.X, rotated.Y);
                    }

                    // 应用插入点偏移
                    return new Point2d(
                        scaled.X + InsertionPoint.X,
                        scaled.Y + InsertionPoint.Y);
                }).ToList();

                outlines.Add(transformed);
            }

            currentX += font.GetCharWidth(ch) * Height * WidthFactor;
        }

        return outlines;
    }

    public bool NeedsUpdate { get; private set; }
    public void OnUpdate(UpdateContext context) { NeedsUpdate = false; }
}

public enum TextHorizontalAlignment
{
    Left,
    Center,
    Right
}

public enum TextVerticalAlignment
{
    Baseline,
    Bottom,
    Middle,
    Top
}

18.7 表格系统

18.7.1 表格实体

public class LcTable : LcEntity, IUpdateObject
{
    public Point2d InsertionPoint { get; set; }
    public int RowCount { get; set; }
    public int ColumnCount { get; set; }
    public double[] RowHeights { get; set; }
    public double[] ColumnWidths { get; set; }
    public TableCell[,] Cells { get; set; }
    public TableStyle Style { get; set; }

    public override string TypeName => "Table";

    /// <summary>
    /// 获取指定单元格
    /// </summary>
    public TableCell GetCell(int row, int col)
    {
        return Cells[row, col];
    }

    /// <summary>
    /// 设置单元格内容
    /// </summary>
    public void SetCellContent(int row, int col, string content)
    {
        Cells[row, col].Content = content;
    }

    /// <summary>
    /// 计算表格总宽度
    /// </summary>
    public double TotalWidth => ColumnWidths.Sum();

    /// <summary>
    /// 计算表格总高度
    /// </summary>
    public double TotalHeight => RowHeights.Sum();

    public bool NeedsUpdate { get; private set; }
    public void OnUpdate(UpdateContext context) { NeedsUpdate = false; }
}

public class TableCell
{
    public string Content { get; set; }
    public TextHorizontalAlignment Alignment { get; set; }
    public LcColor TextColor { get; set; }
    public LcColor BackgroundColor { get; set; }
    public double TextHeight { get; set; }
    public int MergeRight { get; set; } = 0;
    public int MergeDown { get; set; } = 0;
}

18.8 SHX字体解析

18.8.1 SHX字体格式

SHX(Shape eXtension)是AutoCAD专用的矢量字体格式:

public class ShxFont
{
    private Dictionary<char, ShxGlyph> glyphs = new();
    public string Name { get; set; }
    public double Above { get; set; }
    public double Below { get; set; }

    /// <summary>
    /// 加载SHX文件
    /// </summary>
    public static ShxFont Load(string filePath)
    {
        var font = new ShxFont();
        font.Name = Path.GetFileNameWithoutExtension(filePath);

        using var stream = File.OpenRead(filePath);
        using var reader = new BinaryReader(stream);

        // 读取文件头
        var header = reader.ReadBytes(32);
        font.Above = reader.ReadDouble();
        font.Below = reader.ReadDouble();

        // 读取字形定义
        while (stream.Position < stream.Length)
        {
            var charCode = reader.ReadUInt16();
            var glyphData = ReadGlyphData(reader);
            font.glyphs[(char)charCode] = glyphData;
        }

        return font;
    }

    /// <summary>
    /// 获取字符的轮廓点
    /// </summary>
    public List<Point2d> GetCharOutline(char ch)
    {
        if (glyphs.TryGetValue(ch, out var glyph))
        {
            return glyph.Points;
        }
        return null;
    }

    /// <summary>
    /// 获取字符宽度
    /// </summary>
    public double GetCharWidth(char ch)
    {
        if (glyphs.TryGetValue(ch, out var glyph))
        {
            return glyph.Width;
        }
        return 0.6; // 默认宽度
    }
}

18.9 本章小结

本章全面介绍了LightCAD的标注和文本系统。标注系统支持线性、角度、半径、直径等多种标注类型,通过标注样式(DimStyle)统一管理标注的外观。文本系统支持单行和多行文本,通过TTF和SHX字体渲染。表格系统支持创建和编辑工程表格。SHX字体解析器确保了与AutoCAD字体的兼容性。


上一章第十七章:数据库与项目管理

下一章第十九章:调试测试与性能优化