Author Topic: Using a side database to clone standard layers, textstyles, mleaders, etc  (Read 1914 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
I have been using this and has worked nicely for me and a handful of others for cloning in standard layers and styles for a year or so

I keep it open through out the session and only close it if file is updated and drawing used is copied to local drive when autocad is open and if told under certain circumstances. I nor any one else has noticed any performance issues.

I use is with a number of things such as WPF components bound to it and other things that need to clone standard crap in.

All the following code is just nothing than passing a Database to class that iterates the SymbolTables and dictionaries and stores the Objectid in dictionaries keyed by its name. Once stored just keep database open so ObjectId will still be valid and from there just check for names and pass ObjectIds to WblockClone



Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Autodesk.AutoCAD.DatabaseServices;
  7. using Autodesk.AutoCAD.LayerManager;
  8.  
  9. namespace HpadCad.App
  10. {
  11.     public abstract class WblockDatabase : IDisposable
  12.     {
  13.         public WblockBlocks Blocks { get; private set; }
  14.         public WblockTextStyles TextStyles { get; private set; }
  15.         public WblockDimStyles DimStyles { get; private set; }
  16.         public WblockMleaderStyles MleaderStyles { get; private set; }
  17.         public WblockLayers Layers { get; private set; }
  18.         public WblockLineTypes LineTypes { get; private set; }
  19.  
  20.  
  21.         public Database Db { get; set; }
  22.  
  23.  
  24.         public WblockDatabase(string filePath)
  25.         {
  26.             IsDisposed = false;
  27.             Db = new Database(false, true);
  28.             Db.ReadDwgFile(filePath, FileOpenMode.OpenForReadAndAllShare, true, "");
  29.  
  30.             using (Transaction trx = Db.TransactionManager.StartTransaction())
  31.             {
  32.  
  33.                 Blocks = new WblockBlocks(Db.BlockTable());
  34.                 TextStyles = new WblockTextStyles(Db.TextStyleTable());
  35.                 DimStyles = new WblockDimStyles(Db.DimStyleTable());
  36.                 MleaderStyles = new WblockMleaderStyles(Db.MLeaderStyleDBDictionary());
  37.                 Layers = new WblockLayers(Db.LayerTable());
  38.                 LineTypes = new WblockLineTypes(Db.LinetypeTable());
  39.                 IntializeContainers();
  40.                 trx.Commit();
  41.  
  42.             }
  43.         }
  44.  
  45.         protected abstract void IntializeContainers();
  46.  
  47.         public bool IsDisposed { get; private set; }
  48.  
  49.         public void Dispose()
  50.         {
  51.             Dispose(true);
  52.             GC.SuppressFinalize(this);
  53.         }
  54.  
  55.         protected virtual void Dispose(bool disposing)
  56.         {
  57.             if (IsDisposed) return;
  58.             if (disposing)
  59.             {
  60.                 if (Db != null && !Db.IsDisposed)
  61.                 {
  62.                     Db.Dispose();
  63.                     Db = null;
  64.                 }
  65.             }
  66.  
  67.             IsDisposed = true;
  68.         }
  69.  
  70.         public void Close()
  71.         {
  72.             Dispose();
  73.         }
  74.     }
  75.  

Code - C#: [Select]
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.LayerManager;
  9.  
  10. namespace HpadCad.App
  11. {
  12.     public class WblockBlocks : WblockStyleContainer
  13.     {
  14.  
  15.         public WblockBlocks(BlockTable blockTbl)
  16.         {
  17.  
  18.             foreach (var block in blockTbl.GetUserDefinedBlockTableRecords())
  19.             {
  20.                 Add(block.Name, block.ObjectId);
  21.             }
  22.  
  23.         }
  24.  
  25.  
  26.         protected override ObjectId ownerId
  27.         {
  28.             get { return HostApplicationServices.WorkingDatabase.BlockTableId;}
  29.         }
  30.     }
  31.  
  32.  
  33.     public class WblockTextStyles : WblockStyleContainer
  34.     {
  35.  
  36.         public WblockTextStyles(TextStyleTable textStlyeTbl)
  37.         {
  38.  
  39.             foreach (var textStlye in textStlyeTbl.GetTextStyleTableRecords())
  40.             {
  41.                 Add(textStlye.Name, textStlye.ObjectId);
  42.             }
  43.  
  44.         }
  45.  
  46.         protected override ObjectId ownerId
  47.         {
  48.             get { return HostApplicationServices.WorkingDatabase.TextStyleTableId; }
  49.         }
  50.     }
  51.  
  52.     public class WblockDimStyles : WblockStyleContainer
  53.     {
  54.  
  55.         public WblockDimStyles(DimStyleTable dimStlyeTbl)
  56.         {
  57.  
  58.             foreach (var dimStlye in dimStlyeTbl.GetDimStyleTableRecords())
  59.             {
  60.                 Add(dimStlye.Name, dimStlye.ObjectId);
  61.             }
  62.  
  63.         }
  64.         protected override ObjectId ownerId
  65.         {
  66.             get { return HostApplicationServices.WorkingDatabase.DimStyleTableId; }
  67.         }
  68.     }
  69.     public class WblockLineTypes : WblockStyleContainer
  70.     {
  71.  
  72.         public WblockLineTypes(LinetypeTable lTypeTable)
  73.         {
  74.  
  75.             foreach (var lType in lTypeTable.GetLinetypeTableRecords())
  76.             {
  77.                 Add(lType.Name, lType.ObjectId);
  78.             }
  79.  
  80.         }
  81.         protected override ObjectId ownerId
  82.         {
  83.             get { return HostApplicationServices.WorkingDatabase.LinetypeTableId; }
  84.         }
  85.     }
  86.  
  87.     public class WblockMleaderStyles : WblockStyleContainer
  88.     {
  89.  
  90.         public WblockMleaderStyles(DBDictionary mleaderDictionay)
  91.         {
  92.             foreach (var entry in mleaderDictionay)
  93.             {
  94.                 Add(entry.Key, entry.Value);
  95.             }
  96.         }
  97.  
  98.         protected override ObjectId ownerId
  99.         {
  100.             get { return HostApplicationServices.WorkingDatabase.MLeaderStyleDictionaryId; }
  101.         }
  102.  
  103.     }
  104.  
  105.  
  106.     public class WblockLayers : WblockStyleContainer
  107.     {
  108.  
  109.         public WblockLayers(LayerTable layerTbl)
  110.         {
  111.  
  112.             foreach (var layerTableRecord in layerTbl.IncludingHidden.GetLayerTableRecords())
  113.             {
  114.                 Add(layerTableRecord.Name, layerTableRecord.ObjectId);
  115.             }
  116.  
  117.         }
  118.  
  119.         public WblockLayers(IEnumerable<LayerTableRecord> layers , LayerFilter filter)
  120.         {
  121.  
  122.             foreach (var layerTableRecord in layers)
  123.             {
  124.                 if (filter.Filter(layerTableRecord))
  125.                 {
  126.                     Add(layerTableRecord.Name, layerTableRecord.ObjectId);
  127.                 }
  128.                
  129.             }
  130.  
  131.         }
  132.  
  133.  
  134.         protected override ObjectId ownerId
  135.         {
  136.             get { return HostApplicationServices.WorkingDatabase.LayerTableId; }
  137.         }
  138.  
  139.     }
  140.  
  141.     public class StandardLayerFilters : IEnumerable<StandardLayerFilter>
  142.     {
  143.         List<StandardLayerFilter>  layerFilters = new List<StandardLayerFilter>();
  144.        
  145.         public StandardLayerFilters(Database db)
  146.         {
  147.             var layers = db.LayerTable().GetLayerTableRecords().ToList();
  148.             foreach (LayerFilter lf in db.LayerFilters.Root.NestedFilters)
  149.             {
  150.                 if (!lf.AllowDelete) continue;
  151.                 Add(new StandardLayerFilter(layers, lf));
  152.             }
  153.            
  154.            
  155.  
  156.         }
  157.  
  158.         public void Add(StandardLayerFilter item)
  159.         {
  160.             layerFilters.Add(item);
  161.         }
  162.  
  163.         //public void Clear()
  164.         //{
  165.         //    throw new NotImplementedException();
  166.         //}
  167.  
  168.         //public bool Contains(StandardLayerFilter item)
  169.         //{
  170.         //    throw new NotImplementedException();
  171.         //}
  172.  
  173.         //public void CopyTo(StandardLayerFilter[] array, int arrayIndex)
  174.         //{
  175.         //    throw new NotImplementedException();
  176.         //}
  177.  
  178.         public int Count
  179.         {
  180.             get { return layerFilters.Count; }
  181.         }
  182.  
  183.  
  184.         public IEnumerator<StandardLayerFilter> GetEnumerator()
  185.         {
  186.            return layerFilters.GetEnumerator();
  187.         }
  188.  
  189.         IEnumerator IEnumerable.GetEnumerator()
  190.         {
  191.             return GetEnumerator();
  192.         }
  193.     }
  194.  
  195.  
  196.  
  197.     public class StandardLayerFilter
  198.     {
  199.         public string Name { get; set; }
  200.         public WblockLayers StandardLayers { get; set; }
  201.  
  202.  
  203.         public StandardLayerFilter(IEnumerable<LayerTableRecord> layers, LayerFilter filter)
  204.         {
  205.             Name = filter.Name;
  206.             StandardLayers = new WblockLayers(layers, filter);
  207.         }
  208.  
  209.         public IdMapping WblockCloneObjects()
  210.         {
  211.             ObjectIdCollection ids = new ObjectIdCollection();
  212.             foreach (var stdLayers in StandardLayers)
  213.             {
  214.                 ids.Add(stdLayers.Value);
  215.             }
  216.  
  217.             //LayerFilter df = null;
  218.             //foreach (LayerFilter lf in HostApplicationServices.WorkingDatabase.LayerFilters.Root.NestedFilters)
  219.             //{
  220.             //    if (!lf.AllowDelete) continue;
  221.  
  222.             //    if (String.Equals(lf.Name, Name, StringComparison.CurrentCultureIgnoreCase))
  223.             //    {
  224.             //        df = lf;
  225.             //    }
  226.             //}
  227.             IdMapping map = new IdMapping();
  228.             HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, HostApplicationServices.WorkingDatabase.LayerTableId, map, DuplicateRecordCloning.Ignore, false);
  229.             return map;
  230.         }
  231.  
  232.  
  233.  
  234.     }
  235.  
  236.     public class WblockBlockTableRecord : WblockContainer
  237.     {
  238.         public WblockBlockTableRecord(BlockTableRecord btr)
  239.         {
  240.             this.Add(btr.GetObjectIds());
  241.         }
  242.  
  243.         protected override ObjectId ownerId
  244.         {
  245.             get { return SymbolUtilityServices.GetBlockModelSpaceId(HostApplicationServices.WorkingDatabase); }
  246.         }
  247.     }
  248.  
  249.  
  250.     public abstract class WblockStyleContainer : IEnumerable<KeyValuePair<string, ObjectId>>
  251.     {
  252.         private Dictionary<string, ObjectId> dic;
  253.        protected abstract ObjectId ownerId { get;}
  254.  
  255.         public WblockStyleContainer()
  256.         {
  257.             dic = new Dictionary<string, ObjectId>(StringComparer.OrdinalIgnoreCase);
  258.         }
  259.  
  260.        
  261.  
  262.         protected void Add(string key, ObjectId value)
  263.         {
  264.            dic.Add(key, value);
  265.         }
  266.  
  267.         public bool Has(string key)
  268.         {
  269.             return dic.ContainsKey(key);
  270.         }
  271.  
  272.         public ICollection<string> Keys
  273.         {
  274.             get { return dic.Keys; }
  275.         }
  276.  
  277.        
  278.  
  279.         public bool TryGetValue(string key, out ObjectId value)
  280.         {
  281.             return dic.TryGetValue(key, out value);
  282.         }
  283.  
  284.         public ICollection<ObjectId> Values
  285.         {
  286.             get { return dic.Values; }
  287.         }
  288.  
  289.         public ObjectIdCollection IdsCollection
  290.         {
  291.  
  292.             get
  293.             {
  294.                 ObjectIdCollection ids = new ObjectIdCollection();
  295.                 foreach (var objectId in dic)
  296.                 {
  297.                     ids.Add(objectId.Value);
  298.                 }
  299.                 return ids;
  300.             }
  301.         }
  302.  
  303.         public ObjectId this[string key]
  304.         {
  305.             get { return dic[key]; }
  306.           protected  set
  307.             {
  308.                 dic[key] = value;
  309.             }
  310.         }
  311.  
  312.  
  313.         protected virtual ObjectId WblockCloneObject(string name, ObjectId ownerId, DuplicateRecordCloning dcr)
  314.         {
  315.             ObjectId id;
  316.             if (this.TryGetValue(name, out id))
  317.             {
  318.                 ObjectIdCollection ids = new ObjectIdCollection(){id};
  319.                 IdMapping map = new IdMapping();
  320.                 HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, ownerId, map, dcr, false);
  321.                 return map[id].Value;
  322.             }
  323.             return ObjectId.Null;
  324.         }
  325.  
  326.         protected virtual IdMapping WblockCloneObjects(IEnumerable<string> names, ObjectId ownerId, DuplicateRecordCloning dcr)
  327.         {
  328.             ObjectIdCollection ids = new ObjectIdCollection();
  329.  
  330.  
  331.             foreach (var name in names)
  332.             {
  333.                 ObjectId id;
  334.                 if (this.TryGetValue(name, out id))
  335.                 {
  336.                     ids.Add(id);
  337.                 }
  338.             }
  339.             IdMapping map = new IdMapping();
  340.             HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, ownerId, map, dcr, false);
  341.             return map;
  342.         }
  343.  
  344.         protected virtual IdMapping WblockCloneAllObjects(ObjectId ownerId, DuplicateRecordCloning dcr)
  345.         {
  346.  
  347.             //var idArray = Ids;
  348.  
  349.             //ObjectIdCollection ids = new ObjectIdCollection(idArray);
  350.  
  351.             ObjectIdCollection ids = IdsCollection;
  352.  
  353.             IdMapping map = new IdMapping();
  354.             HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, ownerId, map, dcr, false);
  355.             return map;
  356.         }
  357.  
  358.         public ObjectId WblockCloneObject(string name, DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
  359.         {
  360.             return WblockCloneObject(name, ownerId, dcr);
  361.         }
  362.  
  363.         public IdMapping WblockCloneObjects(IEnumerable<string> names, DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
  364.         {
  365.             return WblockCloneObjects(names, ownerId, dcr);
  366.         }
  367.         public IdMapping WblockCloneAllObjects(DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
  368.         {
  369.             return WblockCloneAllObjects(ownerId,dcr );
  370.         }
  371.  
  372.         public int Count
  373.         {
  374.             get { return dic.Count; }
  375.         }
  376.  
  377.  
  378.  
  379.         public IEnumerator<KeyValuePair<string, ObjectId>> GetEnumerator()
  380.         {
  381.             return dic.GetEnumerator();
  382.         }
  383.  
  384.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  385.         {
  386.             return GetEnumerator();
  387.         }
  388.     }
  389.  
  390.     public abstract class WblockContainer : IEnumerable<ObjectId>
  391.     {
  392.         private List<ObjectId> ids = new List<ObjectId>();
  393.         protected abstract ObjectId ownerId { get; }
  394.  
  395.         public WblockContainer()
  396.         {
  397.         }
  398.  
  399.  
  400.         public void Add(ObjectId item)
  401.         {
  402.             ids.Add(item);
  403.         }
  404.  
  405.         public void Add(IEnumerable<ObjectId> items)
  406.         {
  407.             foreach (var item in items)
  408.             {
  409.                 ids.Add(item);
  410.             }
  411.            
  412.         }
  413.  
  414.    
  415.         protected virtual IdMapping WblockCloneObjects(ObjectId ownerId, DuplicateRecordCloning dcr)
  416.         {
  417.          
  418.            ObjectIdCollection idCollection = new ObjectIdCollection(ids.ToArray());
  419.          
  420.             IdMapping map = new IdMapping();
  421.             HostApplicationServices.WorkingDatabase.WblockCloneObjects(idCollection, ownerId, map, dcr, false);
  422.             return map;
  423.         }
  424.  
  425.  
  426.  
  427.  
  428.         public IdMapping WblockCloneObjects( DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
  429.         {
  430.             return WblockCloneObjects(ownerId, dcr);
  431.         }
  432.  
  433.         public int Count
  434.         {
  435.             get { return ids.Count; }
  436.         }
  437.  
  438.  
  439.  
  440.         public IEnumerator<ObjectId> GetEnumerator()
  441.         {
  442.             return ids.GetEnumerator();
  443.         }
  444.  
  445.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  446.         {
  447.             return GetEnumerator();
  448.         }
  449.  
  450.  
  451.  
  452.         public ObjectId this[int index]
  453.         {
  454.             get { return ids[index]; }
  455.             set { ids[index] = value; }
  456.         }
  457.    
  458.  
  459.     }
  460. }
  461.  


Jeff H

  • Needs a day job
  • Posts: 6150
This following example using code below in my case or above depending on your settings is about the most needed in my case for any cloning as it group layers by LayerFilters and if you double click Filter it clones filter and all the layers in it, and it only show the layers for selected filter, and if you double click layer it clones it and will set anything selected on that layer.

And yes I spend about 5 minutes a day daydreaming about slapping the person who came up with layer naming scheme that the palette is currently using



Class for WPF Pallette set
Code - C#: [Select]
  1. using System;
  2. using System.Linq;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.LayerManager;
  5.  
  6. namespace HpadCad.App
  7. {
  8.     public class StandardsDatabase : WblockDatabase
  9.     {
  10.         public StandardsDatabase(string filePath) : base(filePath)
  11.         {
  12.         }
  13.  
  14.         public StandardLayerFilters LayerFilters { get; private set; }
  15.      
  16.         public LayerFilter GetFilter(string name)
  17.         {
  18.             return Db.LayerFilters.Root.NestedFilters.Cast<LayerFilter>().Where(lf => lf.AllowDelete).FirstOrDefault(lf => String.Equals(lf.Name, name, StringComparison.CurrentCultureIgnoreCase));
  19.         }
  20.  
  21.         protected override void IntializeContainers()
  22.         {
  23.             LayerFilters = new StandardLayerFilters(Db);
  24.         }
  25.     }
  26. }
  27.  

Here is xaml for binding
Code - C#: [Select]
  1. <UserControl x:Class="HpadCad.App.UI.PaletteSets.Standards.StandardsLayerPalette"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.              xmlns:app="clr-namespace:HpadCad.App"
  7.              mc:Ignorable="d"
  8.              d:DesignHeight="300" d:DesignWidth="300">
  9.    
  10.     <UserControl.Resources>
  11.         <Style x:Key="myListboxStyle">
  12.             <Style.Resources>
  13.                 <!-- Background of selected item when focussed -->
  14.                 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource {x:Static SystemColors.HotTrackColorKey}}" />
  15.                 <!-- Background of selected item when not focussed -->
  16.                 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource {x:Static SystemColors.GradientActiveCaptionColorKey}}" />
  17.             </Style.Resources>
  18.         </Style>
  19.     </UserControl.Resources>
  20.     <Grid Name="LayersGrid">
  21.         <Grid.RowDefinitions>
  22.             <RowDefinition Height="*" />
  23.             <RowDefinition Height="2" />
  24.             <RowDefinition Height="*" />
  25.         </Grid.RowDefinitions>
  26.         <DockPanel Grid.Row="0">
  27.             <Label Content="Filters" DockPanel.Dock="Top" HorizontalContentAlignment="Center" Foreground="#FFFAFAFA" Background="#CC5C5C5C" FontWeight="Bold" ></Label>
  28.             <ListBox Style="{StaticResource myListboxStyle}" Name="ListViewFilters" Grid.Row="0" MouseDoubleClick="ListViewFilters_OnMouseDoubleClick" Background="#FF5C5C5C" Foreground="White" BorderBrush="#FF5C5C5C" OpacityMask="#FF5C5C5C">
  29.                 <ListBox.ItemTemplate>
  30.                     <DataTemplate>
  31.                         <TextBlock Text="{Binding Name}" />
  32.                     </DataTemplate>
  33.                 </ListBox.ItemTemplate>
  34.             </ListBox>
  35.         </DockPanel>
  36.  
  37.         <GridSplitter Grid.Row="1" Height="2" HorizontalAlignment="Stretch" />
  38.         <DockPanel Grid.Row="2" >
  39.             <Label Content="Layers" DockPanel.Dock="Top" HorizontalContentAlignment="Center" Foreground="#FFFAFAFA" Background="#CC5C5C5C" FontWeight="Bold"   />
  40.             <ListBox Style="{StaticResource myListboxStyle}" Name="ListViewLayers" Grid.Row="2" MouseDoubleClick="ListViewLayers_OnMouseDoubleClick"
  41.                   ItemsSource="{Binding ElementName=ListViewFilters, Path=SelectedItem.(app:StandardLayerFilter.StandardLayers)}" Background="#FF5C5C5C" Foreground="White" BorderBrush="#FF5C5C5C" OpacityMask="#FF5C5C5C">
  42.              <ListBox.ItemTemplate>
  43.                     <DataTemplate>
  44.                         <TextBlock Text="{Binding Key}" />
  45.                     </DataTemplate>
  46.                 </ListBox.ItemTemplate>
  47.             </ListBox>
  48.         </DockPanel>
  49.     </Grid>
  50. </UserControl>
  51.  
  52.  

