Author Topic: vlax-create-object or vla-getInterfaceObject?  (Read 16748 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
vlax-create-object or vla-getInterfaceObject?
« on: December 12, 2010, 12:08:44 PM »
I was going to add these two functions to my website, but I have a couple of questions which have been on my mind for a little while.

Here are the functions:

Code: [Select]
;;----------------------=={ Open File }==---------------------;;
;;                                                            ;;
;;  Uses the 'Open' method of the Shell Object to open the    ;;
;;  specified file.                                           ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  filename - filename of file to open                       ;;
;;------------------------------------------------------------;;
;;  Returns:  T if file opened successfully, else nil         ;;
;;------------------------------------------------------------;;

(defun LM:OpenFile ( filename / Shell result ) (vl-load-com)
  ;; © Lee Mac 2010
 
  (setq Shell (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))

  (setq result
    (and (setq filename (findfile filename))
      (not
        (vl-catch-all-error-p
          (vl-catch-all-apply 'vlax-invoke (list Shell 'Open filename))
        )
      )
    )
  )
 
  (vlax-release-object Shell)
  result
)


Code: [Select]
;;-----------------------=={ Explore }==----------------------;;
;;                                                            ;;
;;  Opens a specified folder in a Windows Explorer window.    ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  Directory - Directory or ShellSpecialFolderConstants      ;;
;;              Enumeration to open in Windows Explorer       ;;
;;------------------------------------------------------------;;
;;  Returns:  T if directory opened in Explorer, else nil     ;;
;;------------------------------------------------------------;;

(defun LM:Explore ( Directory / Shell result ) (vl-load-com)
  ;; © Lee Mac 2010

  (setq Shell  (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))

  (setq result (vl-catch-all-apply 'vlax-invoke (list Shell 'Explore Directory)))
  (vlax-release-object Shell)

  (not (vl-catch-all-error-p result))
)

Upon testing, I notice that they operate correcty if either:

Code: [Select]
(vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application")
Or

Code: [Select]
(vlax-create-object "Shell.Application")
is used.

So, my question would be: Which is the correct method (if there is a preferred method), and what determines which to use?

Thank you for your time,

Lee

kruuger

  • Swamp Rat
  • Posts: 635
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #1 on: December 13, 2010, 04:40:47 AM »
aaaa, i love this OpenFile  :lol: :lol: :lol:

i can throw away this vba shi...
Code: [Select]
(command "vbastmt" (strcat "AcadApplication.Documents.Open" (chr 34) "D:/CAD_UTIL/Menu/Support/CABINET ORGANIZATION SYSTEM.dwg" (chr 34)))
thank you
kruuger

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #2 on: December 13, 2010, 07:47:39 AM »
aaaa, i love this OpenFile  :lol: :lol: :lol:

i can throw away this vba shi...
Code: [Select]
(command "vbastmt" (strcat "AcadApplication.Documents.Open" (chr 34) "D:/CAD_UTIL/Menu/Support/CABINET ORGANIZATION SYSTEM.dwg" (chr 34)))
thank you
kruuger

 :-D  You're welcome Kruuger!  :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #3 on: December 13, 2010, 10:04:55 AM »
I think, if I remember right, it has more to do with the releasing of the objects then the creating  of them; I think there was an error in some AutoCAD releases where if you created objects with the VLAX-CREATE-OBJECT it (the external process) wouldnt terminate properly or at all.

So that meant that this would be the "proper" (again, I think, I dont know for sure anymore) method.
   
Code: [Select]
( (lambda ( / sh )
    (setq sh (vlax-create-object "WScript.Shell"))
    ;;
    ;; (vlax-invoke-method...do some stuff)
    ;;
   (vlax-release-object sh)
   (gc); force a garbage collection
   )
 )

I would study some code of the giants (giants in that: "we stand on the sholders of giants") and adopt a similar method.

hth
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #4 on: December 13, 2010, 11:10:06 AM »
I would study some code of the giants (giants in that: "we stand on the sholders of giants") and adopt a similar method.

Thanks for your input Se7en - perhaps I'll look at some of Tony Tanzillo's code for clues (he's the first 'giant' that comes to mind).

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #5 on: December 13, 2010, 11:24:59 AM »
np.

Dave Stein, MP, Stig, Kerry, ElpanovEvgeniy, Gile, Patrick_35, Owen, Renni, herman... the list goes on and on and on.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10638
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #6 on: December 13, 2010, 11:33:21 AM »
Sorry for leaving everyone else out; there are way too many names to mention.

Luis, Matt Stachoni, Jürg Menzi, etc. ...
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Dashmonkey

  • Newt
  • Posts: 29
  • (defun sleep nil nil)
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #7 on: December 13, 2010, 11:59:17 AM »
Lee,

Good stuff! I can ditch my old "^C^Cbrowser;C:\Temp;" macro :P

If my testing is correct, LM:Openfile will provide the same functionality as LM:Explore.
"Findfile" will return a directory location as well as a file location.
It also solves the problems of LM:Explore returning "T" whether the location exists or not and it opens a new explorer window, rather than using one that is already open.

...if my testing is correct.  :-)
I didn't break it, I swear! ...ok, I broke it.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #8 on: December 13, 2010, 12:00:46 PM »
Not as elegant, but one could also use (startapp "EXPLORER" <FolderPath>) to open a specified directory.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #9 on: December 13, 2010, 12:08:48 PM »
Good stuff! I can ditch my old "^C^Cbrowser;C:\Temp;" macro :P

If my testing is correct, LM:Openfile will provide the same functionality as LM:Explore.
"Findfile" will return a directory location as well as a file location.
It also solves the problems of LM:Explore returning "T" whether the location exists or not and it opens a new explorer window, rather than using one that is already open.

...if my testing is correct.  :-)

