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

0 Members and 1 Guest are viewing this topic.

Bob Wahr

  • Guest
Re: Code to create modules
« Reply #15 on: December 03, 2007, 12:22:32 PM »
I would definitely write it to an external file then.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Code to create modules
« Reply #16 on: December 03, 2007, 12:31:43 PM »
MP, you are getting way ahead of me

It may appear that way Mark but I'm trying to underscore the importance of identifying these things before you commit anything to code. You do want to minimize code maintenance issues as much as you can from the onset and asking these kinds of questions before you start is a lot cheaper than code / data file rewrites down the road. To answer your question "What is the best method" ... well it depends largely upon those kinds of questions being asked and answered. I'm not trying to be funny, it's the nature of the beast.

Subtitle: Everything you do should be done to a spec. So, work with me, let's define a simple spec to define your requirements. I'll start.



1.  Drawing centric.

    We'll use the current drawing name (if named), replacing the ".dwg" extension with ".points"
   
    Example:
   
        MyDrawing.points

2.  Format is Point_ID,Point_Type,Point_Descr,Point_X, Point_Y, Point_Z
   
    Point_ID   = long integer

    Point_Type = string, 2 characters (a coded integer would be more efficient but less self documenting)
   
          "BS" = beginning of segment / curve
          "ES" = end of segment / curve
          "PS" = point on curve / segment
          "AP" = arbitrary point

    Point_Desc = string, max 32 characters.

    Point_X    = double precision
    Point_Y    = double precision
    Point_Z    = double precision

    Example record:
   
        42,"AP","Sample",1280.2370,988.3768,0.0000
   
3.  File format must support comments

    We'll define any line that starts with at least one semi colon (;) as being a comment line, ignored by the data retrieval code.
   
    Example:
   
        ;;  the following is a sample data file, created 2007/11/03
        42,"AP","Sample",1280.237,988.3768,0.0
        43,"AP","Yada",1261.4934,979.4054,0.0
        44,"AP","Yada",1279.1810,970.9617,0.0
   
4.  Data retrieval code must

    • ignore blank lines
    • ignore extra white space between fields
    • process point_type case agnosticly
    • qualify data; sanity check on unique Point_IDs, report records with bogus data (but not crash) etc.
    • etc.

5.  etc.




Notice, I'm not writing any code yet. That's because the spec is likely going to change. For example, making the data file drawing centric is probably not a good idea. Once the data is committed to entities any drawing association is likely moot -- so what is a better idea?

/Back to you
« Last Edit: December 03, 2007, 12:51:36 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ML

  • Guest
Re: Code to create modules
« Reply #17 on: December 03, 2007, 05:30:29 PM »

Hi MP,

I really do appreciate you defining all of those types for me; I did not even know what drawing centric meant  LOL
If I had to take a stab at it, I might have guessed it was at drawing level or drawing related.

I would agree with Bob and to where I initially started that writing to an external file or data source may be the best way to go.

I have done this before a bit with writing to excel and text files using The textstream method and I have also read the data back in; I guess the next step might have been trying to assign that external data to a new variable and doing something (significant) with it. I think I successfully (in the past) looped (int erated) through my layers collection, wrote it out to a text file and was able to read it back in, then return the layers via a message box, which then was really cool. So, with this method, I can do it, I guess I just need to expound on it.

Also, you did touch on that method in your last paragraph

Quote
4.  Data retrieval code must

    • ignore blank lines
    • ignore extra white space between fields
    • process point_type case agnosticly
    • qualify data; sanity check on unique Point_IDs, report records with bogus data (but not crash) etc.
    • etc.

Your first two lines can be done with The VBScript, textstream method.

Thanks,
Mark


ML

  • Guest
Re: Code to create modules
« Reply #18 on: December 03, 2007, 05:44:09 PM »

OK, while this example is nothing mind blowing, this method (using VB Scripting code in VBA) can be very useful:

In this example I am simply writing my Support and Search Paths to a text file called Supportpaths.txt

Code: [Select]
Sub WriteToATextFile()

'Write Support Paths to a text File
Dim WshNetwork
Dim FSO, MyFile, Username
Dim Allpaths As String

Set WshNetwork = CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")

Allpaths = Preferences.Files.SupportPath
Username = WshNetwork.Username

Set MyFile = FSO.CreateTextFile("C:\Documents and Settings\" & Username & "\Desktop\SupportPaths.txt", True)

MyFile.WriteLine Replace(Allpaths, ";", vbCrLf)

End Sub

Now, with this method, I am reading from the same text file (located on the desktop) and assigning the (paths) strings of text to a variable called Textstr and displaying the paths (variable) with a message box.

Code: [Select]
Sub ReadTextFile()

Dim FSO As Variant
Dim Stream As Variant
Dim Username As Variant
Dim Textstr As String

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = CreateObject("WScript.Network")

Username = WshNetwork.Username

Set Stream = FSO.OpenTextFile("C:\Documents and Settings\" & Username & "\Desktop\SupportPaths.txt")
'Set FSO = Nothing

Textstr = ""


'While there are lines to read, continue on to the end of the file.
'If the file is empty, once we reach the end of the file, the variable Textstr will be returned as null.
Do While Not Stream.AtEndOfStream
 Textstr = Textstr & Stream.ReadLine & vbCrLf 'or VbNewline
Loop
'Set Stream = Nothing

If Textstr <> "" Then
 MsgBox Textstr
Else
 MsgBox "The file you selected is empty"
End If
 
End Sub

Mark


Bob Wahr

  • Guest
Re: Code to create modules
« Reply #19 on: December 03, 2007, 10:54:08 PM »
OK, my challenge for you now is to do exactly what you have there without VB Scripting.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Code to create modules
« Reply #20 on: December 04, 2007, 07:07:28 AM »
Adding to what Bob said, using solely intrinsic functions (no external libraries).

PS: In my opinion you should explicitly close all files you open for read or write.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ML

  • Guest
Re: Code to create modules
« Reply #21 on: December 04, 2007, 03:58:15 PM »

OK, so then we put

Myfile.close

Or in the second example

Stream.close

So, is the bottom line that the metohd I posted the preferrable way to handle what I am asking?

Thank you,

Mark

ML

  • Guest
Re: Code to create modules
« Reply #22 on: December 07, 2007, 05:07:23 PM »

Here we go:

The answer was right in the help screen; I just tweaked it a bit.
This is a very basic example , however with so creativity, this can be very useful.

Mark

Code: [Select]
Sub CreateSub()

'This example uses the VBA IDE extensibility model to dynamically create a VBA subroutine.
 Dim VBEModel As Object
 Dim SubName As String
 Dim EndOfSub As String
 Dim CodeLn As String
 
'Get the VBE object
 Set VBEModel = VBE
 
'Define and create new subroutine
  SubName = "Sub Macro1 ()" & vbCrLf
  CodeLn = "MsgBox ""This is a test""" & vbCrLf
  CodeLn = CodeLn & "MsgBox Preferences.Profiles.ActiveProfile, vbApplicationModal" & vbCrLf
  EndOfSub = "End Sub"
 
 
 'Insert New Subroutine
 VBEModel.CodePanes(1).CodeModule.InsertLines 1, SubName & CodeLn & EndOfSub
 
 MsgBox "A new subroutine called " & SubName & "was added"
 
End Sub

ML

  • Guest
Re: Code to create modules
« Reply #23 on: December 07, 2007, 05:17:19 PM »

With the above code:
Could anyone tell me how to create a Sub Macro2 () If Macro1 already exists? And so on..... ?

Thanks

Mark

ML

  • Guest
Pratical Example
« Reply #24 on: December 10, 2007, 10:58:14 AM »

Here is an example that works and does something:

You will be prompted with an input box; type anything in it and watch what happens.
On the same token, you could retrieve info from a user and start building a new sub with it.
I am still not sure how to append to an existing module or how to create Macro2 if 1 exists and so on.
I suppose there is probably a way to loop though all subs in a project then
 If Macro1 exists
  Create Macro2
 End if

Anyone want to weigh in?

Mark

Code: [Select]
Sub CreateSub()
'This example uses the VBA IDE extensibility model to dynamically create a VBA subroutine.

 Dim VBEModel As Object
 Dim SubName As String
 Dim EndOfSub As String
 Dim Ln As String
   
'Get the VBE object
 Set VBEModel = VBE
 
'Define and create new subroutine
  SubName = "Sub Macro1 ()" & vbCrLf
  Ln = "Dim StrTxt as String" & vbCrLf
  Ln = Ln & "StrTxt = " & """" & InputBox("Please type something") & "" & vbCrLf
  Ln = Ln & "MsgBox StrTxt" & vbCrLf
  EndOfSub = "End Sub"
  'MsgBox Ln
 
'Insert New Subroutine
 VBEModel.CodePanes(1).CodeModule.InsertLines 1, SubName & Ln & EndOfSub
 
 'MsgBox "A new subroutine called " & SubName & "was added"
End Sub