TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Patrick_35 on February 23, 2007, 05:22:11 AM

Title: Download with vlisp
Post by: Patrick_35 on February 23, 2007, 05:22:11 AM
Hello,

How download a link without Dialog Box and without use SendKey ?

I try
Code: [Select]
(setq shell (vlax-create-object "Shell.Application"))
(vlax-invoke shell 'open "http://carnet-de-cablage.chez-alice.fr/Lisp/Rbloc.zip")
(vlax-release-object shell)

And
Code: [Select]
(setq ie (vlax-Create-Object "InternetExplorer.Application"))
(vlax-invoke ie 'navigate "http://carnet-de-cablage.chez-alice.fr/Lisp/Rbloc.zip")
(vlax-release-object ie)

but i have the dialog box  :-(

Thanks
Title: Re: Download with vlisp
Post by: ElpanovEvgeniy on February 23, 2007, 09:37:03 AM
see code VBA...
Save Internet File using XMLHTTP Object (http://vbaexpress.com/kb/getarticle.php?kb_id=799)
Title: Re: Download with vlisp
Post by: JohnK on February 23, 2007, 09:42:11 AM
What about ``GetRemoteFile''.

GetRemoteFile URL, LocalFile, IgnoreCache
Title: Re: Download with vlisp
Post by: Patrick_35 on February 23, 2007, 10:46:52 AM
Excel ElpanovEvgeniy: -)
I thus arrive at well translating into vlisp, but how to write the response into binary  :?

Code: [Select]
'Create local file and save results to it
    vFF = FreeFile
    If Dir(vLocalFile) <> "" Then Kill vLocalFile
    Open vLocalFile For Binary As #vFF
    Put #vFF, , oResp
    Close #vFF

Se7en, I will make a research with regard to ``GetRemoteFile''.

Thank you with you two  :-)

@+
Title: Re: Download with vlisp
Post by: Mark on February 23, 2007, 11:49:23 AM
This used to work ...

Code: [Select]
(defun mst-activate (/ key)
  (setq key (MST-GetFileFromURL
      "http://www.theswamp.org/lilly.pond/public/key.txt"
      )
)
  (if key
    (alert
      (strcat
"Thank you for being my beta tester\n Make note of this number -> "
key
)
      )
    )
  (princ)
  )


(defun MST-GetFileFromURL (strURL / fo key vlUtilObj RemoteFile TmpFile)
  (setq vlUtilObj (vla-get-Utility
    (vla-get-ActiveDocument (vlax-get-acad-object))
    )
)
  (if (and (= (type strURL) 'STR)
   (= (vla-IsURL vlUtilObj strURL) :vlax-true)
   )
    (setq RemoteFile
   (vl-catch-all-apply
     'vlax-invoke-method
     (list vlUtilObj 'GetRemoteFile strURL 'TmpFile :vlax-false)
     )
  )
    ) ; if
  (if (not (vl-catch-all-error-p RemoteFile))
    (if (setq fo (open TmpFile "r"))
      (if (setq key (read-line fo))
(close fo)
)
      )
    )
  (if (vl-file-delete TmpFile)
    key
    )
  )
(mst-activate)
Title: Re: Download with vlisp
Post by: Patrick_35 on February 26, 2007, 03:46:05 AM
Great Mark

Thank you very much  :-)

@+
Title: Re: Download with vlisp
Post by: Patrick_35 on February 26, 2007, 05:55:26 AM
For the pleasure, and in spite of the great solution of Mark, I continued with the ElpanovEvgeniy proposal but a problem arises. How to write in a file the character Null (chr 0) ?

Thanks in advance

@+
Title: Re: Download with vlisp
Post by: ElpanovEvgeniy on February 26, 2007, 06:34:20 AM
For the pleasure, and in spite of the great solution of Mark, I continued with the ElpanovEvgeniy proposal but a problem arises. How to write in a file the character Null (chr 0) ?

Thanks in advance

@+

Hi Patrick_35 :-)
This code writes to a file of 100 lines on 8 (chr 0)

Code: [Select]
(setq f(open "d:\\test.txt" "w"))
(repeat 100
 (WRITE-LINE (VL-LIST->STRING '(0 0 0 0 0 0 0 0)) f)
 )
(close f)
Title: Re: Download with vlisp
Post by: Patrick_35 on February 26, 2007, 08:10:20 AM
Thank you Evgeniy  :-)
I'm there almost what I don't understand, this is why when I open the zip and that I look at the files, those are empty  :?

Code: [Select]
(defun download (url dir / byte fic file http tbl)
  (setq http (vlax-create-object "MSXML2.XMLHTTP"))
  (vlax-invoke-method http 'open "get" url :vlax-false)
  (vlax-invoke http 'send)
  (while (not (eq (vlax-get http 'readyState) 4))
    (repeat 100)
  )
  (setq file (strcat dir (vl-filename-base url) (vl-filename-extension url))
tbl  (vlax-safearray->list (vlax-variant-value (vlax-get-property http 'responsebody)))
fic  (open file "w") n 0)
  (foreach byte tbl
    (if (eq byte 0)
      (princ (vl-list->string '(0)) fic)
      (write-char byte fic)
    )
  )
  (close fic)
  (vlax-release-object http)
  (princ)
)

Code: [Select]
(download "http://carnet-de-cablage.chez-alice.fr/Lisp/Latt.zip" "c:/")
@+
Title: Re: Download with vlisp
Post by: ElpanovEvgeniy on February 26, 2007, 08:43:47 AM
Thank you Evgeniy  :-)
I'm there almost what I don't understand, this is why when I open the zip and that I look at the files, those are empty  :?

Code: [Select]
(defun download (url dir / byte fic file http tbl)
  (setq http (vlax-create-object "MSXML2.XMLHTTP"))
  (vlax-invoke-method http 'open "get" url :vlax-false)
  (vlax-invoke http 'send)
  (while (not (eq (vlax-get http 'readyState) 4))
    (repeat 100)
  )
  (setq file (strcat dir (vl-filename-base url) (vl-filename-extension url))
tbl  (vlax-safearray->list (vlax-variant-value (vlax-get-property http 'responsebody)))
fic  (open file "w") n 0)
  (foreach byte tbl
    (if (eq byte 0)
      (princ (vl-list->string '(0)) fic)
      (write-char byte fic)
    )
  )
  (close fic)
  (vlax-release-object http)
  (princ)
)

Code: [Select]
(download "http://carnet-de-cablage.chez-alice.fr/Lisp/Latt.zip" "c:/")
@+

Hello Patrick.
On my computer, this code works! :)
Title: Re: Download with vlisp
Post by: ElpanovEvgeniy on February 26, 2007, 09:31:28 AM
  (while (not (eq (vlax-get http 'readyState) 4))
    (repeat 100)
  )

If express established, such variant will load less processor...

Code: [Select]
(while (not (eq (vlax-get http 'readyState) 4))
   (if acet-sys-sleep
     (acet-sys-sleep 5);;(acet-sys-sleep [time-pause-msec])
    (repeat 100)
    )
  )
Title: Re: Download with vlisp
Post by: Patrick_35 on February 26, 2007, 10:17:06 AM
Thank you Evgeniy  :-)
I'm there almost what I don't understand, this is why when I open the zip and that I look at the files, those are empty  :?

Code: [Select]
(defun download (url dir / byte fic file http tbl)
  (setq http (vlax-create-object "MSXML2.XMLHTTP"))
  (vlax-invoke-method http 'open "get" url :vlax-false)
  (vlax-invoke http 'send)
  (while (not (eq (vlax-get http 'readyState) 4))
    (repeat 100)
  )
  (setq file (strcat dir (vl-filename-base url) (vl-filename-extension url))
tbl  (vlax-safearray->list (vlax-variant-value (vlax-get-property http 'responsebody)))
fic  (open file "w") n 0)
  (foreach byte tbl
    (if (eq byte 0)
      (princ (vl-list->string '(0)) fic)
      (write-char byte fic)
    )
  )
  (close fic)
  (vlax-release-object http)
  (princ)
)

Code: [Select]
(download "http://carnet-de-cablage.chez-alice.fr/Lisp/Latt.zip" "c:/")
@+

Hello Patrick.
On my computer, this code works! :)

Good idea with express  8-)

but the code works in my computer to
But if you launch the zip and extract the files, they're empty  :?

@+
Title: Re: Download with vlisp
Post by: ElpanovEvgeniy on February 26, 2007, 10:22:08 AM
but the code works in my computer to
But if you launch the zip and extract the files, they're empty  :?

@+

All works for me!  :-)

Title: Re: Download with vlisp
Post by: Patrick_35 on February 26, 2007, 11:04:52 AM
Thanks Evgeniy
I don't understand.  :-(
It's a windows version ? . I work with Xp Professional.

@+
Title: Re: Download with vlisp
Post by: ElpanovEvgeniy on February 26, 2007, 11:10:04 AM
Thanks Evgeniy
I don't understand.  :-(
It's a windows version ? . I work with Xp Professional.

@+

I too use XP Professional +SP2 (ru)
The program checked for AutoCAD 2007 +SP1 (en)
Title: Re: Download with vlisp
Post by: ElpanovEvgeniy on February 26, 2007, 11:12:07 AM
Unique my change:
(download "http://carnet-de-cablage.chez-alice.fr/Lisp/Latt.zip" "D:/")

add

I replase
"c:/" => " D:/"
I cannot write from the program to a root of disk C:/
Title: Re: Download with vlisp
Post by: fools on March 01, 2007, 07:01:40 AM
Patrick_35 's codes no work because of function write-char , (write-char 10 file) will write two chars to file -- 013(CR) & 010 (LF)
Use some HEX Editor open downloaded file , and change 0D 0A to 0A , downloaded file will be OK.
But i don't know how to only write HEX0A (LF)  to file.
Title: Re: Download with vlisp
Post by: Patrick_35 on March 01, 2007, 07:39:32 AM
Thank you.
I noticed that my file actually made 7707 bytes and that with the writing by Write-char, princ, print or prin1, I found myself with a file of 7737 bytes, that is to say a difference in 30 bytes. I checked the size of my table which contains 7707 elements, therefore the size to be written well.  :?
The question
It's possible to write a file in binary with vlisp ?

@+
Title: Re: Download with vlisp
Post by: Patrick_35 on March 01, 2007, 09:23:05 AM
It's good. I found how to do it :-)
Code: [Select]
(defun download (url dir / byte fic file fso http tbl)
  (setq http (vlax-create-object "MSXML2.XMLHTTP")
        fso  (vlax-create-object "Scripting.FileSystemObject"))
  (vlax-invoke-method http 'open "get" url :vlax-false)
  (vlax-invoke http 'send)
  (while (not (eq (vlax-get http 'readyState) 4))
    (repeat 100)
  )
  (setq file (strcat dir (vl-filename-base url) (vl-filename-extension url))
tbl  (vlax-safearray->list (vlax-variant-value (vlax-get-property http 'responsebody)))
fic  (vlax-invoke fso 'CreateTextFile file))
  (foreach byte tbl
    (vlax-invoke fic 'write (vl-list->string (list byte)))
  )
  (vlax-invoke fic 'close)
  (vlax-release-object http)
  (vlax-release-object fso)
  (princ)
)

And with the solution of Mark (sorry it's in french)
Code: [Select]
(defun telecharger(lien rep / cp ok tmp util)
  (setq util (vla-get-Utility (vla-get-ActiveDocument (vlax-get-acad-object))))
  (if (eq (vla-isurl util lien) :vlax-true)
    (if (vl-catch-all-error-p (vl-catch-all-apply 'vla-GetRemoteFile (list util lien 'tmp :vlax-true)))
      (princ "\nErreur lors du téléchargement.")
      (progn
(setq cp (strcat rep (vl-filename-base lien) (vl-filename-extension lien)))
(if (findfile cp)
  (vl-file-delete cp)
)
(if (vl-catch-all-error-p (vl-catch-all-apply 'vl-file-copy (list tmp cp)))
  (progn
      (princ "\nImpossible de déplacer le fichier \""
   (strcat (vl-filename-base cp)(vl-filename-extension cp))
   "\" depuis le répertoire \n\""
   tmp
    )
    (vl-file-delete tmp)
  )
  (progn
    (vl-file-delete tmp)
            (if (zerop (vl-file-size cp))
              (progn
(vl-file-delete cp)
(princ "\nImpossible de télécharger le fichier.")
      )
      (setq ok T)
    )
  )
)
      )
    )
    (princ "\nLe lien n'est pas valide.")
  )
  ok
)

@+