コード例 #1
0
        private void TryCalcMpu(string mapDef)
        {
            BusyWaitDialog.Run(Strings.CalculatingMpu, () =>
            {
                var currentPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
                var mpuCalc     = Path.Combine(currentPath, "AddIns/Local/MpuCalc.exe"); //NOXLATE
                if (!File.Exists(mpuCalc) && mapDef.EndsWith(ResourceTypes.MapDefinition.ToString()))
                {
                    int[] cmdTypes = m_connection.Capabilities.SupportedCommands;
                    if (Array.IndexOf(cmdTypes, (int)OSGeo.MapGuide.MaestroAPI.Commands.CommandType.CreateRuntimeMap) < 0)
                    {
                        IMapDefinition mdf = (IMapDefinition)m_connection.ResourceService.GetResource(mapDef);
                        var calc           = m_connection.GetCalculator();
                        return(new MpuCalcResult()
                        {
                            Method = MpuMethod.BuiltIn,
                            Result = Convert.ToDecimal(calc.Calculate(mdf.CoordinateSystem, 1.0))
                        });
                    }
                    else
                    {
                        ICreateRuntimeMap create = (ICreateRuntimeMap)m_connection.CreateCommand((int)OSGeo.MapGuide.MaestroAPI.Commands.CommandType.CreateRuntimeMap);
                        create.MapDefinition     = mapDef;
                        create.RequestedFeatures = (int)RuntimeMapRequestedFeatures.None;
                        var info = create.Execute();
                        return(new MpuCalcResult()
                        {
                            Method = MpuMethod.CreateRuntimeMap,
                            Result = Convert.ToDecimal(info.CoordinateSystem.MetersPerUnit)
                        });
                    }
                }
                else
                {
                    IResource res          = m_connection.ResourceService.GetResource(mapDef);
                    ITileSetDefinition tsd = res as ITileSetDefinition;
                    IMapDefinition mdf     = res as IMapDefinition;

                    string coordSys = null;
                    if (mdf != null)
                    {
                        coordSys = mdf.CoordinateSystem;
                    }
                    else if (tsd != null)
                    {
                        coordSys = tsd.GetDefaultCoordinateSystem();
                    }

                    string output = string.Empty;
                    if (coordSys != null)
                    {
                        var proc = new Process
                        {
                            StartInfo = new ProcessStartInfo
                            {
                                FileName               = mpuCalc,
                                Arguments              = coordSys,
                                UseShellExecute        = false,
                                RedirectStandardOutput = true,
                                CreateNoWindow         = true
                            }
                        };
                        proc.Start();
                        StringBuilder sb = new StringBuilder();
                        while (!proc.StandardOutput.EndOfStream)
                        {
                            string line = proc.StandardOutput.ReadLine();
                            // do something with line
                            sb.AppendLine(line);
                        }
                        output = sb.ToString();
                    }
                    double mpu;
                    if (double.TryParse(output, out mpu))
                    {
                        return new MpuCalcResult()
                        {
                            Method = MpuMethod.MpuCalcExe, Result = Convert.ToDecimal(mpu)
                        }
                    }
                    ;
                    else
                    {
                        return(string.Format(Strings.FailedToCalculateMpu, output));
                    }
                }
            }, (res, ex) =>
            {
                if (ex != null)
                {
                    ErrorDialog.Show(ex);
                }
                else
                {
                    var mres = res as MpuCalcResult;
                    if (mres != null)
                    {
                        MetersPerUnit.Value = mres.Result;
                        if (mres.Method == MpuMethod.BuiltIn)
                        {
                            MessageBox.Show(Strings.ImperfectMpuCalculation);
                        }
                    }
                    else
                    {
                        MessageBox.Show(res.ToString());
                    }
                }
            });
        }