Jump to content

how can i make a lisp that opens a drawing


MikeP

Recommended Posts

I want something like this. Obviously this does not work though and there is a lot im missing. but how can I make it work

 

(defun c:opend ( / dwg1 dwg2 dwg3)
(setq dwg1 (C:\Jobs\dwg1.dwg))
(setq dwg2 (C:\Jobs\dwg2.dwg))
(setq dwg3 (C:\Jobs\dwg3.dwg))
(prompt "\nwhat drawing would you like to open?")
(command "open" dwg2)

Link to comment
Share on other sites

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • MikeP

    9

  • BlackBox

    8

  • MSasu

    3

  • Lee Mac

    2

Top Posters In This Topic

Posted Images

Since you are using an AutoCAD version that still supports VBA:

(setq dwg1 "\"C:\\Jobs\\dwg1.dwg\"")
(command "_VBASTMT" (strcat "AcadApplication.Documents.Open " dwg1))

But, please keep in mind that each drawing has his own namespace.

 

 

As general rule, when want to store a path on a variable:

[s](setq dwg1 (C:\Jobs\dwg1.dwg))[/s]
(setq dwg1 [color=red]"[/color]C:\[color=red]\[/color]Jobs\[color=red]\[/color]dwg1.dwg[color=red]"[/color])

Link to comment
Share on other sites

am i getting closer?

 

 

(defun c:opend ( / dwg1 )
(setq dwg1 "C:\\Jobs\\dwg1.dwg"))
((setq dwg (getkword "\nwhat drawing would you like to open? <>"))
(command "_VBASTMT" (strcat "AcadApplication.Documents.Open dwg))
)

Link to comment
Share on other sites

Or, using VisualLISP extension:

(vl-load-com)
(setq dwg1 "\"C:\\Jobs\\dwg1.dwg\"")
(vla-open (vla-get-documents (vla-get-application (vlax-get-acad-object))) "C:\\d1.dwg")

Link to comment
Share on other sites

Something like this? Although I'm really not sure what are you looking to achieve by that...

(defun c:opend ( / dwgName dwgPath )
  (setq dwgPath "C:\\Jobs\\")
  
  (initget 1 "DWG1 DWG2 DWG3")
  (setq dwgName (getkword "\nWhat drawing would you like to open? (Dwg1/Dwg2/Dwg3):"))
  (setq dwgName (strcat dwgPath "\"" dwgName ".dwg" "\""))
  (command "_VBASTMT" (strcat "AcadApplication.Documents.Open " dwgName))
 )

 

You must be more careful with usage of parenthesis!

Link to comment
Share on other sites

i ave a few drawings I like a access quickly and i figured a list that i can type a nickname would be easiest, rather than opening it using the open box

Link to comment
Share on other sites

FWIW - I use this for many commonly opened reference drawings. Can also be used in a toolbar/ribbon button macro. :thumbsup:

 

(defun _OpenDwg  (dwg readOnly / f oDwg)
 [color=green];; RenderMan, CADTutor.net
 ;; Example: (_OpenDwg "[/color][color=red]FilePath[/color][color=green]\\[/color][color=blue]FileName[/color][color=green].dwg" T)[/color]
 (vl-load-com)  
 (if (and (setq f (findfile dwg)) (/= 1 (getvar 'sdi)))
   (vla-activate
     (vla-open (vla-get-documents (vlax-get-acad-object))
               f
               (cond ((= T readOnly) :vlax-true)
                     ((:vlax-false)))))
   (cond (f (prompt "\n** Command not available in SDI mode ** "))
         ((prompt (strcat "\n** \""
                          (strcase (vl-filename-base dwg))
                          "\" cannot be found ** ")))))
 (princ))

Link to comment
Share on other sites

Lee - Out of curiosity, how has that code worked with multiple versions (and the whole which version is default scenario)...?

 

I haven't test LM:Open for myself yet, but we've got Land Desktop 2009, Raster Design 2009, Civil 3D 2011, Map 3D 2011, Raster Design 2011, Civil 3D 2012, AMEP 2012, Raster Design 2012 all installed. I'm wondering if the Shell.Application InterfaceObject knows to open the drawing argument with the Active version calling the function or not.

 

Cheers! :beer:

Link to comment
Share on other sites

Lee - Out of curiosity, how has that code worked with multiple versions (and the whole which version is default scenario)...?

 

The behaviour of the Open method of the Shell.Application object I believe is the same as double-clicking the file in Explorer to open it - i.e. it will open with the default application associated with the filetype.

Link to comment
Share on other sites

FWIW - I use this for many commonly opened reference drawings. Can also be used in a toolbar/ribbon button macro. :thumbsup:

 

(defun _OpenDwg  (dwg readOnly / f oDwg)
 [color=green];; RenderMan, CADTutor.net
 ;; Example: (_OpenDwg "[/color][color=red]FilePath[/color][color=green]\\[/color][color=blue]FileName[/color][color=green].dwg" T)[/color]
 (vl-load-com)  
 (if (and (setq f (findfile dwg)) (/= 1 (getvar 'sdi)))
   (vla-activate
     (vla-open (vla-get-documents (vlax-get-acad-object))
               f
               (cond ((= T readOnly) :vlax-true)
                     ((:vlax-false)))))
   (cond (f (prompt "\n** Command not available in SDI mode ** "))
         ((prompt (strcat "\n** \""
                          (strcase (vl-filename-base dwg))
                          "\" cannot be found ** ")))))
 (princ))

 

how do i modify this to open the specific dwg i want? I dont see where to enter the file path

Link to comment
Share on other sites

[ATTACH=CONFIG]34975[/ATTACH]

 

[ATTACH=CONFIG]34974[/ATTACH]

pencil.png

 

 

 

Thanks dick..... only reason I'm confused is because the ;; tells the program to ignore that line...... Obviously im not the best at lisp

Link to comment
Share on other sites

So I did this.

(defun ODwg (dwg readOnly / f oDwg)

;; RenderMan, CADTutor.net

;; Example: (_OpenDwg "S:\Blocks\SBUX Shop Drawings\Countertops\Saved\Temporary Ctop Library\Master Ctop Drawing.dwg" T)

(vl-load-com)

(if (and (setq f (findfile dwg)) (/= 1 (getvar 'sdi)))

(vla-activate

(vla-open (vla-get-documents (vlax-get-acad-object))

f

(cond ((= T readOnly) :vlax-true)

((:vlax-false)))))

(cond (f (prompt "\n** Command not available in SDI mode ** "))

((prompt (strcat "\n** \""

(strcase (vl-filename-base dwg))

"\" cannot be found ** ")))))

(princ))

I enter "_OPENDWG" at the command line

 

And it tells me unknown command

Link to comment
Share on other sites

Perhaps I was unclear... _OpenDwg is a sub-function that requires two arguments:

 

dwg - a file (path and/or name) as string

readOnly - T or nil

 

 

In order to call this sub-function from the command line, you must encapsulate the call with parens, like so:

 

(_OpenDwg "S:\\Blocks\\SBUX Shop Drawings\\Countertops\\Saved\\Temporary Ctop Library\\Master Ctop Drawing.dwg" T)

 

** Note the double back-slashes "\\"

 

The comments (the lines prefixed by ";;" are meant to be useful to you, the user, alone, and are not passed onto the command line, or the VLIDE.

 

 

A sample macro, for your CUI command could be:

 

^C^C^P(_OpenDwg "S:\\Blocks\\SBUX Shop Drawings\\Countertops\\Saved\\Temporary Ctop Library\\Master Ctop Drawing.dwg" T)^P

HTH

Link to comment
Share on other sites

FWIW -

 

The purpose of a sub-function, generally speaking, is to isolate a specific task, or series of (in this case to open a drawing), in order to be used multiple times, sometimes in a row, or sometimes simply to allow the user (in this case you) to feed the sub-function a different parameter (aka argument)... in this instance, the sub-function can perform the same task on any valid drawing file that you supply to the sub-function as an argument.

 

Is this starting to make (more?) sense now...? :unsure:

Link to comment
Share on other sites

FWIW -

 

The purpose of a sub-function, generally speaking, is to isolate a specific task, or series of (in this case to open a drawing), in order to be used multiple times, sometimes in a row, or sometimes simply to allow the user (in this case you) to feed the sub-function a different parameter (aka argument)... in this instance, the sub-function can perform the same task on any valid drawing file that you supply to the sub-function as an argument.

 

 

 

Is this starting to make (more?) sense now...? :unsure:

Honestly, just barely. *If I enter

(_OpenDwg "S:\\Blocks\\SBUX Shop Drawings\\Countertops\\Saved\\Temporary Ctop Library\\Master Ctop Drawing.dwg" T)

*

into the command line it opens the dwg. *the LISP file does not want to work though

The part that I am confused about it, What command do I enter to open this file path? Should typing"_opendwg" open the dwg?

Link to comment
Share on other sites

Okay, let's back up for a moment.

 

If you want to create a LISP Command that can be invoked at the command line by typing in the command name, this is the syntax:

 

;; Define the function
(defun [color=red][b]c:[/b][/color][color=blue]HelloWorld[/color] ()
 (princ "\nHello World! ")
 (princ)
)

Sample command line output:

 

Command: [color=blue]HelloWorld[/color] 

Hello World!

Command:

Where the c: prefix (optional) to the Defun function's Symbol argument tells AutoCAD to allow this function to be called from the command line, and HelloWorld is the command that one would type at the command line. Together these are the function's Symbol. This syntax does not accept arguments.

 

By not including the c: prefix, the Defun function's Symbol argument is treated as a sub-function... Something that can accept arguments (optional), and can only be invoked by encapsulating the call within parenthesis (aka parens), like so:

 

;; Define the sub-function
(defun [color=blue]SayIt [/color]( [color=magenta]string [/color])
 (prompt [color=magenta]string[/color])
 (princ)
)

Sample command line output:

 

Command:  ([color=blue]SayIt [color=magenta]"\nHello World!"[/color][/color])

Hello World!

Command:

Note that any Defun can be invoke when encapsulated in parens, like so:

 

Command:  ([color=red][b]c:[/b][/color][color=blue]HelloWorld[/color])

Hello World!

Command:

Now, the _OpenDwg sub-function... The LISP *is* working, and you've successfully called the sub-function with the required arguments.

 

Does any of this help, or are you still lost in a world of [developer] malarkey? :unsure:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...