private void S57Control_PaintOverlaysEvent(object sender, Graphics gr)
{
// Initialize usvTrail if not already done
if (usvTrail == null)
{
usvTrail = new List>();
}
// USV Position
if (DataRead._USVGeoCoordinate != null)
{
usvPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = DataRead._USVGeoCoordinate.Latitude,
Lon = DataRead._USVGeoCoordinate.Longitude
});
//var usvGeoPoint = m_draw.GeoPix.GeoPointFromPixelPoint(usvPixelPoint);
if (m_draw.GeoPix.PointInView || !m_draw.GeoPix.PointInView)
{
var currentusvGeocordinate = new GeoCoordinate(DataRead._USVGeoCoordinate.Latitude, DataRead._USVGeoCoordinate.Longitude);
if (usvTrail.Count > 0)
{
lastPosition = usvTrail[usvTrail.Count - 1].Item1;
lastPositionAngle = usvTrail[usvTrail.Count - 1].Item2;
float distance = DataRead.DistanceBetweenPoints(lastPosition, currentusvGeocordinate);
if (distance >= 10)
{
usvTrail.Add(new Tuple(currentusvGeocordinate, DataRead.USV_Angle));
}
}
else
{
usvTrail.Add(new Tuple(currentusvGeocordinate, DataRead.USV_Angle));
}
if (DataRead._USVGeoCoordinate != null)
{
//Draw the USV at the current position
usvRectangle = new Rectangle(Convert.ToInt32(usvPixelPoint.X - 14),
Convert.ToInt32(usvPixelPoint.Y - 14), 30, 30);
}
//Draw Circle around USV
Color transparentOrange = Color.FromArgb(50, Color.DarkOrange);
var usvPixelPerMeters = m_draw.PixelSizeMetres * m_draw.GeoPix.DisplayScale;
int radius = (int)(600 / (2 * Math.PI) / usvPixelPerMeters);
using (Brush brush = new SolidBrush(transparentOrange))
{
gr.FillEllipse(brush, usvPixelPoint.X - radius, usvPixelPoint.Y - radius, radius * 2, radius * 2);
}
Font distFont = new Font("Calibri", 11,FontStyle.Bold);
Brush textBrush = Brushes.Black;
PointF topPoint = new PointF(usvPixelPoint.X - 30, usvPixelPoint.Y - radius - 15);
gr.DrawString("Safe Zone", distFont, textBrush, topPoint);
if (_rotatechartbuttonChecked)
{
m_draw.SetDirectionOfUp(DataRead.USV_Angle, _defaultPixelPoint);
//Draw USVImage
Bitmap rotatedImage = RotateImage(_usvImage, 0);
gr.DrawImage(rotatedImage, usvRectangle);
gr.SmoothingMode = SmoothingMode.AntiAlias;
//Draw trail
foreach (var point in usvTrail)
{
var trailPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = point.Item1.Latitude,
Lon = point.Item1.Longitude
});
trailRectangle = new Rectangle(Convert.ToInt32(trailPixelPoint.X), Convert.ToInt32(trailPixelPoint.Y), 10, 10);
RotateRectangle(gr, trailRectangle, point.Item2 - DataRead.USV_Angle);
if (point.Item2 >= 0 && point.Item2 < 90)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X - 3, trailRectangle.Y, 3, 10);
}
else if (point.Item2 >= 90 && point.Item2 < 180)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y + 7, 3, 10);
}
else if (point.Item2 >= 180 && point.Item2 <= 270)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X + 7, trailRectangle.Y, 3, 10);
}
else if (point.Item2 > 270 && point.Item2 < 360)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y, 3, 10);
}
}
gr.SmoothingMode = SmoothingMode.Default;
}
else if (!_rotatechartbuttonChecked)
{
m_draw.SetDirectionOfUp(0, _defaultPixelPoint);
//Draw USVImage
Bitmap rotatedImage = RotateImage(_usvImage, DataRead.USV_Angle);
gr.DrawImage(rotatedImage, usvRectangle);
gr.SmoothingMode = SmoothingMode.AntiAlias;
// Draw trail
foreach (var point in usvTrail)
{
var trailPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = point.Item1.Latitude,
Lon = point.Item1.Longitude
});
trailRectangle = new Rectangle(Convert.ToInt32(trailPixelPoint.X), Convert.ToInt32(trailPixelPoint.Y), 10, 10);
RotateRectangle(gr, trailRectangle, point.Item2);
if (point.Item2 >= 0 && point.Item2 < 90)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X - 3, trailRectangle.Y, 3, 10);
}
else if (point.Item2 >= 90 && point.Item2 < 180)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y + 7, 3, 10);
}
else if (point.Item2 >= 180 && point.Item2 <= 270)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X + 7, trailRectangle.Y, 3, 10);
}
else if (point.Item2 > 270 && point.Item2 < 360)
{
gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y, 3, 10);
}
}
gr.SmoothingMode = SmoothingMode.Default;
}
//Draw AIS
if (windowsUIButtonPanel4.Buttons["AIS"].Properties.Checked)
{
DrawAis(gr);
}
// Draw RangeBar
//var futureDistance = DataRead.USV_SOG * mpsToKmPerHour * 1 * MinToHour; // distance in nautical miles for 1 minute
//var futurePoint = DataRead.FindPointAtDistanceFrom(
// currentusvGeocordinate,
// DataRead.ToRad(DataRead.USV_Angle),
// futureDistance);
//var futureDistancePixelPoint = m_draw.GeoPix.Point(new GeoPoint { Lat = futurePoint.Latitude, Lon = futurePoint.Longitude });
//var futureGeopoint = m_draw.GeoPix.GeoPointFromPixelPoint(futureDistancePixelPoint);
//gr.SmoothingMode = SmoothingMode.AntiAlias;
//using (var rangeBarPen = new Pen(Color.Black, 3))
//{
// gr.DrawLine(rangeBarPen, (int)usvGeoPoint.Lat, (int)usvGeoPoint.Lon, (int)futureGeopoint.Lat, (int)futureGeopoint.Lon);
//}
//gr.SmoothingMode = SmoothingMode.Default;
}
}
S57Control1.Invalidate();
}
private void DrawAis(Graphics gr)
{
//AIS Position
if (aisTrail == null) //Initialize usvTrail if not already done
{
aisTrail = new List>();
}
if (DataRead._AISGeoCoordinate != null)
{
aisPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = DataRead._AISGeoCoordinate.Latitude,
Lon = DataRead._AISGeoCoordinate.Longitude
});
//var usvGeoPoint = m_draw.GeoPix.GeoPointFromPixelPoint(usvPixelPoint);
if (m_draw.GeoPix.PointInView || !m_draw.GeoPix.PointInView)
{
var currentaisGeocordinate = new GeoCoordinate(DataRead._AISGeoCoordinate.Latitude,
DataRead._AISGeoCoordinate.Longitude);
if (aisTrail.Count > 0)
{
lastaisPosition = aisTrail[aisTrail.Count - 1].Item2;
lastaisPositionAngle = aisTrail[aisTrail.Count - 1].Item1;
float aisdistance = DataRead.DistanceBetweenPoints(lastaisPosition, currentaisGeocordinate);
if (aisdistance >= 10)
{
aisTrail.Add(new Tuple(DataRead.AIS_Angle,currentaisGeocordinate));
}
}
else
{
aisTrail.Add(new Tuple(DataRead.AIS_Angle, currentaisGeocordinate));
}
if (DataRead._AISGeoCoordinate != null)
{
//Draw the USV at the current position
aisRectangle = new Rectangle(Convert.ToInt32(aisPixelPoint.X - 14),
Convert.ToInt32(aisPixelPoint.Y - 14), 30, 30);
}
//Draw AIS Image
Bitmap rotatedAISImage = RotateImage(_aisImage, DataRead.AIS_Angle);
gr.DrawImage(rotatedAISImage, aisRectangle);
gr.SmoothingMode = SmoothingMode.AntiAlias;
// Draw trail
foreach (var aispoint in aisTrail)
{
var trailaisPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = aispoint.Item2.Latitude,
Lon = aispoint.Item2.Longitude
});
trailaisRectangle = new Rectangle(Convert.ToInt32(trailaisPixelPoint.X),
Convert.ToInt32(trailaisPixelPoint.Y), 10, 10);
RotateRectangle(gr, trailaisRectangle, aispoint.Item1);
if (aispoint.Item1 >= 0 && aispoint.Item1 < 90)
{
gr.DrawRectangle(Pens.Black, trailaisRectangle.X - 3, trailaisRectangle.Y, 3, 10);
}
else if (aispoint.Item1 >= 90 && aispoint.Item1 < 180)
{
gr.DrawRectangle(Pens.Black, trailaisRectangle.X, trailaisRectangle.Y + 7, 3, 10);
}
else if (aispoint.Item1 >= 180 && aispoint.Item1 <= 270)
{
gr.DrawRectangle(Pens.Black, trailaisRectangle.X + 7, trailaisRectangle.Y, 3, 10);
}
else if (aispoint.Item1 > 270 && aispoint.Item1 < 360)
{
gr.DrawRectangle(Pens.Black, trailaisRectangle.X, trailaisRectangle.Y, 3, 10);
}
}
gr.SmoothingMode = SmoothingMode.Default;
}
}
}
here in this code i am drawing two images usv and ais. but when i am changing the angle of usv and speed of usv its effecting the drawing of ais image so i dont want call drawais(gr) inside that paintoverlay event any other way i can call that method somewhere?
NaveenPosted Jul 15, 2024, 8:31 AM
Hi Chetan,
Thank you for your answer,but when i tried this its not drawing AIS image at all.actually i tried this way before also but its not drawing
Chetan SanghaniPosted Jul 13, 2024, 6:20 AM
To separate the drawing of the USV and AIS images so that changes to the USV do not affect the AIS drawing, you can handle the drawing of the AIS in a different event or method. Here are the steps to achieve this:
DrawAis(gr)call fromS57Control_PaintOverlaysEvent.Step-by-Step Implementation
Remove
DrawAis(gr)call fromS57Control_PaintOverlaysEvent:Create a separate method for AIS drawing:
Create a separate event or method to call
DrawAis:You can create a new Paint event for AIS or call
DrawAisfrom another appropriate location in your code. For instance, you can use the form's Paint event or create a timer to periodically update the AIS drawing.Example: Using a Timer to Update AIS Drawing
Add a Timer to your form:
Initialize and start the Timer in your form's constructor or load event:
Handle the control's Paint event and call
DrawAis:Final Step
Make sure to bind the Paint event of
S57Control1to the new event handler.By following these steps, you separate the drawing logic for USV and AIS, ensuring that changes to USV do not affect AIS drawing. The use of a timer ensures that AIS information is updated and redrawn at regular intervals.