【环境】CAD2014+VS2010
1 using System.IO; 2 using System.Windows.Forms; 3 using Autodesk.AutoCAD.DatabaseServices; 4 using Autodesk.AutoCAD.EditorInput; 5 using Autodesk.AutoCAD.Runtime; 6 using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application; 7 using OpenFileDialog = Autodesk.AutoCAD.Windows.OpenFileDialog; 8 using SaveFileDialog = Autodesk.AutoCAD.Windows.SaveFileDialog; 9 using ofdFlags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags; 10 using sfdFlags = Autodesk.AutoCAD.Windows.SaveFileDialog.SaveFileDialogFlags; 11 12 namespace CGabriel 13 { 14 public class LispFileDialogs 15 { 16 public static string title;//对话框标题 17 public static string defaultFileName;//默认路径 18 public static string defaultExtension;//拓展名 19 public static short flags;// 20 //(setq dwgnm (GetOpenFileDialog "Hello" "D:\\test\\" "dwg" 2)) 21 [LispFunction("GetOpenFileDialog")] 22 public static ResultBuffer GetOpenFileDialog(ResultBuffer args) 23 { 24 if (!parseArguments(args)) return null; 25 26 var dlgFlags = (ofdFlags) flags; 27 if (((dlgFlags & ofdFlags.DefaultIsFolder) != 0) && Path.HasExtension(defaultFileName)) 28 defaultFileName = Path.GetDirectoryName(defaultFileName); 29 30 var dlg = new OpenFileDialog(title, defaultFileName, defaultExtension, title, dlgFlags); 31 if (dlg.ShowDialog() != DialogResult.OK) 32 return null; 33 34 var result = new ResultBuffer(); 35 foreach (string file in dlg.GetFilenames()) 36 result.Add(new TypedValue((int) LispDataType.Text, file)); 37 return result; 38 } 39 40 [LispFunction("GetSaveFileDialog")] 41 public static TypedValue GetSaveFileDialog(ResultBuffer args) 42 { 43 if (!parseArguments(args)) 44 return new TypedValue((int) LispDataType.Nil, null); 45 46 var dlgFlags = (sfdFlags) flags; 47 if (((dlgFlags & sfdFlags.DefaultIsFolder) != 0) && Path.HasExtension(defaultFileName)) 48 defaultFileName = Path.GetDirectoryName(defaultFileName); 49 50 var dlg = new SaveFileDialog(title, defaultFileName, defaultExtension, title, dlgFlags); 51 if (dlg.ShowDialog() != DialogResult.OK) 52 return new TypedValue((int) LispDataType.Nil, null); 53 54 return new TypedValue((int) LispDataType.Text, dlg.Filename); 55 } 56 57 public static bool parseArguments(ResultBuffer args) 58 { 59 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 60 if (args == null) 61 return notEnoughArguments(ed); 62 63 ResultBufferEnumerator iter = args.GetEnumerator(); 64 65 iter.MoveNext(); 66 if (iter.Current.TypeCode != (short) LispDataType.Text) 67 return wrongArguments(ed); 68 title = (string) iter.Current.Value; 69 70 if (iter.MoveNext() == false) 71 return notEnoughArguments(ed); 72 if (iter.Current.TypeCode != (short) LispDataType.Text) 73 return wrongArguments(ed); 74 defaultFileName = (string) iter.Current.Value; 75 76 if (iter.MoveNext() == false) 77 return notEnoughArguments(ed); 78 if (iter.Current.TypeCode != (short) LispDataType.Text) 79 return wrongArguments(ed); 80 defaultExtension = (string) iter.Current.Value; 81 82 if (iter.MoveNext() == false) 83 return notEnoughArguments(ed); 84 if (iter.Current.TypeCode != (short) LispDataType.Int16) 85 return wrongArguments(ed); 86 flags = (short) iter.Current.Value; 87 88 return true; 89 } 90 91 public static bool notEnoughArguments(Editor ed) 92 { 93 ed.WriteMessage("\nToo few arguments."); 94 return false; 95 } 96 97 public static bool wrongArguments(Editor ed) 98 { 99 ed.WriteMessage("\nExpected string string string int."); 100 return false; 101 } 102 } 103 }