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

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28683
Re: Update to this
« Reply #15 on: July 21, 2005, 05:02:49 PM »
Quote from: CmdrDuh
Question - Is everyone familiar with the concept of passing a Sub/Function an Argument?  I ask because I want us to reduce the amount of typing that went into our code.

I am.
TheSwamp.org  (serving the CAD community since 2003)

jonesy

  • SuperMod
  • Seagull
  • Posts: 15502
New Project Step 2 Layers
« Reply #16 on: July 21, 2005, 05:10:38 PM »
Please Sir

I am now (I think!)
Thanks for explaining the word "many" to me, it means a lot.

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Passing an Argument By Calling a Sub
« Reply #17 on: July 22, 2005, 12:34:22 PM »
Consider the following snippet:
Code: [Select]
Public Sub LayerTestExtraTyping()

Dim Ylayer As AcadLayer
Dim Wlayer As AcadLayer

Set Ylayer = ThisDrawing.Layers.Add("Layer1")
Ylayer.color = acYellow

Set Wlayer = ThisDrawing.Layers.Add("Layer2")
Wlayer.color = acGreen

End Sub

If you look close, you see that we typed the exact same thing twice for the creation of 2 layers.  We declared 2 objects, we set both objects, and we set the color of the objects.  For just 2 layers, you can see the duplicate effort already.  What we need is a function/sub that would take a list of arguments (Name, Color, & Linetype) and process the list so we do not have to retype the exact same code over and over.  Looking at this snippet, you will see variable names that Im passing in from another 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

OK, so how does this work? Well, we have to CALL the sub and pass the arguments to it.  From the help file...
Quote from: VBA Help File
Syntax [Call] name [argumentlist]

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

^This will be very important to us in a few minutes.^
Quote from: VBA Help File

ArgumentList - Optional. Comma-delimited list of variables, arrays, or expressions to pass to the procedure. Components of argumentlist may include the keywords ByVal or ByRef to describe how the arguments are treated by the called procedure. However, ByVal and ByRef can be used with Call only when calling a DLL procedure.

ByVal Optional. Indicates that the argument is passed by value.
ByRef Optional. Indicates that the argument is passed by reference. ByRef is the default in Visual Basic.

by value
A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.

by reference
A way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference.

So, we need another sub to call the layer creation sub.
Code: [Select]
Public Sub test()
    Call LLayer("Layer1", acWhite, "HIDDEN")
    Call LLayer("Layer2", acGreen, "DASHED")
End Sub
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

daron

  • Guest
New Project Step 2 Layers
« Reply #18 on: July 22, 2005, 12:58:48 PM »
I assume that LLayer sub can be called from a separate file, like you can with lisp?

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
New Project Step 2 Layers
« Reply #19 on: July 22, 2005, 01:06:22 PM »
yes and no. If its in the same module, it can be called as is (the way I typed it)  If its in a different module, you have to call it by its full name (file.module.procedure name)
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

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
New Project Step 2 Layers
« Reply #20 on: July 22, 2005, 01:33:56 PM »
Once everyone gets their heads wrapped around this, we will move on to the next step, 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

Birdy

  • Guest
New Project Step 2 Layers
« Reply #21 on: July 22, 2005, 02:39:54 PM »
Quote from: CmdrDuh
^ Need a show of hands to move on.

<out of breath>  <raises hand>
gnarly week... gotta play catchup this weekend.
oh, thank you Kerry.

daron

  • Guest
New Project Step 2 Layers
« Reply #22 on: July 22, 2005, 02:40:40 PM »
Quote from: CmdrDuh
yes and no. If its in the same module, it can be called as is (the way I typed it)  If its in a different module, you have to call it by its full name (file.module.procedure name)

That's what I was wondering. So, there's nothing like (load "somecode.lsp") for vba? You have to type where it comes from, huh? That could be useful incase you have more than one sub with the same name.

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
New Project Step 2 Layers
« Reply #23 on: July 22, 2005, 05:25:16 PM »
..... Got code ready for monday
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 #24 on: July 23, 2005, 08:27:01 AM »
Ok.. starting to add up.
I like this:
Code: [Select]
Dim ColorProperty As Integer
    Ylayer.color = acYellow


Question: acYellow doesn't LOOK like an integer to me.  Is that because it is an enumerated value?
From the help file:
Quote
You can also use Integer variables to represent enumerated values. An enumerated value can contain a finite set of unique whole numbers, each of which has special meaning in the context in which it is used. Enumerated values provide a convenient way to select among a known number of choices, for example, black = 0, white = 1, and so on.


This make sense to me too, and seems rather simple:
Code: [Select]
Dim Newlayer As AcadLayer
    Set Newlayer = ThisDrawing.Layers.Add("FourtyOne")
    Newlayer.color = 41


Thanks Kerry

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
New Project Step 2 Layers
« Reply #25 on: July 23, 2005, 08:51:13 AM »
Hi Birdy,
acYellow is a Constant actually ..

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

Birdy

  • Guest
New Project Step 2 Layers
« Reply #26 on: July 23, 2005, 09:39:26 AM »
Thanks.  That clears it up even more.

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
New Project Step 2 Layers
« Reply #27 on: July 23, 2005, 05:53:24 PM »
Quote from: daron
So, there's nothing like (load "somecode.lsp") for vba?

Guru's correct me, but off the top of my head, I can not think of how to load another DVB from within VBA. (I have never done it, so it may be possible)

I typically write all the code I need for a particular Function/program in one or more modules (depending on what im doing).  I do have some utility code I CALL from other modules (Files) but the utility code is already loaded.  I just had a thought, I seem to remember a snippet somewhere to UNLOAD a dvb, so there must be a way to load a dvb.
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

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16722
  • Superior Stupidity at its best
New Project Step 2 Layers
« Reply #28 on: July 23, 2005, 08:26:44 PM »
Ok ... there is some fact and some fiction in that....

You cannot reference anything that is not part of the project, for example:

-> Project2 code
Project1.MyModule.MyProc()

However .....

Look into the "AddFromFile" method ...you may find it somewhat helpful ...

Or you can use

AcadApplication.LoadDVB
AcadApplication.UnloadDVB
AcadApplication.RunMacro

Each one with it's specific purpose ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
New Project Step 2 Layers
« Reply #29 on: July 24, 2005, 11:20:27 PM »
I have never used those, but it sounds like I should look into 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