Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #682 from Esri/dev
Browse files Browse the repository at this point in the history
October, 2019 Release
  • Loading branch information
csmoore authored Oct 3, 2019
2 parents 4ea125c + 57d22bb commit d39de79
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 58 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
using ESRI.ArcGIS.ADF;
using DistanceAndDirectionLibrary;
using ESRI.ArcGIS.Display;
using System.Text.RegularExpressions;
using System.Linq;

namespace ArcMapAddinDistanceAndDirection.Models
{
Expand Down Expand Up @@ -290,6 +292,35 @@ public IFeatureClass CreateFCOutput(string outputPath, SaveAsType saveAsType, Li
}
}

/// <summary>
/// Checks if file name has illegal characters
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
internal bool ContainsInvalidChars(string path)
{
var fileName = System.IO.Path.GetFileNameWithoutExtension(path);
var regexItem = new Regex("^[A-Za-z_][A-Za-z0-9_]*$");
var isValidFileName = regexItem.IsMatch(fileName);
if (!isValidFileName)
return true;
else if (fileName.Length > 32)
return true;
else if (ValidateReservedWords(fileName))
return true;
return false;
}

private static bool ValidateReservedWords(string fileName)
{
//https://support.esri.com/en/technical-article/000010906 - Used the keyword list mentioned here for 10.1 and above
var reservedWords = new List<string>() {
"ADD","ALTER","AND","BETWEEN","BY","COLUMN","CREATE","DELETE","DROP","EXISTS","FOR","FROM","GROUP","IN","INSERT","INTO","IS","LIKE","NOT","NULL","OR","ORDER","SELECT","SET","TABLE","UPDATE","VALUES","WHERE"
};
return reservedWords.Where(x => fileName.ToUpper() == x).Any();
}


