/
GCS.cs
3904 lines (3283 loc) · 147 KB
/
GCS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using DotSpatial.Data;
using DotSpatial.Projections;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using log4net;
using MissionPlanner.Comms;
using MissionPlanner.Controls;
using MissionPlanner.Controls.Waypoints;
using MissionPlanner.Maps;
using MissionPlanner.Utilities;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using ILog = log4net.ILog;
namespace MissionPlanner
{
public partial class GCS : Form
{
Thread thisthread;
Thread serialreaderthread;
bool serialThread = false;
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static MAVLinkInterface comPort = new MAVLinkInterface();
Hashtable param = new Hashtable();
public List<PointLatLngAlt> pointlist = new List<PointLatLngAlt>(); // used to calc distance
public List<PointLatLngAlt> fullpointlist = new List<PointLatLngAlt>();
List<int> groupmarkers = new List<int>();
public GMapRoute homeroute = new GMapRoute("home route");
public static GMapOverlay polygonsoverlay; // where the track is drawn
GMapMarkerRect CurentRectMarker;
public static GMapOverlay objectsoverlay; // where the markers a drawn
int selectedrow;
GMapMarker CurrentGMapMarker;
GMapMarker currentMarker;
GMapMarker center = new GMarkerGoogle(new PointLatLng(0.0, 0.0), GMarkerGoogleType.none);
bool isMouseClickOffMenu;
bool isMouseDown;
bool isMouseDraging;
bool splinemode;
internal PointLatLng MouseDownEnd;
List<List<Locationwp>> history = new List<List<Locationwp>>();
public bool quickadd;
PointLatLngAlt mouseposdisplay = new PointLatLngAlt(0, 0);
public static GMapControl mymap;
public GCS()
{
InitializeComponent();
mymap = gMapControl1;
//config map
gMapControl1.CacheLocation = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + "mapCache" + Path.DirectorySeparatorChar;
gMapControl1.MapProvider = GMapProviders.AMap;
gMapControl1.Manager.Mode = AccessMode.ServerAndCache;
gMapControl1.MinZoom = 0;
gMapControl1.MaxZoom = 24;
gMapControl1.Zoom = 3;
// setup main serial reader
serialreaderthread = new Thread(SerialReader)
{
IsBackground = true,
Name = "Main Serial reader",
//Priority = ThreadPriority.BelowNormal
Priority = ThreadPriority.AboveNormal
};
serialreaderthread.Start();
thisthread = new Thread(mainloop)
{
Name = "Mainloop",
IsBackground = true,
// Priority = ThreadPriority.BelowNormal
};
thisthread.Start();
//初始化端口控件
this.comboBoxComPort.Items.AddRange(SerialPort.GetPortNames());
comboBoxBoundrate.SelectedIndex = 0;
// setup guids for droneshare
if (!MainV2.config.ContainsKey("plane_guid"))
MainV2.config["plane_guid"] = Guid.NewGuid().ToString();
if (!MainV2.config.ContainsKey("copter_guid"))
MainV2.config["copter_guid"] = Guid.NewGuid().ToString();
if (!MainV2.config.ContainsKey("rover_guid"))
MainV2.config["rover_guid"] = Guid.NewGuid().ToString();
RegeneratePolygon();
updateCMDParams();
//map events
gMapControl1.MouseUp += MainMap_MouseUp;
gMapControl1.MouseDown += MainMap_MouseDown;
gMapControl1.MouseMove += MainMap_MouseMove;
gMapControl1.OnMarkerEnter += MainMap_OnMarkerEnter;
gMapControl1.OnMarkerClick += MainMap_OnMarkerClick;
gMapControl1.OnMarkerLeave += MainMap_OnMarkerLeave;
gMapControl1.RoutesEnabled = true;
routesoverlay = new GMapOverlay("routes");
gMapControl1.Overlays.Add(routesoverlay);
polygonsoverlay = new GMapOverlay("polygons");
gMapControl1.Overlays.Add(polygonsoverlay);
//airportsoverlay = new GMapOverlay("airports");
//MainMap.Overlays.Add(airportsoverlay);
objectsoverlay = new GMapOverlay("objects");
gMapControl1.Overlays.Add(objectsoverlay);
drawnpolygonsoverlay = new GMapOverlay("drawnpolygons");
gMapControl1.Overlays.Add(drawnpolygonsoverlay);
gMapControl1.Overlays.Add(poioverlay);
objectsoverlay.Markers.Clear();
gMapControl1.Position = new PointLatLng(36, 103);
gMapControl1.Zoom = 4;
currentMarker = new GMarkerGoogle(gMapControl1.Position, GMarkerGoogleType.red);
this.Commands.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboBox1.SelectedIndex = 1;
gMapControl1.ContextMenuStrip = this.contextMenuStrip1;
}
void MainMap_OnMarkerEnter(GMapMarker item)
{
if (!isMouseDown)
{
if (item is GMapMarkerRect)
{
GMapMarkerRect rc = item as GMapMarkerRect;
rc.Pen.Color = Color.Red;
gMapControl1.Invalidate(false);
int answer;
if (item.Tag != null && rc.InnerMarker != null && int.TryParse(rc.InnerMarker.Tag.ToString(), out answer))
{
try
{
Commands.CurrentCell = Commands[0, answer - 1];
item.ToolTipText = "高度: " + Commands[Alt.Index, answer - 1].Value;
item.ToolTipMode = MarkerTooltipMode.OnMouseOver;
}
catch { }
}
CurentRectMarker = rc;
}
if (item is GMapMarkerRallyPt)
{
CurrentRallyPt = item as GMapMarkerRallyPt;
}
if (item is GMapMarkerAirport)
{
// do nothing - readonly
return;
}
if (item is GMapMarker)
{
CurrentGMapMarker = item;
}
}
}
void MainMap_OnMarkerLeave(GMapMarker item)
{
if (!isMouseDown)
{
if (item is GMapMarkerRect)
{
CurentRectMarker = null;
GMapMarkerRect rc = item as GMapMarkerRect;
rc.ResetColor();
gMapControl1.Invalidate(false);
}
if (item is GMapMarkerRallyPt)
{
CurrentRallyPt = null;
}
if (item is GMapMarker)
{
// when you click the context menu this triggers and causes problems
CurrentGMapMarker = null;
}
}
}
// click on some marker
void MainMap_OnMarkerClick(GMapMarker item, MouseEventArgs e)
{
int answer;
try // when dragging item can sometimes be null
{
if (item.Tag == null)
{
// home.. etc
return;
}
if (ModifierKeys == Keys.Control)
{
try
{
groupmarkeradd(item);
//log.Info("add marker to group");
}
catch { }
}
if (int.TryParse(item.Tag.ToString(), out answer))
{
Commands.CurrentCell = Commands[0, answer - 1];
}
}
catch { }
}
void MainMap_MouseDown(object sender, MouseEventArgs e)
{
//在规划模式下生效
if (isPlanMode)
{
if (isMouseClickOffMenu)
return;
MouseDownStart = gMapControl1.FromLocalToLatLng(e.X, e.Y);
if (e.Button == MouseButtons.Left && (groupmarkers.Count > 0 || ModifierKeys == Keys.Control))
{
isMouseDown = true;
isMouseDraging = false;
if (currentMarker.IsVisible)
currentMarker.Position = gMapControl1.FromLocalToLatLng(e.X, e.Y);
}
if (e.Button == MouseButtons.Left && ModifierKeys != Keys.Alt && ModifierKeys != Keys.Control)
{
isMouseDown = true;
isMouseDraging = false;
if (currentMarker.IsVisible)
{
currentMarker.Position = gMapControl1.FromLocalToLatLng(e.X, e.Y);
}
}
}
}
/// <summary>
/// Used for current mouse position
/// </summary>
/// <param name="lat"></param>
/// <param name="lng"></param>
/// <param name="alt"></param>
public void SetMouseDisplay(double lat, double lng, int alt)
{
mouseposdisplay.Lat = lat;
mouseposdisplay.Lng = lng;
mouseposdisplay.Alt = alt;
coords1.Lat = mouseposdisplay.Lat;
coords1.Lng = mouseposdisplay.Lng;
coords1.Alt = srtm.getAltitude(mouseposdisplay.Lat, mouseposdisplay.Lng, gMapControl1.Zoom).alt;
try
{
PointLatLng last;
if (pointlist[pointlist.Count - 1] == null)
return;
last = pointlist[pointlist.Count - 1];
double lastdist = gMapControl1.MapProvider.Projection.GetDistance(last, currentMarker.Position);
double lastbearing = 0;
if (pointlist.Count > 0)
{
lastbearing = gMapControl1.MapProvider.Projection.GetBearing(last, currentMarker.Position);
}
//lbl_prevdist.Text = rm.GetString("lbl_prevdist.Text") + ": " + FormatDistance(lastdist, true) + " AZ: " + lastbearing.ToString("0");
// 0 is home
if (pointlist[0] != null)
{
double homedist = gMapControl1.MapProvider.Projection.GetDistance(currentMarker.Position, pointlist[0]);
//lbl_homedist.Text = rm.GetString("lbl_homedist.Text") + ": " + FormatDistance(homedist, true);
}
}
catch { }
}
DateTime lastscreenupdate = DateTime.Now;
object updateBindingSourcelock = new object();
volatile int updateBindingSourcecount;
GMapMarkerRallyPt CurrentRallyPt;
void MainMap_MouseMove(object sender, MouseEventArgs e)
{
//在规划模式下生效
if (isPlanMode)
{
PointLatLngAlt point = gMapControl1.FromLocalToLatLng(e.X, e.Y);
if (MouseDownStart == point)
return;
currentMarker.Position = point;
if (!isMouseDown)
{
SetMouseDisplay(point.Lat, point.Lng, 0);
}
//draging
if (e.Button == MouseButtons.Left && isMouseDown)
{
isMouseDraging = true;
if (CurrentRallyPt != null)
{
PointLatLng pnew = gMapControl1.FromLocalToLatLng(e.X, e.Y);
CurrentRallyPt.Position = pnew;
}
else if (groupmarkers.Count > 0)
{
// group drag
double latdif = MouseDownStart.Lat - point.Lat;
double lngdif = MouseDownStart.Lng - point.Lng;
MouseDownStart = point;
Hashtable seen = new Hashtable();
foreach (var markerid in groupmarkers)
{
if (seen.ContainsKey(markerid))
continue;
seen[markerid] = 1;
for (int a = 0; a < objectsoverlay.Markers.Count; a++)
{
var marker = objectsoverlay.Markers[a];
if (marker.Tag != null && marker.Tag.ToString() == markerid.ToString())
{
var temp = new PointLatLng(marker.Position.Lat, marker.Position.Lng);
temp.Offset(latdif, -lngdif);
marker.Position = temp;
}
}
}
}
else if (CurentRectMarker != null) // left click pan
{
try
{
// check if this is a grid point
if (CurentRectMarker.InnerMarker.Tag.ToString().Contains("grid"))
{
drawnpolygon.Points[int.Parse(CurentRectMarker.InnerMarker.Tag.ToString().Replace("grid", "")) - 1] = new PointLatLng(point.Lat, point.Lng);
gMapControl1.UpdatePolygonLocalPosition(drawnpolygon);
gMapControl1.Invalidate();
}
}
catch { }
PointLatLng pnew = gMapControl1.FromLocalToLatLng(e.X, e.Y);
// adjust polyline point while we drag
try
{
if (CurrentGMapMarker != null && CurrentGMapMarker.Tag is int)
{
int? pIndex = (int?)CurentRectMarker.Tag;
if (pIndex.HasValue)
{
if (pIndex < wppolygon.Points.Count)
{
wppolygon.Points[pIndex.Value] = pnew;
lock (thisLock)
{
gMapControl1.UpdatePolygonLocalPosition(wppolygon);
}
}
}
}
}
catch { }
// update rect and marker pos.
if (currentMarker.IsVisible)
{
currentMarker.Position = pnew;
}
CurentRectMarker.Position = pnew;
if (CurentRectMarker.InnerMarker != null)
{
CurentRectMarker.InnerMarker.Position = pnew;
}
}
else if (CurrentGMapMarker != null)
{
PointLatLng pnew = gMapControl1.FromLocalToLatLng(e.X, e.Y);
CurrentGMapMarker.Position = pnew;
}
}
}
}
void groupmarkeradd(GMapMarker marker)
{
groupmarkers.Add(int.Parse(marker.Tag.ToString()));
if (marker is GMapMarkerWP)
{
((GMapMarkerWP)marker).selected = true;
}
if (marker is GMapMarkerRect)
{
((GMapMarkerWP)((GMapMarkerRect)marker).InnerMarker).selected = true;
}
}
bool polygongridmode;
altmode currentaltmode = altmode.Relative;
bool sethome;
GMapOverlay drawnpolygonsoverlay;
/// <summary>
/// Used to create a new WP
/// </summary>
/// <param name="lat"></param>
/// <param name="lng"></param>
/// <param name="alt"></param>
public void AddWPToMap(double lat, double lng, int alt)
{
if (polygongridmode)
{
addPolygonPointToolStripMenuItem_Click(null, null);
return;
}
if (sethome)
{
sethome = false;
callMeDrag("H", lat, lng, alt);
return;
}
// creating a WP
selectedrow = Commands.Rows.Add();
if (splinemode)
{
Commands.Rows[selectedrow].Cells[Command.Index].Value = MAVLink.MAV_CMD.SPLINE_WAYPOINT.ToString();
ChangeColumnHeader(MAVLink.MAV_CMD.SPLINE_WAYPOINT.ToString());
}
else
{
Commands.Rows[selectedrow].Cells[Command.Index].Value = MAVLink.MAV_CMD.WAYPOINT.ToString();
ChangeColumnHeader(MAVLink.MAV_CMD.WAYPOINT.ToString());
}
setfromMap(lat, lng, alt);
}
/// <summary>
/// Actualy Sets the values into the datagrid and verifys height if turned on
/// </summary>
/// <param name="lat"></param>
/// <param name="lng"></param>
/// <param name="alt"></param>
public void setfromMap(double lat, double lng, int alt, double p1 = 0)
{
if (selectedrow > Commands.RowCount)
{
CustomMessageBox.Show("Invalid coord, How did you do this?");
return;
}
// add history
history.Add(GetCommandList());
// remove more than 20 revisions
if (history.Count > 20)
{
history.RemoveRange(0, history.Count - 20);
}
DataGridViewTextBoxCell cell;
if (Commands.Columns[Lat.Index].HeaderText.Equals(cmdParamNames["WAYPOINT"][4]/*"Lat"*/) || Commands.Columns[Lat.Index].HeaderText.Equals("纬度"))
{
cell = Commands.Rows[selectedrow].Cells[Lat.Index] as DataGridViewTextBoxCell;
cell.Value = lat.ToString("0.0000000");
cell.DataGridView.EndEdit();
}
if (Commands.Columns[Lon.Index].HeaderText.Equals(cmdParamNames["WAYPOINT"][5]/*"Long"*/) || Commands.Columns[Lon.Index].HeaderText.Equals("经度"))
{
cell = Commands.Rows[selectedrow].Cells[Lon.Index] as DataGridViewTextBoxCell;
cell.Value = lng.ToString("0.0000000");
cell.DataGridView.EndEdit();
}
if (alt != -1 && Commands.Columns[Alt.Index].HeaderText.Equals(cmdParamNames["WAYPOINT"][6]/*"Alt"*/) || alt != -1 && Commands.Columns[Alt.Index].HeaderText.Equals("高度"))
{
cell = Commands.Rows[selectedrow].Cells[Alt.Index] as DataGridViewTextBoxCell;
{
double result;
bool pass = double.TryParse(TXT_homealt.Text, out result);
if (pass == false)
{
CustomMessageBox.Show("输入回家高度");
string homealt = "100";
if (DialogResult.Cancel == InputBox.Show("Home Alt", "Home Altitude", ref homealt))
return;
TXT_homealt.Text = homealt;
}
int results1;
if (!int.TryParse(TXT_DefaultAlt.Text, out results1))
{
CustomMessageBox.Show("Your default alt is not valid");
return;
}
if (results1 == 0)
{
string defalt = "100";
if (DialogResult.Cancel == InputBox.Show("默认高度", "默认高度", ref defalt))
return;
TXT_DefaultAlt.Text = defalt;
}
}
cell.Value = TXT_DefaultAlt.Text;
float ans;
if (float.TryParse(cell.Value.ToString(), out ans))
{
ans = (int)ans;
if (alt != 0) // use passed in value;
cell.Value = alt.ToString();
if (ans == 0) // default
cell.Value = 50;
if (ans == 0 && (comPort.MAV.cs.firmware == MainV2.Firmwares.ArduCopter2))
cell.Value = 15;
writeKML();
cell.DataGridView.EndEdit();
}
else
{
CustomMessageBox.Show("Invalid Home or wp Alt");
cell.Style.BackColor = Color.Red;
}
}
// Add more for other params
if (Commands.Columns[Param1.Index].HeaderText.Equals(cmdParamNames["WAYPOINT"][1]/*"Delay"*/))
{
cell = Commands.Rows[selectedrow].Cells[Param1.Index] as DataGridViewTextBoxCell;
cell.Value = p1;
cell.DataGridView.EndEdit();
}
writeKML();
Commands.EndEdit();
}
private void ChangeColumnHeader(string command)
{
try
{
if (cmdParamNames.ContainsKey(command))
for (int i = 1; i <= 7; i++)
{
Commands.Columns[i].HeaderText = cmdParamNames[command][i - 1];
if (Commands.Columns[i].HeaderText == "Lat")
Commands.Columns[i].HeaderText = "纬度";
if (Commands.Columns[i].HeaderText == "Long")
Commands.Columns[i].HeaderText = "经度";
if (Commands.Columns[i].HeaderText == "Delay")
Commands.Columns[i].HeaderText = "延时";
if (Commands.Columns[i].HeaderText == "Alt")
Commands.Columns[i].HeaderText = "高度";
if (Commands.Columns[i].HeaderText == "speed m/s")
Commands.Columns[i].HeaderText = "速度";
if (Commands.Columns[i].HeaderText == "Dist (m)")
Commands.Columns[i].HeaderText = "距离(米)";
if (Commands.Columns[i].HeaderText == "Enable(1)/Release(2)")
Commands.Columns[i].HeaderText = "启用(1)/开伞(2)";
}
else
for (int i = 1; i <= 7; i++)
Commands.Columns[i].HeaderText = "setme";
}
catch (Exception ex) { CustomMessageBox.Show(ex.ToString()); }
}
/// <summary>
/// used to adjust existing point in the datagrid including "H"
/// </summary>
/// <param name="pointno"></param>
/// <param name="lat"></param>
/// <param name="lng"></param>
/// <param name="alt"></param>
public void callMeDrag(string pointno, double lat, double lng, int alt)
{
if (pointno == "")
{
return;
}
// dragging a WP
if (pointno == "H")
{
TXT_homelat.Text = lat.ToString();
TXT_homelng.Text = lng.ToString();
return;
}
if (pointno == "Tracker Home")
{
comPort.MAV.cs.TrackerLocation = new PointLatLngAlt(lat, lng, alt, "");
return;
}
try
{
selectedrow = int.Parse(pointno) - 1;
Commands.CurrentCell = Commands[1, selectedrow];
}
catch
{
return;
}
setfromMap(lat, lng, alt);
}
private void addPolygonPointToolStripMenuItem_Click(object sender, EventArgs e)
{
if (polygongridmode == false)
{
CustomMessageBox.Show("开始规划模式,清除规划区域后结束规划模式.");
}
polygongridmode = true;
List<PointLatLng> polygonPoints = new List<PointLatLng>();
if (drawnpolygonsoverlay.Polygons.Count == 0)
{
drawnpolygon.Points.Clear();
drawnpolygonsoverlay.Polygons.Add(drawnpolygon);
}
drawnpolygon.Fill = Brushes.Transparent;
// remove full loop is exists
if (drawnpolygon.Points.Count > 1 && drawnpolygon.Points[0] == drawnpolygon.Points[drawnpolygon.Points.Count - 1])
drawnpolygon.Points.RemoveAt(drawnpolygon.Points.Count - 1); // unmake a full loop
drawnpolygon.Points.Add(new PointLatLng(MouseDownStart.Lat, MouseDownStart.Lng));
addpolygonmarkergrid(drawnpolygon.Points.Count.ToString(), MouseDownStart.Lng, MouseDownStart.Lat, 0);
gMapControl1.UpdatePolygonLocalPosition(drawnpolygon);
gMapControl1.Invalidate();
}
private void addpolygonmarkergrid(string tag, double lng, double lat, int alt)
{
try
{
PointLatLng point = new PointLatLng(lat, lng);
GMarkerGoogle m = new GMarkerGoogle(point, GMarkerGoogleType.red);
m.ToolTipMode = MarkerTooltipMode.Never;
m.ToolTipText = "grid" + tag;
m.Tag = "grid" + tag;
//MissionPlanner.GMapMarkerRectWPRad mBorders = new MissionPlanner.GMapMarkerRectWPRad(point, (int)float.Parse(TXT_WPRad.Text), MainMap);
GMapMarkerRect mBorders = new GMapMarkerRect(point);
{
mBorders.InnerMarker = m;
}
drawnpolygonsoverlay.Markers.Add(m);
drawnpolygonsoverlay.Markers.Add(mBorders);
}
catch (Exception ex)
{
log.Info(ex.ToString());
}
}
void MainMap_MouseUp(object sender, MouseEventArgs e)
{
//在规划模式下生效
if (isPlanMode)
{
if (isMouseClickOffMenu)
{
isMouseClickOffMenu = false;
return;
}
MouseDownEnd = gMapControl1.FromLocalToLatLng(e.X, e.Y);
if (e.Button == MouseButtons.Right)
{
return;
}
if (isMouseDown)
{
if (e.Button == MouseButtons.Left)
isMouseDown = false;
if (ModifierKeys == Keys.Control)
{
// group select wps
GMapPolygon poly = new GMapPolygon(new List<PointLatLng>(), "temp");
poly.Points.Add(MouseDownStart);
poly.Points.Add(new PointLatLng(MouseDownStart.Lat, MouseDownEnd.Lng));
poly.Points.Add(MouseDownEnd);
poly.Points.Add(new PointLatLng(MouseDownEnd.Lat, MouseDownStart.Lng));
foreach (var marker in objectsoverlay.Markers)
{
if (poly.IsInside(marker.Position))
{
try
{
if (marker.Tag != null)
{
groupmarkeradd(marker);
}
}
catch { }
}
}
isMouseDraging = false;
return;
}
}
if (!isMouseDraging)
{
if (CurentRectMarker != null)
{
}
else
{
AddWPToMap(currentMarker.Position.Lat, currentMarker.Position.Lng, 0);
}
}
else
{
if (groupmarkers.Count > 0)
{
Dictionary<string, PointLatLng> dest = new Dictionary<string, PointLatLng>();
foreach (var markerid in groupmarkers)
{
for (int a = 0; a < objectsoverlay.Markers.Count; a++)
{
var marker = objectsoverlay.Markers[a];
if (marker.Tag != null && marker.Tag.ToString() == markerid.ToString())
{
dest[marker.Tag.ToString()] = marker.Position;
break;
}
}
}
foreach (KeyValuePair<string, PointLatLng> item in dest)
{
var value = item.Value;
callMeDrag(item.Key, value.Lat, value.Lng, -1);
}
gMapControl1.SelectedArea = RectLatLng.Empty;
groupmarkers.Clear();
// redraw to remove selection
writeKML();
CurentRectMarker = null;
}
if (CurentRectMarker != null)
{
if (CurentRectMarker.InnerMarker.Tag.ToString().Contains("grid"))
{
try
{
drawnpolygon.Points[int.Parse(CurentRectMarker.InnerMarker.Tag.ToString().Replace("grid", "")) - 1] = new PointLatLng(MouseDownEnd.Lat, MouseDownEnd.Lng);
gMapControl1.UpdatePolygonLocalPosition(drawnpolygon);
gMapControl1.Invalidate();
}
catch { }
}
else
{
callMeDrag(CurentRectMarker.InnerMarker.Tag.ToString(), currentMarker.Position.Lat, currentMarker.Position.Lng, -1);
}
CurentRectMarker = null;
}
}
}
isMouseDraging = false;
}
// polygons
GMapPolygon wppolygon;
internal GMapPolygon drawnpolygon;
GMapPolygon geofencepolygon;
public static GMapOverlay routesoverlay;// static so can update from gcs
public static GMapOverlay poioverlay = new GMapOverlay("POI"); // poi layer
public bool autopan { get; set; }
DateTime mapupdate = DateTime.MinValue;
static GMapOverlay rallypointoverlay;
private DateTime heatbeatSend = DateTime.Now;
private Dictionary<string, string[]> cmdParamNames = new Dictionary<string, string[]>();
Dictionary<string, string[]> readCMDXML()
{
Dictionary<string, string[]> cmd = new Dictionary<string, string[]>();
// do lang stuff here
string file = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + "mavcmd.xml";
if (!File.Exists(file))
{
CustomMessageBox.Show("Missing mavcmd.xml file");
return cmd;
}
using (XmlReader reader = XmlReader.Create(file))
{
reader.Read();
reader.ReadStartElement("CMD");
if (comPort.MAV.cs.firmware == MainV2.Firmwares.ArduPlane || comPort.MAV.cs.firmware == MainV2.Firmwares.Ateryx)
{
reader.ReadToFollowing("APM");
}
else if (comPort.MAV.cs.firmware == MainV2.Firmwares.ArduRover)
{
reader.ReadToFollowing("APRover");
}
else
{
reader.ReadToFollowing("AC2");
}
XmlReader inner = reader.ReadSubtree();
inner.Read();
inner.MoveToElement();
inner.Read();
while (inner.Read())
{
inner.MoveToElement();
if (inner.IsStartElement())
{
string cmdname = inner.Name;
string[] cmdarray = new string[7];
int b = 0;
XmlReader inner2 = inner.ReadSubtree();
inner2.Read();
while (inner2.Read())
{
inner2.MoveToElement();
if (inner2.IsStartElement())
{
cmdarray[b] = inner2.ReadString();
b++;
}
}
cmd[cmdname] = cmdarray;
}
}
}
return cmd;
}
void updateCMDParams()
{
cmdParamNames = readCMDXML();
List<string> cmds = new List<string>();
foreach (string item in cmdParamNames.Keys)
{
cmds.Add(item);
}
cmds.Add("UNKNOWN");
Command.DataSource = cmds;
}
static public Object thisLock = new Object();
/// <summary>
/// used to redraw the polygon
/// </summary>
void RegeneratePolygon()
{
List<PointLatLng> polygonPoints = new List<PointLatLng>();
if (objectsoverlay == null)
return;
foreach (GMapMarker m in objectsoverlay.Markers)
{
if (m is GMapMarkerRect)
{
if (m.Tag == null)
{
m.Tag = polygonPoints.Count;
polygonPoints.Add(m.Position);
}
}
}
if (wppolygon == null)
{
wppolygon = new GMapPolygon(polygonPoints, "polygon test");
polygonsoverlay.Polygons.Add(wppolygon);
}
else
{
wppolygon.Points.Clear();
wppolygon.Points.AddRange(polygonPoints);