Originally Posted by
peter
I added them back into the example at the top.
What happens in the event of an error?
Originally Posted by
peter
Although the thread was more about how to do that with .net and not with the shell application.
Fair enough; here's an alternative that provides some native 'invalid argument' handling:
Code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System;
using System.IO;
using System.Windows.Forms;
[assembly: CommandClass(typeof(BlackBox.AutoCAD.FolderBrowser.Commands))]
namespace BlackBox.AutoCAD.FolderBrowser
{
public class Commands
{
private bool showNewFolderButton;
private DocumentCollection acDocs = acApp.DocumentManager;
private string selectedPath;
private string description;
private object Browse()
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.Description = description;
folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
folderBrowserDialog.SelectedPath = selectedPath;
folderBrowserDialog.ShowNewFolderButton = showNewFolderButton;
if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
return null;
return folderBrowserDialog.SelectedPath;
}
[LispFunction("FolderBrowser")]
public object FolderBrowser(ResultBuffer resbuf)
{
showNewFolderButton = false;
selectedPath = "C:\\";
description = "";
try
{
if (resbuf == null)
return Browse();
TypedValue[] args = resbuf.AsArray();
int i = args.Length;
if (args[0].TypeCode != (int)LispDataType.Text)
throw new ArgumentTypeException("stringp", args[0]);
selectedPath = (string)args[0].Value;
if (!Directory.Exists(selectedPath))
throw new DirectoryDoesNotExistException();
if (i >= 2)
{
if (args[1].TypeCode != (int)LispDataType.Text)
throw new ArgumentTypeException("stringp", args[1]);
description = (string)args[1].Value;
}
if (i == 3 && args[2].TypeCode != (int)LispDataType.Nil)
showNewFolderButton = true;
return Browse();
}
catch (LispException ex)
{
acDocs.MdiActiveDocument.Editor.WriteMessage(
"\n; error: LispException: " + ex.Message + "\n");
return null;
}
catch (System.Exception ex)
{
acDocs.MdiActiveDocument.Editor.WriteMessage(
"\n; error: System.Exception: " + ex.Message + "\n");
return null;
}
}
}
/// <summary>
/// Special thanks to Gile for his LispException classes:
/// </summary>
class LispException : System.Exception
{
public LispException(string msg) : base(msg) { }
}
class TooFewArgsException : LispException
{
public TooFewArgsException() : base("too few arguments") { }
}
class TooManyArgsException : LispException
{
public TooManyArgsException() : base("too many arguments") { }
}
class ArgumentTypeException : LispException
{
public ArgumentTypeException(string s, TypedValue tv)
: base(string.Format(
"invalid argument type: {0}: {1}",
s, tv.TypeCode == (int)LispDataType.Nil ? "nil" : tv.Value))
{ }
}
// Added by BlackBox
class DirectoryDoesNotExistException : LispException
{
public DirectoryDoesNotExistException() : base("directory does not exist") { }
}
}
Cheers