Author Topic: Learning VBA  (Read 14279 times)

0 Members and 1 Guest are viewing this topic.

nivuahc

  • Guest
Learning VBA
« on: October 03, 2003, 09:41:57 AM »
Since I haven't really used VBA in a very long time I've decided that I'm gonna teach myself, from scratch, to use it with AutoCAD.

I'll document what I do and how I do it here so that you can follow along with my progress or, if you already know VBA, point me in the right direction.

This will be open to anyone so feel free to tell me what you think.

To start off, I'll be using a couple of things as a reference.

AfraLisp has a great section on VBA as it pertains to AutoCAD and I highly recommend it.

They have a great article called An Introduction to VB and VBA that you should check out if you're new to VBA.

As my project, I'm going to be creating a VBA application in AutoCAD that takes the place of my companies 'Details' book.

So I'll start by explaining what I hope to accomplish.

The menu I'm using is in a folder called

C:\clc\acadmenu

Under this directory I have a folder called DETAILS which contains sub-folders of my details broken into categories.

We're an electrical engineering firm so our categories are as such:

COMM - Communications Details
LIGHTS - Lighting Details
MECH - Mechanical Details
MISC - Miscellaneous Details
POWER - Power Details

All of my details are named as such:

Comm0001.dwg
Comm0002.dwg
Comm0003.dwg
Comm0004.dwg
Comm0005.dwg
Comm0006.dwg
etc...

and

Lite0001.dwg
Lite0002.dwg
Lite0003.dwg
Lite0004.dwg
Lite0005.dwg
Lite0006.dwg
etc...

So my thinking, at this point, is this:

I have created a text file in the base directory called DETAILS.TXT which contains a list of directories (categories) with a description separated by the pipe (|) symbol

COMM|Communications Details
LIGHTS|Lighting Details
MECH|Mechanical Details
MISC|Miscellaneous Details
POWER|Power Details

In each subdirectory I have a text file, named for the folder, containing a similar list of the detail drawings along with a description such as

Comm0001|Sample Sound System Conduit Schedule
Comm0002|Ceiling Smoke Detector Mounting Detail
Comm0003|Wall Mounted Data/Telephone Outlet Detail
Comm0004|Data/Voice/Video Outlets Detail
Comm0005|Antenna Mounting Detail
etc...

My hope s that I can have the application load the information from the first text file allowing the user to select which type of detail he intends to view/insert.

Once selected, load the information from the corresponding text file and display the description of each type of detail. When the user selects the description they intend to view/insert, a large preview of the drawing will be shown and they have the option of inserting the drawing into AutoCAD.

I'll be using the AcPreview ActiveX Control module for the preview (more on control modules later) and I'll work through the process step by step so, if you like, you can create the same project yourself.

The way I figure it, if I ever want to add another detail or change the description of one it will simply mean modifying the text file pertaining to the category I wish to add/remove from.

And, as always, if you think I might be on the wrong track feel free to say so. Have a better idea? I'm open to suggestion.

So I'm off to read that introduction to VBA and I'll return here and post my thoughts on it when I'm done.

nivuahc

  • Guest
Learning VBA
« Reply #1 on: October 03, 2003, 11:06:15 AM »
Well, if you've read the introduction linked above you now know that Visual Basic for Applications (as well as VB) is an 'Event Driven Application'. In other words, it waits for something to happen before it does anything. That something can be a number of different 'events' like clicking the mouse, pressing a button, or whatever else you decide to put into your application.

The events in our project will be triggered by the objects that we create in our application.

The objects I have in mind are

