更好的写法
我正试图压缩我的dvb文件以使事情更简单,我想知道是否有人可以将这个子文件压缩得更小。该接头使用Select Case替换6-8个其他接头。如果这不合理,我会尽量解释得更好。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
**** Hidden Message ***** 这里是 SetupAndPlot 子
代码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
这是我要替换的内容代码2] 在我看来,它看起来很流线型。你可以移动情节,靠近SetupandPlot潜艇。然后,你可以离开他们。看起来不错。
你听起来像个气象员。
[气象员声音]今天可能会下雨,然后又可能不会[/气象员声音] 使用单独的函数并没有什么问题,事实上,这样可能会更快,因为测试更少(您的switch语句包含许多测试),单独的函数在被直接调用时会直接进入函数的地址偏移量。
如果您想简化开关,请按最常用的顺序摆放您的箱子。也就是说,如果BATCHPLOT1824使用最频繁,将其作为您的第一个开关,这样您可以更频繁地退出您的开关进行测试。
为了对您现有的函数进行流处理,我在每个函数中都看到了相同的代码行-
(注意:这只是一个示例,而不是对这个特定代码片段的要求)代码3]
也许可以将它放在自己的sub中,并在每个工作sub中将它减少到一行,这样每个sub都可以重用相同的代码(内存中相同的地址,从而节省空间)。
这可能不是在所有情况下都适用,但您会明白。
这也是为什么一旦你完成了第一个版本或测试版,几乎任何应用程序都需要重写。
我不知道这是否适合您,但我经常使用goto来尝试缩短我的代码。
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
谢谢布莱科,我甚至没有看到我重复了8遍的最后两行。这缩短了很多。 谢谢你,米克!在我开始把它压缩成可重复使用的碎片之前,你应该看看这是多么的混乱。不管怎么说,你说的有道理,所以我会尽我所能把零件拔出来。 那么米克,
你会用这个列表做什么?
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)
由于它们都是独立的,您会保持原样还是尝试压缩? 所有这些都在同一个VBA模块中
页:
[1]
2