Author Topic: New Project Step 2 Layers  (Read 22167 times)

0 Members and 2 Guests are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #45 on: July 26, 2005, 03:12:40 PM »
Quote from: daron
Are you saying you need optional arguments or default to what's current?

Please re-phrase that, as I dont quite get it.

Im saying that if you wanted name and color, you could do 1 of 2 things:
1- code 4 version for each possibility: name color/ name linetype/ name color linetype/ name only

or 2- write one that accepts optional arguments and pass them the ones you know.

Does that make sense?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

daron

  • Guest
New Project Step 2 Layers
« Reply #46 on: July 26, 2005, 03:21:35 PM »
That's what I thought and was trying to ask. I just wasn't sure if you needed to make sure that the current values didn't change by forcing them as default. I do know that vba accepts optional arguments.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #47 on: July 26, 2005, 03:36:07 PM »
I also have 2 types of error checking in this function.  As it is writen right now, the error handler takes care of the error.  to utilize the other method, the '   needs to be removed in fron of IF and END IF
Code: [Select]
Private Sub LLayer(ByRef Lname As String, Optional Lcolor As Integer, Optional Ltype As String)
    Dim objLayer As AcadLayer
    On Error GoTo ErrorHandler

    If DoesLayerExist(Lname) = False Then
        Set objLayer = ThisDrawing.Layers.Add(Lname)
        'SHOW 2 WAYS TO ERROR TRAP THIS
     '   If Lcolor <> 0 Then
            objLayer.color = Lcolor
     '   End If
     '   If Ltype <> "" Then
            objLayer.Linetype = Ltype
     '   End If
    End If
    GoTo Clean_Up

ErrorHandler:
    Select Case Err
        Case -2145320939:
            Err.Clear
            MsgBox "Invalid Color call"
            Resume Next
        Case -2145386493:
            Err.Clear
            MsgBox "Invalid Linetype call"
            Resume Next
        Case Else:
            Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    End Select
Clean_Up:
   Set objLayer = Nothing
   Lcolor = 0
   Lname = ""
   Ltype = ""
   Exit Sub
End Sub
Notice the commented out lines (the lines beginning with an  ')

There are 2 ways to error check this function, and I'm not sure I should show this, but it might inspire somebody to great things.

One way should be very obvious:  On Error GoTo ErrorHandler.
The second way uses If..Then statements to test the values of the Arguments passed in to check their values.  When we do not pass in an integer, Lcolor defaults to 0 (Zero).  A layer can not have a color of 0, agreed?  So, we can trap this 2 ways.

1- Let the ErrorHandler handle the error created when objLayer.color=0 is called or
2- Check to see IF Lcolor <> (is not equal to) 0 and if its not, THEN set objLayer.color = ArgumentColor


Does everyone understand this?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Birdy

  • Guest
New Project Step 2 Layers
« Reply #48 on: July 26, 2005, 03:47:39 PM »
<shifting back to remedial mode>:)
Quote from: CmdrDuh
Quote from: Birdy
btw, I'm trying to combine the layers and linetypes by doing this:...  ..... .....
One minute I feel like a dope with this, the next like a bright and shiny light bulb.... and then like a dope again. :oops:

You guys are getting this and making good progress, its just that your moving faster than I can type.  (Which is not a bad thing)  I just hadn't gotten to the part where we create 1 function to do all the code.  I was creating small modules to teach the concept, and we would put them all together later.

