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

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
New Project Step 2 Layers
« Reply #30 on: July 25, 2005, 09:08:42 AM »
Thanks for answering that Keith. I have always found vba to be somewhat limiting in that respect. I guess it has to do with keeping things structured.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
New Project Step 2 Layers
« Reply #31 on: July 25, 2005, 02:49:47 PM »
Everybody ready to move on?
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

jonesy

  • SuperMod
  • Seagull
  • Posts: 15567
New Project Step 2 Layers
« Reply #32 on: July 25, 2005, 03:43:20 PM »
I think so.
Thanks for explaining the word "many" to me, it means a lot.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
New Project Step 2 Layers
« Reply #33 on: July 26, 2005, 02:32:43 PM »
OK, before we do a function for if the layer exists, I wanted to talk about OverLoading.  VBA does not Overload like C#, (which is where I got this from), but it does allow us to kinda-not really-sorta-maybe maybe not overload a function.  I know that none of that made any sense.  Thats OK, humor me.  I'm sure someone will correct me, but thats ok too.

Code: [Select]
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
What if you knew the name and color, but not the linetype?  Name and Linetype, but not color?  What happens if you leave out a value that the function is expecting?  Where I'm going with this is we could have 3-5 versions of this code to accomplish each version we could think of.

Code: [Select]
Public Sub LLayer(ByRef Lname As String)
    Dim objLayer As AcadLayer
    Set objLayer = ThisDrawing.Layers.Add(Lname)
    Set objLayer = Nothing
    Exit Sub
End Sub
Code: [Select]
Public Sub LLayer(ByRef Lname As String, Lcolor As Integer)
    Dim objLayer As AcadLayer
    Set objLayer = ThisDrawing.Layers.Add(Lname)
    objLayer.color = Lcolor
    Set objLayer = Nothing
    Exit Sub
End Sub
Code: [Select]
Public Sub LLayer(ByRef Lname As String, Ltype As String)
    Dim objLayer As AcadLayer
    Set objLayer = ThisDrawing.Layers.Add(Lname)
    objLayer.Linetype = Ltype
    Set objLayer = Nothing
    Exit Sub
End Sub
Code: [Select]
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
« Last Edit: January 11, 2006, 08:10:01 AM by CmdrDuh »
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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
New Project Step 2 Layers
« Reply #34 on: July 26, 2005, 02:40:53 PM »
Does everybody understand the 4 versions I loaded?  What if we could do the same thing with ONLY 1 version of the code?
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

Birdy

  • Guest
New Project Step 2 Layers
« Reply #35 on: July 26, 2005, 02:49:30 PM »
The main difference I see (quick glance) is the
Code: [Select]
Public Sub LLayer(ByRef...)
line. Am I on the right track?

daron

  • Guest
New Project Step 2 Layers
« Reply #36 on: July 26, 2005, 02:53:23 PM »
Are you saying you need optional arguments or default to what's current?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
New Project Step 2 Layers
« Reply #37 on: July 26, 2005, 02:53:41 PM »
heh  David.

must admit my ears pricked up when I saw VBA and overloading in the same sentence
.. for a  bit anyway.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
New Project Step 2 Layers
« Reply #38 on: July 26, 2005, 02:58:03 PM »
Here is a bit of code that uses the Keyword Optional.
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


When we CALL this code, we can pass our arguments normally, but now if we do not know one of them or we do not wish to change from the default, we do not have too.

Here is 4 examples of code using the optional arguments
Code: [Select]
Public Sub SetupDwgWithLayersAndLinetypes()
    LLINE
    Call LLayer("Layer1", acRed, "HIDDEN")
    Call LLayer("Layer2")
    Call LLayer("Layer3", acBlue)
    Call LLayer("Layer4", , "DASHED")
End Sub

Each of the arguments must be separated by commas (,).
For Layer1, we are using all 3 arguments.  Layer2 is just the name.  Layer3 uses Name and Color. and Layer4 is Name and Linetype.  
Notice in Layer4, we added a comma (,) to show that color was blank.
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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
New Project Step 2 Layers
« Reply #39 on: July 26, 2005, 02:59:45 PM »
Quote from: Kerry Brown
heh  David.

must admit my ears pricked up when I saw VBA and overloading in the same sentence
.. for a  bit anyway.

Please correct me where I'm wrong.  Im hoping you and MickD steer me straight on this.  I know the terminology is wrong, but its what I know.
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

Birdy

  • Guest
New Project Step 2 Layers
« Reply #40 on: July 26, 2005, 03:02:23 PM »
btw, I'm trying to combine the layers and linetypes by doing this:
Code: [Select]
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
Public Sub test()
    Call LLayer("Layer1", acRed, "HIDDEN")
    Call LLayer("Layer2", acGreen, "DASHED")
End Sub


but it seems that I have to alt+f8 two seperate times (that is.. two seperate files?) to get it to work as I'd expect.  Am I missing something in how I'm saving and/or loading these? (will re-review the primers this evening, DOH!)

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:

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
New Project Step 2 Layers
« Reply #41 on: July 26, 2005, 03:06:53 PM »
Quote from: Birdy
btw, I'm trying to combine the layers and linetypes by doing this:
Code: [Select]
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



Public Sub test()
Call LLine ' Cmdrduh Added this to load Linetypes before creating layers
    Call LLayer("Layer1", acRed, "HIDDEN")
    Call LLayer("Layer2", acGreen, "DASHED")
End Sub


but it seems that I have to alt+f8 two seperate times (that is.. two seperate files?) to get it to work as I'd expect.  Am I missing something in how I'm saving and/or loading these? (will re-review the primers this evening, DOH!)

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:

Look at the code for the sub TEST.  I added one line to load your linetypes just before you create your layers.  You should only have to run Test 1 time to get what you want.
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

Birdy

  • Guest
New Project Step 2 Layers
« Reply #42 on: July 26, 2005, 03:08:15 PM »
:)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
New Project Step 2 Layers
« Reply #43 on: July 26, 2005, 03:09:32 PM »
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.
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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
New Project Step 2 Layers
« Reply #44 on: July 26, 2005, 03:10:06 PM »
Overloading is the correct term David.

essentially having multiple method definitions, with the same name, but each with its own unique signature < Type and order of parameters >
C# does not support optional method arguments.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--