Classement
Contenu populaire
Affichage du contenu avec la meilleure réputation le 09/04/2025 dans toutes les zones
-
Salut, Il m'a semblé que la question pouvait être un bon exemple pour une palette C#/WPF. Télécharger LayoutPaletteSetup.msi. Débloquer le fichier et lancer l'installation. La commande LAYOUTS affiche la palette (compatible toutes versions d'AutoCAD pleines ou verticales depuis 2013). Pour ceux que ça intéresse, le code (de plus amples explications à propos des interfaces utilisateur en .NET sur cette page). La classe CustomPaletteSet définit un jeu de palette. using Autodesk.AutoCAD.Windows; using System; using AcCoreAp = Autodesk.AutoCAD.ApplicationServices.Core.Application; namespace LayoutPaletteNetCore { internal class CustomPaletteSet : PaletteSet { static bool wasVisible; public CustomPaletteSet() : base("Layouts", "LAYOUTS", new Guid("{92007042-8476-48DB-9003-AD5AA0E47C98}")) { Style = PaletteSetStyles.ShowAutoHideButton | PaletteSetStyles.ShowCloseButton | PaletteSetStyles.ShowPropertiesMenu; MinimumSize = new System.Drawing.Size(150, 450); AddVisual("Layouts", new LayoutControl()); var docs = AcCoreAp.DocumentManager; docs.DocumentBecameCurrent += (s, e) => Visible = e.Document != null && wasVisible; docs.DocumentCreated += (s, e) => Visible = wasVisible; docs.DocumentToBeDeactivated += (s, e) => wasVisible = Visible; docs.DocumentToBeDestroyed += (s, e) => { wasVisible = Visible; if (docs.Count == 1) Visible = false; }; } } } Le fichier PaletteControl.xaml définit le contrôle WPF (palette) <UserControl x:Class="LayoutPaletteNetCore.LayoutControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:LayoutPaletteNetCore" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid Background="WhiteSmoke"> <ListBox Margin="5" ItemsSource="{Binding LayoutList}" SelectionMode="Single" SelectedItem="{Binding CurrentLayout}"/> </Grid> </UserControl> Le fichier PaletteControl.xaml.cs définit la logique d'interaction du contrôle using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using System.Collections; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using System.Windows.Controls; using AcCoreAp = Autodesk.AutoCAD.ApplicationServices.Core.Application; namespace LayoutPaletteNetCore { public partial class LayoutControl : UserControl, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); private string currentLayout; public ObservableCollection<string> LayoutList { get; private set; } public string CurrentLayout { get { return currentLayout; } set { currentLayout = value; SetCurrentLayout(); NotifyPropertyChanged(nameof(CurrentLayout)); } } public LayoutControl() { InitializeComponent(); DataContext = this; LayoutList = new ObservableCollection<string>(); AcCoreAp.DocumentManager.DocumentActivated += OnDocumentActivated; var doc = AcCoreAp.DocumentManager.MdiActiveDocument; if (doc != null) { UpdateLayouts(); HandleEvents(LayoutManager.Current); } } private async void SetCurrentLayout() { var docs = AcCoreAp.DocumentManager; await docs.ExecuteInCommandContextAsync( (_) => { LayoutManager.Current.CurrentLayout = CurrentLayout; return Task.CompletedTask; }, null); } private void OnDocumentActivated(object sender, DocumentCollectionEventArgs e) { UpdateLayouts(); HandleEvents(LayoutManager.Current); } private void HandleEvents(LayoutManager layoutManager) { layoutManager.LayoutCopied += (_, _1) => UpdateLayouts(); layoutManager.LayoutCreated += (_, _1) => UpdateLayouts(); layoutManager.LayoutRemoved += (_, _1) => UpdateLayouts(); layoutManager.LayoutRenamed += (_, _1) => UpdateLayouts(); layoutManager.LayoutsReordered += (_, _1) => UpdateLayouts(); layoutManager.LayoutSwitched += (_, _1) => CurrentLayout = layoutManager.CurrentLayout; } private void UpdateLayouts() { var db = HostApplicationServices.WorkingDatabase; using (var tr = new OpenCloseTransaction()) { LayoutList.Clear(); var layouts = ((DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead)) .Cast<DictionaryEntry>() .Select(e => (Layout)tr.GetObject((ObjectId)e.Value, OpenMode.ForRead)) .OrderBy(l => l.TabOrder) .Select(l => l.LayoutName); foreach (string layoutName in layouts) { LayoutList.Add(layoutName); } CurrentLayout = LayoutManager.Current.CurrentLayout; } } } } La commande pour afficher la palette. using Autodesk.AutoCAD.Runtime; [assembly: CommandClass(typeof(LayoutPaletteNetCore.Commands))] namespace LayoutPaletteNetCore { public class Commands { static CustomPaletteSet palette; [CommandMethod("LAYOUTS")] public static void ShowLayoutsPalette() { if (palette == null) { palette = new CustomPaletteSet(); } palette.Visible = true; } } }1 point
