一个使用C#和ArcPy实现的版本压缩工具(三)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了一个使用C#和ArcPy实现的版本压缩工具(三),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4988字,纯文字阅读大概需要8分钟。
内容图文
ArcGIS版本压缩功能设计与开发
目录
参数的设置和存取
上一篇文章中,我们已经将进行压缩等操作的代码写好,并在PyCharm中调试完毕。从代码中可以看出,功能的调用,是需要传入一定的外部参数的。这些参数,我们在C#中进行设置和存取。
为此,我们定义了一个类和一些静态变量,用于保存相关参数。
public class CompressionParameter
{
public static readonly string m_sRegistryNode = @"Software\Microsoft\Windows\CurrentVersion\Run"; //注册表开机启动项位置
public static readonly string m_sRegistryKey = "FissCompressVersions"; //注册表键名
public static readonly string m_sPythonInterpreter = "PythonInterpreter"; //Python解释器路径
public static readonly string m_sConnectionFile = "ConnectionFile"; //SDE连接文件路径
public static readonly string m_sConflictDefinition = "ConflictDefinition"; //冲突定义(0:按行,1:按列)
public static readonly string m_sConflictResolution = "ConflictResolution"; //冲突解决(0:默认版本优先,1:编辑版本优先)
public static readonly string m_sCompressionMode = "CompressionMode"; //定时压缩方式(0:普通,1:自动,1:彻底)
public static readonly string m_sHour = "Hour";//定时压缩时间(小时)
public static readonly string m_sMinute = "Minute";//定时压缩时间(分钟)
public static readonly string m_sRepeat = "Repeat";//定时压缩重复(0:一次,1:每天)
public static readonly string m_sAutoStart = "AutoStart";//是否自动启动(0:否,1:是)
public static readonly string m_sServerIP = "ServerIP"; //服务器IP地址
public static readonly string m_sServerPort = "ServerPort"; //服务器端口号
public static readonly string m_sNull = ""; //为空
public static readonly string m_Checked = "1"; //选中
public static readonly string m_Unchecked = "0"; //未选中
public static ConfigManager m_configManager = new ConfigManager(); //配置管理器
public static Dictionary<string, string> m_dictConfig = new Dictionary<string, string>() //默认配置项
{
{ m_sPythonInterpreter,m_sNull },
{ m_sConnectionFile,m_sNull},
{ m_sConflictDefinition,"0"},
{ m_sConflictResolution, "0"},
{ m_sCompressionMode,"1"},
{ m_sHour,"0"},
{ m_sMinute,"0" },
{ m_sRepeat,"1"},
{ m_sAutoStart,m_Unchecked},
{ m_sServerIP,"127.0.0.1"},
{m_sServerPort,"9071"}
};
}
使用C#调用Python脚本(带参数)
接下来,就是使用C#进行调用的时候了。
调用的关键,是使用 Python解释器 去执行相应的代码。
我们实现了一个核心的方法,去启动Python解释器,传入相关执行代码和参数:
private void ExecuteMission(string scriptFile, string param = "")
{
try
{
WriteLine("------------*------------>>>");
WriteLine("Mission Starts...");
var sParam = string.Format("{0} {1}", scriptFile, "\"" + CompressionParameter.m_dictConfig[CompressionParameter.m_sConnectionFile] + "\"");
if (!string.IsNullOrEmpty(param))
sParam += string.Format(" {0}", param);
ProcessStartInfo start = new ProcessStartInfo
{
WorkingDirectory = Application.StartupPath,
FileName = CompressionParameter.m_dictConfig[CompressionParameter.m_sPythonInterpreter],
Arguments = sParam,
UseShellExecute = false,
ErrorDialog = true,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true
};
WriteLine("Processing...");
Process process = Process.Start(start);
var reader = process.StandardOutput;
string sLine = reader.ReadLine();
WriteLine(sLine);
while (!reader.EndOfStream)
{
Application.DoEvents();
sLine = reader.ReadLine();
WriteLine(sLine);
}
process.WaitForExit();
process.Close();
reader.Close();
WriteLine("Mission Ends");
WriteLine("<<<------------*------------");
}
catch (Exception e)
{
MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
WriteLine(e.Message);
Log.Loging.Error(e.Message);
}
}
在上述方法中,我们截获了Python代码执行过程中的输出,将其读取到界面输出中,避免弹出Python解释器窗口,造成观感上的混乱。
WriteLine方法用于输出日志到界面上。为解决在界面控件线程以外修改控件内容的带来的冲突,需要判断是否有必要使用委托来输出日志。WriteLine方法如下:
public delegate void DelegateWriteLine(string sMessage); //输出到窗口日志的委托
private void WriteLine(string sMessage)
{
if (InvokeRequired)
{
var logDelegate = new DelegateWriteLine(WriteLine);
Invoke(logDelegate, sMessage);
}
else
{
Log.Loging.Info(sMessage);
if (textBoxOutput.GetLineFromCharIndex(textBoxOutput.Text.Length) > 100)
textBoxOutput.Clear();
textBoxOutput.AppendText(DateTime.Now.ToString("HH:mm:ss ") + sMessage + "\r\n");
textBoxOutput.ScrollToCaret();
}
}
三种模式压缩
public void CompressNormally()
{
ExecuteMission(@"PyScript\CompressNormally.py");
}
public void CompressAutomatically()
{
ExecuteMission(@"PyScript\CompressAutomatically.py",
string.Format("\"{0}\" \"{1}\"",
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictDefinition),
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictResolution)));
}
public void CompressCompletely()
{
ExecuteMission(@"PyScript\CompressCompletely.py",
string.Format("\"{0}\" \"{1}\"",
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictDefinition),
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictResolution)));
}
内容总结
以上是互联网集市为您收集整理的一个使用C#和ArcPy实现的版本压缩工具(三)全部内容,希望文章能够帮你解决一个使用C#和ArcPy实现的版本压缩工具(三)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。