What's best way to save this data?

What's best way to save this data?

Anonymous
Not applicable
576 Views
14 Replies
Message 1 of 15

What's best way to save this data?

Anonymous
Not applicable
I'm going to collect some info from objects in dwg.
In lisp I'd store it momentarily in a list of lists to be operated on later.
eg;
(setq lst
(list
((0 . "lyr1")(( lst1 lst2 ... lstn)))
((0 . "lyr2")(( lst1 lst2 ... lstn)))
((0 . "lyr3")(( lst1 lst2 ... lstn)))
)
)
lst1 lst2 ... lstn would each be in the form (for example)
(
(5 handle)
(10 pointlist)
(11 . real)
(12 . real)
(13 . real)
(14 pointlist)
(15 string)
(16 ename)
etc
)
assuming I'm only going to collect and then act on the info and don't need
to store it in the dwg persistently what kind of object would I want to use
in vb?
collections are just for objects right?
there are no lists per se in vb are there?
would i use arrays of arrays?
or can you have collections of variables?
whats the equivalent of an association list?
or do i need to store in like xrecords or???
or is this where classes come in so an association list would translate to
something like for example
class circle
properties center layer size elevation color - whatever else
where circle1 would be the equivalent of lst1 above
circle2 = lst2 circlen = lstn etc???
or user data type or is that the same as class?
sorry for my ignorance, I've just been reading and reading and searching
posts and archives but I can't seem to get it yet.
Thanks for any help
Mark
0 Likes
577 Views
14 Replies
Replies (14)
Message 2 of 15

Anonymous
Not applicable
Mark,

I think it's fairly safe to say that arrays are VBA's equivalent to list
structures. By setting up a two-dimensional array (think rows and columns in
a spreadsheet or database table), you can store information for one object
in one row of the array, then move down to the next row for the next object,
etc.

Collections are not limited to just objects. You can use them to hold data
of whatever type you want. I wrote a little procedure for you to illustrate
the use of both arrays and collections. I declared the m_CircleData variable
as a private module level variable so it could be used elsewhere in the
module (perhaps to update a list box with fresh data or something). I use
thee array varData to collect information about each circle entity in
modelspace, storing the handle, radius and layer. I then added the array to
the collection and then processed the next circle.

Finally, I loop through the collection (note that it is 1-based instead of
0-based as most arrays are) using a different array to "capture" each item
and then display each circle's data in a MsgBox.

Private m_CircleData As New Collection

Sub test2()
Dim objEnt As AcadEntity
Dim objCircle As AcadCircle
Dim varData(0 To 2) As Variant
Dim varInfo As Variant
Dim I As Integer

For Each objEnt In ThisDrawing.ModelSpace
If TypeOf objEnt Is AcadCircle Then
Set objCircle = objEnt
varData(0) = objCircle.Handle
varData(1) = objCircle.Radius
varData(2) = objCircle.Layer
m_CircleData.Add varData
End If
Next objEnt

For I = 1 To m_CircleData.Count
varInfo = m_CircleData.Item(I)
MsgBox "Handle: " & varInfo(0) & vbCr & _
"Radius: " & varInfo(1) & vbCr & _
"Layer: " & varInfo(2)
Next I
End Sub

Hope that helps.

Ben Rand
LeadenSky Consulting
0 Likes
Message 3 of 15

Anonymous
Not applicable
Ben,

Thanks again.
So if I can store the same info in a collection or an array, what would
determine the preferrable method in any given case? Memory, speed, ease of
writing, amount of data, numbers of items, or numbers of fields for each
item, or???

Do I understand this correctly?:
In your example, if m_CircleData were an array instead of a collection, I
would have had to declare it as dynamic since I don't know how many circles
I'll find presumably?

Dim m_CircleData () As Variant

and then re-dim each time i find another circle to add to it? Is that
right?

Dim i as integer
i = 0

For Each objEnt In ThisDrawing.ModelSpace
If TypeOf objEnt Is AcadCircle Then
Redim m_CircleData (i = i + 1)
Set objCircle = objEnt
varData(0) = objCircle.Handle
varData(1) = objCircle.Radius
varData(2) = objCircle.Layer
m_CircleData(i) = varData
End If
Next objEnt

