using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace GeoSigma.SigmaDrawerUtil
{
///
/// 全局设置
///
public class DrawerGlobalConfig
{
private static DrawerGlobalConfig _globalConfig = null;
public static DrawerGlobalConfig Instance
{
get
{
if (_globalConfig == null)
{
_globalConfig = ReadFromFile();
}
return _globalConfig;
}
}
private static string configFile = Path.Combine(Application.StartupPath, "DrawGlobalConfig.xml");
///
public RangSelectConfig RangSelectNode { get; set; } = new RangSelectConfig();
public FillConfig FillNode { get; set; } = new FillConfig();
public MeshColorEditConfig MeshColorEditNode { get; set; } = new MeshColorEditConfig();
public MeshConfig MeshNode { get; set; } = new MeshConfig();
/// 计算设置
///
///
/// Initializes a new instance of the class.
///
public DrawerGlobalConfig() { }
///
/// Reads the specified XML file.
///
/// The XML file.
/// 经读取生成的对象
private static DrawerGlobalConfig ReadFromFile()
{
//DialogResult result = DialogResult.Cancel;
string xmlFile = configFile;
if (!File.Exists(xmlFile))
{
return new DrawerGlobalConfig();
}
try
{
using (StreamReader reader = new StreamReader(xmlFile, Encoding.Default))
{
XmlSerializer serializer = new XmlSerializer(typeof(DrawerGlobalConfig));
DrawerGlobalConfig objRead = (DrawerGlobalConfig)serializer.Deserialize(reader);
return objRead;
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
/*
const string message =
"解析配置文件(DrawerGlobalConfig.xml)失败,点击确定创建缺省配置,点击取消保留错误配置文件!";
const string caption = "配置文件错误";
result = MessageBox.Show(message, caption,
MessageBoxButtons.OKCancel,
MessageBoxIcon.Error);
*/
}
/*
if (result == DialogResult.OK)
{
return new DrawerGlobalConfig();
}
else
{
return null;
}
*/
return new DrawerGlobalConfig();
}
///
/// Saves this instance.
///
/// 是否保存成功
public bool Save()
{
string xmlFile = configFile;
using (StreamWriter sw = new StreamWriter(xmlFile, false, Encoding.Default))
{
// 创建 XmlSerializer 实例
XmlSerializer serializer = new XmlSerializer(typeof(DrawerGlobalConfig));
// 将 MyClass 对象序列化到 XML
serializer.Serialize(sw, this);
}
return true;
}
}
///
/// 相交面积因子
///
public class RangSelectConfig
{
///
/// Gets or sets 相交面积百分比.
///
[System.ComponentModel.Category("相交面积"), System.ComponentModel.DisplayName("面积百分比")]
public int AreaFactor { get; set; } = 100;
}
///
/// 区域填充相交填充相交延伸长度
///
public class FillConfig
{
///
/// Gets or sets 相交填充延伸长度.
///
[System.ComponentModel.Category("相交填充"), System.ComponentModel.DisplayName("线段延伸长度")]
public int IntersectionExtendLength { get; set; } = 5000;
/*
///
/// Gets or sets 自动填充延伸长度.
///
[System.ComponentModel.Category("自动填充"), System.ComponentModel.DisplayName("线段延伸长度")]
public int AutoExtendLength { get; set; } = 50;
*/
}
///
/// bool型数据属性转换
///
public class CustomBooleanConverter : BooleanConverter
{
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is bool)
{
return (bool)value ? "是" : "否";
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
/// Converts from.
///
/// The context.
/// The culture.
/// The value.
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string strValue = ((string)value).Trim().ToLower();
if (strValue == "是" || strValue == "true")
{
return true;
}
else if (strValue == "否" || strValue == "false")
{
return false;
}
}
return base.ConvertFrom(context, culture, value);
}
}
///
/// 抗锯齿设置
///
public class MeshConfig
{
///
/// Gets or sets 是否抗锯齿.
///
[System.ComponentModel.Category("抗锯齿设置"), System.ComponentModel.DisplayName("是否抗锯齿")]
[TypeConverter(typeof(CustomBooleanConverter))]
public bool AntiAliasingEnable { get; set; } = false;
}
public class MeshColorEditConfig
{
///
/// Gets or sets 是否抗锯齿.
///
[System.ComponentModel.Category("曲面颜色编辑设置"), System.ComponentModel.DisplayName("实时应用")]
[TypeConverter(typeof(CustomBooleanConverter))]
public bool AutoApplyEnable { get; set; } = false;
[System.ComponentModel.Category("曲面颜色编辑设置"), System.ComponentModel.DisplayName("循环获得颜色")]
[TypeConverter(typeof(CustomBooleanConverter))]
public bool LoopColorEnable { get; set; } = false;
}
}