Author Topic: Better way to write this  (Read 9307 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Better way to write this
« on: June 21, 2006, 02:35:28 PM »
I'm trying to compress my dvb files to make things simpler, and was wondering if anyone could compress this sub smaller.   This sub is replacing 6-8 other subs by using the Select Case.  If that doesn't make sense, I'll try and explain better.

Code: [Select]
Public Sub BATCHP(SIZE As String)
    Dim currentline As String
    Dim PLOTTYPE As String


    Open "c:\dwgnum.dat" For Input As 1
    While Not EOF(1)
        Line Input #1, currentline
        Documents.Open currentline
        Debug.Print currentline

        ThisDrawing.Regen acAllViewports
        Select Case SIZE
        Case "VA"
            Call SetupAndPlot("11x17Draft.pc3", "STANDARDS.ctb", "Business_Letter_(8.50_x_11.00_Inches)", ac1_1, ac0degrees)
            ThisDrawing.Plot.PlotToDevice
            ThisDrawing.Close (True)
        Case "V11"
            Call SetupAndPlot("11x17Draft.pc3", "11X17-CHECKSET.ctb", "ANSI_B_(11.00_x_17.00_Inches)", acScaleToFit, ac90degrees)
            ThisDrawing.Plot.PlotToDevice
            ThisDrawing.Close (True)
        Case "VC"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "VENDOR MEDIUM.ctb", "ARCH_expand_C_(24.00_x_18.00_Inches)", acScaleToFit, ac0degrees)
            ThisDrawing.Plot.PlotToDevice
            ThisDrawing.Close (True)
        Case "VD"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "VENDOR MEDIUM.ctb", "ARCH_expand_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
            ThisDrawing.Plot.PlotToDevice
            ThisDrawing.Close (True)
        Case "T11"
            Call SetupAndPlot("11x17Draft.pc3", "11X17-CHECKSET.ctb", "ANSI_B_(11.00_x_17.00_Inches)", acScaleToFit, ac90degrees)
            ThisDrawing.Plot.PlotToDevice
            ThisDrawing.Close (True)
        Case "TC"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "tep.ctb", "ARCH_expand_C_(24.00_x_18.00_Inches)", acScaleToFit, ac0degrees)
            ThisDrawing.Plot.PlotToDevice
            ThisDrawing.Close (True)
        Case "TD"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "tep.ctb", "ARCH_expand_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
            ThisDrawing.Plot.PlotToDevice
            ThisDrawing.Close (True)
        Case Else
            Call Vendor1117
        End Select
    Wend
    Close 1
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Better way to write this
« Reply #1 on: June 21, 2006, 02:36:14 PM »
here is the SetupAndPlot sub
Code: [Select]
Public Sub SetupAndPlot(ByRef Plotter As String, CTB As String, SIZE As String, PSCALE As String, ROT As String)
    Dim Layout As AcadLayout
    On Error GoTo Err_Control
    Set Layout = ThisDrawing.ActiveLayout
    Layout.RefreshPlotDeviceInfo
    Layout.ConfigName = Plotter    ' CALL PLOTTER
    Layout.PLOTTYPE = acExtents
    Layout.PlotRotation = ROT    ' CALL ROTATION
    Layout.StyleSheet = CTB    ' CALL CTB FILE
    Layout.PlotWithPlotStyles = True
    Layout.CanonicalMediaName = SIZE    ' CALL SIZE
    Layout.PaperUnits = acInches
    Layout.StandardScale = PSCALE    'CALL PSCALE
    Layout.ShowPlotStyles = False
    ThisDrawing.Plot.NumberOfCopies = 1
    Layout.CenterPlot = True
    Layout.ScaleLineweights = False
    Layout.RefreshPlotDeviceInfo
    ThisDrawing.Regen acAllViewports
    ZoomExtents
    Set Layout = Nothing
    ThisDrawing.Save
Exit_Here:
    Exit Sub

