Draw polygon by mouse snap to grid

  • Thread starter Thread starter Hany Metry
  • Start date Start date
H

Hany Metry

Guest
Hi Mr. Tommy,

I try to draw polygon by mouse snap to grid or not snap to grid and I write the below code which is taken partially from Tommy code How to draw by mouse a Polygon of any shape (snap to grid or not snap to grid) (the snap maybe off and the snap maybe on) and the distance between snaps are input data as well as the distance between grids are input data.

I did not success, Please advice.


Option Strict On
Option Explicit On
Option Infer Off
Public Class Form33inputbydraw
Private MouseDownPt, MouseMovePt As PointF
Private MouseStatus As Integer = 0
Private Shapes As New List(Of Shape)
Public Class Shape
Public pt1 As PointF
Public pt2 As PointF
Public color As Color = color.Black
Public Sub Draw(g As Graphics)
Using p As New Pen(color, g.VisibleClipBounds.Width / 500)
g.DrawLine(p, pt1.X, pt1.Y, pt2.X, pt2.Y)
End Using
End Sub
End Class
Private Sub Form33inputbydraw_shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Form34Snap.Show()
End Sub
Private Sub picturebox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Pirosol = 2 Then
Pty = CInt(((PictureBox1.Height / 2) - e.Y) * (2000 / ((0.97 * PictureBox1.Height))))
Ptx = CInt((e.X - (PictureBox1.Width / 2)) * (2000 / ((0.97 * PictureBox1.Height))))
ToolStripStatusLabel1.Text = ("X = " & Ptx & " mm")
ToolStripStatusLabel2.Text = ("Y = " & Pty & " mm")
ElseIf Pirosol = 3 Or DrawingAreaToolStripMenuItem.Checked = True Then
If CDbl((Ymaxl + Math.Abs(Yminl)) / (Xmaxl + Math.Abs(Xminl))) <= CDbl(PictureBox1.Height / PictureBox1.Width) Then
Xorgin = CInt(0.015 * PictureBox1.Width + Math.Abs(Xminl) * 0.97 * PictureBox1.Width / (Math.Abs(Xminl) + Xmaxl))
Yorgin = CInt(0.5 * PictureBox1.Height - ((0.5 * (Ymaxl + Math.Abs(Yminl)) - Ymaxl) * 0.97 * PictureBox1.Width) / (Math.Abs(Xminl) + Xmaxl))
Pty = CInt(((Yorgin) - e.Y) * ((Xmaxl - Xminl) / ((0.97 * PictureBox1.Width))))
Ptx = CInt((e.X - (Xorgin)) * ((Xmaxl - Xminl) / ((0.97 * PictureBox1.Width))))
Else
Xorgin = CInt(0.5 * PictureBox1.Width - ((0.5 * (Xmaxl + Math.Abs(Xminl)) - Math.Abs(Xminl)) * 0.97 * PictureBox1.Height) / (Math.Abs(Yminl) + Ymaxl))
Yorgin = CInt(0.015 * PictureBox1.Height + Math.Abs(Ymaxl) * 0.97 * PictureBox1.Height / (Math.Abs(Yminl) + Ymaxl))
Pty = CInt(((Yorgin) - e.Y) * ((Ymaxl - Yminl) / ((0.97 * PictureBox1.Height))))
Ptx = CInt((e.X - (Xorgin)) * ((Ymaxl - Yminl) / ((0.97 * PictureBox1.Height))))
End If
ToolStripStatusLabel1.Text = ("X = " & Ptx & " mm")
ToolStripStatusLabel2.Text = ("Y = " & Pty & " mm")
End If
Dim pt As New PointF(Ptx, Pty)
Select Case MouseStatus
Case 1
MouseMovePt = SnapToGrid(pt)
PictureBox1.Invalidate()
End Select
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If Pirosol = 2 Then
yyy1 = (PictureBox1.Height * 0.015)
intyyy = (PictureBox1.Height * 0.97 / 20)
xxx1 = (PictureBox1.Width / 2 - intyyy * 10)
mpen5.DashStyle = Drawing2D.DashStyle.DashDot
For I = 1 To 21
e.Graphics.DrawLine(mpen5, CInt(xxx1 + (I - 1) * (intyyy)), CInt(yyy1), CInt(xxx1 + (I - 1) * (intyyy)), CInt(yyy1 + 20 * intyyy))
Next
For I = 1 To 21
e.Graphics.DrawLine(mpen5, CInt(xxx1), CInt(yyy1 + (I - 1) * intyyy), CInt(xxx1 + 20 * (intyyy)), CInt(yyy1 + (I - 1) * intyyy))
Next
xxx2 = CInt(PictureBox1.Width / 2 + 0.05 * PictureBox1.Width / 2)
xxx3 = CInt(PictureBox1.Width / 2)
'xxx4 = CInt(PictureBox1.Width / 2 - 0.05 * intyyy * 10)
yyy2 = CInt(PictureBox1.Height / 2 - 0.05 * PictureBox1.Height / 2)
yyy3 = CInt(PictureBox1.Height / 2)
'yyy4 = CInt(PictureBox1.Height / 2 + 0.05 * intyyy * 10)
Dim DrawFont As New Font("Verdana", 10, FontStyle.Regular)
e.Graphics.DrawLine(mpen, CInt(xxx2), CInt(yyy3), CInt(xxx3), CInt(yyy3))
e.Graphics.DrawLine(mpen, CInt(xxx3), CInt(yyy2), CInt(xxx3), CInt(yyy3))
e.Graphics.DrawString(Str3, DrawFont, Brush2, CInt(xxx2 + 5), CInt(yyy3 - 8))
e.Graphics.DrawString(Str4, DrawFont, Brush2, CInt(xxx3 - 6), CInt(yyy2 - 18))
ElseIf Pirosol = 3 Then
Numgridxp = CInt(Xmaxl / Sxg)
Numgridxn = CInt(Xminl / Sxg)
Numgridyp = CInt(Ymaxl / Syg)
Numgridyn = CInt(Yminl / Syg)
If CDbl((Ymaxl + Math.Abs(Yminl)) / (Xmaxl + Math.Abs(Xminl))) <= CDbl(PictureBox1.Height / PictureBox1.Width) Then
Xorgin = CInt(0.015 * PictureBox1.Width + Math.Abs(Xminl) * 0.97 * PictureBox1.Width / (Math.Abs(Xminl) + Xmaxl))
Yorgin = CInt(0.5 * PictureBox1.Height - ((0.5 * (Ymaxl + Math.Abs(Yminl)) - Ymaxl) * 0.97 * PictureBox1.Width) / (Math.Abs(Xminl) + Xmaxl))
Sxgsc = (Sxg * 0.97 * PictureBox1.Width / (Math.Abs(Xminl) + Xmaxl))
Sygsc = (Syg * 0.97 * PictureBox1.Width / (Math.Abs(Xminl) + Xmaxl))
xxx2 = CInt((Xorgin + 0.05 * (0.97 * PictureBox1.Width - Xorgin)))
yyy2 = CInt((Yorgin - 0.05 * (Yorgin)))
Else
Xorgin = CInt(0.5 * PictureBox1.Width - ((0.5 * (Xmaxl + Math.Abs(Xminl)) - Math.Abs(Xminl)) * 0.97 * PictureBox1.Height) / (Math.Abs(Yminl) + Ymaxl))
Yorgin = CInt(0.015 * PictureBox1.Height + Math.Abs(Ymaxl) * 0.97 * PictureBox1.Height / (Math.Abs(Yminl) + Ymaxl))
Sxgsc = (Sxg * 0.97 * PictureBox1.Height / (Math.Abs(Yminl) + Ymaxl))
Sygsc = (Syg * 0.97 * PictureBox1.Height / (Math.Abs(Yminl) + Ymaxl))
xxx2 = CInt((Xorgin + 0.05 * (0.97 * PictureBox1.Width - Xorgin)))
yyy2 = CInt((Yorgin - 0.05 * (Yorgin)))
End If
mpen5.DashStyle = Drawing2D.DashStyle.DashDot
If Form34Snap.CheckBox1.Checked = True Then
For I = 0 To Numgridxp
e.Graphics.DrawLine(mpen5, CInt(Xorgin + Sxgsc * (I)), CInt(0.015 * PictureBox1.Height), CInt(Xorgin + Sxgsc * (I)), CInt(0.97 * PictureBox1.Height))
Next
If Numgridxn <> 0 Then
For I = 1 To Numgridxn
e.Graphics.DrawLine(mpen5, CInt(Xorgin - Sxgsc * (I)), CInt(0.015 * PictureBox1.Height), CInt(Xorgin - Sxgsc * (I)), CInt(0.97 * PictureBox1.Height))
Next
End If
For I = 0 To Numgridyp
e.Graphics.DrawLine(mpen5, CInt(0.015 * PictureBox1.Width), CInt(Yorgin - Sygsc * (I)), CInt(0.97 * PictureBox1.Width), CInt(Yorgin - Sygsc * (I)))
Next
If Numgridyn <> 0 Then
For I = 1 To Numgridyn
e.Graphics.DrawLine(mpen5, CInt(0.015 * PictureBox1.Width), CInt(Yorgin + Sygsc * (I)), CInt(0.97 * PictureBox1.Width), CInt(Yorgin + Sygsc * (I)))
Next
End If
End If
Dim DrawFont As New Font("Verdana", 10, FontStyle.Regular)
e.Graphics.DrawLine(mpen, CInt(xxx2), CInt(Yorgin), CInt(Xorgin), CInt(Yorgin))
e.Graphics.DrawLine(mpen, CInt(Xorgin), CInt(yyy2), CInt(Xorgin), CInt(Yorgin))
e.Graphics.DrawString(Str3, DrawFont, Brush2, CInt(xxx2 + 5), CInt(Yorgin - 8))
e.Graphics.DrawString(Str4, DrawFont, Brush2, CInt(Xorgin - 6), CInt(yyy2 - 18))
End If
With e.Graphics
For Each shp As Shape In Shapes
shp.Draw(e.Graphics)
Next