And now the code behind
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using Autodesk.AutoCAD.ApplicationServices;
  17. using Autodesk.AutoCAD.DatabaseServices;
  18. using Autodesk.AutoCAD.EditorInput;
  19. using Autodesk.AutoCAD.LayerManager;
  20. using Application = System.Windows.Application;
  21.  
  22. namespace HpadCad.App.UI.PaletteSets.Standards
  23. {
  24.     /// <summary>
  25.     /// Interaction logic for StandardsLayerPalette.xaml
  26.     /// </summary>
  27.     public partial class StandardsLayerPalette : UserControl
  28.     {
  29.         private ObservableCollection<StandardLayerFilter> observableFilters;
  30.         public StandardsLayerPalette(StandardLayerFilters layerFilters)
  31.         {
  32.             InitializeComponent();
  33.             observableFilters = new ObservableCollection<StandardLayerFilter>(layerFilters);
  34.             ListViewFilters.ItemsSource = observableFilters;
  35.             ListViewFilters.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
  36.             ListViewLayers.Items.SortDescriptions.Add(new SortDescription("Key", ListSortDirection.Ascending));
  37.         }
  38.  
  39.         public void Reset(StandardLayerFilters layerFilters)
  40.         {
  41.             observableFilters.Clear();
  42.             foreach (var standardLayerFilter in layerFilters)
  43.             {
  44.                 observableFilters.Add(standardLayerFilter);
  45.             }
  46.  
  47.  
  48.         }
  49.  
  50.         private void ListViewFilters_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
  51.         {
  52.             ListBox lb = sender as ListBox;
  53.             if (lb != null && lb.SelectedItem != null)
  54.             {
  55.                 StandardLayerFilter stdLayerFilter = lb.SelectedItem as StandardLayerFilter;
  56.                 Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
  57.                 Database db = doc.Database;
  58.                 Editor ed = doc.Editor;
  59.                 using (DocumentLock docLock = doc.LockDocument())
  60.                 {
  61.                     LayerFilterTree lft = db.LayerFilters;
  62.                     using (Transaction trx = doc.TransactionManager.StartTransaction())
  63.                     {
  64.                         if (stdLayerFilter != null)
  65.                         {
  66.                             IdMapping idmap = stdLayerFilter.WblockCloneObjects();
  67.                             LayerFilter sf = App.Standards.Current.StandardDatabase.GetFilter(stdLayerFilter.Name);
  68.                             LayerFilter df = null;
  69.                             LayerFilter destFilter = lft.Root;
  70.                             foreach (LayerFilter f in destFilter.NestedFilters)
  71.                             {
  72.                                 if (f.Name.Equals(sf.Name))
  73.                                 {
  74.                                     df = f;
  75.                                     break;
  76.                                 }
  77.                             }
  78.  
  79.                             if (df == null)
  80.                             {
  81.                                 if (sf.IsIdFilter)
  82.                                 {
  83.                                     LayerGroup sfgroup = sf as LayerGroup;
  84.                                     LayerGroup dfgroup = new LayerGroup();
  85.                                     dfgroup.Name = sf.Name;
  86.  
  87.                                     df = dfgroup;
  88.  
  89.                                     LayerCollection lyrs = sfgroup.LayerIds;
  90.                                     foreach (ObjectId lid in lyrs)
  91.                                     {
  92.                                         if (idmap.Contains(lid))
  93.                                         {
  94.                                             IdPair idp = idmap[lid];
  95.                                             dfgroup.LayerIds.Add(idp.Value);
  96.                                         }
  97.                                     }
  98.                                     destFilter.NestedFilters.Add(df);
  99.                                 }
  100.                                 else
  101.                                 {
  102.  
  103.                                     df = new LayerFilter();
  104.                                     df.Name = sf.Name;
  105.                                     df.FilterExpression = sf.FilterExpression;
  106.                                     destFilter.NestedFilters.Add(df);
  107.                                 }
  108.                             }
  109.                         }
  110.                         trx.Commit();
  111.                         Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
  112.                     }
  113.                     db.LayerFilters = lft;
  114.                 }
  115.  
  116.             }
  117.         }
  118.  
  119.         private void ListViewLayers_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
  120.         {
  121.             ListBox lb = sender as ListBox;
  122.             if (lb != null && lb.SelectedItem != null)
  123.             {
  124.                 KeyValuePair<string, ObjectId> kvp = (KeyValuePair<string, ObjectId>)lb.SelectedItem;
  125.                 Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
  126.                 Database db = doc.Database;
  127.                 Editor ed = doc.Editor;
  128.                 using (DocumentLock docLock = doc.LockDocument())
  129.                 {
  130.                     using (Transaction trx = doc.TransactionManager.StartTransaction())
  131.                     {
  132.                         LayerTable layerTable = db.LayerTable();
  133.                         ObjectId id = ObjectId.Null;
  134.                         if (layerTable.Has(kvp.Key))
  135.                         {
  136.                             id = layerTable[kvp.Key];
  137.                         }
  138.                         if (id.IsNull || id.IsErased)
  139.                         {
  140.                             if (App.Standards.Current.StandardDatabase.Layers.Has(kvp.Key))
  141.                             {
  142.                                 id = App.Standards.Current.StandardDatabase.Layers.WblockCloneObject(kvp.Key);
  143.                             }
  144.                         }
  145.  
  146.                         if (!id.IsNull)
  147.                         {
  148.                             PromptSelectionOptions pso = new PromptSelectionOptions();
  149.                             pso.RejectObjectsOnLockedLayers = true;
  150.                             PromptSelectionResult psr = ed.SelectImplied();
  151.                             if (psr.Status == PromptStatus.OK)
  152.                             {
  153.                                 var ents = psr.Value.GetObjectIds().GetEntities(OpenMode.ForWrite);
  154.                                 foreach (var ent in ents)
  155.                                 {
  156.                                     ent.LayerId = id;
  157.                                 }
  158.  
  159.                             }
  160.                             db.Clayer = id;
  161.                             ed.SetImpliedSelection(psr.Value);
  162.                            
  163.                         }
  164.                         trx.Commit();
  165.                         Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
  166.                     }
  167.                 }
  168.  
  169.             }
  170.         }
  171.     }
  172. }
  173.  
  174.  

I am doing something wrong as I should not be calling IntializeContainers(virtual or abstract method) inside the base class constructor

57gmc

  • Bull Frog
  • Posts: 366
Funny you should post this just now. Kean just posted this blog topic. Your solution sounds like what he's asking for.

Kean

  • Newt
  • Posts: 48
Funny you should post this just now. Kean just posted this blog topic. Your solution sounds like what he's asking for.

I was actually thinking more along these lines...

http://through-the-interface.typepad.com/through_the_interface/2015/08/preventing-autocad-objects-from-being-purged-using-net-part-1.html

Kean