Err_Control:
    Select Case Err.Number
    Case "-2145320861"
        MsgBox "Unable to Save Drawing- " & Err.Description
    Case "-2145386493"
        MsgBox "Drawing is setup for Named Plot Styles." & (Chr(13)) & (Chr(13)) & "Run CONVERTPSTYLES command", vbCritical, "Change Plot Style"
    Case Else
        MsgBox "Unknown Error " & Err.Number
    End Select
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Better way to write this
« Reply #2 on: June 21, 2006, 02:53:35 PM »
This is what i am replacing
Code: [Select]
Public Sub BATCHPLOT1824()
    Dim currentline As String
    Open "c:\dwgnum.dat" For Input As 1
    While Not EOF(1)
        Line Input #1, currentline
        Documents.Open currentline
        Debug.Print currentline

        ThisDrawing.Regen acAllViewports
        Call VendorQuickPlotC

    Wend
    Close 1
End Sub

Public Sub BATCHPLOT2436()
    Dim currentline As String
    Open "c:\dwgnum.dat" For Input As 1
    While Not EOF(1)
        Line Input #1, currentline
        Documents.Open currentline
        Debug.Print currentline

        ThisDrawing.Regen acAllViewports

        ThisDrawing.MSpace = False
        Call VendorQuickPlotD

    Wend
    Close 1
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bob Wahr

  • Guest
Re: Better way to write this
« Reply #3 on: June 21, 2006, 04:09:50 PM »
It looks pretty streamlined to me.  You could move the plot and close to the SetupandPlot sub.  Then again, you can leave them.  Looks good.

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Better way to write this
« Reply #4 on: June 21, 2006, 04:15:04 PM »
It looks pretty streamlined to me.  You could move the plot and close to the SetupandPlot sub.  Then again, you can leave them.  Looks good.

You sound like a weatherman.

[weatherman voice]It might rain today, then again it might not[/weatherman voice]

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Better way to write this
« Reply #5 on: June 21, 2006, 06:41:42 PM »
There is nothing really wrong with having seperate functions, in fact it's probably quicker as there are fewer tests (your switch statement contains many tests) where as a seperate function goes straight to the address offset of the function when called directly.
If you want to streamline your switch, put your cases in the most used order. i.e. if BATCHPLOT1824 is used most often, put it as your first switch, this way you exit your switch with lest tests more often.

To stream line your existing functions I see the same lines of code repeated in each -
(note: more as an example than a requirement for this particular code snippet)
Code: [Select]
    Dim currentline As String
    Open "c:\dwgnum.dat" For Input As 1
    While Not EOF(1)
        Line Input #1, currentline
        Documents.Open currentline
        Debug.Print currentline

perhaps this could be put in its own sub and reduce it to one line in each working sub, this way each sub re-uses the same code (same address in memory which saves space).

This may not be applicable in all cases but you get the idea.
This is also why almost any app needs re-writng once you've finished the first version or beta :)
hth,
Mick.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Better way to write this
« Reply #6 on: June 21, 2006, 11:46:27 PM »
I don't know if this will work for you but I use goto a lot to try shorten my code.