Is that even close?

Also where does a class fit into all this?
When would I want to make CircleData a class, how does that work, and what
other opportunities for information manipulation would that afford?
Suppose I did want to have the data persistent across drawing sessions? Is
that when I'd need to make them objects(instances of a class) instead of
just circles? An array would not be persistent would it?
It's just like a variable in that sense, right?

Thanks again for your help.
Mark
0 Likes
Message 4 of 15

Anonymous
Not applicable
Mark,

As far as the array goes, yes it has to be dynamic, but you would want to
preserve what was in the array as you redimensioned it. I do this a lot--I
don't use collections nearly enough for this type of task. Collections don't
care how many items you add, you just keep adding until you're done. For
arrays where you need to keep the data you've previously collected, use
"Redim Preserve ..." as shown below.
> In your example, if m_CircleData were an array instead of a collection, I
> would have had to declare it as dynamic since I don't know how many
circles
> I'll find presumably?

> Dim m_CircleData () As Variant
>
> and then re-dim each time i find another circle to add to it? Is that
> right?
>
> Dim i as integer
> i = 0
>
> For Each objEnt In ThisDrawing.ModelSpace
> If TypeOf objEnt Is AcadCircle Then
> Redim m_CircleData (i = i + 1)<===========Redim Preserve
m_CircleData(i=i+1)
> Set objCircle = objEnt
> varData(0) = objCircle.Handle
> varData(1) = objCircle.Radius
> varData(2) = objCircle.Layer
> m_CircleData(i) = varData
> End If
> Next objEnt

Now you're poking into fuzzy areas of my knowledge. Unfortunately I don't
understand how to create classes well enough to give you code examples. I'm
sure classes could make the whole process we're building a lot more powerful
and flexible. However, the actual data you collect (whether you use arrays,
collections or classes) wouldn't be persistent unless you stored that in
some way, perhaps using XData (attached to something in your drawing) or
external files.
> Also where does a class fit into all this?
> When would I want to make CircleData a class, how does that work, and what
> other opportunities for information manipulation would that afford?
> Suppose I did want to have the data persistent across drawing sessions?
Is
> that when I'd need to make them objects(instances of a class) instead of
> just circles? An array would not be persistent would it?

Yes, an array IS just a variable, it's just like an egg carton that can hold
multiple values within the "carton" of one variable name.
> It's just like a variable in that sense, right?


Ben
0 Likes
Message 5 of 15

Anonymous
Not applicable
Mark,

Just so you know, I'm fairly new to VBA and I had to ask myself these same
questions when beginning to create my first app in VB. I decided to go the
route of creating classes to store and manage my data. Ben is correct in
that to store data between drawing sessions, you are going to have to save
that data somewhere and then read it back into your code at a latter date.
If it's info that's not drawing specific, I like to use the registry. If
it's specific to a drawing, I use dictionaries. And if it's specific to an
entity, I'll use xdata. Specific code may require otherwise, but that's the
general rule of thumb that I follow. I'm pretty short on time for the next
few days and can't offer much in the way of code, but if I can point you in
a direction, maybe someone else can help you with some specifics (I'll still
help when I can).

