using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.LayerManager;
namespace HpadCad.App
{
public class WblockBlocks : WblockStyleContainer
{
public WblockBlocks(BlockTable blockTbl)
{
foreach (var block in blockTbl.GetUserDefinedBlockTableRecords())
{
Add(block.Name, block.ObjectId);
}
}
protected override ObjectId ownerId
{
get { return HostApplicationServices.WorkingDatabase.BlockTableId;}
}
}
public class WblockTextStyles : WblockStyleContainer
{
public WblockTextStyles(TextStyleTable textStlyeTbl)
{
foreach (var textStlye in textStlyeTbl.GetTextStyleTableRecords())
{
Add(textStlye.Name, textStlye.ObjectId);
}
}
protected override ObjectId ownerId
{
get { return HostApplicationServices.WorkingDatabase.TextStyleTableId; }
}
}
public class WblockDimStyles : WblockStyleContainer
{
public WblockDimStyles(DimStyleTable dimStlyeTbl)
{
foreach (var dimStlye in dimStlyeTbl.GetDimStyleTableRecords())
{
Add(dimStlye.Name, dimStlye.ObjectId);
}
}
protected override ObjectId ownerId
{
get { return HostApplicationServices.WorkingDatabase.DimStyleTableId; }
}
}
public class WblockLineTypes : WblockStyleContainer
{
public WblockLineTypes(LinetypeTable lTypeTable)
{
foreach (var lType in lTypeTable.GetLinetypeTableRecords())
{
Add(lType.Name, lType.ObjectId);
}
}
protected override ObjectId ownerId
{
get { return HostApplicationServices.WorkingDatabase.LinetypeTableId; }
}
}
public class WblockMleaderStyles : WblockStyleContainer
{
public WblockMleaderStyles(DBDictionary mleaderDictionay)
{
foreach (var entry in mleaderDictionay)
{
Add(entry.Key, entry.Value);
}
}
protected override ObjectId ownerId
{
get { return HostApplicationServices.WorkingDatabase.MLeaderStyleDictionaryId; }
}
}
public class WblockLayers : WblockStyleContainer
{
public WblockLayers(LayerTable layerTbl)
{
foreach (var layerTableRecord in layerTbl.IncludingHidden.GetLayerTableRecords())
{
Add(layerTableRecord.Name, layerTableRecord.ObjectId);
}
}
public WblockLayers(IEnumerable<LayerTableRecord> layers , LayerFilter filter)
{
foreach (var layerTableRecord in layers)
{
if (filter.Filter(layerTableRecord))
{
Add(layerTableRecord.Name, layerTableRecord.ObjectId);
}
}
}
protected override ObjectId ownerId
{
get { return HostApplicationServices.WorkingDatabase.LayerTableId; }
}
}
public class StandardLayerFilters : IEnumerable<StandardLayerFilter>
{
List
<StandardLayerFilter
> layerFilters
= new List
<StandardLayerFilter
>();
public StandardLayerFilters(Database db)
{
var layers = db.LayerTable().GetLayerTableRecords().ToList();
foreach (LayerFilter lf in db.LayerFilters.Root.NestedFilters)
{
if (!lf.AllowDelete) continue;
Add(new StandardLayerFilter
(layers, lf
)); }
}
public void Add(StandardLayerFilter item)
{
layerFilters.Add(item);
}
//public void Clear()
//{
// throw new NotImplementedException();
//}
//public bool Contains(StandardLayerFilter item)
//{
// throw new NotImplementedException();
//}
//public void CopyTo(StandardLayerFilter[] array, int arrayIndex)
//{
// throw new NotImplementedException();
//}
public int Count
{
get { return layerFilters.Count; }
}
public IEnumerator<StandardLayerFilter> GetEnumerator()
{
return layerFilters.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class StandardLayerFilter
{
public string Name { get; set; }
public WblockLayers StandardLayers { get; set; }
public StandardLayerFilter(IEnumerable<LayerTableRecord> layers, LayerFilter filter)
{
Name = filter.Name;
StandardLayers
= new WblockLayers
(layers, filter
); }
public IdMapping WblockCloneObjects()
{
ObjectIdCollection ids
= new ObjectIdCollection
(); foreach (var stdLayers in StandardLayers)
{
ids.Add(stdLayers.Value);
}
//LayerFilter df = null;
//foreach (LayerFilter lf in HostApplicationServices.WorkingDatabase.LayerFilters.Root.NestedFilters)
//{
// if (!lf.AllowDelete) continue;
// if (String.Equals(lf.Name, Name, StringComparison.CurrentCultureIgnoreCase))
// {
// df = lf;
// }
//}
IdMapping map
= new IdMapping
(); HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, HostApplicationServices.WorkingDatabase.LayerTableId, map, DuplicateRecordCloning.Ignore, false);
return map;
}
}
public class WblockBlockTableRecord : WblockContainer
{
public WblockBlockTableRecord(BlockTableRecord btr)
{
this.Add(btr.GetObjectIds());
}
protected override ObjectId ownerId
{
get { return SymbolUtilityServices.GetBlockModelSpaceId(HostApplicationServices.WorkingDatabase); }
}
}
public abstract class WblockStyleContainer : IEnumerable<KeyValuePair<string, ObjectId>>
{
private Dictionary<string, ObjectId> dic;
protected abstract ObjectId ownerId { get;}
public WblockStyleContainer()
{
dic
= new Dictionary
<string, ObjectId
>(StringComparer
.OrdinalIgnoreCase); }
protected void Add(string key, ObjectId value)
{
dic.Add(key, value);
}
public bool Has(string key)
{
return dic.ContainsKey(key);
}
public ICollection<string> Keys
{
get { return dic.Keys; }
}
public bool TryGetValue(string key, out ObjectId value)
{
return dic.TryGetValue(key, out value);
}
public ICollection<ObjectId> Values
{
get { return dic.Values; }
}
public ObjectIdCollection IdsCollection
{
get
{
ObjectIdCollection ids
= new ObjectIdCollection
(); foreach (var objectId in dic)
{
ids.Add(objectId.Value);
}
return ids;
}
}
public ObjectId this[string key]
{
get { return dic[key]; }
protected set
{
dic[key] = value;
}
}
protected virtual ObjectId WblockCloneObject(string name, ObjectId ownerId, DuplicateRecordCloning dcr)
{
ObjectId id;
if (this.TryGetValue(name, out id))
{
ObjectIdCollection ids
= new ObjectIdCollection
(){id
}; IdMapping map
= new IdMapping
(); HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, ownerId, map, dcr, false);
return map[id].Value;
}
return ObjectId.Null;
}
protected virtual IdMapping WblockCloneObjects(IEnumerable<string> names, ObjectId ownerId, DuplicateRecordCloning dcr)
{
ObjectIdCollection ids
= new ObjectIdCollection
();
foreach (var name in names)
{
ObjectId id;
if (this.TryGetValue(name, out id))
{
ids.Add(id);
}
}
IdMapping map
= new IdMapping
(); HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, ownerId, map, dcr, false);
return map;
}
protected virtual IdMapping WblockCloneAllObjects(ObjectId ownerId, DuplicateRecordCloning dcr)
{
//var idArray = Ids;
//ObjectIdCollection ids = new ObjectIdCollection(idArray);
ObjectIdCollection ids = IdsCollection;
IdMapping map
= new IdMapping
(); HostApplicationServices.WorkingDatabase.WblockCloneObjects(ids, ownerId, map, dcr, false);
return map;
}
public ObjectId WblockCloneObject(string name, DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
{
return WblockCloneObject(name, ownerId, dcr);
}
public IdMapping WblockCloneObjects(IEnumerable<string> names, DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
{
return WblockCloneObjects(names, ownerId, dcr);
}
public IdMapping WblockCloneAllObjects(DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
{
return WblockCloneAllObjects(ownerId,dcr );
}
public int Count
{
get { return dic.Count; }
}
public IEnumerator<KeyValuePair<string, ObjectId>> GetEnumerator()
{
return dic.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public abstract class WblockContainer : IEnumerable<ObjectId>
{
private List
<ObjectId
> ids
= new List
<ObjectId
>(); protected abstract ObjectId ownerId { get; }
public WblockContainer()
{
}
public void Add(ObjectId item)
{
ids.Add(item);
}
public void Add(IEnumerable<ObjectId> items)
{
foreach (var item in items)
{
ids.Add(item);
}
}
protected virtual IdMapping WblockCloneObjects(ObjectId ownerId, DuplicateRecordCloning dcr)
{
ObjectIdCollection idCollection
= new ObjectIdCollection
(ids
.ToArray());
IdMapping map
= new IdMapping
(); HostApplicationServices.WorkingDatabase.WblockCloneObjects(idCollection, ownerId, map, dcr, false);
return map;
}
public IdMapping WblockCloneObjects( DuplicateRecordCloning dcr = DuplicateRecordCloning.Ignore)
{
return WblockCloneObjects(ownerId, dcr);
}
public int Count
{
get { return ids.Count; }
}
public IEnumerator<ObjectId> GetEnumerator()
{
return ids.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public ObjectId this[int index]
{
get { return ids[index]; }
set { ids[index] = value; }
}
}
}