⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module1.vb

📁 苏金明编写的《用VB.NET和VC#.NET开发交互式CAD系统》一书的源代码
💻 VB
字号:
Imports System.Math
imports System.Drawing.Drawing2D 

Module Module1

    Public Const PI = 3.1416

    Public Enum geDrawMode
        Normal = 1
        Selec = 2
        Drag = 3
        Delete = 4
    End Enum

    Public Enum geStyle
        Solid = 1
        Dash = 2
        Dot = 3
        DashDot = 4
        DashDotDot = 5
    End Enum

    Public viewDX, viewDY, viewScale As Single
    Public PickRadius As Single = 3
    Public aCommand As ICommand
    Public ges As New ArrayList()
    Public geSels As New ArrayList()
    Public creLine As New CCreateLine()
    Public selected As New CSelect()
    Public transMove As New CMove()
    Public transRotate As New CRotate()
    Public transMirror As New CMirror()
    Private mat As New Matrix()


    '判断拾取点是否位于包围矩形中
    Public Function InBox(ByVal aBox As CBox, ByVal aPos As PointF) As Boolean
        If aPos.X > aBox.minX And aPos.Y > aBox.minY And aPos.X < aBox.maxX And aPos.Y < aBox.maxY Then
            Return True
        Else
            Return False
        End If
    End Function

    '计算点与点之间的距离
    Public Function DistPtoP(ByVal p1 As PointF, ByVal p2 As PointF) As Single
        Dim dx, dy As Single
        dx = p2.X - p1.X
        dy = p2.Y - p1.Y
        Return Sqrt((dx * dx) + (dy * dy))
    End Function

    '计算点间连线与X轴正向之间的夹角
    Public Function GetAngle(ByVal p1 As PointF, ByVal p2 As PointF) As Single
        Dim tansita, sita, dx As Single
        dx = Abs(p2.X - p1.X)
        If p2.X = p1.X Then dx = 0.000001
        tansita = Abs(p2.Y - p1.Y) / dx
        sita = Atan(tansita)
        '如果终点横坐标大于、等于起点横坐标,并且终点纵坐标大于、等于起点纵坐标
        If p2.X >= p1.X And p2.Y >= p1.Y Then
            Return sita
            '如果终点横坐标小于、等于起点横坐标,并且终点纵坐标大于、等于起点纵坐标
        ElseIf p2.X <= p1.X And p2.Y >= p1.Y Then
            Return PI - sita
            '如果终点横坐标小于、等于起点横坐标,并且终点纵坐标小于、等于起点纵坐标
        ElseIf p2.X <= p1.X And p2.Y <= p1.Y Then
            Return PI + sita
            '如果终点横坐标大于、等于起点横坐标,并且终点纵坐标小于、等于起点纵坐标
        ElseIf p2.X >= p1.X And p2.Y <= p1.Y Then
            Return PI * 2 - sita
        End If
    End Function

    Public Sub Coordinate(ByVal g As Graphics)
        g.TranslateTransform(viewDX, viewDY)
        g.Clear(Color.White)
        DrawAll(g)
    End Sub

    Public Sub DrawAll(ByVal g As Graphics)
        Dim i As Integer
        For i = 0 To ges.Count - 1
            ges(i).draw(g, geDrawMode.Normal)
        Next
    End Sub

    Public Sub DrawSel(ByVal g As Graphics)
        Dim i As Integer
        For i = 0 To geSels.Count - 1
            geSels(i).draw(g, geDrawMode.Selec)
        Next
    End Sub

    '页面坐标转换为世界坐标
    Public Function PagetoWorld(ByVal ep As PointF) As PointF
        Dim p As New PointF()
        With ep
            p.X = (.X - viewDX)
            p.Y = -(.Y - viewDY)
        End With
        Return p
    End Function

    '世界坐标转换为页面坐标
    Public Function WorldtoPage(ByVal pp As PointF) As PointF
        Dim p As New PointF()
        With pp
            p.X = .X + viewDX
            p.Y = -.Y + viewDY
        End With
        Return p
    End Function

    '计算旋转变换以后点的坐标
    Public Function pRotate(ByVal baseP As PointF, ByVal Pos As PointF, ByVal angle As Single) As PointF
        Dim pr As New PointF()
        Dim cosv, sinv As Single

        cosv = Cos(angle)
        sinv = Sin(angle)
        With pr
            .X = Pos.X * cosv - Pos.Y * sinv + (1 - cosv) * baseP.X + baseP.Y * sinv
            .Y = sinv * Pos.X + cosv * Pos.Y + (1 - cosv) * baseP.Y - sinv * baseP.X
        End With
        Return pr
    End Function

    '计算镜像以后点的坐标
    Public Function pMirror(ByVal Pos1 As PointF, ByVal Pos2 As PointF, ByVal Pos As PointF) As PointF
        Dim pm As New PointF()
        Dim Angle, cos2v, sin2v As Single
        Dim x1, y1, x2, y2 As Single
        Dim aa, desX, desY As Single
        With Pos1
            x1 = .X
            y1 = .Y
        End With
        With Pos2
            x2 = .X
            y2 = .Y
        End With
        If x2 = x1 Then
            aa = 10000000
        Else
            aa = (x2 * y1 - x1 * y2) / (x2 - x1)
        End If
        Angle = GetAngle(Pos1, Pos2)
        cos2v = Cos(Angle * 2)
        sin2v = Sin(Angle * 2)
        desX = Pos.X * cos2v + Pos.Y * sin2v - aa * sin2v
        desY = Pos.X * sin2v - Pos.Y * cos2v + aa * cos2v + aa
        With pm
            .X = desX
            .Y = desY
        End With
        Return pm
    End Function

    Public Sub ScaleZoom(ByVal g As Graphics, ByVal dx As Single, ByVal dy As Single)
        Dim i As Integer
        For i = 0 To geSels.Count - 1
            geSels(i).draw(g, geDrawMode.Delete)
            geSels(i).scale(g, dx, dy)
            geSels(i).draw(g, geDrawMode.Selec)
        Next
    End Sub

End Module

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -