B站视频:
https://www.bilibili.com/video/BV1Hm411X7Rc/

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Public Class Butterfly_curve1
    ReadOnly R As Double = 100 '比例(半径)
    ReadOnly T_Total As Integer = 4320 '= 24*180 
    ReadOnly Pi As Double = Math.PI
    ReadOnly e As Double = Math.E
    ReadOnly acDc As Document = Application.DocumentManager.MdiActiveDocument
    ReadOnly acDb As Database = acDc.Database
    ReadOnly T1 As New Timer
    Dim I As Integer
    Dim Ps As New Point3d(0, 0, 0)
    <CommandMethod("TcHuDie")>
    Public Sub TcHuDie()
        '绘制之前清除以前绘制的,遍历所有对象并删除
        Using acTr As Transaction = acDb.TransactionManager.StartTransaction()
            Dim Block_Table As BlockTable = acTr.GetObject(acDb.BlockTableId, OpenMode.ForRead)
            Dim BlockRecord As BlockTableRecord = acTr.GetObject(Block_Table(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            For Each EntId As ObjectId In BlockRecord
                Dim Ent As Entity = acTr.GetObject(EntId, OpenMode.ForWrite)
                Try
                    Ent.Erase()
                Catch ex As Exception
                    acDc.Editor.WriteMessage(vbCrLf & ex.Message)
                    Continue For
                End Try
            Next
            acTr.Commit()
        End Using
        '调整当前视口中心点为Point2d(0, 0)
        Dim viewTableRecord As ViewTableRecord = acDc.Editor.GetCurrentView()
        viewTableRecord.CenterPoint = New Point2d(0, 0)
        viewTableRecord.Height = 800
        viewTableRecord.Width = 800
        acDc.Editor.SetCurrentView(viewTableRecord)
        acDc.Editor.UpdateScreen()
        T1.Enabled = True
        T1.Interval = 5
        AddHandler T1.Tick, New EventHandler(AddressOf Timer1_Tick)
        T1.Start()
    End Sub
    Public Sub Timer1_Tick(ByVal sender As Object, ByVal _e As EventArgs)
        Dim docLock As Autodesk.AutoCAD.ApplicationServices.DocumentLock = acDc.LockDocument()
        I += 1 '增加1度(角度制)
        Dim T As Double = I * Pi / 180 '公式中的变量t
        Dim Sint As Double = Math.Sin(T)
        Dim Cost As Double = Math.Cos(T)
        Dim Cos4t As Double = Math.Cos(4 * T)
        Dim Sint5 As Double = Math.Sin(T / 24) ^ 5
        Dim x As Double = Sint * (e ^ Cost - 2 * Cos4t - Sint5) * R
        Dim y As Double = Cost * (e ^ Cost - 2 * Cos4t - Sint5) * R
        Dim Pe As New Point3d(x, y, 0)
        Dim L As New Line(Ps, Pe) With {
                .Color = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0)
            }
        AddEnt(L)
        Ps = Pe '紧接上一点
        '移动鼠标至当前点
        Dim point2 As System.Drawing.Point
        Dim handle As System.IntPtr = acDc.Window.Handle
        point2 = TcFunctions.CAD.ScreenFromWCSPoint_DPI(acDc.Editor, handle, Pe, 0)
        WinAPI_USER32.SetCursorPos(point2.X, point2.Y)
        acDc.Editor.UpdateScreen()
        '当t=24*Pi时 停止绘图
        If I >= T_Total Then
            I = 0
            RemoveHandler T1.Tick, New EventHandler(AddressOf Timer1_Tick)
        End If
    End Sub
    Public Shared Sub AddEnt(ByVal E As Entity)
        Dim acDc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acDb As Database = acDc.Database
        Using acTr As Transaction = acDc.TransactionManager.StartTransaction()
            Dim acBt As BlockTable
            acBt = acTr.GetObject(acDb.BlockTableId, OpenMode.ForWrite)
            Dim acBtRec As BlockTableRecord
            acBtRec = acTr.GetObject(acDb.CurrentSpaceId, OpenMode.ForWrite)
            acBtRec.AppendEntity(E)
            acTr.AddNewlyCreatedDBObject(E, True)
            acTr.Commit()
        End Using
    End Sub
    Private Declare Function ClientToScreen Lib "user32.dll" (ByVal hWnd As System.IntPtr,
                                                              ByRef pt As System.Drawing.Point) As Boolean
    Public Shared Function ScreenFromWCSPoint_DPI(ByVal acEd As Editor,
                                                  ByVal hWnd As System.IntPtr,
                                                  ByVal wcsPt As Point3d, ByVal _
                                                  vpNum As Short) As System.Drawing.Point
        Dim point1 As System.Windows.Point = acEd.PointToScreen(wcsPt, CInt(vpNum))
        Dim point2 As New System.Drawing.Point(CInt(point1.X * CurrentDPI()), CInt(point1.Y * CurrentDPI()))
        ClientToScreen(hWnd, point2)
        Return point2
    End Function
    'Dpi
    Public Shared Function CurrentDPI() As Double
        Return CDec((Graphics.FromHwnd(IntPtr.Zero).DpiX / 96.0F))
    End Function
End Class
[本日志由 tiancao1001 于 2024-02-16 10:08 PM 编辑]
      暂时没有评论