The Form (A form is the window or dialog box in this project. We're going to draw our controls on this form. It's also going to trigger an event. When it loads, I'll want it to read the contents of my first text file.)

A ComboBox (Usually referred to as a 'pull-down', this is where we'll have the user select the detail category. According to the VBA Reference library a ComboBox combines the features of a ListBox and a TextBox. The user can enter a new value, as with a TextBox, or the user can select an existing value as with a ListBox. Since I don't want them typing in anything, I just want them selecting it maybe this isn't the way to go. I don't know yet.)

A ListBox (Displays a list of values and lets you select one or more. This is where we'll display the description of the details and allow the user to select the one they wish to view/insert)

Command Buttons (Two, actually. One to exit the application an another to insert the selected detail into AutoCAD)

A Preview Box (I intend on showing a large preview of the selected detail since some of our details are similar. This should make it easier for the user to select the right one. This one is special. I'll describe how to get it later.)

More to come...

daron

  • Guest
Learning VBA
« Reply #2 on: October 03, 2003, 11:15:14 AM »
Well that makes it all easier. Now, you double-click the form tool, i.e. checkbox and up pops a window for you to code. Time for the harder part, knowing what to put in.

daron

  • Guest
Learning VBA
« Reply #3 on: October 04, 2003, 02:55:16 AM »
Well, I've gone through this again. Either from the fact that it's almost midnight or their's too much information from the get-go, I don't fully know where this is going. As I read, I feel it's better suited for the Lisp application, since you are using lists. I'm sure that's just the lisper in me. One thing I do know, pseudo-code. Could you write some pseudo-code for the order in which things should happen, including if, then, else, and for each, so we can make this code modular. Also, what's in the drawings that will be inserted and can you post some of the info that will be accessed in the text files? The reason I ask may or may not be relevant, but I have a lisp that goes through a text file that has the same name as the drawing it inserted. It performs a read-file on six lines at a time. If the first line returns true, it processes each of the next five lines, if nil, it reads the next six until it finds a match or reaches the end of the file. I'm not sure how we're going to do this in VBA, but I guess that's why we learn it, right.

nivuahc

  • Guest
Learning VBA
« Reply #4 on: October 06, 2003, 08:39:35 AM »
Quote
I'm not sure how we're going to do this in VBA, but I guess that's why we learn it, right.


Exactly.

This is what I intend to have happen:

1. The form opens with a pull-down menu where the user selects the detail category.
I'd have the form, on load, read the text file DETAILS.TXT that I have listed above

2. Once a category is selected, the list of detail descriptions in that category are displayed in a ListBox.
Each of the entries in DETAILS.TXT corresponds to a directory containing detail drawings. Each of those directories also contain a text file with the name of each drawing followed by a description as shown above.

3. When the user selects a description from the ListBox it shows a preview of the drawing using the AcPreview ActiveX control module.


4. The click the 'Insert Detail' button and it inserts the drawing into the current AutoCAD drawing.


My reasons for using VBA in this instance, instead of LISP, are simple:

First, I need to re-learn VBA. Second, I really like that AcPreview control and I think it's perfect for this type of application. :)

Hopefully, I'll get to spend some time on this today.

Trev

  • Guest
Learning VBA
« Reply #5 on: October 06, 2003, 09:58:40 AM »
Hey nivuahc  I started (a while ago) working on something very similar to what you have described.
A basic block manager, containing an option to select desired service ie:Electrical then option buttons to select either lighting, power, comms etc
upon selection a list box would display all the blocks relating to selection ie:power. also showing a preview of highlighted block. I have also added an option to add in addition blocks to the library which would then copy the block to the appropriate folder and place it into the listbox.

I made a sample type version of this to show a company I worked with a tool that they could do with, Since theyre symbols library etc was so crap.
I didn't get much response so I never got around to finishing the program off. It would be a very handy tool.

nivuahc

  • Guest
Learning VBA
« Reply #6 on: October 06, 2003, 11:40:31 AM »
Trev,

Would you mind, terribly, posting it here? I'm sure it would help me develop this one.

In fact, that's how I first learned LISP. I would take a routine that I sorta liked and go through it tweaking it to my liking.

And I'm sure it would be helpful to anyone else who's trying to get a handle on this! :)

hendie

  • Guest
Learning VBA
« Reply #7 on: October 17, 2003, 07:39:19 AM »
Quote from: nivuahc

A ComboBox (Usually referred to as a 'pull-down', this is where we'll have the user select the detail category. According to the VBA Reference library a ComboBox combines the features of a ListBox and a TextBox. The user can enter a new value, as with a TextBox, or the user can select an existing value as with a ListBox. Since I don't want them typing in anything, I just want them selecting it maybe this isn't the way to go. I don't know yet.)


just set the Styleproperty of the combobox to suit:
Quote

The settings for fmStyle are:

fmStyleDropDownCombo 0   
The ComboBox behaves as a drop-down combo box. The user can type a value in the edit region or select a value from the drop-down list (default).
fmStyleDropDownList 2   
The ComboBox behaves as a list box. The user must choose a value from the list.


alternatively, you could zap the user with a "Daily quote from Dent" if the value typed in isn't in your list !

Trev

  • Guest
Learning VBA
« Reply #8 on: October 17, 2003, 07:46:35 AM »
Yep No problems nivuahc
I have a few things to tidy it up a bit to make it a little more functional
then I'll post what I've got.

nivuahc

  • Guest
Learning VBA
« Reply #9 on: October 17, 2003, 09:14:00 AM »
I've spent the little amount of spare time I have trying to figure out how to take the information in the TXT files and put it on the form making it so that when the user selects something it sets the value as a variable.

I'm having very little luck. :(

daron

  • Guest
Learning VBA
« Reply #10 on: October 17, 2003, 10:25:00 AM »
Well Chuck, if it's any consolation, I've been reading a book on VBA for Excel. A friend gave it to me. Anyway, I tried making a vba function the other day and it bombed. I'm still trying to figure out the difference between a module, a class module, and the objects folder that contains thisdrawing as a default. How do you know which one to use and when.

hendie

  • Guest
Learning VBA
« Reply #11 on: October 20, 2003, 04:14:16 AM »
Quote from: nivuahc
I've spent the little amount of spare time I have trying to figure out how to take the information in the TXT files and put it on the form making it so that when the user selects something it sets the value as a variable.

I'm having very little luck. :(


which bit are you having problems with ?

I assume you have dim(med) the variable ? in which case, when the button is pressed or whatever event triggers it,
Code: [Select]

myVar = theComboBox.value

hendie

  • Guest
Learning VBA
« Reply #12 on: October 20, 2003, 04:26:41 AM »
and might I offer a suggestion ?
instead of using a text file ~ an Access database may be a more useful / versatile tool.


have you pseudo-coded your proposed app ? and listed all the functions you want it to perform ?
a mock-up of the interface is also useful to have

Trev

  • Guest
Learning VBA
« Reply #13 on: October 20, 2003, 08:56:07 AM »
Actually Hendie thats a good idea, easy to edit an existing entry etc. etc.
Just spark a whole load of new ideas in my head     :shock:
Damn you.................
 :P

nivuahc

  • Guest
Learning VBA
« Reply #14 on: October 20, 2003, 10:22:51 AM »
Well my thinking on the TXT file, as opposed to a DB is that

1. My company is cheap. I have Access but I'm the only one.
2. If, for any reason, someone else wants to add to the list at a later time, using a TXT file is the easiest way for anyone to do it. Like say, for instance, I stop working here.

And the problem I'm having is this:

I've got the TXT file (formatted as it is above).

I've got a form.

How do I get the data in the TXT file displayed in the form (either in the combo box or the list box)?

Form opens, reads the contents of DETAILS.TXT and lists the descriptions in the combo box.

A user selects a description and that sets a variable to POWER or COMM depending on the description they chose.

With the format being

POWER|Power Details
COMM|Communications Details
etc...

as single lines of text in the DETAILS.TXT file.

So let's say the user selects 'Communication Details' from the list.

That sets a variable (let's call it VAR1 for now) to COMM and the program looks for a folder called COMM containing a TXT file called _COMM.TXT (named that way so that it's always at the top of the list when I open the folder).

Inside that TXT file is a list of details and descriptions like this:

Comm0001|Sample Sound System Conduit Schedule
Comm0002|Ceiling Smoke Detector Mounting Detail
Comm0003|Wall Mounted Data/Telephone Outlet Detail
Comm0004|Data/Voice/Video Outlets Detail
Comm0005|Antenna Mounting Detail

The program lists all of the descriptions in the list box, not the 'Comm0001' bit, and the user selects 'Wall Mounted Data/Telephone Outlet Detail' from the listbox.

A variable (let's call it VAR2 for now) is set to 'Comm0003' and the drawing 'Comm0003' is displayed in the extra large preview window.

Should the user select the button labelled 'Insert Detail into AutoCAD' the drawing is inserted into the current drawing.

Should the user change their mind and select a different description from the list, say 'Sample Sound System Conduit Schedule', the variable VAR2 would be changed to 'Comm0001' and so on.

Should the user decide that what they wanted was actually a POWER detail and the select that from the combo box the value of VAR1 changes to 'POWER' and so on.


That make any sense?