Em mới tập viết một VBA vẽ lưới toạ độ N, E nhưng không biết cách tạo form trong VBA . Form có dạng Pick điểm thứ nhất và thứ 2 của vùng định vẽ lưới, chọn khoảng cách giữa các lưới theo trục X và trục Y, một nút bấm OK và một nút bấm cancel. Các bác có kinh nghiệm về VBA giúp em với. Link file là
http://www.cadviet.com/upfiles/line_Cor_Thao.dvb
Đây là đoạn mã của VBA cho vẽ lưới toạ độ (cái này thì chạy ngon).
Sub duongthang()
Dim lineObj1 As AcadLine
Dim lineObj2 As AcadLine
Dim startPoint As Variant
Dim endPoint As Variant
Dim startPoint1(0 To 2) As Double
Dim endPoint1(0 To 2) As Double
Dim startPoint2(0 To 2) As Double
Dim endPoint2(0 To 2) As Double
Dim Xaxis As Integer
Dim Yaxis As Integer
Dim X1 As Long
Dim Y1 As Long
Dim X2 As Long
Dim Y2 As Long
' Define the start and end points for the area
startPoint = ThisDrawing.Utility.GetPoint(, "Enter a first point: ")
endPoint = ThisDrawing.Utility.GetPoint(, "Enter a second point: ")
Xaxis = ThisDrawing.Utility.GetInteger("Enter distance of X axis:")
Yaxis = ThisDrawing.Utility.GetInteger("Enter distance of Y axis:")
X1 = Round(startPoint(0) / Xaxis, 0) * Xaxis
Y1 = Round(startPoint(1) / Yaxis, 0) * Yaxis
X2 = Round(endPoint(0) / Xaxis, 0) * Xaxis
Y2 = Round(endPoint(1) / Yaxis, 0) * Yaxis
MsgBox "The Coordinate of the first point is: " & X1 & ", " & Y1 & ", " & 0
MsgBox "The Coordinate of the second point is: " & X2 & ", " & Y2 & ", " & 0
startPoint1(0) = X1 + 10: startPoint1(1) = Y1 - 0: startPoint1(2) = 0
endPoint1(0) = X1 - 10: endPoint1(1) = Y1 - 0: endPoint1(2) = 0
startPoint2(0) = X1 - 0: startPoint2(1) = Y1 - 10: startPoint2(2) = 0
endPoint2(0) = X1 - 0: endPoint2(1) = Y1 + 10: endPoint2(2) = 0
' Create the cross line in model space with length is 4
Set lineObj1 = ThisDrawing.ModelSpace.AddLine(startPoint1, endPoint1)
Set lineObj2 = ThisDrawing.ModelSpace.AddLine(startPoint2, endPoint2)
Dim layerObj As AcadLayer
Set layerObj = ThisDrawing.Layers.Add("Grid")
lineObj1.Layer = "Grid"
lineObj1.Update
lineObj2.Layer = "Grid"
lineObj2.Update
ZoomAll
'Create cross line grid
Dim retObjec1 As Variant
Dim retObjec2 As Variant
Dim numberOfRows As Long
Dim numberOfColumns As Long
Dim numberOfLevels As Long
Dim distanceBwtnRows As Double
Dim distanceBwtnColumns As Double
Dim distanceBwtnLevels As Double
numberOfRows = Round((Y2 - Y1) / Yaxis, 0) + 1
numberOfColumns = Round((X2 - X1) / Xaxis, 0) + 1
numberOfLevels = 1
distanceBwtnRows = Yaxis
distanceBwtnColumns = Xaxis
distanceBwtnLevels = 1
retObj1 = lineObj1.ArrayRectangular(numberOfRows, numberOfColumns, numberOfLevels, distanceBwtnRows, distanceBwtnColumns, distanceBwtnLevels)
retObj2 = lineObj2.ArrayRectangular(numberOfRows, numberOfColumns, numberOfLevels, distanceBwtnRows, distanceBwtnColumns, distanceBwtnLevels)
'Create Coordinate text at the cross line
Dim CoordinteN As Long
Dim CoordinteE As Long
Dim textObj1 As AcadText
Dim textObj2 As AcadText
Dim textStringN As String
Dim textStringE As String
Dim insertionPoint(0 To 2) As Double
Dim N As Double
Dim E As Double
Dim height As Double
Dim i As Integer
For i = 0 To Round((numberOfColumns - 1) / 2) Step 1
E = X1 + Xaxis * i * 2
insertionPoint(0) = E + 1: insertionPoint(1) = Y1 + 11: insertionPoint(2) = 0
height = 2
textStringE = "E " & Round(E, 4) & ".000"
Set textObj1 = ThisDrawing.ModelSpace.AddText(textStringE, insertionPoint, height)
textObj1.Rotation = 1.570796327
textObj1.Layer = "Grid"
textObj1.Update
retObj1 = textObj1.ArrayRectangular(Round((numberOfRows / 2), 0), 1, numberOfLevels, distanceBwtnRows * 2, distanceBwtnColumns * 2, distanceBwtnLevels)
Next
For i = 0 To Round((numberOfRows - 1) / 2) Step 1
N = Y1 + Yaxis * 2 * i
insertionPoint(0) = X1 + 11: insertionPoint(1) = N - 1: insertionPoint(2) = 0
height = 2
textStringN = "N " & Round(N, 4) & ".000"
Set textObj2 = ThisDrawing.ModelSpace.AddText(textStringN, insertionPoint, height)
textObj2.Rotation = 0
textObj2.Layer = "Grid"
textObj2.Update
retObj2 = textObj2.ArrayRectangular(1, Round((numberOfColumns / 2), 0), numberOfLevels, distanceBwtnRows * 2, distanceBwtnColumns * 2, distanceBwtnLevels)
Next
ZoomExtents
End Sub
Còn đây là mã của Userform (Em chưa biết làm cách nào để khi chạy file dvb thì nó sẽ hiện ra một bảng có các lựa chọn pick điểm, nhập khoảng cách phương X, phương Y, OK, cancel...)
Private Sub CommandButton1_Click() 'Dùng để pick tọa độ điểm 1
End Sub
Private Sub CommandButton2_Click() 'Dùng để pick tọa độ điểm 2
End Sub
Private Sub Label1_Click() ' Chọn giá trị khoảng cách theo trục X
End Sub
Private Sub Label2_Click() ' Chọn giá trị khoảng cách theo trục Y
End Sub
Private Sub TextBox1_Change() ' Nhập giá trị khoảng cách theo trục X
End Sub
Private Sub TextBox2_Change() ' Nhập giá trị khoảng cách theo trục X
End Sub
Private Sub CommandButton3_Click() ' Nút OK
End Sub
Private Sub CommandButton4_Click() ' Nút Cancel
End Sub
Private Sub UserForm_Click()
End Sub