ANEXO 1. Obtener la lista de estados de capa /// /// Obtiene un lista con los estados de capas almacenados en el dibujo actual /// /// Lista de los estados de capa o null si se producen errores public static List GetListLayerStates() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; List RetList = new List(); try { using (Transaction trans = db.TransactionManager.StartTransaction()) { LayerStateManager acLyrStMan = db.LayerStateManager; DBDictionary acDbDict = trans.GetObject(acLyrStMan.LayerStatesDictionaryId(true), OpenMode.ForRead) as DBDictionary; foreach (DBDictionaryEntry acDbDictEnt in acDbDict) { RetList.Add(acDbDictEnt.Key); } } return RetList; } catch (Exception ex) { FRMS.MessageBox.Show(ex.Message, "Error", FRMS.MessageBoxButtons.OK, FRMS.MessageBoxIcon.Error); return null; } finally { db.Dispose(); } } ANEXO 2. Almacenar el estado de capas actual /// /// Guarda el estado de capas actual /// /// Nombre para el estado de capas public static void SaveLayerState(string LayerStateName) { Document doc = Application.DocumentManager.MdiActiveDocument; LayerStateManager acLyrStMan; acLyrStMan = doc.Database.LayerStateManager; if (!acLyrStMan.HasLayerState(LayerStateName)) { acLyrStMan.SaveLayerState(LayerStateName, LayerStateMasks.Color | LayerStateMasks.LineType | LayerStateMasks.LineWeight | LayerStateMasks.Frozen | LayerStateMasks.On | LayerStateMasks.Locked, ObjectId.Null); } } ANEXO 3. Renombrar un estado de capas /// /// Renombra un estado de capas /// /// Nombre del estado de capas a renombrar /// Nuevo nombre para el estado de capas public static void RenameLayerState(string OldName, string NewName) { Document doc = Application.DocumentManager.MdiActiveDocument; LayerStateManager acLyrStMan = doc.Database.LayerStateManager; if (acLyrStMan.HasLayerState(OldName) && !acLyrStMan.HasLayerState(NewName)) { acLyrStMan.RenameLayerState(OldName, NewName); } } ANEXO 4. Eliminar un estado de capas /// /// Elimina un estado de capas /// /// public static void RemoveLayerState(string StateName) { Document doc = Application.DocumentManager.MdiActiveDocument; LayerStateManager acLyrStMan = doc.Database.LayerStateManager; if (acLyrStMan.HasLayerState(StateName)) { acLyrStMan.DeleteLayerState(StateName); } } ANEXO 5. Restaurar estado de capas /// /// Restaura un estado de capas /// /// Nombre de estado de capas a restaurar public static void RestoreLayerState(string StateName) { Document doc = Application.DocumentManager.MdiActiveDocument; LayerStateManager acLyrStMan = doc.Database.LayerStateManager; if (acLyrStMan.HasLayerState(StateName)) { acLyrStMan.RestoreLayerState(StateName, ObjectId.Null, 1, LayerStateMasks.Color | LayerStateMasks.LineType | LayerStateMasks.LineWeight | LayerStateMasks.Frozen | LayerStateMasks.On | LayerStateMasks.Locked); } } ANEXO 6. Exportar estado de capas /// /// Exporta un estado de capas hacia un archivo /// /// Nombre del estado de capas a exportar /// Ruta donde va a almacenarse el estado de capas. public static void ExportLayerState(string StateName, string PathName) { if (!Directory.Exists(PathName)) { return; } Document doc = Application.DocumentManager.MdiActiveDocument; LayerStateManager acLyrStMan = doc.Database.LayerStateManager; if (acLyrStMan.HasLayerState(StateName)) { acLyrStMan.ExportLayerState(StateName, PathName + "\\" + StateName + ".las"); } } ANEXO 7. Importar estado de capas /// /// Importa un archivo de configuración de capas. /// /// Nombre del archivo (*.las) a importar public static void ImportLayerState(string FileName) { Document doc = Application.DocumentManager.MdiActiveDocument; LayerStateManager acLyrStMan = doc.Database.LayerStateManager; if (System.IO.File.Exists(FileName)) { try { acLyrStMan.ImportLayerState(FileName); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Application.ShowAlertDialog(ex.Message); } } } /// Código en MyCommands public static void GetListLayerStates() { List LayerStates = AcFunctions.GetListLayerStates(); if (LayerStates != null) { if (LayerStates.Count == 0) { FRMS.MessageBox.Show("No hay estados de capa guardados.", "Test", FRMS.MessageBoxButtons.OK, FRMS.MessageBoxIcon.Information); } else { string cad = "Lista de estados de capa obtenidos: " + Environment.NewLine; foreach (string s in LayerStates) { cad += s + Environment.NewLine; } FRMS.MessageBox.Show(cad, "Test", FRMS.MessageBoxButtons.OK, FRMS.MessageBoxIcon.Information); } } } public static void SaveLayerState() { string s = AcFunctions.GetStringFromUser("\nEstablecer nombre para el nuevo estado de capas", true, false, ""); if (!string.IsNullOrWhiteSpace(s)) { AcFunctions.SaveLayerState(s); } } public static void RenameLayerState() { string OldName = AcFunctions.GetStringFromUser("\nNombre del estado de capas existente", true, false, ""); if (!string.IsNullOrWhiteSpace(OldName)) { string NewName = AcFunctions.GetStringFromUser("\nNuevo nombre para " + OldName, true, false, ""); if (!string.IsNullOrWhiteSpace(NewName)) { AcFunctions.RenameLayerState(OldName, NewName); } } } public static void RemoveLayerState() { string StName = AcFunctions.GetStringFromUser("\nNombre del estado de capas a eliminar", true, false, ""); if (!string.IsNullOrWhiteSpace(StName)) { AcFunctions.RemoveLayerState(StName); } } public static void RestoreLayerState() { string StName = AcFunctions.GetStringFromUser("\nNombre del estado de capas a restaurar", true, false, ""); if (!string.IsNullOrWhiteSpace(StName)) { AcFunctions.RestoreLayerState(StName); } } public static void ExportLayerState() { FRMS.FolderBrowserDialog odiag = new FRMS.FolderBrowserDialog(); odiag.Description = "Seleccionar carpeta donde almacenar el estado de capas."; odiag.ShowNewFolderButton = true; if (odiag.ShowDialog() == FRMS.DialogResult.OK) { string StName = AcFunctions.GetStringFromUser("\nNombre del estado de capas a exportar", true, false, ""); if (!string.IsNullOrWhiteSpace(StName)) { AcFunctions.ExportLayerState (StName, odiag.SelectedPath); } } } public static void ImportLayerState() { FRMS.OpenFileDialog odiag = new FRMS.OpenFileDialog(); odiag.Title = "Seleccionar archivo de estado de capas"; odiag.Filter = "Archivos de estado de capas (*.las)|*.las"; odiag.Multiselect = false; odiag.DefaultExt = "*.las"; odiag.FileName = ""; if (odiag.ShowDialog() == FRMS.DialogResult.OK) { AcFunctions.ImportLayerState(odiag.FileName); } } /// Código en AppEntry [CommandMethod("CA_LAYERSTATES")] public void _LayerStates() { MyCommands.GetListLayerStates(); } [CommandMethod("CA_SAVELAYERSTATE")] public void _SaveLayerState() { MyCommands.SaveLayerState(); } [CommandMethod("CA_RENAMELAYERSTATE")] public void _RenameLayerState() { MyCommands.RenameLayerState(); } [CommandMethod("CA_REMOVELAYERSTATE")] public void _RemoveLayerState() { MyCommands.RemoveLayerState(); } [CommandMethod("CA_RESTORELAYERSTATE")] public void _RestoreLayerState() { MyCommands.RestoreLayerState(); } [CommandMethod("CA_EXPORTLAYERSTATE")] public void _ExportLayerState() { MyCommands.ExportLayerState(); } [CommandMethod("CA_IMPORTLAYERSTATE")] public void _ImportLayerState() { MyCommands.ImportLayerState(); }