public static class EditorExtensions
{
/// <summary>
/// Private field holding the Environment's newline ("/r/n")
/// </summary>
private static string newLine = Environment.NewLine;
/// <summary>
/// Private field holding IFormatProvider
/// </summary>
private static IFormatProvider _formatProvider;
/// <summary>
/// Property to get current IFormatProvider
/// </summary>
private static IFormatProvider formatProvider
{
get
{
if (_formatProvider == null)
{
_formatProvider = Thread.CurrentThread.CurrentCulture;
}
return _formatProvider;
}
}
/// <summary>
/// Writes the specified subarray of Unicode characters to the CommandLine
/// </summary>
/// <param name="ed">Extension method of Editor</param>
/// <param name="buffer">An array of Unicode characters.</param>
/// <param name="index">The starting position in buffer.</param>
/// <param name="count">The number of characters to write.</param>
/// <exception cref="System.ArgumentNullException">Thrown if buffer is null</exception>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown if index is less than zero.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown if count is less than zero.</exception>
/// <exception cref="System.ArgumentException">Thrown if index plus count specify a position that is not within buffer.</exception>
public static void Write(this Editor ed, char[] buffer, int index, int count)
{
buffer.ThrowIfNull("buffer");
index.ThrowIfLessThanZero("index");
count.ThrowIfLessThanZero("count");
if ((buffer.Length - index) < count)
{
throw new ArgumentException
("Buffer Invalid Length"); }
for (int i = 0; i < count; i++)
{
ed.Write(buffer[index + i]);
}
}
/// <summary>
/// Writes the specified subarray of Unicode characters to the CommandLine
/// </summary>
/// <param name="ed">Extension method of Editor</param>
/// <param name="buffer"> A Unicode character array.</param>
public static void Write(this Editor ed, char[] buffer)
{
ed.Write(buffer, 0, buffer.Length);
}
/// <summary>
/// Writes the text representation of the specified Boolean value to the CommandLine
/// </summary>
/// <param name="ed">Extension method of Editor</param>
/// <param name="value">The value to write.</param>
public static void Write(this Editor ed, bool value)
{
ed.WriteMessage(value ? "True" : "False");
}
/// <summary>
/// Writes the specified Unicode character value to the CommandLine
/// </summary>
/// <param name="ed">Extension method of Editor</param>
/// <param name="value">The value to write.</param>
public static void Write(this Editor ed, char value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, decimal value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, double value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, int value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, Int64 value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, Point2d value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, Point3d value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, object value)
{
if (!value.IsNull())
{
IFormattable formattable = value as IFormattable;
if (!formattable.IsNull())
{
ed.WriteMessage(formattable.ToString(null, formatProvider));
}
else
{
ed.Write(value.ToString());
}
}
}
public static void Write(this Editor ed, Single value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, string value)
{
if (!value.IsNullOrEmpty())
{
ed.WriteMessage(value);
}
}
//public static void Write(this Editor ed, UInt32 value)
//{
// ed.WriteMessage(value.ToString(formatProvider));
//}
//public static void Write(this Editor ed, UInt64 value)
//{
// ed.WriteMessage(value.ToString(formatProvider));
//}
public static void Write(this Editor ed, ObjectId value)
{
ed.WriteMessage(value.ToString(formatProvider));
}
public static void Write(this Editor ed, string format, object arg0)
{
ed
.WriteMessage(string.Format(formatProvider, format,
new object[] { arg0
})); }
public static void Write(this Editor ed, string format, params object[] arg)
{
ed.WriteMessage(string.Format(formatProvider, format, arg));
}
public static void Write(this Editor ed, string format, object arg0, object arg1)
{
ed
.WriteMessage(string.Format(formatProvider, format,
new object[] { arg0, arg1
})); }
public static void Write(this Editor ed, string format, object arg0, object arg1, object arg2)
{
ed
.WriteMessage(string.Format(formatProvider, format,
new object[] { arg0, arg1, arg2
})); }
public static void WriteLine(this Editor ed)
{
ed.WriteMessage(newLine);
}
public static void WriteLine(this Editor ed, char[] buffer, int index, int count)
{
ed.Write(buffer, index, count);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, char[] buffer)
{
ed.Write(buffer);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, bool value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, char value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, decimal value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, double value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, int value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, Int64 value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, Point2d value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, Point3d value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, object value)
{
if (value.IsNull())
{
ed.WriteLine();
}
else
{
IFormattable formattable = value as IFormattable;
if (!formattable.IsNull())
{
ed.Write(formattable.ToString(null, formatProvider));
}
else
{
ed.WriteMessage(value.ToString());
}
ed.WriteLine();
}
}
public static void WriteLine(this Editor ed, Single value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, string value)
{
ed.Write(value);
ed.WriteLine();
}
//public static void WriteLine(this Editor ed, UInt32 value)
//{
// ed.Write(value);
// ed.WriteLine();
//}
//public static void WriteLine(this Editor ed, UInt64 value)
//{
// ed.Write(value);
// ed.WriteLine();
//}
public static void WriteLine(this Editor ed, ObjectId value)
{
ed.Write(value);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, string format, object arg0)
{
ed.Write(format, arg0);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, string format, params object[] arg)
{
ed.Write(format, arg);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, string format, object arg0, object arg1)
{
ed.Write(format, arg0, arg1);
ed.WriteLine();
}
public static void WriteLine(this Editor ed, string format, object arg0, object arg1, object arg2)
{
ed.Write(format, arg0, arg1, arg2);
ed.WriteLine();
}
public static void PrintItems<T>(this Editor ed, IEnumerable<T> items)
{
foreach (var item in items)
{
ed.WriteLine(item);
}
}
public static void SendPause(this Editor ed)
{
Command.Pause.Execute();
}
public static int ExecuteCommand(this Editor ed, Command cmd, bool waitForCommandToEnd = false)
{
return cmd.Execute(waitForCommandToEnd);
}
public static int ExecuteCommand(this Editor ed, bool waitForCommandToEnd, string commandName, params object[] args)
{
Command cmd
= new Command
(commandName, args
); return cmd.Execute(waitForCommandToEnd);
}
public static bool IsCommandActive(this Editor ed, string commandName)
{
string names = (string)Application.GetSystemVariable(("CMDNAMES"));
return names.ToUpper().IndexOf(commandName) >= 0;
}
}