Select Case MouseStatus
Case 1 'draw line tracer
.DrawLine(New Pen(Color.Red, .VisibleClipBounds.Width / 500), MouseDownPt.X, MouseDownPt.Y, MouseMovePt.X, MouseMovePt.Y)
End Select
End With
End Sub
Private Sub DrawingAreaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DrawingAreaToolStripMenuItem.Click
Form34Snap.Show()
End Sub
Private Sub Picturebox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If Pirosol = 2 Then
Pty = CInt(((PictureBox1.Height / 2) - e.Y) * (2000 / ((0.97 * PictureBox1.Height))))
Ptx = CInt((e.X - (PictureBox1.Width / 2)) * (2000 / ((0.97 * PictureBox1.Height))))
ElseIf Pirosol = 3 Or DrawingAreaToolStripMenuItem.Checked = True Then
If CDbl((Ymaxl + Math.Abs(Yminl)) / (Xmaxl + Math.Abs(Xminl))) <= CDbl(PictureBox1.Height / PictureBox1.Width) Then
Xorgin = CInt(0.015 * PictureBox1.Width + Math.Abs(Xminl) * 0.97 * PictureBox1.Width / (Math.Abs(Xminl) + Xmaxl))
Yorgin = CInt(0.5 * PictureBox1.Height - ((0.5 * (Ymaxl + Math.Abs(Yminl)) - Ymaxl) * 0.97 * PictureBox1.Width) / (Math.Abs(Xminl) + Xmaxl))
Pty = CInt(((Yorgin) - e.Y) * ((Xmaxl - Xminl) / ((0.97 * PictureBox1.Width))))
Ptx = CInt((e.X - (Xorgin)) * ((Xmaxl - Xminl) / ((0.97 * PictureBox1.Width))))
Else
Xorgin = CInt(0.5 * PictureBox1.Width - ((0.5 * (Xmaxl + Math.Abs(Xminl)) - Math.Abs(Xminl)) * 0.97 * PictureBox1.Height) / (Math.Abs(Yminl) + Ymaxl))
Yorgin = CInt(0.015 * PictureBox1.Height + Math.Abs(Ymaxl) * 0.97 * PictureBox1.Height / (Math.Abs(Yminl) + Ymaxl))
Pty = CInt(((Yorgin) - e.Y) * ((Ymaxl - Yminl) / ((0.97 * PictureBox1.Height))))
Ptx = CInt((e.X - (Xorgin)) * ((Ymaxl - Yminl) / ((0.97 * PictureBox1.Height))))
End If
End If
Dim pt as new PointF(Ptx , Pty)
'for multiline dont need to set mouse down
If MouseStatus = 0 Then MouseDownPt = pt
MouseMovePt = MouseDownPt
MouseStatus = 1
If e.Button = MouseButtons.Right Then MouseStatus = 0
PictureBox1.Invalidate()
End Sub
Private Sub Picturebox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If Pirosol = 2 Then
Pty = CInt(((PictureBox1.Height / 2) - e.Y) * (2000 / ((0.97 * PictureBox1.Height))))
Ptx = CInt((e.X - (PictureBox1.Width / 2)) * (2000 / ((0.97 * PictureBox1.Height))))
ElseIf Pirosol = 3 Or DrawingAreaToolStripMenuItem.Checked = True Then
If CDbl((Ymaxl + Math.Abs(Yminl)) / (Xmaxl + Math.Abs(Xminl))) <= CDbl(PictureBox1.Height / PictureBox1.Width) Then
Xorgin = CInt(0.015 * PictureBox1.Width + Math.Abs(Xminl) * 0.97 * PictureBox1.Width / (Math.Abs(Xminl) + Xmaxl))
Yorgin = CInt(0.5 * PictureBox1.Height - ((0.5 * (Ymaxl + Math.Abs(Yminl)) - Ymaxl) * 0.97 * PictureBox1.Width) / (Math.Abs(Xminl) + Xmaxl))
Pty = CInt(((Yorgin) - e.Y) * ((Xmaxl - Xminl) / ((0.97 * PictureBox1.Width))))
Ptx = CInt((e.X - (Xorgin)) * ((Xmaxl - Xminl) / ((0.97 * PictureBox1.Width))))
Else
Xorgin = CInt(0.5 * PictureBox1.Width - ((0.5 * (Xmaxl + Math.Abs(Xminl)) - Math.Abs(Xminl)) * 0.97 * PictureBox1.Height) / (Math.Abs(Yminl) + Ymaxl))
Yorgin = CInt(0.015 * PictureBox1.Height + Math.Abs(Ymaxl) * 0.97 * PictureBox1.Height / (Math.Abs(Yminl) + Ymaxl))
Pty = CInt(((Yorgin) - e.Y) * ((Ymaxl - Yminl) / ((0.97 * PictureBox1.Height))))
Ptx = CInt((e.X - (Xorgin)) * ((Ymaxl - Yminl) / ((0.97 * PictureBox1.Height))))
End If
End If
Dim pt As New PointF(Ptx, Pty)
If e.Button = MouseButtons.Right Then
MouseStatus = 0
Else
Select Case MouseStatus
Case 1
MouseMovePt = SnapToGrid(pt)
Dim shp As New Shape
shp.pt1 = MouseDownPt
shp.pt2 = MouseMovePt
shp.color = Color.Black
Shapes.Add(shp)
MouseStatus = 0
'for multiline repeat last point as new second point
MouseStatus = 1
MouseDownPt = shp.pt2
End Select
End If
PictureBox1.Invalidate()
End Sub
Private Function SnapToGrid(thispt As PointF) As PointF
Dim x, y As Single
If Form34Snap.CheckBox2.Checked = True Then
x = CInt(thispt.X / Sxs) * Sxs
y = CInt(thispt.Y / Sys) * Sys
Else
x = thispt.X
y = thispt.Y
End If
Return New PointF(x, y)
End Function
End Class


Kind Regards,


Hany Metry

Continue reading...
 
Back
Top