📄 miwrapper.cs
字号:
/// Override this to support descendants of the Vertices class.
/// </summary>
/// <returns>This parts vertices.</returns>
protected internal virtual Vertices CreateVertices(Part part) {
return new Vertices(this);
}
public override string ToString() {
return "Part: "+Index+"\nVertices:\n"+this.Vertices.ToString();
}
}
/// <summary>
/// Contains the set of Parts belonging to a single Feature.
/// </summary>
/// <remarks>This class descends EnumImple, meaning the Parts in the
/// set can be iterated using foreach.
/// It also has an index property allowing any Part between 0 and Parts.Count-1
/// to be accessed directly with Parts[idx]
/// </remarks>
public class Parts : EnumImpl {
/// <summary>
/// The feature these parts belong to.
/// </summary>
public readonly Feature Feature;
protected internal Parts(Feature feature):base(MiApi.mitab_c_get_parts(feature.Handle)) {
this.Feature = feature;
}
/// <summary>
/// Override this to support descendants of the Part class.
/// </summary>
/// <returns>A part with the given index</returns>
protected internal virtual Part CreatePart(int partIdx) {
return new Part(this.Feature, partIdx);
}
public Part this[int index] {
get {
return index < Count ? CreatePart(index) : null;
}
}
public override object GetObj(int idx) {
return this[idx];
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append("Part Count: "+this.Count+"\n");
foreach (Part part in this)
str.Append(part.ToString()+"\n");
return str.ToString();
}
}
/// <summary>
/// Represents a single feature beloning to a layer.
/// </summary>
public class Feature : IDisposable {
/// <summary>
/// Handle used to manipulate the object in the C API.
/// </summary>
public readonly IntPtr Handle;
/// <summary>
/// The id of this feature.
/// </summary>
public readonly int Id;
/// <summary>
/// The feature type.
/// </summary>
public readonly FeatureType Type;
/// <summary>
/// The layer the Feature belongs to.
/// </summary>
public readonly Layer Layer;
/// <summary>
/// The set of parts comprising this feature.
/// </summary>
public readonly Parts Parts;
protected internal Feature(Layer layer, int featureId) {
this.Id = featureId;
this.Layer = layer;
this.Handle = MiApi.mitab_c_read_feature(layer.Handle, featureId);
this.Type = MiApi.mitab_c_get_type(Handle);
this.Parts = CreateParts(this);
}
/// <summary>
/// Override this to support descendants of the Parts class.
/// </summary>
/// <returns>This layers fields</returns>
protected internal virtual Parts CreateParts(Feature feature) {
return new Parts(this);
}
/// <summary>
/// Returns text associated with this feature
/// </summary>
/// <remarks>This will return an empty string, unless this features type is
/// TABFC_Text.</remarks>
public string Text {
get {
return (this.Type == FeatureType.TABFC_Text) ?
MiApi.mitab_c_get_text(this.Handle) :
"";
}
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append("Feature: "+Id+"\nFields:\n");
foreach (Field field in this.Layer.Fields)
str.Append(field.GetValueAsString(this).Trim()+"\t");
str.Append("\n"+this.Parts.ToString());
return str.ToString();
}
/// <summary>
/// Convenience method to return the next Feature in the file.
/// </summary>
/// <returns>A following feature in the file.</returns>
public Feature GetNext() {
return new Feature(this.Layer, MiApi.mitab_c_next_feature_id(Layer.Handle, this.Id));
}
private bool disposed = false;
public void Dispose(bool disposing) {
if (disposing && !disposed) {
MiApi.mitab_c_destroy_feature(this.Handle);
disposed = true;
}
}
public void Dispose() {
Dispose(true);
}
~Feature() {
Dispose(false);
}
}
/// <summary>
/// Unlike the other enumerators. The feature id set isn't guaranteed to be sequential.
/// So we override the default seqeuntial iterator.
/// </summary>
internal class FeaturesEnum : IndexedEnum {
private readonly Layer layer;
internal FeaturesEnum(IObjectProvider objProvider, Layer layer) : base(objProvider) {
this.layer = layer;
}
public override bool MoveNext() {
return (eIdx = MiApi.mitab_c_next_feature_id(layer.Handle, eIdx)) != -1;
}
}
/// <summary>
/// Contains the set of features belonging to a single layer.
/// </summary>
/// <remarks>This class descends EnumImpl, meaning the features in the
/// set can be iterated using foreach.
/// It also has an index property allowing any feature between 0 and Features.Count-1
/// to be accessed directly with Features[idx]</remarks>
public class Features : EnumImpl {
/// <summary>
/// The layer the features belong to
/// </summary>
public readonly Layer Layer;
protected internal Features(Layer layer) :
base(MiApi.mitab_c_get_feature_count(layer.Handle)) {
this.Layer = layer;
}
/// <summary>
/// Override this to support descendants of the Feature class.
/// </summary>
/// <returns>This layers fields</returns>
protected internal virtual Feature CreateFeature(int index) {
return new Feature(this.Layer, index);
}
public Feature this[int index] {
get {
return (index != -1) ? CreateFeature(index) : null;
}
}
public Feature GetFirst() {
return this[MiApi.mitab_c_next_feature_id(Layer.Handle, -1)];
}
public override object GetObj(int idx) {
return this[idx];
}
public override IEnumerator GetEnumerator() {
return new FeaturesEnum(this, Layer);
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append("Feature Count: "+this.Count+"\n");
foreach (Feature feature in this)
str.Append(feature.ToString()+"\n");
return str.ToString();
}
}
public class Layer {
/// <summary>
/// Handle used to manipulate the object in the C API
/// </summary>
public readonly IntPtr Handle;
public readonly Fields Fields;
public readonly Features Features;
public readonly string FileName;
protected internal Layer(string fileName) {
this.Handle = MiApi.mitab_c_open(fileName);
if (this.Handle == IntPtr.Zero)
throw new FileNotFoundException("File "+fileName+" not found", fileName);
this.Fields = CreateFields();
this.Features = CreateFeatures();
this.FileName = fileName;
}
/// <summary>
/// Override this to support descendants of the Fields class.
/// </summary>
/// <returns>This layers fields</returns>
protected internal virtual Fields CreateFields() {
return new Fields(this);
}
/// <summary>
/// Override this to support descendants of the Feature class.
/// </summary>
/// <returns>This layers features</returns>
protected internal virtual Features CreateFeatures() {
return new Features(this);
}
/// <summary>
/// Factory method to return the layer with a given name.
/// </summary>
/// <param name="tabFileName"></param>
/// <returns></returns>
public static Layer GetByName(string tabFileName) {
return new Layer(tabFileName);
}
public override string ToString() {
return "Layer: "+this.FileName;
}
/// <summary>
/// Writes this layers features to the given textwriter
/// </summary>
/// <param name="writer">Destintation for the layers features</param>
public void ToText(TextWriter writer) {
writer.WriteLine(this);
writer.WriteLine(this.Fields+"\n");
writer.WriteLine(this.Features);
}
/// <summary>
/// Writes this layers features as a text file.
/// </summary>
/// <param name="fileName">The name of the file that will be created.</param>
public void ToText(string fileName) {
ToText(new StreamWriter(fileName));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -