Author Topic: Code to create modules  (Read 3139 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Code to create modules
« on: November 30, 2007, 04:19:54 PM »

I know this "may" sound a little bizarre but has anyone ever written code that creates new sub modules?

For instance, if I have the user do something, like pick points, I want module 1 to store those points in a variable, then possibly use that info to write a new module, module 2 and so on.

It is kind of weird but in a sense, you can have the user actually creating the code for you, depending on their input or event.

Anyone have any examples of this sort of thing?

Thanks

Mark

MP

  • Seagull
  • Posts: 17454
Re: Code to create modules
« Reply #1 on: November 30, 2007, 04:58:23 PM »
Self modifying code is a bit of a black art, and generally speaking, easier in scripting languages like lisp, python etc.

However, little is impossible, so I'd start by referencing the Microsoft Visual Basic for Applications Extensibility library (e.g.  C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB) and then examine the object model (F2).

For example, you might end up playing with something like this --

Code: [Select]
Dim component As VBComponent, _
    subDef    As String, _
    QT        As String
   
QT = Chr$(34)
   
subDef = "Sub Woot ()" & vbCrLf & vbCrLf & _
         vbTab & "Call Msgbox ( " & QT & "Woot!" & QT & ") " & vbCrLf & vbCrLf & _
         "End Sub"

Set component = VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)

component.CodeModule.AddFromString subDef

In the interests of brevity / laziness I'm glossing over MANY details, including but not limited to checking if you've already added a code module, module naming, invoking techniques ... yada.

Have fun but don't hurt any small animals.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

ML

  • Guest
Re: Code to create modules
« Reply #2 on: November 30, 2007, 05:41:01 PM »

Hi MP
I have accessed the extensibility library before for one other thing in the past but I don't think that type library can be checked on via code; therefore, that may already defeat the purpose.

I don't want to have to tell each user that they have to go check that type library on, nor do I want to have to do it for them.

So, that idea (unless the library can be checked on via code) would probably not work too well.

You are right, my boss did something very interesting in LISP using wtf, write to file
When I saw what it did, I then said WTF (not write to file :)
It was pretty cool.

May be in VBA it is not as simple.

Of course, using VB Scripting language (in vba), we can use the text stream methods to write out and read into VBA.
So, we get the user input, write it out, then we would need a (hopefully) simpler way to create a new module and from there read it in.





Mark

MP

  • Seagull
  • Posts: 17454
Re: Code to create modules
« Reply #3 on: November 30, 2007, 05:51:27 PM »
If you load a dvd project that references libraries you don't have to re-reference said libraries; they be, ermmm, sticky.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

ML

  • Guest
Re: Code to create modules
« Reply #4 on: November 30, 2007, 06:01:14 PM »

That makes sense as there is now a reference to that particular library.
That is certainly a good piece of info

Thank you,

Mark




MP

  • Seagull
  • Posts: 17454
Re: Code to create modules
« Reply #5 on: November 30, 2007, 06:06:46 PM »
I don't write self modifying VB code (SMVBC) myself (tangent: while interesting, I think alternative methods can achieve similar results as SMVBC) so I can't help you out much beyond this.

Best of luck -- and you're most welcome.

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Dnereb

  • Guest
Re: Code to create modules
« Reply #6 on: December 02, 2007, 03:30:35 PM »
My two cents...

I do not see why you need to create new modules to store data for creating new data.
A text file, Excel sheet, Access database, Sql express Database, Oracle database, MS SQl database and even a binary file or files should do the job just as well and in the case of the databases retrieving data should be much easier and faster as well.
If you want to store data in memory for later use collections, arrays, dictionary's ar ussually the most obvious way.

And after this rant a question.... Can you explain why storage in a module is really needed and what advantage it would bring.

ML

  • Guest
Re: Code to create modules
« Reply #7 on: December 03, 2007, 09:36:52 AM »

Hi Dnerb,

You rant was actually a very useful for :)
I agree; I am sure there are better methods of storing data and retrieving it later. If you read my post, just above MP's, I did say that I could use The VBScript Textstream method to write out to text files, then to read in when needed. Also, I have never tried the dictionary object methods for anything in VBA, do you have an example of how that might be useful to store data and retreive data?