public void DeleteShapeFile(string shapeFilePath)
{
string shapeFile = shapeFilePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,26 @@
// limitations under the License.

// System
using ArcMapAddinDistanceAndDirection.Models;
using DistanceAndDirectionLibrary;
using DistanceAndDirectionLibrary.Helpers;
using DistanceAndDirectionLibrary.Models;
using DistanceAndDirectionLibrary.ViewModels;
using DistanceAndDirectionLibrary.Views;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
// Esri
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Collections.ObjectModel;

// Esri
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;

using DistanceAndDirectionLibrary;
using DistanceAndDirectionLibrary.Helpers;
using DistanceAndDirectionLibrary.ViewModels;
using ArcMapAddinDistanceAndDirection.Models;
using DistanceAndDirectionLibrary.Models;
using DistanceAndDirectionLibrary.Views;

namespace ArcMapAddinDistanceAndDirection.ViewModels
{
Expand Down Expand Up @@ -465,7 +463,7 @@ public virtual bool IsToolActive
OnActivateTool(null);
else
if (ArcMap.Application.CurrentTool != null)
ArcMap.Application.CurrentTool = null;
ArcMap.Application.CurrentTool = null;

RaisePropertyChanged(() => IsToolActive);
}
Expand Down Expand Up @@ -572,7 +570,13 @@ private void OnSaveAs(object obj)
}
else
{
fc = fcUtils.CreateFCOutput(path, SaveAsType.FileGDB, typeGraphicsList, ArcMap.Document.FocusMap.SpatialReference);
if (!fcUtils.ContainsInvalidChars(path))
fc = fcUtils.CreateFCOutput(path, SaveAsType.FileGDB, typeGraphicsList, ArcMap.Document.FocusMap.SpatialReference);
else
MessageBox.Show(DistanceAndDirectionLibrary.Properties.Resources.FeatureClassNameError,
DistanceAndDirectionLibrary.Properties.Resources.DistanceDirectionLabel,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
Expand Down Expand Up @@ -645,7 +649,7 @@ private void AddFeatureLayerToMap(IFeatureClass fc)

ESRI.ArcGIS.Carto.IMap map = ArcMap.Document.FocusMap;

map.AddLayer((ILayer)outputFeatureLayer);
map.AddLayer(outputFeatureLayer);
}

private string PromptSaveFileDialog()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
<value>Center Point</value>
</data>
<data name="LabelClearGraphics" xml:space="preserve">
<value>Clear</value>
<value>Clear All</value>
</data>
<data name="LabelCreateCircleFrom" xml:space="preserve">
<value>From</value>
Expand Down Expand Up @@ -376,7 +376,7 @@
<value>Ring Type</value>
</data>
<data name="LabelSaveAs" xml:space="preserve">
<value>Save As</value>
<value>Export</value>
</data>
<data name="LabelStartingPoint" xml:space="preserve">
<value>Starting Point</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

using DistanceAndDirectionLibrary;
using System.Windows;
using System.Text.RegularExpressions;

namespace ProAppDistanceAndDirectionModule.Models
{
Expand Down Expand Up @@ -82,10 +83,11 @@ public string PromptUserWithSaveDialog(bool featureShapeChecked)
if (ok == true)
{
if (ContainsInvalidChars(Path.GetFileName(saveItemDlg.FilePath)))
{
MessageBox.Show(DistanceAndDirectionLibrary.Properties.Resources.FeatureClassNameError,
DistanceAndDirectionLibrary.Properties.Resources.DistanceDirectionLabel, MessageBoxButton.OK,
MessageBoxImage.Exclamation);
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(
DistanceAndDirectionLibrary.Properties.Resources.FeatureClassNameError,
DistanceAndDirectionLibrary.Properties.Resources.DistanceDirectionLabel,
System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
return null;
}
previousLocation = Path.GetDirectoryName(saveItemDlg.FilePath);
Expand Down Expand Up @@ -206,10 +208,28 @@ private static IReadOnlyList<string> makeValueArray (string featureClass, string
/// Checks if file name has illegal characters
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private static bool ContainsInvalidChars(string filename)
/// <returns></returns>
private static bool ContainsInvalidChars(string path)
{
var fileName = System.IO.Path.GetFileNameWithoutExtension(path);
var regexItem = new Regex("^[A-Za-z_][A-Za-z0-9_]*$");
var isValidFileName = regexItem.IsMatch(fileName);
if (!isValidFileName)
return true;
else if (fileName.Length > 32)
return true;
else if (ValidateReservedWords(fileName))
return true;
return false;
}

private static bool ValidateReservedWords(string fileName)
{
return Path.GetInvalidFileNameChars().Concat(new[] { ' ', '-' }).Any(item => filename.Contains(item));
//https://support.esri.com/en/technical-article/000010906 - Used the keyword list mentioned here for 10.1 and above
var reservedWords = new List<string>() {
"ADD","ALTER","AND","BETWEEN","BY","COLUMN","CREATE","DELETE","DROP","EXISTS","FOR","FROM","GROUP","IN","INSERT","INTO","IS","LIKE","NOT","NULL","OR","ORDER","SELECT","SET","TABLE","UPDATE","VALUES","WHERE"
};
return reservedWords.Where(x => fileName.ToUpper() == x).Any();
}

private static List<Graphic> ClearTempGraphics(List<Graphic> graphicsList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,11 @@ private async void CreateCircleFeature(Geometry geom, CircleAttributes circleAtt
await QueuedTask.Run(async () =>
message = await AddFeatureToLayer(geom, circleAttributes));

RaisePropertyChanged(() => HasMapGraphics);

if (!string.IsNullOrEmpty(message))
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(message,
DistanceAndDirectionLibrary.Properties.Resources.ErrorFeatureCreateTitle);
else
HasMapGraphics = true;
}

private async Task<string> AddFeatureToLayer(Geometry geom, CircleAttributes attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,11 @@ private async void CreateEllipseFeature(Geometry geom, EllipseAttributes ellipse
await QueuedTask.Run(async () =>
message = await AddFeatureToLayer(geom, ellipseAttributes));

RaisePropertyChanged(() => HasMapGraphics);

if (!string.IsNullOrEmpty(message))
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(message,
DistanceAndDirectionLibrary.Properties.Resources.ErrorFeatureCreateTitle);
else
HasMapGraphics = true;
}

private async Task<string> AddFeatureToLayer(Geometry geom, EllipseAttributes attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,11 @@ private async void CreateLineFeature(Geometry geom, LineAttributes lineAttribute
await QueuedTask.Run(async () =>
message = await AddFeatureToLayer(geom, lineAttributes));

RaisePropertyChanged(() => HasMapGraphics);

if (!string.IsNullOrEmpty(message))
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(message,
DistanceAndDirectionLibrary.Properties.Resources.ErrorFeatureCreateTitle);
else
HasMapGraphics = true;
}

private async Task<string> AddFeatureToLayer(Geometry geom, LineAttributes attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ private async void CreateRangeRingOrRadialFeature(Geometry geom, RangeAttributes
await QueuedTask.Run(async () =>
message = await AddFeatureToLayer(geom, rangeAttributes));

RaisePropertyChanged(() => HasMapGraphics);

if (!string.IsNullOrEmpty(message))
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(message,
DistanceAndDirectionLibrary.Properties.Resources.ErrorFeatureCreateTitle);
else
HasMapGraphics = true;
}

private async Task<string> AddFeatureToLayer(Geometry geom, RangeAttributes attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ public ProTabBaseViewModel()
System.Diagnostics.Debug.WriteLine("Probably Running from Unit Tests");
}

ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEvent.Subscribe((args) =>
{
// Subscribe to this event in case the ActiveMap already has layers with features (so buttons are enabled on load)
RaisePropertyChanged(() => HasMapGraphics);
});
ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChanged);

configObserver = new PropertyObserver<DistanceAndDirectionConfig>(DistanceAndDirectionConfig.AddInConfig)
.RegisterHandler(n => n.DisplayCoordinateType, n =>
Expand Down Expand Up @@ -182,17 +178,17 @@ public virtual bool IsToolActive

public bool HasMapGraphics
{
set
{
hasMapGraphics = value;
RaisePropertyChanged(() => HasMapGraphics);
}
get
{
// Call helper method (must be run on MCT)
bool hasFeatures = QueuedTask.Run<bool>(async () =>
{
return await this.HasLayerFeatures();
}).Result;

return hasFeatures;
return hasMapGraphics;
}
}
private bool hasMapGraphics = false;

private MapPoint point1 = null;
/// <summary>
Expand Down Expand Up @@ -614,7 +610,7 @@ private void OnClearGraphics()
return this.DeleteAllFeatures();
});

RaisePropertyChanged(() => HasMapGraphics);
HasMapGraphics = false;
}

/// <summary>
Expand Down Expand Up @@ -1275,6 +1271,13 @@ private async void OnSaveAs()
}
}

private async void OnActiveMapViewChanged(ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEventArgs obj)
{
// Subscribe to this event in case the ActiveMap already has layers with features (so buttons are enabled on load)
await HasLayerFeatures();
RaisePropertyChanged(() => HasMapGraphics);
}

#endregion Private Functions

#region Feature Class Support
Expand Down Expand Up @@ -1408,16 +1411,24 @@ await System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.T
return featureClass;
}

protected async Task<bool> HasLayerFeatures()
protected async Task HasLayerFeatures()
{
FeatureClass fc = null;
string featureLayerName = this.GetLayerName();

await QueuedTask.Run(async () =>
FeatureLayer featureLayer = GetFeatureLayerByNameInActiveView(featureLayerName);

if (featureLayer == null)
{
fc = await GetFeatureClass(addToMapIfNotPresent: false);
});
hasMapGraphics = false;
return;
}

return fc == null ? false : fc.GetCount() > 0;
FeatureClass featureClass = null;
await QueuedTask.Run(() =>
{
featureClass = featureLayer.GetFeatureClass();
hasMapGraphics = (featureClass == null) ? false : featureClass.GetCount() > 0;
});
}

protected async Task<bool> DeleteAllFeatures()
Expand Down

0 comments on commit d39de79

Please sign in to comment.