Jump to content

Adding layers and setting properties from VBA macro


sritter09

Recommended Posts

I know a lot of AutoCAD users find themselves doing the same functions over and over. Most of us can type the command prompts blindfolded and finish half the drawing. I am just starting out with VBA for AutoCAD (I've done some pretty involved VBA for excel if anyone needs help with that) so I figured I would post an example of my "hello world" macro for Mechanical 2017.

 

Our company uses boundary lines around our views in model space and here is a way to add a new layer, change the color, and turn off plot. I use it when I'm updating older drawings.

 

Sub ADD_NOPLOT()

   Dim oNEWLAYER As AcadLayer
   
   On Error Resume Next
   
   Set oNEWLAYER = ThisDrawing.Layers.Add("NO_PLOT")
   oNEWLAYER.color = acMagenta
   oNEWLAYER.Plottable = False
       
End Sub

 

Sorry there wasn't a question in all that. I just figured since it took me a couple searches to piece it together I would share it with all of you.

Link to comment
Share on other sites

Just a suggestion why dont you make it a library function so can use it any future coding.

 

add_layer layname "acMagenta" "False"

 

There is some uncertainty about VBA & Autocad its well documented so you may be better looking at VL lisp. Its syntax is very similar.

Link to comment
Share on other sites

I did some work with lisp about 10 years ago so I'm not entirely unfamiliar with it, but I would certainly need to "brush up" on the coding. I just used VBA for this because I've been doing a lot in Excel lately. It was more of a quickfire macro just to save some time

Link to comment
Share on other sites

BIGAL,

 

Here's what I got for a lisp routine...

 

(DEFUN C:NOPLOT ()
 (COMMAND "-LAYER" "M" "NO_PLOT" "P" "N" "" "S" "0" "C" "MAGENTA" "NO_PLOT" "")
 (PRINC)
)

 

I'm not sure I understand what you mean about turning it into a library function though. Never been down that road so I don't even know where to start.

 

Thanks

Link to comment
Share on other sites

A lot of code uses make a new layer so my suggestion is have a library function that in simple terms is added to acaddoc.lsp The make layer command is forgiving in that if the layer exists it does not error so you do not need to use a check if exists. Another example of a library function is I have a 1 2 or 3 line library dcl so when I ask for a value I pop the dcl its code is only 1 line in the current program, (ahgetvals "Enter 1st val" "Enter 2nd val " "Enter 3rd val") slowly I am changing all the old programs to this method.

 

Any good programming will always take advantage of sub routines rather than repeated code.

 

So any time you need to make a no plot layer you can do it this way.

 

(DEFUN makelay ( layname  col LT / )
 (COMMAND "-LAYER" "M" layname "P" "N" "" "S" layname "C" col" layname "")
 (PRINC)
)

;to use add to acaddoc.lsp then in any program
(makelay "1stone" 2 "Dashed")

; for no plot
(DEFUN NPLOT ( layname col lt / )
 (COMMAND "-LAYER" "M" layname "P" "N" "" "S" "0" "C" col layname "LT" lt layname "")
 (PRINC)
)

(nplot "2ndlayer" 123 "continuous")

;If you still want a keyboard option you can type the line above direct to the command line or as you did previously add the defun to your library again
;if you want a no plot layer always then add it to your DWT which is probably the best option. 

(defun c:noplot ()(nplot "2ndlayer" 6 "continuous"))

Link to comment
Share on other sites

Okay, that makes more sense. I just wasn't sure if there was actually a "library" you had to add it to. And yes it is already a part of my dwt files. I just use this for updating old files that haven't seen the light of day in a couple years or from autocad ops who don't pay attention to the little details.

 

now 2 questions...

 

1. on the defun line in makelay or nplot, what is the purpose of the forward slash (/). I was able to put the rest in context reading through the code, but that one eluded me. Is there another variable slot that needs defined?

 

2. do I have to add it to the acaddoc.lsp or can i use the lisp file that I already added to the startup content? I keep it on the network so it is backed up and for transferring from one PC to another.

Link to comment
Share on other sites

Item 1 (variables to be pased to defun / local variables to this defun) in defun above there are no locals.

(string1 / x y z)

 

Item 2 Add it to your lisp, thats the way we have it also.

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