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
http://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.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
namespace MapControlSaveLayerFile
{
[Guid("294563e7-b475-43db-a2d1-a6b5f95c7113")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("MapControlSaveLayerFile.SaveLayerFileCmd")]
public sealed class SaveLayerFileCmd : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
ArcGISCategoryRegistration(registerType);
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
ArcGISCategoryUnregistration(registerType);
}
#region ArcGIS Component Category Registrar generated code
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Register(regKey);
ControlsCommands.Register(regKey);
}
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Unregister(regKey);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper = null;
public SaveLayerFileCmd()
{
base.m_category = ".NET Samples";
base.m_caption = "Create Layer File";
base.m_message = "Save the current layer as a layer file";
base.m_toolTip = "Create As Layer File";
base.m_name = "MapControlSaveLayerFile_SaveLayerFileCmd";
try
{
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overriden Class Methods
public override void OnCreate(object hook)
{
if (hook == null)
return;
try
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
if (m_hookHelper.ActiveView == null)
m_hookHelper = null;
}
catch
{
m_hookHelper = null;
}
if (m_hookHelper == null)
base.m_enabled = false;
else
base.m_enabled = true;
}
public override void OnClick()
{
if (null == m_hookHelper)
return;
object hook = null;
if (m_hookHelper.Hook is IToolbarControl2)
{
hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy;
}
else
{
hook = m_hookHelper.Hook;
}
object customProperty = null;
IMapControl3 mapControl = null;
if (hook is IMapControl3)
{
mapControl = (IMapControl3)hook;
customProperty = mapControl.CustomProperty;
}
else
return;
if (null == customProperty || !(customProperty is ILayer))
return;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Layer File|*.lyr|All Files|*.*";
saveFileDialog.Title = "Create Layer File";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.FileName = System.IO.Path.Combine(saveFileDialog.InitialDirectory, ((ILayer)customProperty).Name + ".lyr");
DialogResult dr = saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != "" && dr == DialogResult.OK)
{
if (System.IO.File.Exists(saveFileDialog.FileName))
{
System.IO.File.Delete(saveFileDialog.FileName);
}
ILayerFile layerFile = new LayerFileClass();
layerFile.New(saveFileDialog.FileName);
layerFile.ReplaceContents((ILayer)customProperty);
layerFile.Save();
if (DialogResult.Yes == MessageBox.Show("Would you like to add the layer to the map?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
mapControl.AddLayerFromFile(saveFileDialog.FileName, 0);
}
}
}
#endregion
}
}