明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2077|回复: 2

[命令] 怎样用VB.NET实现动态文字对齐

[复制链接]
发表于 2020-12-3 15:45:04 | 显示全部楼层 |阅读模式
怎么编写一个动态库,实现图片所示功能

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2020-12-11 15:58:14 | 显示全部楼层
那位大神指导一下,如果效果不错,能分享代码的,付费
发表于 2025-7-18 11:11:49 | 显示全部楼层
    Public Sub AlignTextDynamic()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor
      
        Dim pso As New PromptSelectionOptions With {
            .MessageForAdding = "选择要对齐的单行文字:"
        }
        Dim filter As New SelectionFilter({New TypedValue(DxfCode.Start, "TEXT")})
        Dim psr As PromptSelectionResult = ed.GetSelection(pso, filter)
        If psr.Status <> PromptStatus.OK Then Return

        Dim textIds = psr.Value.GetObjectIds()
        
        Dim ppr1 As PromptPointResult = ed.GetPoint("指定对齐点:")
        If ppr1.Status <> PromptStatus.OK Then Return
        Dim basePt As Point3d = ppr1.Value
        
        Dim jig As New TextAlignJig(textIds, basePt)
        Dim res As PromptResult = ed.Drag(jig)

        If res.Status = PromptStatus.OK Then
            
            jig.Update()
        End If
    End Sub
End Class

' 自定义Jig类,实现动态对齐
Public Class TextAlignJig
    Inherits DrawJig

    Private ReadOnly _textIds As ObjectId()
    Private _basePt As Point3d
    Private _currentPt As Point3d
    Private ReadOnly _alignedPts As List(Of Point3d)

    Public Sub New(textIds As ObjectId(), basePt As Point3d)
        _textIds = textIds
        _basePt = basePt
        _currentPt = basePt
        _alignedPts = New List(Of Point3d)
    End Sub

    Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
        Dim ppo As New JigPromptPointOptions(vbLf & "指定参考对齐直线的第二点:") With {
            .UseBasePoint = True,
            .BasePoint = _basePt
        }
        Dim ppr As PromptPointResult = prompts.AcquirePoint(ppo)
        If ppr.Status = PromptStatus.Cancel Then
            Return SamplerStatus.Cancel
        End If
        If ppr.Value = _currentPt Then
            Return SamplerStatus.NoChange
        End If
        _currentPt = ppr.Value
        Return SamplerStatus.OK
    End Function

    Protected Overrides Function WorldDraw(draw As Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean
        
        Dim lineVec As Vector3d = _currentPt - _basePt
        If lineVec.Length = 0 Then Return True

         draw.Geometry.WorldLine(_basePt, _currentPt)

        _alignedPts.Clear()
        Using tr As Transaction = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction()
            For Each id In _textIds
                Dim txt As DBText = CType(tr.GetObject(id, OpenMode.ForRead), DBText)
                Dim oldPt As Point3d = txt.Position
               
                Dim projPt As Point3d = ProjectPointToLine(oldPt, _basePt, lineVec)
                _alignedPts.Add(projPt)
               
                Dim txtCopy As New DBText()
                txtCopy.SetDatabaseDefaults()
                txtCopy.Position = projPt
                txtCopy.TextString = txt.TextString
                txtCopy.Height = txt.Height
                txtCopy.Rotation = txt.Rotation
                draw.Geometry.Draw(txtCopy)
            Next
            tr.Commit()
        End Using
        Return True
    End Function

    Private Function ProjectPointToLine(pt As Point3d, basePt As Point3d, lineVec As Vector3d) As Point3d
      
        Dim AP As Vector3d = pt - basePt
        Dim t As Double = AP.DotProduct(lineVec) / lineVec.LengthSqrd
        Dim proj As Point3d = basePt + lineVec.MultiplyBy(t)

        Return proj

    End Function

    Public Function Update() As Boolean
        
        Using tr As Transaction = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction()
            For i = 0 To _textIds.Length - 1
                Dim txt As DBText = CType(tr.GetObject(_textIds(i), OpenMode.ForWrite), DBText)
                txt.Position = _alignedPts(i)
            Next
            tr.Commit()
        End Using
        Return True
    End Function

End Class
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-12-17 10:27 , Processed in 0.182200 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表