Dunno bout the progress...but...
My vbamanager(edit: uh, that's MACRO dialog, sorry) looks like this:

and I have to run both LLINE and test, and then they work.
(maybe I'm getting ahead of myself combining these subs into one...?)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
New Project Step 2 Layers
« Reply #49 on: July 26, 2005, 03:50:55 PM »
One thing I might add to this thread when dealing with optional parameters is to use the IsMissing function to see if the value is missing ...

Code: [Select]
       If Not (IsMissing(Lcolor)) Then
            objLayer.Color = Lcolor
        End If
        If Not (IsMissing(Ltype)) Then
            objLayer.Linetype = Ltype
        End If
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Birdy

  • Guest
New Project Step 2 Layers
« Reply #50 on: July 26, 2005, 04:15:25 PM »
<Sylvania Moment>
I did notice that if I run the Macro 'test' first, it all works as expected.
One change I made was to put the sub 'test' at the top;
Code: [Select]
Public Sub test()
Call LLINE
    Call LLayer("Layer1", acRed, "HIDDEN")
    Call LLayer("Layer2", acGreen, "DASHED")
End Sub

Public Sub LLINE()
On Error GoTo ErrorHandler
ThisDrawing.Linetypes.Load "HIDDEN", "ACAD.LIN"
ThisDrawing.Linetypes.Load "DASHED", "ACAD.LIN"
Exit Sub

ErrorHandler:
Select Case Err
Case -2145386405:
Err.Clear
Resume Next

Case Else:
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext

End Select
End Sub
Public Sub LLayer(ByRef Lname As String, Lcolor As Integer, Ltype As String)
    Dim objLayer As AcadLayer
    Set objLayer = ThisDrawing.Layers.Add(Lname)
    objLayer.color = Lcolor
    objLayer.Linetype = Ltype
    Set objLayer = Nothing
    Exit Sub
End Sub



Does that make a difference?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #51 on: July 26, 2005, 05:04:14 PM »
Quote from: Birdy
<Sylvania Moment>
I did notice that if I run the Macro 'test' first, it all works as expected.
One change I made was to put the sub 'test' at the top....
Does that make a difference?


Technically, NO, does it help you see whats happening, YES , so at the top is good.  Some people like to put the main code at the top, others at the bottem.  It makes no difference to ACAD.

Back to your question about the Macro dialog box showing 2 macros:  Re-read Public vs Private.  Eventually, we will make LLayer and LLine Private, and they wont show up in that list.  More on this later
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #52 on: July 26, 2005, 07:27:41 PM »
Quote from: Birdy
Code: [Select]

Public Sub test()
Call LLINE
    Call LLayer("Layer1", acRed, "HIDDEN")
    Call LLayer("Layer2", acGreen, "DASHED")
End Sub

Private Sub LLINE()
On Error GoTo ErrorHandler
ThisDrawing.Linetypes.Load "HIDDEN", "ACAD.LIN"
ThisDrawing.Linetypes.Load "DASHED", "ACAD.LIN"
Exit Sub

ErrorHandler:
Select Case Err
Case -2145386405:
Err.Clear
Resume Next

Case Else:
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext

End Select
End Sub

Private Sub LLayer(ByRef Lname As String, Lcolor As Integer, Ltype As String)
    Dim objLayer As AcadLayer
    Set objLayer = ThisDrawing.Layers.Add(Lname)
    objLayer.color = Lcolor
    objLayer.Linetype = Ltype
    Set objLayer = Nothing
    Exit Sub
End Sub


Notice I changed the Public subs to Private subs for LLINE and LLAYER.  This does 2 (or more) things
for us. 1 it removes the macros from the list of available macros you can run from ACAD.
2 More importantly, it changes the SCOPE of the variables and functions within our project module.
By declaring the subs Private, all variables inside those subs are private as well.  In a nutshell
this means they are NOT available outside the sub they are contained within.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #53 on: July 26, 2005, 07:29:41 PM »
Quote from: Birdy
Code: [Select]

Public Sub test()
Call LLINE
    Call LLayer("Layer1", acRed, "HIDDEN")
    Call LLayer("Layer2", acGreen, "DASHED")
End Sub

Private Sub LLINE()
On Error GoTo ErrorHandler
ThisDrawing.Linetypes.Load "HIDDEN", "ACAD.LIN"
ThisDrawing.Linetypes.Load "DASHED", "ACAD.LIN"
Exit Sub

ErrorHandler:
Select Case Err
Case -2145386405:
Err.Clear
Resume Next

Case Else:
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext

End Select
End Sub

Private Sub LLayer(ByRef Lname As String, Lcolor As Integer, Ltype As String)
    Dim objLayer As AcadLayer
    Set objLayer = ThisDrawing.Layers.Add(Lname)
    objLayer.color = Lcolor
    objLayer.Linetype = Ltype
    Set objLayer = Nothing
    Exit Sub
End Sub


Notice I changed the Public subs to Private subs for LLINE and LLAYER.  This does 2 (or more) things
for us. 1 it removes the macros from the list of available macros you can run from ACAD.
2 More importantly, it changes the SCOPE of the variables and functions within our project module.
By declaring the subs Private, all variables inside those subs are private as well.  In a nutshell
this means they are NOT available outside the sub they are contained within.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Birdy

  • Guest
New Project Step 2 Layers
« Reply #54 on: July 27, 2005, 06:49:28 AM »
wow.  That makes things pretty clear(er).
Thanks.  This is starting to gel a little bit.

"...miles to go before I sleep."

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #55 on: July 27, 2005, 03:05:18 PM »
Everyone still following along?  I just about ready to move to checking to see if the layer exists.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #56 on: July 27, 2005, 03:06:39 PM »
Quote from: Keith
One thing I might add to this thread when dealing with optional parameters is to use the IsMissing function to see if the value is missing ...

Code: [Select]
       If Not (IsMissing(Lcolor)) Then
            objLayer.Color = Lcolor
        End If
        If Not (IsMissing(Ltype)) Then
            objLayer.Linetype = Ltype
        End If

I have never used this, but it looks promising.  Is it easier to error trap than blank values (Duh its missing.  Im typing this before I go test it)
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #57 on: July 27, 2005, 03:11:56 PM »
Ok, I tried to use IsMissing, and it didnt work.  Are the Not s   needed?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
New Project Step 2 Layers
« Reply #58 on: July 27, 2005, 03:30:38 PM »
Well, actually I penned that rather hastily ... IsMissing returns True or False ..

If an optional argument of a function is missing, it returns True, otherwise it returns False .. so you would need to check for True or False condition
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
New Project Step 2 Layers
« Reply #59 on: July 27, 2005, 03:37:53 PM »
i'll keep messing with this...
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)