The reason I want to do something like this is not totally clear yet.
I just had a few thoughts.

I saw someone write a LISP routine using The (wtf) Write to File method, they were retrieving points from where the user picked and some other data, then writing that info to another (probably .txt) file and creating a new Lisp file with it.
So, in essence, the user was actually creating the Lisp routines (dynamically) for him.
I just thought that was very interesting and could be useful for somethings.

Again, Lisp is more ASCII based and might have been better equipped then VB to do these sort of things.
I just wanted to toss it out there and see what people have to say.

We also have methods of dynamic arrays and Redim etc. in VB

So, the whole idea of doing user created modules may just not be a good one at all.

Thoughts?

Thank you,

Mark

MP

  • Seagull
  • Posts: 17454
Re: Code to create modules
« Reply #8 on: December 03, 2007, 09:46:47 AM »
Guess I didn't clue in to the impetus for this thread. If what you're talking about is simply data persistence VB/A is more than capable of addressing a your needs.

In addition to stream (sequential), random and binary file I/O you can also exploit extrinsic libraries like ADO et al.

From your description a simple csv file should suffice.

Like I said earlier, "while interesting, I think alternative methods can achieve similar results as self modifying code".

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

ML

  • Guest
Re: Data Storage and Retrieval
« Reply #9 on: December 03, 2007, 10:59:55 AM »

MP
So you are saying, stream the input to a csv file?

I guess to better summarize the need or question (and I think you guys are addressing it) is data collection and storage basically.

So, let's just use points as our example:

We collect picked point data from the user, then we realize that we may want to use that info at a later time; how can we hold that info until it is needed? Again, it could be streamed out to a csv file, or something like that but may be there is a method to which VBA can hold it long term; then we can call that variable or whatever is holding the info up later when needed. ?

Mark
 

Bob Wahr

  • Guest
Re: Code to create modules
« Reply #10 on: December 03, 2007, 11:13:40 AM »
By "long term", do you mean later in the current session of AutoCAD, or three weeks from now?

MP

  • Seagull
  • Posts: 17454
Re: Code to create modules
« Reply #11 on: December 03, 2007, 11:17:26 AM »
How many points Mark?
Does any associated data have to accompany the point data, e.g. descriptors, dates etc?
Is the data drawing centric?
User centric?
Project centric?
Client centric?
Do you expect the magnitude of data to grow in time?
By how much?
Will the data be used by other applications? e.g. data collectors, total stations etc. Read "Should the data format honor existing industry standards?"
Are you sure?
...

/curiosity killed the cat
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

ML

  • Guest
Re: Code to create modules
« Reply #12 on: December 03, 2007, 11:17:44 AM »
Hi Bob,

Yes, I don't really have a set time but 3 weeks is a good time frame to use.
Definitely not in this session. Once we close ACAD, the variables are cleared, correct?

So, I would like a way to hold the data indefinitely.

Then later, I can say:
With Saved data
Let's use it in this current module or project.

Mark

ML

  • Guest
Re: Code to create modules
« Reply #13 on: December 03, 2007, 11:21:28 AM »

Wo wo wo LOL
MP, you are getting way ahead of me :)

I am just looking for the best method right now.
I said that we can use points as an example but I don't care what we use.

Whatever the data is, I am just looking for the best method.
I guess what you are getting at is, depending on what the data is, will determine the best method to use, correct?

Again, I am even ahead of myself.
I don't have a specific example or immediate need.
I can see a future need for this sort of thing, that is why I am askking.

Mark

Dnereb

  • Guest
Re: Code to create modules
« Reply #14 on: December 03, 2007, 12:01:45 PM »
The main reason to avoid creating modules and (Selfre-)generating code is the fact that it is one off the most virus like behaviors a virus scanner can imagine.

The lisp loophole to create a text file with actual Lisp and load it afterwards is one of the reasons Lisp is concidered to be very unsafe. it was of course developed in a time The internet was merely a private network between university's.

I do not think you have malicious intensions but it's imagineble a user of your code creating utility is, for instance when he is fired. So think thrice before busting all security measurments....