Author Topic: Macro to open a dwg  (Read 2258 times)

0 Members and 1 Guest are viewing this topic.

Keith Brown

  • Swamp Rat
  • Posts: 601
Macro to open a dwg
« on: April 11, 2014, 08:59:12 AM »
I searched the boards but didn't really find anything to help me but instead found that I am doing this right but it is just not working.  I have a macro that i created to open a specific dwg.


Code - Text: [Select]
  1. ^C^C_filedia;0;_open;"C:/ProgramData/EastCoast/Fabrication for AutoCAD MEP 2014/Styles/EC Schedules and Tags.dwg";_filedia;1;


Instead of opening the drawing i get the following error.



Code - Text: [Select]
  1. Command:
  2. Command: _filedia
  3.  
  4.  
  5. Enter new value for FILEDIA <1>: 0
  6.  
  7.  
  8. Command: _open
  9.  
  10.  
  11. Command: "C:/ProgramData/EastCoast/Fabrication for AutoCAD MEP 2014/Styles/EC Schedules and Tags.dwg"
  12. Unknown command "DWG"".  Press F1 for help.
  13.  
  14.  
  15. Command: _filedia
  16.  
  17.  
  18. Enter new value for FILEDIA <0>: 1


I thought at first that it might be because of the spaces in the drawing name but found posts where this is working.  Is it because there are spaces in the folder structure names?  Is there any macro that will work for this?  I don't mind including an inline lisp but i need to stay away from creating a lisp or a script file.  Any help would be appreciated.

Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

andrew_nao

  • Guest
Re: Macro to open a dwg
« Reply #1 on: April 11, 2014, 11:33:48 AM »
if you have vba installed you can try this
Code: [Select]
(setq D1 "c:\\ dwgname.dwg")
(command "._VBASTMT" (strcat "AcadApplication.Documents.Open \"" D1 "\""))

(setq dwgName "\"C:\\dwgname.dwg\"")
(command "vbastmt" (strcat "AcadApplication.Documents.Open " dwgName))

or you can use Lee Mac's function just place the code in your acaddoc.lsp and use the function call in toolbar button or a pulldown

Code: [Select]
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;

(defun LM:Open ( target / shell result )
    (if
        (and
            (or
                (eq 'INT (type target))
                (setq target (findfile target))
            )
            (setq shell (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
        )
        (progn
            (setq result (vl-catch-all-apply 'vlax-invoke (list shell 'open target)))
            (vlax-release-object shell)
            (not (vl-catch-all-error-p result))
        )
    )
)
(vl-load-com)

Example to Open a File Selected by the User
(LM:Open (getfiled "Select File to Open" "" "" 16))

Example to Open a Drawing File at a Specified Location:
(LM:Open "C:\\My Folder\\File.dwg")

Example to Open a Directory:
(LM:Open "C:\\My Folder\\My SubFolder")

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Macro to open a dwg
« Reply #2 on: April 11, 2014, 12:16:38 PM »
Thanks.  That will work for me.  I was able to copy/paste it into the macro command line and get it to work. 


I am just curious as to why my original one did not work.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Macro to open a dwg
« Reply #3 on: April 11, 2014, 03:12:26 PM »
Thanks.  That will work for me.  I was able to copy/paste it into the macro command line and get it to work. 


I am just curious as to why my original one did not work.
I'm not real familiar with macros, but I do remember trying to get a macro to work that had a filepath with spaces in it, and I ended up having to just use a line of LISP to get it to work.

On a related note, you could also open it this way...

Code: [Select]
((lambda (/)
   (vl-load-com)
   (vl-catch-all-apply
     (function
       (lambda (/ file)
         (setq file (vla-open (vla-get-documents (vlax-get-acad-object))
                              "C:/ProgramData/EastCoast/Fabrication for AutoCAD MEP 2014/Styles/EC Schedules and Tags.dwg"
                              :vlax-false
                    )
         )
         (vla-activate file)
       )
     )
   )
   (princ)
 )
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox