/
Initialize.cs
109 lines (93 loc) · 4.1 KB
/
Initialize.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
/*
Copyright 2019 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Events;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Editing.Events;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
namespace EditOperationRowEvent
{
internal class Initialize : Button
{
private SubscriptionToken _rowChangedToken, _rowCreatedToken, _rowDeletedToken;
private StandaloneTable _ehTable;
protected override void OnClick()
{
QueuedTask.Run(async() =>
{
//find layer and derive geodatabase
var cpLayer = MapView.Active.Map.FindLayers("CrowdPlanning").FirstOrDefault() as FeatureLayer;
if (cpLayer == null) return;
var geodatabase = cpLayer.GetFeatureClass().GetDatastore() as Geodatabase;
//Advise if the project has edits. Need to clear edits to make schema changes.
if (Project.Current.HasEdits)
{
MessageBox.Show("Please save or discard edits", "Pending Edits");
return;
}
//Delete and Create the editlog table
//For the purpose of this sample, start with a fresh table
var mva = Geoprocessing.MakeValueArray(geodatabase.GetPath().AbsolutePath,"EditLog");
var cts = new System.Threading.CancellationTokenSource();
await Geoprocessing.ExecuteToolAsync("CreateTable_management", mva);
//add fields to editlog
var tablePath = geodatabase.GetPath().AbsolutePath + @"\EditLog";
mva = Geoprocessing.MakeValueArray(tablePath, "Layer", "STRING");
await Geoprocessing.ExecuteToolAsync("AddField_management", mva);
mva = Geoprocessing.MakeValueArray(tablePath,"OID","LONG");
await Geoprocessing.ExecuteToolAsync("AddField_management", mva);
mva = Geoprocessing.MakeValueArray(tablePath, "Date", "DATE");
await Geoprocessing.ExecuteToolAsync("AddField_management", mva);
mva = Geoprocessing.MakeValueArray(tablePath, "EditType", "STRING");
await Geoprocessing.ExecuteToolAsync("AddField_management", mva);
_ehTable = MapView.Active.Map.FindStandaloneTables("EditLog").FirstOrDefault();
//setup row events for layer
if (_rowChangedToken == null)
_rowChangedToken = RowChangedEvent.Subscribe(OnRowEvent,cpLayer.GetTable());
if (_rowCreatedToken == null)
_rowCreatedToken = RowCreatedEvent.Subscribe(OnRowEvent, cpLayer.GetTable());
if (_rowDeletedToken == null)
_rowDeletedToken = RowDeletedEvent.Subscribe(OnRowEvent, cpLayer.GetTable());
});
}
private void OnRowEvent(RowChangedEventArgs obj)
{
//get the parent operation
var parentEditOp = obj.Operation;
//create values for the editlog
var atts = new Dictionary<string, object>();
atts.Add("Layer", obj.Row.GetTable().GetName());
atts.Add("OID", obj.Row.GetObjectID());
atts.Add("Date", DateTime.Now.ToShortTimeString());
atts.Add("EditType", obj.EditType.ToString());
//add a row to the editlog table
//as part of the current running edit operation
parentEditOp.Create(_ehTable, atts);
}
}
}