Jump to content

Recommended Posts

Posted

Can anyone tell me why the following bit of code won't work to start a new drawing based on a template?

 

(defun msc_elec (templateName)
(setq templateName ("L:/MSC/Templates/msc_elec.dwt")
(vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)

 

The error I get from this code is: "error: too few arguments"

 

I got this bit of code from h t t p ://hyperpics.blogs.com/beyond_the_ui/autolispvisual_lisp and have it inside another lisp routine that takes a users input to select the correct project from a list box which then triggers this function calling the template file. This is on AutoCAD 2009.

 

Also if there is a better way to start a drawing based on a template then I'm all ears. I even tried passing the basic commands in a lisp routine as such:

 

(defun msc_elec()
(command "new" "msc_elec.dwt")
(princ)

 

In this case, it tries to pass the "msc_elec.dwt" as a command so I get "Unknown command 'msc_elec.dwt'. Press F1 for help."

 

Thanks,

 

Lonnie

Posted

Where is (vl-load-com)?

Posted

I have it within my acaddoc.lsp so it's always loaded. Is this not right?

Posted

I was just double checking.

Posted

(setq templateName (findfile "L:/MSC/Templates/msc_elec.dwt")

 

I think you are missing this. But I am not sure.

Posted

Thanks. I'll give that a try.

Posted

untested... but try

(defun msc_elec (/ templateName)
(setq templateName "L:/MSC/Templates/msc_elec.dwt")
(vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)

Posted

lpseifert, your code worked!! Thanks so much. Buzzard, thanks for your help too, I appreciate it.

 

Since I'm trying to learn instead of just copying code, the two differences I see between your code is the backslash in this line (missing in mine):

 

(defun msc_elec (/ templateName)

 

and the parenthesis around the template path in this line (I included them and you did not):

 

(setq templateName "L:/MSC/Templates/msc_elec.dwt")

 

Can you tell me what the backslash means in the first part and why parenthesis are not needed to enclose the path in the second part? I'd really appreciate it.

 

BTW, I discovered an error in my code when I was comparing my code to yours. I was missing a right parenthesis after the path. So even if the logic of my code was good it still wouldn't have worked and I would have been here all day, lol.

Posted

Probably not the best explanation, but the way I see it...

 

The arguments after the / are local variables; the ones before (or if the / is not present) are arguments to be passed to the function (pre-defined prior to running the function)

this will work

(setq templateName "L:/MSC/Templates/msc_elec.dwt")
(defun msc_elec (templateName)
(vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)

but you have to run the function as such to pass the variable to the function

(msc_elec templatename)

The parenthesis are not needed because you are setting the variable templateName to a string (defined by the open and closed quotes). Lisp expects the first thing after an open parenthesis to be a function.

Posted

This option already exists, just set your QNew tempalte path.

Posted

Alan,

 

What if I have multiple templates I need to load based on the users input? I read a little about the QNew template path but didn't think it would work for me because I have more than one template that could be loaded.

 

The actual code from my "load_project.lsp" shows this:

(defun msc_elec (/ templateName)
(setq templateName "L:/Scripts/Templates/msc_elec.dwt")
(vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)
(defun msc_outfit(/ templateName)
(setq templateName "L:/Scripts/Templates/msc_outfit.dwt")
(vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)
(defun msc_pipe()
(setq templateName "L:/Scripts/Templates/msc_pipe.dwt")
(vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)
(defun msc_struct()
(setq templateName "L:/Scripts/Templates/msc_struct.dwt")
(vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)

Posted

Multiple is a different story. However, you can choose your template when you type/select New (will go to proper path if set in options).

 

For what you are doing, I'd keep going the way you are, but when you were only speaking of one, I wanted to make sure you or anyone that comes along in the future, knew about QNew. Good luck. :)

Posted

I would also consider adding a findfile check, just to be safe and LOCALIZE YOUR VARIABLES!!

 

You could also write it as a subroutine...

 

(defun _TemplateLoad (templatePath)
 (if (findfile templatePath)
   (vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templatePath))
 )
)

(defun c:msc_struct nil (_TemplateLoad "L:/Scripts/Templates/msc_struct.dwt") (princ))

Posted

Another way would be to have different icons on your desktop for each template. The templates can be called using the /t switch (under properties of the icon)

/t "Template File"

Posted

Thanks for the suggestions guys. I really appreciate it. I'm really a novice when it comes to lisp and I'm trying to teach myself as I go along. I'll have to read up on localizing variables.

Posted
Another way would be to have different icons on your desktop for each template. The templates can be called using the /t switch (under properties of the icon)
Excellent suggestion. I completely forgot about that one.
Posted
Excellent suggestion. I completely forgot about that one.

 

Dear friend,

 

I can't understand that what he is telling and how to do it? Can you explain in detail?

 

Yours,

Muthu.

Posted
Dear friend,

 

I can't understand that what he is telling and how to do it? Can you explain in detail?

 

Yours,

Muthu.

 

He means that if you have a shortcut to AutoCAD on your desktop with the /t switch (which tells AutoCAD it's opening a template file) followed by the complete path to the template itself, then every time you click on that shortcut AutoCAD will open with the template you specify. See below.

 

temp_path.JPG

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...