Author Topic: Oh the joys of a "high level" language!  (Read 8158 times)

0 Members and 1 Guest are viewing this topic.

Chuck Gabriel

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #15 on: May 16, 2008, 07:14:28 AM »
Chuck,

Similar short of thing :) happened in your 'C# HotKeys' thread if memory serves and the solution then was a cast to short as well.

Glad it worked for you and good to see you having a dabble again.

Cheers,
Glenn.

You have an excellent memory.  I wasn't going to bring that up, because it makes me feel like a complete schmuck that I missed the exact same thing again. Apparently I have some sort of subconscious prejudice against shorts.  :D

Chuck Gabriel

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #16 on: May 19, 2008, 10:45:51 PM »
Here is the final code I ended up with if anybody is interested:

Code: [Select]
using System;
using System.IO;
using System.Text;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;

using ofdFlags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags;
using sfdFlags = Autodesk.AutoCAD.Windows.SaveFileDialog.SaveFileDialogFlags;

namespace CGabriel
{
    public class LispFileDialogs
    {
        public static string title;
        public static string defaultFileName;
        public static string defaultExtension;
        public static short flags;

        [LispFunction("GetOpenFileDialog")]
        public static ResultBuffer GetOpenFileDialog(ResultBuffer args)
        {
            if (!parseArguments(args)) return null;

            ofdFlags dlgFlags = (ofdFlags)flags;
            if (((dlgFlags & ofdFlags.DefaultIsFolder) != 0) && Path.HasExtension(defaultFileName))
                defaultFileName = Path.GetDirectoryName(defaultFileName);

            OpenFileDialog dlg = new OpenFileDialog(title, defaultFileName, defaultExtension, title, dlgFlags);
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return null;

            ResultBuffer result = new ResultBuffer();
            foreach (string file in dlg.GetFilenames())
                result.Add(new TypedValue((int)LispDataType.Text, file));
            return result;
        }

        [LispFunction("GetSaveFileDialog")]
        public static TypedValue GetSaveFileDialog(ResultBuffer args)
        {
            if (!parseArguments(args))
                return new TypedValue((int)LispDataType.Nil, null);
         
            sfdFlags dlgFlags = (sfdFlags)flags;
            if (((dlgFlags & sfdFlags.DefaultIsFolder) != 0) && Path.HasExtension(defaultFileName))
                defaultFileName = Path.GetDirectoryName(defaultFileName);

            SaveFileDialog dlg = new SaveFileDialog(title, defaultFileName, defaultExtension, title, dlgFlags);
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return new TypedValue((int)LispDataType.Nil, null);

            return new TypedValue((int)LispDataType.Text, dlg.Filename);
        }

        public static bool parseArguments(ResultBuffer args)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            if (args == null)
                return notEnoughArguments(ed);

            ResultBufferEnumerator iter = args.GetEnumerator();

            iter.MoveNext();
            if (iter.Current.TypeCode != (short)LispDataType.Text)
                return wrongArguments(ed);
            title = (string)iter.Current.Value;

            if (iter.MoveNext() == false)
                return notEnoughArguments(ed);
            if (iter.Current.TypeCode != (short)LispDataType.Text)
                return wrongArguments(ed);
            defaultFileName = (string)iter.Current.Value;

            if (iter.MoveNext() == false)
                return notEnoughArguments(ed);
            if (iter.Current.TypeCode != (short)LispDataType.Text)
                return wrongArguments(ed);
            defaultExtension = (string)iter.Current.Value;

            if (iter.MoveNext() == false)
                return notEnoughArguments(ed);
            if (iter.Current.TypeCode != (short)LispDataType.Int16)
                return wrongArguments(ed);
            flags = (short)iter.Current.Value;

            return true;
        }

        public static bool notEnoughArguments(Editor ed)
        {
            ed.WriteMessage("\nToo few arguments.");
            return false;
        }

        public static bool wrongArguments(Editor ed)
        {
            ed.WriteMessage("\nExpected string string string int.");
            return false;
        }
    }
}

There are a couple of reasons I did this.

1) I've had getfiled return bogus results.
2) It wanted the ability to select multiple files like dos_getfilem, but with the drawing preview in the file dialog like getfiled.

I hope somebody finds it useful.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Oh the joys of a "high level" language!
« Reply #17 on: May 20, 2008, 02:57:06 AM »
Very nice Chuck!

Glenn R

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #18 on: May 20, 2008, 03:05:03 AM »
Well done old chap!

Chuck Gabriel

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #19 on: May 20, 2008, 07:38:27 AM »
Thanks guys.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Oh the joys of a "high level" language!
« Reply #20 on: May 20, 2008, 07:48:04 AM »
Nice Chuck.
Looks like Lisp is going to get a helping hand as NET gets more use.


OT
Quote
Well done old chap!

very British !
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #21 on: May 20, 2008, 07:50:23 AM »
Hehe...when in Rome Kerry... :)

Glenn R

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #22 on: May 20, 2008, 07:52:16 AM »
Actually, I'll be in Italy tomorrow...

BillZndl

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #23 on: September 27, 2011, 02:20:02 PM »
Thanks Gab!

This solves my problem of the Autolisp Getfiled function not working properly in 64 bit.




BillZndl

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #24 on: July 26, 2012, 08:04:32 AM »
Thanks Gab!

This solves my problem of the Autolisp Getfiled function not working properly in 64 bit.

I have found one problem with the OpenFileDialog Lisp Function.

When using inside my lisp programs, the OpenFileDialog will open to a directory other than the one that's input.
It appears to open to the previously selected directory instead of the designated input string.

example:

(setq dwgnm (GetOpenFileDialog "Hello" "G:\\2013\\Options\\"  "dwg" 2)
  )
will open to: "G:\\2013\\Options\\Outfitter\\".
Even if I change the input string to just "G:\\" it will do the same thing.

The OpenSaveDialog does not exhibit the same behavior curiously enough.




MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Oh the joys of a "high level" language!
« Reply #25 on: July 26, 2012, 12:37:13 PM »
The fourth parameter of OpenFileDialog is the name AutoCAD uses to store the screen location and last opened directory from within that dialog.  Every time you use that name you get the default location and folder that AutoCAD used last time when using that dialog.
Revit 2019, AMEP 2019 64bit Win 10

BillZndl

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #26 on: July 30, 2012, 10:45:05 AM »
Every time you use that name you get the default location and folder that AutoCAD used last time when using that dialog.

Thanks for the reply.

So the dialog ignores the second argument completely?
Any work arounds on this one?


MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Oh the joys of a "high level" language!
« Reply #27 on: July 30, 2012, 11:14:53 AM »
Not that I've tried.  I've assumed AutoCAD stores these values in the Registry. You might try looking there to see if you can overwrite or delete these values.
Revit 2019, AMEP 2019 64bit Win 10