Drawing Layer Manager
This contribution is stolen from Frank Zander with his blessing.
Contract CADD Group - http://www.contractcaddgroup.com
This application is the perfect example of how something so simple can
be so powerful. My immediate reaction on
reading this article and then running the coding was, "Why didn't I
think of that?" I implemented it into my office so quickly you
wouldn't believe it.
Using VBA in AutoCAD R14 / 2000 to Control Layer Standards.
Frank Zanders
Introduction
Have you ever wondered why drawings can contain many layers, but
objects such as "text", "dimensions" and
"hatch" do not always end up on the correct layers? This article
addresses a potential solution to that problem. First, I talk briefly
about layer standards and show you how to create a small VBA project for
AutoCAD r14.01 or 2000 to control layers.
Layer Standards
Several times in the past, I have been asked by new users, "Why
doesn't AutoCAD put text objects on the text layer and hatch objects on a
hatch layer, etc?" The answer is that Autodesk has designed AutoCAD
to give users unrestricted flexibility with layer names and to allow
companies to create or adopt any layer standard. Unfortunately, this
flexibility of open-architecture layer names within AutoCAD has allowed
many an AutoCAD user to recklessly create layer names. Sometimes, it
seems, layer names are chosen according to what pops into the user's head
while staring at the AutoCAD layer dialogue box! And after several other
users in the same company use these layer names, the results are then
called the "company layer standards".
There are many recognized layer standards in existence, such as AIA,
Master Format, British BSD, etc. that companies can adopt,
but it seems to be more trendy for companies to make up their own layer
names and standards on the fly. The rationale seems to be "Those
other existing layer standards could not possibly accommodate the type of
work that our company does. " How many times have you come across a
drawing (done by someone else, of course) containing layers called dim,
dimension, and dimensions? The purpose of this small VBA
project is to create a mini layer-manager that enforces layers for
"text", "hatch" and "dimension" objects as
they are created in a drawing. Text goes on to a layer called
"text", hatch patterns are placed on a layer called
"hatch", and dimensions are placed on a layer called
"dim". Seems almost too obvious! Sure, this is a rather
simplistic way of managing layers for objects, but it works! For those
interested in modifying the code to fit your own company layer standard or
for working with multiple disciplines, take a look at the section called
"Extending the Code" later in the article.
I was recently exposed to the "AcadDocument_BeginCommand"
sub-program when I began a technical review for WROX press of a book
called AutoCAD - VBA Programmers Reference (currently being written
by Joe Sutphin). Joe's example code made me think, "Wow! I could
apply his technique to do some layer management of my own!" In the
program outlined in this article, I access this sub-program to check on
the command being accessed by AutoCAD. Once the command being used is
checked, it creates the appropriate layer settings before AutoCAD starts
the command. Therefore, when a user enters the text command, my VBA
program sets-up and/or creates the text layer, transparently in the
background, before the text command starts. The sub-program "AcadDocument_BeginCommand"
picks up on AutoCAD commands used in LISP programs and any alias (defined
in the ACAD.pgp) that references an AutoCAD command. For example, if a
user types "BH" (standard short cut alias for the "bhatch"
command), the sub-program "AcadDocument_BeginCommand" still
traps the "bhatch" command.
The compliment to the "AcadDocument_BeginCommand" sub-program
is the sub-program "AcadDocument_EndCommand". I use the latter
sub-program to check for the name of the command that has just been
completed. If the command name matches my criteria (using the "select
case" method), the layer is set back to the original layer in use
before the command was started. Look at the following commented code to
see if you can follow what is happening inside the VBA program. Start with
the "AcadDocument_BeginCommand" sub-program and finish with the
"AcadDocument_EndCommand" sub-program.
Place this coding into a VBA Module and save it as DrawMan.dvb. Then
add it to your startup suitcase.
' Using Option Explicit -- you must explicitly
declare all variables using the
' Dim, Private, Public, ReDim, or Static statements.
' If you attempt to use an undeclared variable name,
' an error occurs at compile time.
' Option Explicit is a great way to make sure you do not have wonkie typo
mistakes
' in your code.
Option Explicit
' set the public object current Layer to be an AutoCAD Layer
Public objCurrentLayer As AcadLayer
' set the public object Previous Layer to be an AutoCAD Layer
Public objPreviousLayer As AcadLayer
Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
' Save the current / active AutoCAD layer
Set objPreviousLayer = ThisDrawing.ActiveLayer
' Check for the folowing cases of the AutoCAD command.
Select Case CommandName
' Check the AutoCAD command,
' is there a match for TEXT, MTEXT, or DTEXT
Case "TEXT", "MTEXT", "DTEXT" ' then do the
following..
' check for to see if the current layer is Text
' if the current layer is not TEXT then set up the TEXT
' layer for use.
If Not ThisDrawing.ActiveLayer.Name = "4" Then
' set up the TEXT layer
' this layer will be created if it does not exist
Set objCurrentLayer = ThisDrawing.Layers.Add("4")
' make the TEXT layer blue
objCurrentLayer.Color = acCyan
' turn the TEXT layer ON
objCurrentLayer.LayerOn = True
' Thaw the TEXT layer
objCurrentLayer.Freeze = False
' Unlock the TEXT layer
objCurrentLayer.Lock = False
' Make the TEXT layer the current
' active layer in this drawing.
ThisDrawing.ActiveLayer = objCurrentLayer
' end of if checking for TEXT being the current layer.
End If
' Check the AutoCAD command,
' is there a match for HATCH, or the BHATCH command.
Case "HATCH", "BHATCH"
' check for to see if the current layer is HATCH
' if the current layer is not HATCH then set up the HATCH
' layer for use.
If Not ThisDrawing.ActiveLayer.Name = "8" Then
' set up the HATCH layer
Set objCurrentLayer = ThisDrawing.Layers.Add("8")
' make the HATCH layer red
objCurrentLayer.Color = 8
' turn the HATCH layer ON
objCurrentLayer.LayerOn = True
' Thaw the HATCH layer
objCurrentLayer.Freeze = False
' Unlock the HATCH layer
objCurrentLayer.Lock = False
' Make the HATCH layer the current
' active layer in this drawing.
ThisDrawing.ActiveLayer = objCurrentLayer
' end of if checking for HATCH being the current layer.
End If
' check for AutoCAD dimension commands..
Case "DIMALIGNED", "DIMANGULAR", "DIMCENTER",
_
"DIMCONTINUE", "DIMDIAMETER", "DIMLINEAR",
"LEADER", _
"DIMORDINATE", "DIMRADIUS", "DIM",
"DIM1"
If Not ThisDrawing.ActiveLayer.Name = "4" Then
Set objCurrentLayer = ThisDrawing.Layers.Add("4")
objCurrentLayer.Color = acCyan
objCurrentLayer.LayerOn = True
objCurrentLayer.Freeze = False
objCurrentLayer.Lock = False
ThisDrawing.ActiveLayer = objCurrentLayer
' end of if checking for DIM being the current layer.
End If
' end of the Select
End Select
' end of Private Sub AcadDocument_BeginCommand
End Sub
Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
' Check for the folowing cases of the AutoCAD command that has just ended.
Select Case CommandName
' check for a match on the following AutoCAD commands
Case "HATCH", "BHATCH", "TEXT", "MTEXT",
"DTEXT", "DIMALIGNED", "DIMANGULAR", "DIMCENTER",
_
"DIMCONTINUE", "DIMDIAMETER", "DIMLINEAR",
"LEADER", _
"DIMORDINATE", "DIMRADIUS", "DIM",
"DIM1"
' make the current active AutoCAD layer the previously saved layer.
ThisDrawing.ActiveLayer = objPreviousLayer
' clear the objCurrentLayer
Set objPreviousLayer = Nothing
End Select
' clear the objCurrentLayer
Set objCurrentLayer = Nothing
' end Private Sub AcadDocument_EndCommand
End Sub
Would you like the coding for this routine? Then put one leg in the
air,
close one eye, scream as loud as you can and click here.
Enjoy.......
|