True, the 'Open' method of the Shell Object provides the same functionality as the 'Explore' method, however when used with one of the ShellSpecialFolderConstants, Open will create the folder if non-existent - I don't believe this is the case with the Explore method. Although, as the functions stand, the OpenFile function would not accept a ShellSpecialFolderConstant due to the findfile condition, so my point is irrelevant.

Thanks for testing  :-)

I would note that, in Win7, both open a new Explorer window.
« Last Edit: December 13, 2010, 12:13:39 PM by Lee Mac »

Dashmonkey

  • Newt
  • Posts: 29
  • (defun sleep nil nil)
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #10 on: December 13, 2010, 12:16:21 PM »
Not as elegant, but one could also use (startapp "EXPLORER" <FolderPath>) to open a specified directory.

Alan,
You would have to go this route if you wanted a routine that could be used in conjunction with a menu macro, as "\" is the control character to pause for user input in a macro, and window's shell does not know how to interpret "/" (the substitute for "\" in a menu macro).
...I think that makes sense.


Lee,
Thanks for sharing the code  :-)

Edit - Spelling
I didn't break it, I swear! ...ok, I broke it.

Dashmonkey

  • Newt
  • Posts: 29
  • (defun sleep nil nil)
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #11 on: December 13, 2010, 12:22:09 PM »
I would note that, in Win7, both open a new Explorer window.

Of course, now that I've said that, both open a new explorer window for me too, and I'm unable to duplicate my previous result.
Oh Murphy, you and your stupid laws, always out to get me  :-D
I didn't break it, I swear! ...ok, I broke it.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #12 on: December 13, 2010, 12:22:45 PM »
Oh Murphy, you and your stupid laws, always out to get me  :-D

 :-D

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #13 on: December 13, 2010, 12:27:29 PM »
You can see Reini Urban's reply here
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vlax-create-object or vla-getInterfaceObject?
« Reply #14 on: December 13, 2010, 12:31:53 PM »
You can see Reini Urban's reply here

Judging by his reply, it seems he recommends vlax-get-or-create-object, am I right?