It appears as though your data structure is similar to a project of mine. I
created two class modules that where wrapper classes for a collection object
(I actually use dictionary objects more often than collections, but to keep
things simple we'll use collections in this discussion.) These classes are
set up in an object model type hierarchy. You have to start at the top
level and work your way down the tree to get to lower level objects. Just
like getting to a layer object:

AcadObject
|
|__ActiveDrawing
|
|__LayersObject(collection)
|
|__LayerObject

From your example data your top level class would be a wrapper for a
collection object where the keys would be "lyr1", "lyr2", "lyr3", etc...
For discussion sake, we'll call this class MyLyrs.

Next you'd create a class to represent the next object down the tree. It
would also be a wrapper class for a collection. Its keys would be "lst1",
lst2", "lst3", etc... and you would create a name property that would
corresponded to the key associated to it in the MyLyrs class. We'll call
this class MyLsts.

Finally you'd need one more class to represent the object that you are
storing data in. We'll call it MyCircle. It would not be a collection
wrapper, but instead just a container to store all of the information about
your circles. You'd create properties in this class for each piece of
information that you need to store. You can make them read/write or read
only as your needs dictate. You may also create methods that would perform
actions on the object. The model described above would look like this:

MyLyrs(collection)
|
|__MyLsts(collection)
|
|__MyCirlce

MyLyrs could store any number of MyLsts that you could create and each
instance of MyLsts could store any number of MyCircle instances. The thing
to remember is that you will need to spend a lot more time planning how your
data will be used when building classes like this. You may find that you
could simplify this setup and only create a two branch tree. There are
other things to consider when building this code, but I believe that you'll
find the most flexibility in this type of setup. It's a different way of
thinking and will take some time to get used to but once you start down this
path, you'll never go back.
--
Bobby C. Jones
http://www.acadx.com
0 Likes
Message 6 of 15

Anonymous
Not applicable
Ben,
Thank you again.
Yes, now I remember reading about preserve, thanks for the reminder. I need
to start messing around with dictionaries and xdata too, I've been reading
about them and studying code examples but haven't used them yet.
Mark

"Ben Rand" wrote in message
> As far as the array goes, yes it has to be dynamic, but you would want to
> preserve what was in the array as you redimensioned it. use
> "Redim Preserve ..." as shown below.
>
>
> Ben
>
>
0 Likes
Message 7 of 15

Anonymous
Not applicable
Yo boB <- (you're right it works backward too!)
Many thanks for responding,
Yeah, after posting those ques, I went digging back in the archives for
"classes" and saw your discussions re: working states.
I've been reading about classes for a couple years now in c++ books and vb
and am struggling with my little pea brain to figure out how to use them to
automate some of my tasks and come up with some more 'intelligent' objects.
I get some of the basic concepts but getting the big picture to gel with
what i want to do is taking a long time

>. The thing to remember is that you will need to spend a lot more time
planning how your
>data will be used when building classes like this.

in my case that's an understatement!

Thanks again for the info and ideas. I just have to keep plugging away at
the pseudocode at this point, like you said, to get the organizational
structure figured out.

Mark

"Bobby C. Jones" wrote in message
> Just so you know, I'm fairly new to VBA and I had to ask myself these same
> questions when beginning to create my first app in VB. I decided to go
the
> route of creating classes to store and manage my data.
0 Likes
Message 8 of 15

Anonymous
Not applicable
Hi Ben,

The "redim preserve" command uses lots of computer resources. If you can
find the number of elements first up and do a correct dimension you will be
better off.

eg make a selectionset and then dim from 0 to selectionset.count -1 or 1 to
selectionset.count

With larger data sets you may find it warranted to redim preserve by adding
100 (or a 1000, or whatever) elements at a time, filling those then coping
with the fact that the last elements may be nul.


--




Laurie Comerford
CADApps
www.cadapps.com.au

"Ben Rand" wrote in message
news:A0C6978C4F4E80D018D2E36A58612FFA@in.WebX.maYIadrTaRb...
> Mark,
>
> As far as the array goes, yes it has to be dynamic, but you would want to
> preserve what was in the array as you redimensioned it. I do this a lot--I
> don't use collections nearly enough for this type of task. Collections
don't
> care how many items you add, you just keep adding until you're done. For
> arrays where you need to keep the data you've previously collected, use
> "Redim Preserve ..." as shown below.
> > In your example, if m_CircleData were an array instead of a collection,
I
> > would have had to declare it as dynamic since I don't know how many
> circles
> > I'll find presumably?
>
> > Dim m_CircleData () As Variant
> >
> > and then re-dim each time i find another circle to add to it? Is that
> > right?
> >
> > Dim i as integer
> > i = 0
> >
> > For Each objEnt In ThisDrawing.ModelSpace
> > If TypeOf objEnt Is AcadCircle Then
> > Redim m_CircleData (i = i + 1)<===========Redim Preserve
> m_CircleData(i=i+1)
> > Set objCircle = objEnt
> > varData(0) = objCircle.Handle
> > varData(1) = objCircle.Radius
> > varData(2) = objCircle.Layer
> > m_CircleData(i) = varData
> > End If
> > Next objEnt
>
> Now you're poking into fuzzy areas of my knowledge. Unfortunately I don't
> understand how to create classes well enough to give you code examples.
I'm
> sure classes could make the whole process we're building a lot more
powerful
> and flexible. However, the actual data you collect (whether you use
arrays,
> collections or classes) wouldn't be persistent unless you stored that in
> some way, perhaps using XData (attached to something in your drawing) or
> external files.
> > Also where does a class fit into all this?
> > When would I want to make CircleData a class, how does that work, and
what
> > other opportunities for information manipulation would that afford?
> > Suppose I did want to have the data persistent across drawing sessions?
> Is
> > that when I'd need to make them objects(instances of a class) instead of
> > just circles? An array would not be persistent would it?
>
> Yes, an array IS just a variable, it's just like an egg carton that can
hold
> multiple values within the "carton" of one variable name.
> > It's just like a variable in that sense, right?
>
>
> Ben
>
>
0 Likes
Message 9 of 15

Anonymous
Not applicable
Bob, boB, look at the whole name Bobby C. Jones. Could it get any easier?
Can you tell that my dad is a simple East Texas man? 🙂

Looking back at those working states threads, you see that I had the exact
same questions that you've just asked. Frank O., Joe S., and others steered
me the direction of using classes and I find that I'm now approaching all of
my apps from an object oriented pov. Then I go and read Frank's article on
interfaces and my mouth starts watering as I start thinking about things
such as polymorphism with classes deriving & inheriting from each other!!!
Then I remember that my requisition for Visual Studio still hasn't gone thru
and....and I better stop there before I start sounding bitter 🙂

Don't give up on the object oriented approach. It's different than what you
are used too, but it'll be worth it in the long run.
--
Bobby C. Jones
http://www.acadx.com
0 Likes
Message 10 of 15

Anonymous
Not applicable
Laurie,

Thanks for the input. I do know that resources are in issue with arrays.
There are times when you just can't know ahead of time--searching for the
dwg files in a folder etc. However, I'm wondering if collections would be
more effective in some of those situations. If I collected data and instead
of redimming the array just added the new data to a collection, would that
be any more efficient. I haven't used collections a lot and so am not aware
if they have side effects the way arrays do. I'd certainly like to use a
more efficient process where possible.

Ben
0 Likes
Message 11 of 15

Anonymous
Not applicable
Ben,

I'm hoping someone who has experience with collections will answer so I can
learn with you.

I've only used them for a single assignment of data rather than adding new
data as I discover it.



Laurie Comerford
CADApps
www.cadapps.com.au


"Ben Rand" wrote in message
news:81230BB248D3BB19ED0DEA24F0331285@in.WebX.maYIadrTaRb...
> Laurie,
>
> Thanks for the input. I do know that resources are in issue with arrays.
> There are times when you just can't know ahead of time--searching for the
> dwg files in a folder etc. However, I'm wondering if collections would be
> more effective in some of those situations. If I collected data and
instead
> of redimming the array just added the new data to a collection, would that
> be any more efficient. I haven't used collections a lot and so am not
aware
> if they have side effects the way arrays do. I'd certainly like to use a
> more efficient process where possible.
>
> Ben
>
>
>
0 Likes
Message 12 of 15

Anonymous
Not applicable
When storing an unknown number of items, use a collection. Or even better,
use a dictionary object. This is faster than redim preserve and you get the
inherent benefits that come with collections & dictionaries. You can assign
keys that actually have a meaning, they have an Item method, for
dictionaries you have an Exists method, a Remove method, and many others
that allow a great deal of flexibility that Arrays just don't have. Once
your collection, or dictionary, object is created, wrap it in a class module
to gain yet another level of flexibility...

This is a quote from an adesk employee in a VBA class this year at AU,
"Collections Rock.". We were discussing the differences between using
Arrays vs. Collections...

--
Happy Holidays...
Bobby C. Jones
http://www.acadx.com


"Laurie Comerford" wrote in message
news:6DBE0CCBB0190F63E921AC61D521E430@in.WebX.maYIadrTaRb...
> Ben,
>
> I'm hoping someone who has experience with collections will answer so I
can
> learn with you.
>
> I've only used them for a single assignment of data rather than adding new
> data as I discover it.
>
>
>
> Laurie Comerford
> CADApps
> www.cadapps.com.au
>
>
> "Ben Rand" wrote in message
> news:81230BB248D3BB19ED0DEA24F0331285@in.WebX.maYIadrTaRb...
> > Laurie,
> >
> > Thanks for the input. I do know that resources are in issue with arrays.
> > There are times when you just can't know ahead of time--searching for
the
> > dwg files in a folder etc. However, I'm wondering if collections would
be
> > more effective in some of those situations. If I collected data and
> instead
> > of redimming the array just added the new data to a collection, would
that
> > be any more efficient. I haven't used collections a lot and so am not
> aware
> > if they have side effects the way arrays do. I'd certainly like to use a
> > more efficient process where possible.
> >
> > Ben
> >
> >
> >
>
>
0 Likes
Message 13 of 15

Anonymous
Not applicable
Bobby,
I have just started getting into the class act, having put most of vba routines
into classes in VB6. Actually, the classes I am using are more for housekeeping.

What do you mean when you say 'wrap it in a class module' and exactly how do you
accomplish this in the case of the dictionary? What gain in flexibility would I
experience?
Thx

Tom Craft
Desert Mantech


"Bobby C. Jones" wrote:

> When storing an unknown number of items, use a collection. Or even better,
> use a dictionary object. This is faster than redim preserve and you get the
> inherent benefits that come with collections & dictionaries. You can assign
> keys that actually have a meaning, they have an Item method, for
> dictionaries you have an Exists method, a Remove method, and many others
> that allow a great deal of flexibility that Arrays just don't have. Once
> your collection, or dictionary, object is created, wrap it in a class module
> to gain yet another level of flexibility...
>
> This is a quote from an adesk employee in a VBA class this year at AU,
> "Collections Rock.". We were discussing the differences between using
> Arrays vs. Collections...
>
> --
> Happy Holidays...
> Bobby C. Jones
> http://www.acadx.com
>
0 Likes
Message 14 of 15

Anonymous
Not applicable
Tom,

Look at the thread, "Nested Collections?" on 12/20/01. I posted a skeleton
class that serves as a wrapper class for a collection object.

1. Just like any other data that you store in a class, declare a private
var to hold your data. In this case it's a collection or dictionary object.
2. On class initialization, create the collection and fill it with your
data or provide a separate method to fill it.
3. Create properties and methods to access the items stored in the
collection.
4. Create other properties, methods, and events that you need in your
class.

The flexibility that you have now gained over simply storing the data in an
array is pretty significant. You gain speed by adding data to a collection
and not calling ReDim Preserve on an array. Collections offer Item & Remove
methods which array's don't have. If you use a dictionary instead of a
collection, then you have even more built in methods such as Exists, Key,
Keys, & Items. Some of those methods can be built into a collection, but
some cannot. And if the properties and methods that collections and
dictionaries don't cover your needs, then the class module allows you to
create your own...
--
Happy Holidays...
Bobby C. Jones
http://www.acadx.com
0 Likes
Message 15 of 15

Anonymous
Not applicable
Thanks,
Like a lot of people I am having a difficult time understanding the class
concept, but slowly, sometimes surely, I am gettng there.
Tom Craft

"Bobby C. Jones" wrote:

> Tom,
>
> Look at the thread, "Nested Collections?" on 12/20/01. I posted a skeleton
> class that serves as a wrapper class for a collection object.
>
> 1. Just like any other data that you store in a class, declare a private
> var to hold your data. In this case it's a collection or dictionary object.
> 2. On class initialization, create the collection and fill it with your
> data or provide a separate method to fill it.
> 3. Create properties and methods to access the items stored in the
> collection.
> 4. Create other properties, methods, and events that you need in your
> class.
>
> The flexibility that you have now gained over simply storing the data in an
> array is pretty significant. You gain speed by adding data to a collection
> and not calling ReDim Preserve on an array. Collections offer Item & Remove
> methods which array's don't have. If you use a dictionary instead of a
> collection, then you have even more built in methods such as Exists, Key,
> Keys, & Items. Some of those methods can be built into a collection, but
> some cannot. And if the properties and methods that collections and
> dictionaries don't cover your needs, then the class module allows you to
> create your own...
> --
> Happy Holidays...
> Bobby C. Jones
> http://www.acadx.com
0 Likes