Code: [Select]
Public Sub BATCHP(SIZE As String)
    Dim currentline As String
    Dim PLOTTYPE As String


    Open "c:\dwgnum.dat" For Input As 1
    While Not EOF(1)
        Line Input #1, currentline
        Documents.Open currentline
        Debug.Print currentline

        ThisDrawing.Regen acAllViewports
        Select Case SIZE
        Case "VA"
            Call SetupAndPlot("11x17Draft.pc3", "STANDARDS.ctb", "Business_Letter_(8.50_x_11.00_Inches)", ac1_1, ac0degrees)
        Case "V11"
            Call SetupAndPlot("11x17Draft.pc3", "11X17-CHECKSET.ctb", "ANSI_B_(11.00_x_17.00_Inches)", acScaleToFit, ac90degrees)
        Case "VC"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "VENDOR MEDIUM.ctb", "ARCH_expand_C_(24.00_x_18.00_Inches)", acScaleToFit, ac0degrees)
        Case "VD"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "VENDOR MEDIUM.ctb", "ARCH_expand_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
        Case "T11"
            Call SetupAndPlot("11x17Draft.pc3", "11X17-CHECKSET.ctb", "ANSI_B_(11.00_x_17.00_Inches)", acScaleToFit, ac90degrees)
        Case "TC"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "tep.ctb", "ARCH_expand_C_(24.00_x_18.00_Inches)", acScaleToFit, ac0degrees)
        Case "TD"
            Call SetupAndPlot("OCE DesignJet 750C.pc3", "tep.ctb", "ARCH_expand_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
        Case Else
            Call Vendor1117
            GoTo skip
        End Select
        ThisDrawing.Plot.PlotToDevice
        ThisDrawing.Close (True)
skip:
    Wend
    Close 1
End Sub

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Better way to write this
« Reply #7 on: June 22, 2006, 10:16:40 AM »
Thanks Bryco, I didnt even see the last 2 lines I was repeating 8 times.  That shortened it up a lot.
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Better way to write this
« Reply #8 on: June 22, 2006, 10:19:03 AM »
Thanks Mick!  You should have seen what a mess this was before I started compressing it into re-useable pieces.  Anyway, what you said made sense, so I will try and pull out the parts  where I can.
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Better way to write this
« Reply #9 on: June 22, 2006, 10:44:10 AM »
So Mick,
what would you do with this list?
Code: [Select]
Public Sub VendorA()
    Call SetupAndPlot("11x17Draft.pc3", "STANDARDS.ctb", "Business_Letter_(8.50_x_11.00_Inches)", ac1_1, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub Vendor1117()
    Call SetupAndPlot("11x17Draft.pc3", "11X17-CHECKSET.ctb", "ANSI_B_(11.00_x_17.00_Inches)", acScaleToFit, ac90degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub VendorQuickPlotC()
    Call SetupAndPlot("OCE DesignJet 750C.pc3", "VENDOR MEDIUM.ctb", "ARCH_expand_C_(24.00_x_18.00_Inches)", acScaleToFit, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub VendorQuickPlotD()
    Call SetupAndPlot("OCE DesignJet 750C.pc3", "VENDOR MEDIUM.ctb", "ARCH_expand_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub TEPQuickPlot11x17()
    Call SetupAndPlot("11x17Draft.pc3", "11X17-CHECKSET.ctb", "ANSI_B_(11.00_x_17.00_Inches)", acScaleToFit, ac90degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub TEPQuickPlotC()
    Call SetupAndPlot("OCE DesignJet 750C.pc3", "tep.ctb", "ARCH_expand_C_(24.00_x_18.00_Inches)", acScaleToFit, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub TEPQuickPlotD()
    Call SetupAndPlot("OCE DesignJet 750C.pc3", "tep.ctb", "ARCH_expand_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub TEP9800D()
    Call SetupAndPlot("OCE 9800 DesignJet 750C.pc3", "tep.ctb", "ARCH_expand_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub TEPDWFD()    'Setups for C size and 11x17 do not exist, need to be created.
    Call SetupAndPlot("DWF6 ePlot.pc3", "tep.ctb", "ARCH_full_bleed_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub
Public Sub VENDORDWFD()    'Setups for C size and 11x17 do not exist, need to be created.
    Call SetupAndPlot("DWF6 ePlot.pc3", "VENDOR MEDIUM.ctb", "ARCH_full_bleed_D_(36.00_x_24.00_Inches)", ac1_1, ac0degrees)
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)

since they are all self contained, would you leave as is or try and compress?  All of these are in the same VBA module
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Better way to write this
« Reply #10 on: June 22, 2006, 11:16:18 AM »
Can you create a plot and close sub?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Better way to write this
« Reply #11 on: June 22, 2006, 11:28:43 AM »
Can you create a plot and close sub?
Im not sure I understand what you asking.

A close sub would look like
Code: [Select]
public sub DwgClose()
ThisDrawing.close true
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Better way to write this
« Reply #12 on: June 22, 2006, 11:56:47 AM »
I just notice that each case has

Code: [Select]
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)

Can't you use a sub to make it one line?

Code: [Select]
Public Sub PlotClose()
    ThisDrawing.Plot.PlotToDevice
    ThisDrawing.Close (True)
End Sub

Then each case would be

Code: [Select]
Public Sub yabba()
  Call dabba
  PlotClose
End Sub

You save a line of code.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Better way to write this
« Reply #13 on: June 22, 2006, 12:00:00 PM »
got ya! yes, that could and should be condensed.  In the example Bryco posted, they moved those two lines to the bottom after the select case.  In the example I just posted for Mick, those are all separate subs, so my question was should they stay separate for memory use or be combined.
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Better way to write this
« Reply #14 on: June 22, 2006, 12:03:32 PM »
aH...got ya...