TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CAB on January 28, 2007, 09:12:18 AM

Title: OpenFileDialog
Post by: CAB on January 28, 2007, 09:12:18 AM
To continue a thread here that I started in the net forum, here (http://www.theswamp.org/index.php?topic=14711.0)  I am looking for a was in visual lisp to evoke the windows dialog box below with the ability to select multiple files.
I am not sure of the method needed or how to set it up.
But if you can do this to browse for folders, why can't you Browse for files?
Code: [Select]
(setq shell (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
(setq folder
(vlax-invoke-method
shell
'BrowseForFolder
0
message
512
path
)
)
<edit: spelling>
Title: Re: OpenFileDialog
Post by: Patrick_35 on January 28, 2007, 03:04:32 PM
Hi
Try this
Code: [Select]
(defun FileBox(/ cdl f)
;OFN_READONLY &H1       1 La case 'Lecture seule' est cochée à la création de la fenêtre.
;OFN_OVERWRITEPROMPT &H2       2 Afficher un message de confirmation d'écrasement de fichier si celui-ci existe déjà.
;OFN_HIDEREADONLY &H4       4 Case à cocher 'Lecture seule' invisible.
;OFN_NOCHANGEDIR &H8       8 Conserve le répertoire d'origine à la fermeture de la fenêtre.
;OFN_SHOWHELP &H10      16 Afficher le bouton 'Aide' dans la boîte de dialogue.
;OFN_NOVALIDATE &H100     256 Ne vérifie pas la validité de la saisie (validité du nom de fichier).
;OFN_ALLOWMULTISELECT &H200     512 Autoriser la sélection multiple de fichiers.
;OFN_EXTENSIONDIFFERENT &H400    1024 Indique que l'utilisateur a choisi une extension différente de celle par défaut.
;OFN_PATHMUSTEXIST &H800    2048 Les chemins et fichiers saisis doivent exister.
;OFN_FILEMUSTEXIST &H1000    4096 Seuls des fichiers existants peuvent être saisis.
;OFN_CREATEPROMPT &H2000    8192 Afficher une fenêtre de confirmation de création de fichier.
;OFN_SHAREAWARE &H4000   16384 Ignorer les erreurs de partage réseau.
;OFN_NOREADONLYRETURN &H8000   32768 Ne sélectionne pas la case à cocher 'Lecture seule'.
;OFN_NOTESTFILECREATE &H10000   65536 Le fichier ne sera pas créé avant la fermeture de la fenêtre.
;OFN_NONETWORKBUTTON &H20000 131072 Cache (désactive) le bouton 'Réseau'.
;OFN_NOLONGNAMES &H40000 262144 Utilise les noms courts de fichier (sans effet dans le cas des fenêtres du type 'Explorer').

;OFN_EXPLORER &H80000 524288 Donne un style 'Explorer' à la boîte de dialogue (par défaut).
; Qui ne fonctionne apparement pas sous XP

;OFN_LONGNAMES &H200000 2097152 Gestion des noms longs pour les boîtes de dialogue n'ayant pas le style 'Explorer'.
;OFN_NODEREFERENCELINKS &H100000 1048576 La boîte de dialogue prendra le nom et le chemin du raccourci sélectionné.

  (setq cdl (vlax-create-object "userAccounts.CommonDialog"))
  (vlax-put-property cdl 'filter (vlax-make-variant "Fichiers dessins (*.dwg)| *.dwg |Fichiers DXF (*.dwf) |Tous les fichiers (*.*)|*.*"))
  (vlax-put-property cdl 'filterindex 1)
  (vlax-put-property cdl 'flags (+ 4 8 512 2048 4096 131072 2097152))
  (vlax-put-property cdl 'initialdir (getvar "dwgprefix"))
  (if (eq (vlax-invoke cdl 'showopen) -1)
    (setq f (vlax-get-property cdl 'filename))
    (setq f nil)
  )
  (vlax-release-object cdl)
  f
)

ps : sorry for the language. It's in french

@+


<edit: Fixed Code tags>
Title: Re: OpenFileDialog
Post by: CAB on January 28, 2007, 03:39:58 PM
Yes, I expected that to work but my Windows2000 does not have "comdlg32.dll" or "comdlg.dll"
Is that file only added when you use NET or Visual basic?
Title: Re: OpenFileDialog
Post by: Keith™ on January 28, 2007, 04:15:04 PM
CAB,
all windows installations have comdlg, but the file may be a different name. To determine if you can create that dialog, try to create it from the class name ..
Code: [Select]
(setq dialog (vlax-create-object "MSComDlg.CommonDialog"))

To determine which values you need to set, you can refer to the constants provided by patrick, but some of the variable names are different depending upon the version of Windows you have.
To get the correct names, just dump the object

Code: [Select]
(vlax-dump-object dialog)

You will now be able to see the values you need to set.

I have revised the code provided by Patrick to work on my system (along with a couple of code corrections)
Code: [Select]
(defun FileBox(/ cdl f)
;OFN_READONLY &H1       1 La case 'Lecture seule' est cochée à la création de la fenêtre.
;OFN_OVERWRITEPROMPT &H2       2 Afficher un message de confirmation d'écrasement de fichier si celui-ci existe déjà.
;OFN_HIDEREADONLY &H4       4 Case à cocher 'Lecture seule' invisible.
;OFN_NOCHANGEDIR &H8       8 Conserve le répertoire d'origine à la fermeture de la fenêtre.
;OFN_SHOWHELP &H10      16 Afficher le bouton 'Aide' dans la boîte de dialogue.
;OFN_NOVALIDATE &H100     256 Ne vérifie pas la validité de la saisie (validité du nom de fichier).
;OFN_ALLOWMULTISELECT &H200     512 Autoriser la sélection multiple de fichiers.
;OFN_EXTENSIONDIFFERENT &H400    1024 Indique que l'utilisateur a choisi une extension différente de celle par défaut.
;OFN_PATHMUSTEXIST &H800    2048 Les chemins et fichiers saisis doivent exister.
;OFN_FILEMUSTEXIST &H1000    4096 Seuls des fichiers existants peuvent être saisis.
;OFN_CREATEPROMPT &H2000    8192 Afficher une fenêtre de confirmation de création de fichier.
;OFN_SHAREAWARE &H4000   16384 Ignorer les erreurs de partage réseau.
;OFN_NOREADONLYRETURN &H8000   32768 Ne sélectionne pas la case à cocher 'Lecture seule'.
;OFN_NOTESTFILECREATE &H10000   65536 Le fichier ne sera pas créé avant la fermeture de la fenêtre.
;OFN_NONETWORKBUTTON &H20000 131072 Cache (désactive) le bouton 'Réseau'.
;OFN_NOLONGNAMES &H40000 262144 Utilise les noms courts de fichier (sans effet dans le cas des fenêtres du type 'Explorer').

;OFN_EXPLORER &H80000 524288 Donne un style 'Explorer' à la boîte de dialogue (par défaut).
; Qui ne fonctionne apparement pas sous XP

;OFN_LONGNAMES &H200000 2097152 Gestion des noms longs pour les boîtes de dialogue n'ayant pas le style 'Explorer'.
;OFN_NODEREFERENCELINKS &H100000 1048576 La boîte de dialogue prendra le nom et le chemin du raccourci sélectionné.

  (setq cdl (vlax-create-object "MSComDlg.CommonDialog"))
  (vlax-put-property cdl 'filter (vlax-make-variant "AutoCAD Drawing (*.dwg)| *.dwg |Drawing eXchange File DXF (*.dwf) |All Files (*.*)|*.*"))
  (vlax-put-property cdl 'filterindex 1)
  (vlax-put-property cdl 'flags (+ 4 8 512 2048 4096 131072 2097152))
  (vlax-put-property cdl 'initdir (getvar "dwgprefix"))
  (vlax-put-property cdl 'maxfilesize 1024)
  (if (eq (vlax-invoke cdl 'showopen) -1)
    (setq f nil)
    (setq f (vlax-get-property cdl 'filename))
  )
  (vlax-release-object cdl)
  f
)
Title: Re: OpenFileDialog
Post by: Patrick_35 on January 28, 2007, 07:31:54 PM
Sorry Keith
But comdlg don't work with Windows XP  and A2000 :-(
(setq cdl (vlax-create-object "MSComDlg.CommonDialog")) return nil

@+
Title: Re: OpenFileDialog
Post by: CAB on January 28, 2007, 08:17:34 PM
Shoot, it returns nil here as well. :-(
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 08:35:15 AM
Well ... I have just tried it again, this time on XP SP2 ... so far I have been able to run it on Win98, WinME, WinXP SP1, and WinXP SP2 ... 4 different computers 2 with XP.

There must be something different in your setup preventing it from being created.

Open the registry, select hkey_classes_root then scroll down and see if you can find "MSComDlg.CommonDialog"
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 08:55:50 AM
OK, found this:
The {xxxx-xxxx-xxx...} are deferent data values.
Code: [Select]
/CLSID/{XXX-XXX-XXXX...}
/MSComDlg.CommonDialog ->Microsoft ...., version 6.0 (SP6)
.../CLSID  -> {xxx-xxxxx-xxx...}
.../CurVer -> MSComDlg.CommonDialog.1


/MSComDlg.CommonDialog.1 -> {xxx-xxxxx-xxx...}
.../CLSID  -> {xxx-xxxxx-xxx...}

PS I'm runnung Windows 2000
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 08:59:50 AM
Try changing the creation object to "MSComDlg.CommonDialog.1"
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 09:08:22 AM
No Joy.
Code: [Select]
_$ (setq dialog (vlax-create-object "MSComDlg.CommonDialog"))
nil
_$ (setq dialog (vlax-create-object "MSComDlg.CommonDialog.1"))
nil
_$ (setq dialog (vlax-create-object "MSComDlg.CommonDialog"))
nil
_$
Title: Re: OpenFileDialog
Post by: Fatty on January 29, 2007, 09:46:29 AM
Alan, agree with you
Tested in A2005 eng
Not works for me also
Maybe need to register this component before?

~'J'~
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 09:57:38 AM
If it is in the registry, then it is already registered.

If you do a search for ComDlg32.ocx do you find anything?
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 10:28:46 AM
Thanks for the help Keith.
This is what I found.
Don't know why I didn't find it on a previous search.
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 10:33:02 AM
Just tried in ACAD2004 & nil was returned.  :-(
Code: [Select]
(setq dialog (vlax-create-object "MSComDlg.CommonDialog"))
(setq dialog (vlax-create-object "MSComDlg.CommonDialog.1"))
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 10:34:09 AM
I cannot fathom why it will not allow you to create the window ... have you tried re-registering the dll or OCX with regsvr32 ?
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 11:55:18 AM
CAB ...

I tried something here in this VLX .. let me know if it works ...

<REMOVED ATTACHMENT -- SEE LATER POST>
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 02:29:09 PM
I regerestered the ocx & then tried this
Code: [Select]
_$ (setq dialog (vlax-create-object "MSComDlg.CommonDialog"))
nil
_$ (setq dialog (vlax-create-object "MSComDlg.CommonDialog.1"))
nil
_$

I then tried to register the dll & got this
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 02:55:02 PM
Cab, try the VLX in the previous post to see if it provides the window, if it does, then I have a sneaking suspicion that the error is related to the design time license I have with the CommDlg32.ocx. Since I have the developer edition of VB both here and at home that may be the defining factor. Either way, if the above code works, it "should" grant you rights to use the dialog based on my giving you rights through my license ... i.e. the type library is on your computer, but the license to use it must come from a licensed developer .. in otherwords I can distribute an application that uses the comdlg window, but I can't distribute the type library and by my distribution of this application in the compiled format, it should .. theoretically give you permissions enough to create the window ... I don't know if it extends past the compiled code .. for example any code you might write.
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 03:25:00 PM
Loaded it, used Start CommonDialog, Here is what I get:
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 03:27:49 PM
WELL DAGNABIT ... The command is (commondialog) .. forgot to mention that ...

It should work ... back to the drawing board ..
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 03:32:47 PM
Code: [Select]
Command: (commondialog)

Error: too many arguments
:-o
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 03:36:49 PM
A different error? Now this is really getting intriguing ...
Title: Re: OpenFileDialog
Post by: Fatty on January 29, 2007, 03:58:52 PM
Code: [Select]
Command: (commondialog)

Error: too many arguments
:-o
Alan
I've got the similar one:
Code: [Select]
Command: (commondialog (getvar "dwgprefix"))
; error: too many arguments
Command: CommonDialog
Unknown command "COMMONDIALOG".  Press F1 for help
Command: (CommonDialog)
; error: too many arguments

~'J'~
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 04:15:42 PM
Ok .. try this one then ...

Same scenario ...

(commondialog) to run
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 05:23:26 PM
 :-(
Code: [Select]
Command: (commondialog)
; error: bad argument type: VLA-OBJECT nil
; reset after error
Title: Re: OpenFileDialog
Post by: Keith™ on January 29, 2007, 05:59:26 PM
well ... darn ... I know I am sending the license through ... I have just never had to do it through lisp before ... I need to do a little more research ...
Title: Re: OpenFileDialog
Post by: CAB on January 29, 2007, 06:47:48 PM
Thnaks for your time Keith.
Title: Re: OpenFileDialog
Post by: ELOQUINTET on January 30, 2007, 08:58:44 AM
cab i've been watching what's going on in this thread and am wondering is this what you had in mind for the plottabs script we were discussing last week. so the main goal here is to be able to open multiple files from multiple folders? just curious
Title: Re: OpenFileDialog
Post by: CAB on January 30, 2007, 09:34:01 AM
Yes, but a useful tool for other lisp as well.
Here is the alternative routine.
Code: [Select]
;;  Get user pick of files, one at a time
(defun c:test (/ file files)
 
  (if (setq file (getfiled "Select a file to include in Batch" "" "dwg" 128))
    (while
      (setq files (cons file files)
            file  (getfiled "Select another file or Cancel to quit" file "dwg" 128)
      )
    )
    (princ "\nNo files were selected.")
  )
 
  (and files (princ files))
  (princ)
)
Title: Re: OpenFileDialog
Post by: hendie on January 30, 2007, 10:53:33 AM
CAB you could smudge it by using the Browse for folders in your first post...
use the browse for folder to *ahem* browse for folders, then add a listbox to the form that displays all the files in the selected folder, and use that listbox to let the user select multiple files.

fwiw, I have the three controls on my system but still cannot use the commondialog and even stranger, I can bring in a routine from home utilising the common dialog and it works fine, but I cannot copy it across modules.
Title: Re: OpenFileDialog
Post by: CAB on January 30, 2007, 11:21:36 AM
Thanks hendie for the feed back.

I did consider that method but no easy way to get files from two different folders.
I could add a button to get another folder I suppose.
Title: Re: OpenFileDialog
Post by: T.Willey on January 30, 2007, 12:04:15 PM
CAB you could smudge it by using the Browse for folders in your first post...
use the browse for folder to *ahem* browse for folders, then add a listbox to the form that displays all the files in the selected folder, and use that listbox to let the user select multiple files.
Thanks hendie for the feed back.

I did consider that method but no easy way to get files from two different folders.
I could add a button to get another folder I suppose.
That is how I do it with my plot dialog box.  I have a button to open the BrowseForFolder dialog, then I add all the drawings in said folder to a list box, then add the selected ones to a list, path and all.
Title: Re: OpenFileDialog
Post by: GDF on January 30, 2007, 12:09:11 PM
Hi
Try this
Code: [Select]
(defun FileBox(/ cdl f)
;OFN_READONLY &H1       1 La case 'Lecture seule' est cochée à la création de la fenêtre.
;OFN_OVERWRITEPROMPT &H2       2 Afficher un message de confirmation d'écrasement de fichier si celui-ci existe déjà.
;OFN_HIDEREADONLY &H4       4 Case à cocher 'Lecture seule' invisible.
;OFN_NOCHANGEDIR &H8       8 Conserve le répertoire d'origine à la fermeture de la fenêtre.
;OFN_SHOWHELP &H10      16 Afficher le bouton 'Aide' dans la boîte de dialogue.
;OFN_NOVALIDATE &H100     256 Ne vérifie pas la validité de la saisie (validité du nom de fichier).
;OFN_ALLOWMULTISELECT &H200     512 Autoriser la sélection multiple de fichiers.
;OFN_EXTENSIONDIFFERENT &H400    1024 Indique que l'utilisateur a choisi une extension différente de celle par défaut.
;OFN_PATHMUSTEXIST &H800    2048 Les chemins et fichiers saisis doivent exister.
;OFN_FILEMUSTEXIST &H1000    4096 Seuls des fichiers existants peuvent être saisis.
;OFN_CREATEPROMPT &H2000    8192 Afficher une fenêtre de confirmation de création de fichier.
;OFN_SHAREAWARE &H4000   16384 Ignorer les erreurs de partage réseau.
;OFN_NOREADONLYRETURN &H8000   32768 Ne sélectionne pas la case à cocher 'Lecture seule'.
;OFN_NOTESTFILECREATE &H10000   65536 Le fichier ne sera pas créé avant la fermeture de la fenêtre.
;OFN_NONETWORKBUTTON &H20000 131072 Cache (désactive) le bouton 'Réseau'.
;OFN_NOLONGNAMES &H40000 262144 Utilise les noms courts de fichier (sans effet dans le cas des fenêtres du type 'Explorer').

;OFN_EXPLORER &H80000 524288 Donne un style 'Explorer' à la boîte de dialogue (par défaut).
; Qui ne fonctionne apparement pas sous XP

;OFN_LONGNAMES &H200000 2097152 Gestion des noms longs pour les boîtes de dialogue n'ayant pas le style 'Explorer'.
;OFN_NODEREFERENCELINKS &H100000 1048576 La boîte de dialogue prendra le nom et le chemin du raccourci sélectionné.

  (setq cdl (vlax-create-object "userAccounts.CommonDialog"))
  (vlax-put-property cdl 'filter (vlax-make-variant "Fichiers dessins (*.dwg)| *.dwg |Fichiers DXF (*.dwf) |Tous les fichiers (*.*)|*.*"))
  (vlax-put-property cdl 'filterindex 1)
  (vlax-put-property cdl 'flags (+ 4 8 512 2048 4096 131072 2097152))
  (vlax-put-property cdl 'initialdir (getvar "dwgprefix"))
  (if (eq (vlax-invoke cdl 'showopen) -1)
    (setq f (vlax-get-property cdl 'filename))
    (setq f nil)
  )
  (vlax-release-object cdl)
  f
)

ps : sorry for the language. It's in french

@+


<edit: Fixed Code tags>


Works for me here. Using windows xp and autocad 2007....but the  dialog box is in french.

Gary
Title: Re: OpenFileDialog
Post by: T.Willey on January 30, 2007, 12:48:23 PM
I was searching on the MSDN site and found this question.
Quote
Hey, Scripting Guy! Is there any way I can use a script to present a user with a dialog box and let him or her select a file?

-- BF
And this answer
Quote
Hey, BF. If you’re using Windows 2000, we don’t know of a way to do this, at least not a way that’s built into the operating system. That’s not the case with Windows XP, however. On Windows XP, you can use the UserAccounts.CommonDialog object to present users with a standard File Open dialog box. Here’s what the script looks like:
Here (http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan05/hey0128.mspx)

It doesn't look good for use Win2000 people Alan.  I will continue to look when I have free time.
Title: Re: OpenFileDialog
Post by: CAB on January 30, 2007, 12:51:01 PM
Oh well, at least I'll quit head butting that wall. :-)
Title: Re: OpenFileDialog
Post by: whdjr on January 30, 2007, 01:52:14 PM
I was searching on the MSDN site and found this question.
Quote
Hey, Scripting Guy! Is there any way I can use a script to present a user with a dialog box and let him or her select a file?

-- BF
And this answer
Quote
Hey, BF. If you’re using Windows 2000, we don’t know of a way to do this, at least not a way that’s built into the operating system. That’s not the case with Windows XP, however. On Windows XP, you can use the UserAccounts.CommonDialog object to present users with a standard File Open dialog box. Here’s what the script looks like:
Here (http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan05/hey0128.mspx)

It doesn't look good for use Win2000 people Alan.  I will continue to look when I have free time.
Once again this doesn't Alan, but Tim's previous post was for a single selection Dialog box, HERE (http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar05/hey0301.mspx) is for a multi-select Dialog Box.

Sorry Alan  :-(
Title: Re: OpenFileDialog
Post by: jbuzbee on January 30, 2007, 02:03:46 PM
 . . . good ole ObjectDCL . . .  :lol:
Title: Re: OpenFileDialog
Post by: T.Willey on January 30, 2007, 02:05:48 PM
. . . good ole ObjectDCL . . .  :lol:
Does ObjectDCL has the same limit as regural dcl?  Limited by the amout of items you can select, I think it's around 255 (Acads magic number).
Title: Re: OpenFileDialog
Post by: CADmium on August 26, 2009, 03:17:38 AM
Is there a flag for the UserAccounts.CommonDialog-Object to get a Filesaveas-dialog ?
Title: Re: OpenFileDialog
Post by: CADmium on August 26, 2009, 03:38:21 AM
ok .. found it ... look at the Object "SAFRCFileDlg.FileSave"