www.pudn.com > AS15044.zip > SaveLayer.cs


using System; 
using System.Drawing; 
using System.Runtime.InteropServices; 
using ESRI.ArcGIS.ADF.BaseClasses; 
using ESRI.ArcGIS.ADF.CATIDs; 
using ESRI.ArcGIS.Controls; 
 
namespace CustomCommands 
{ 
    ///  
    /// Command that works in ArcMap/Map/PageLayout 
    ///  
    [Guid("4dece442-af5a-4bf5-afdc-bcec8ae293e4")] 
    [ClassInterface(ClassInterfaceType.None)] 
    [ProgId("CustomCommands.SaveLayer")] 
    public sealed class SaveLayer : BaseCommand 
    { 
        #region COM Registration Function(s) 
        [ComRegisterFunction()] 
        [ComVisible(false)] 
        static void RegisterFunction(Type registerType) 
        { 
            // Required for ArcGIS Component Category Registrar support 
            ArcGISCategoryRegistration(registerType); 
 
            // 
            // TODO: Add any COM registration code here 
            // 
        } 
 
        [ComUnregisterFunction()] 
        [ComVisible(false)] 
        static void UnregisterFunction(Type registerType) 
        { 
            // Required for ArcGIS Component Category Registrar support 
            ArcGISCategoryUnregistration(registerType); 
 
            // 
            // TODO: Add any COM unregistration code here 
            // 
        } 
 
        #region ArcGIS Component Category Registrar generated code 
        ///  
        /// Required method for ArcGIS Component Category registration - 
        /// Do not modify the contents of this method with the code editor. 
        ///  
        private static void ArcGISCategoryRegistration(Type registerType) 
        { 
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID); 
            MxCommands.Register(regKey); 
            ControlsCommands.Register(regKey); 
        } 
        ///  
        /// Required method for ArcGIS Component Category unregistration - 
        /// Do not modify the contents of this method with the code editor. 
        ///  
        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 SaveLayer() 
        { 
            // 
            // TODO: Define values for the public properties 
            // 
            base.m_category = ""; //localizable text 
            base.m_caption = "";  //localizable text  
            base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl";  //localizable text 
            base.m_toolTip = "Save Layer 0";  //localizable text 
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyCommand") 
 
            try 
            { 
                // 
                // TODO: change bitmap name if necessary 
                // 
                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 
 
        ///  
        /// Occurs when this command is created 
        ///  
        /// Instance of the application 
        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; 
 
            // TODO:  Add other initialization code 
        } 
 
        ///  
        /// Occurs when this command is clicked 
        ///  
        public override void OnClick() 
        { 
            // TODO: Add SaveLayer.OnClick implementation 
             //Demo3 - OnClick 
            string filePath = @"c:\temp\layer0.lyr"; 
            ESRI.ArcGIS.Carto.ILayer lyr; 
            lyr = m_hookHelper.FocusMap.get_Layer(0); 
 
            SaveToLayerFile(filePath, lyr); 
 
            System.Windows.Forms.MessageBox.Show(lyr.Name + " is saved in " + filePath); 
 
 
        } 
 
 
        #region "Save Layer To File" 
        // ArcGIS Snippet Title:  
        // Save Layer To File 
        // 
        // Add the following references to the project: 
        // ESRI.ArcGIS.Carto 
        // ESRI.ArcGIS.System 
        // System.IO 
        //  
        // Intended ArcGIS Products for this snippet: 
        // ArcGIS Desktop 
        // ArcGIS Engine 
        // ArcGIS Server 
        // 
        // Required ArcGIS Extensions: 
        // (NONE) 
        // 
        // Notes: 
        // This snippet is intended to be inserted at the base level of a Class. 
        // It is not intended to be nested within an existing Method. 
        // 
        // Use the following XML documentation comments to use this snippet: 
        /// Write and Layer to a file on disk. 
        /// 
        /// A System.String that is the path and filename for the layer file to be created. Example: "C:\temp\cities.lyr" 
        /// An ILayer interface. 
        ///  
        ///  
        public void SaveToLayerFile(System.String layerFilePath, ESRI.ArcGIS.Carto.ILayer layer) 
        { 
            if (layer == null) 
            { 
                return; 
            } 
            //create a new LayerFile instance 
            ESRI.ArcGIS.Carto.ILayerFile layerFile = new ESRI.ArcGIS.Carto.LayerFileClass(); 
 
            //make sure that the layer file name is valid 
            if (System.IO.Path.GetExtension(layerFilePath) != ".lyr") 
                return; 
            if (layerFile.get_IsPresent(layerFilePath)) 
                System.IO.File.Delete(layerFilePath); 
 
            //create a new layer file 
            layerFile.New(layerFilePath); 
 
            //attach the layer file with the actual layer 
            layerFile.ReplaceContents(layer); 
 
            //save the layer file 
            layerFile.Save(); 
        } 
        #endregion                     
             
        #endregion 
    } 
}