本帖最后由 77077 于 2024-6-7 17:35 编辑
## 起因
CAD图层管理面板的界面太过于庞大,操作起来有点点不方便,所以尝试做这样一个东东,请高手指正。
## 效果
## 源码发布
mainWindow.xaml
- <Window x:Class="AutoCAD_2014_图层管理器.mainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- ResizeMode="CanResizeWithGrip"
- ShowInTaskbar="False"
- Title="图层开关" Height="500" Width="150">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="20"></RowDefinition>
- <RowDefinition Height="*"></RowDefinition>
- </Grid.RowDefinitions>
- <TextBox x:Name="FilterElement" Grid.Row="0"/>
- <ListBox x:Name="listBox1"
- ScrollViewer.VerticalScrollBarVisibility="Visible"
- Grid.Row="1"
- SelectionMode="Single"
- HorizontalContentAlignment="Stretch"
- VerticalContentAlignment="Stretch"
- >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <CheckBox IsChecked="{Binding IsOff}"/>
- <Rectangle Width="15" Height="15" Fill="{Binding LayerColor}" Stroke="#ff000000" StrokeThickness="1" />
- <TextBlock Text="{Binding LayerName}"/>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
- </Window>
复制代码
mainWindow.xaml.cs
- namespace AutoCAD_2014_图层管理器
- {
- /// <summary>
- /// mainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class mainWindow : Window
- {
- public mainWindow()
- {
- InitializeComponent();
- //初始化图层表
- List<LayerColorName> list1 = new List<LayerColorName>();
- // 获取当前图层名称,添加到图层表
- using (Database data = HostApplicationServices.WorkingDatabase)
- {
- using (Transaction trans = data.TransactionManager.StartTransaction())
- {
- using (LayerTable layer = (LayerTable)trans.GetObject(data.LayerTableId, OpenMode.ForRead))
- {
- foreach (ObjectId id in layer)
- {
- LayerTableRecord layer1 = (LayerTableRecord)trans.GetObject(id, OpenMode.ForRead);
- list1.Add(new LayerColorName(layer1.IsOff, ColorIndexConvertToHex(layer1.Color), layer1.Name));
- }
- }
- trans.Commit();
- }
- }
- //根据图层名称重新排序
- List<LayerColorName> listsorted = list1.OrderBy(x => x.LayerName).ToList();
- listBox1.ItemsSource = listsorted;
- }
- /// <summary>
- /// CAD图层的索引色转16进制字符串
- /// </summary>
- /// <param name="color">CAD索引色</param>
- /// <returns>16进制字符串</returns>
- public static string ColorIndexConvertToHex(Autodesk.AutoCAD.Colors.Color colorIndex)
- {
- if (colorIndex.IsByAci)
- {
- string hex = "#"
- + colorIndex.ColorValue.A.ToString("X2")
- + colorIndex.ColorValue.R.ToString("X2")
- + colorIndex.ColorValue.G.ToString("X2")
- + colorIndex.ColorValue.B.ToString("X2");
- return hex;
- }
- return "#FFFFFFFF";
- }
- /// <summary>
- /// 定义一个{图层开关,图层颜色,图层名称}的类
- /// </summary>
- public class LayerColorName
- {
- public bool IsOff { get; set; }
- public string LayerColor { get; set; }
- public string LayerName { get; set; }
- public LayerColorName(bool off, string col, string text)
- {
- IsOff = off;
- LayerColor = col;
- LayerName = text;
- }
- }
- //未完继续...
- }
- }
## 下一步计划:
1.增加勾选关闭图层;
2.增加图层过滤;
3.判断图层名称前缀,做成双层列表(或者treeview),分组控制;
|