Variable problems

Discussion in 'AutoCAD' started by Nathan Guill, Aug 25, 2003.

  1. Nathan Guill

    Nathan Guill Guest

    I have this VBA routine I found that places objects onto a specific layer
    depending on the object (i.e. Dimensions on the Dimension Layer, Text on the
    Text layer, ...). The problem is that the variable that holds the original
    layer does not get passed from the BeginCommand routine to the EndCommand
    routine. Below is a copy of the whole routine. Could someone please point
    me in the right direction? Thanks.

    ' 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)
    Dim layerInfo As ADODB.Recordset

    On Error GoTo errHandler

    ' Save the current / active AutoCAD layer
    Set objPreviousLayer = ThisDrawing.ActiveLayer

    'open CAD database
    dbCreateConnections
    dbOpenDatabase

    ' 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 = "TEXT" Then

    ' set up the TEXT layer this layer will be created if it
    does not _
    exist
    Set objCurrentLayer = ThisDrawing.Layers.Add("TEXT")

    'set SQL connections
    mcmdLocal.CommandType = adCmdText
    mcmdLocal.CommandText = "SELECT * FROM Layers WHERE Layer =
    'Text'"

    ' 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 = "HATCH" Then

    ' set up the HATCH layer
    Set objCurrentLayer = ThisDrawing.Layers.Add("HATCH")

    'set SQL connections
    mcmdLocal.CommandType = adCmdText
    mcmdLocal.CommandText = "SELECT * FROM Layers WHERE Layer =
    'Hatch'"

    ' 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 = "DIM" Then

    Set objCurrentLayer = ThisDrawing.Layers.Add("DIM")

    'set SQL connections
    mcmdLocal.CommandType = adCmdText
    mcmdLocal.CommandText = "SELECT * FROM Layers WHERE Layer =
    'Dim'"

    ' end of if checking for DIM being the current layer.
    End If
    ' end of the Select
    End Select

    Set layerInfo = mcmdLocal.Execute

    objCurrentLayer.color = layerInfo.Fields("Color")
    objCurrentLayer.LayerOn = True
    objCurrentLayer.Freeze = False
    objCurrentLayer.Lock = False
    ThisDrawing.ActiveLayer = objCurrentLayer

    'close CAD database
    dbCloseDatabase

    End
    errHandler:
    Select Case Err.Number
    Case 3705
    dbCloseDatabase
    Resume
    Case Else
    Err.Clear
    End Select
    End

    ' 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", "MTEXT", "TEXT", "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
     
    Nathan Guill, Aug 25, 2003
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.