using System;
using Autodesk.AutoCAD.DatabaseServices;
namespace Autodesk.AutoCAD.ApplicationServices.Core
{
public class SystemVariables
{
private static SystemVariables _current
= new SystemVariables
(); public static SystemVariables Current {get { return _current; } }
internal SystemVariables()
{
}
/// Would be nice to just let the name of the method be passed, and with .NET 4.5 a easy way
/// would be with [CallerMemberName] attribute
/// http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx
/// Requires .NET 4.5
////private T GetSystemVariable<T>([CallerMemberName] string name = null)
////{
//// return (T)this[name];
////}
private T GetSystemVariable<T>(string name)
{
return (T)this[name];
}
private void SetSystemVariable<T>(string name, T value)
{
this[name] = value;
}
private bool GetBoolSystemVariable(string name)
{
short val = GetSystemVariable<short>(name);
if (val == 1)
{
return true;
}
else
{
return false;
}
}
private void SetBoolSystemVariable(string name, bool value)
{
if (value)
{
SetSystemVariable<short>(name, 1);
}
else
{
SetSystemVariable<short>(name, 0);
}
}
public short CVPORT
{
get { return GetSystemVariable<short>("CVPORT"); }
set { SetSystemVariable<short>("CVPORT", value); }
}
public bool DWGTITLED
{
get { return GetBoolSystemVariable("DWGTITLED"); }
}
public bool UCSFOLLOW
{
get { return GetBoolSystemVariable("UCSFOLLOW"); }
set { SetBoolSystemVariable("UCSFOLLOW", value); }
}
public string CMDNAMES
{
get { return GetSystemVariable<string>("CMDNAMES"); }
}
/////// Think commands can have '(apostrophe) in them and would cause problem
// public ICollection<string> CMDNAMES
// {
// get { return GetSystemVariable<string>("CMDNAMES").Split('\''); }
// }
public string CTAB
{
get { return GetSystemVariable<string>("CTAB"); }
set { SetSystemVariable<string>("CTAB", value); }
}
public OsnapMode OSMODE
{
get { return GetSystemVariable<OsnapMode>("OSMODE"); }
set { SetSystemVariable<short>("OSMODE", (short)value); }
}
public virtual object this[string name]
{
get
{
if (name.IsNullOrWhiteSpace())
{
throw new ArgumentNullException
("{0} is Empty or Null", name
); }
try
{
return Application.GetSystemVariable(name);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
if (ex.ErrorStatus == Runtime.ErrorStatus.InvalidInput)
{
throw new ArgumentException
("InvalidName", name
); }
else
{
throw;
}
}
}
set
{
try
{
Application.SetSystemVariable(name, value);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
if (ex.ErrorStatus == Runtime.ErrorStatus.InvalidInput)
{
throw new ArgumentException
(String.Format("{0}:{1} is Invalid", name,
value)); }
throw;
}
}
}
}
}