我使用一些旧代码从Excel工作簿中包含的数据在ACAD中创建图形
- Sub Draw_Lines()
- Dim selRng As Range
- Dim lineData As Variant
- Dim DataStart, DataEnd, DataRange
- Dim WorkSheetInput As String, DataInput As String, ChartInput As String
- Dim i As Integer, j As Integer, NumRows As Integer
- Dim numcols As Integer, StartRow As Integer, EndRow As Integer, StartCol As Integer, EndCol As Integer
- Dim MinY As Double, MaxY As Double, MinX As Double, MaxX As Double, MaxWidth As Double
- ' Switch to AutoCAD
- Dim acad As AutoCAD.AcadApplication
- Dim adoc As AutoCAD.AcadDocument
- Dim aspace As AcadBlock
- Dim appNum As String
- On Error GoTo ErrorHandler
- appNum = acadVerNum
- If appNum = "" Then
- Exit Sub
- End If
- On Error Resume Next
- Set acad = GetObject(, "Autocad.Application." & appNum)
- If Err.Number = 429 Then
- Err.Clear
- On Error GoTo 0
- Set acad = CreateObject("Autocad.Application." & appNum)
- If Err Then
- Exit Sub
- End If
- End If
- acad.WindowState = acMax
- Set adoc = acad.ActiveDocument
- Set aspace = adoc.ActiveLayout.Block
- ' Get the chart data from Excel
- Application.ScreenUpdating = False
- ' Need to selection the chart data from the worksheet
- DataInput = "INSULATOR_SWING"
- ChartInput = "CHART_DATA"
- Worksheets(ChartInput).Activate
- StartRow = 5 ' Need to have method to verify start location with other program
- StartCol = 1
- EndRow = LastRow(Worksheets(ChartInput))
- EndCol = LastCol(Worksheets(ChartInput))
- 'EndRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
- 'EndCol = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
- DataStart = Cells(StartRow, StartCol).Address(False, False)
- DataEnd = Cells(EndRow, EndCol).Address(False, False)
- DataRange = DataStart + ":" + DataEnd
- Set selRng = Worksheets(ChartInput).Range(DataRange)
- 'Set selRng = Selection
- lineData = selRng.Value2
- MaxX = Worksheets(DataInput).Cells(8, 11)
- MinX = Worksheets(DataInput).Cells(7, 11)
- NumRows = UBound(lineData, 1) ' Number of Rows
- numcols = UBound(lineData, 2) ' Number of Columns
- MaxY = 0 ' Reset MaxY Value
- For j = 2 To numcols
- If lineData(NumRows, j) > MaxY Then
- MaxY = lineData(NumRows, j)
- End If
- Next j
- adoc.SetVariable "LTSCALE", (MaxX / 40)
- Dim LineTypeObject As AcadLineType
- Dim oSpline As AcadSpline
- Dim oLWPline As AcadPolyline
- Dim GraphLineHor, GraphLineVer, GridLineHor, GridLineVer, TickLine, ChartLine As AcadLine
- Dim TickLabel As AcadText
- Dim AxixLabel, TempText As AcadText
- Dim TextString As String
- Dim LineType As String
- Dim LayerName As String
- Dim TxtLayName As String
- Dim BrdrLayName As String
- Dim GridLayName As String
- Dim Xscale As Double
- Dim ticklength As Double
- Xscale = 1# ' 2.5 need to add as an input ? On the excel sheet?
- Dim TextPoint(0 To 2) As Double
- Dim GridStart(0 To 2) As Double
- Dim GridEnd(0 To 2) As Double
- Dim GraphStartPt(0 To 2) As Double
- Dim GraphXEndPt(0 To 2) As Double
- Dim GraphYEndPt(0 To 2) As Double
- Dim TickPtStart(0 To 2) As Double
- Dim TickPtEnd(0 To 2) As Double
- ' Setup Layers for text, grids, and borders
- TxtLayName = "Text"
- BrdrLayName = "Border"
- GridLayName = "Grid"
- MakeSetLayer (TxtLayName)
- MakeSetLayer (BrdrLayName)
- MakeSetLayer (GridLayName)
- 'Set LineTypeObject = AcadLineType.Load(LineType, LineTypeFileName)
- On Error Resume Next
- adoc.Linetypes.Load "DOT", "acad.lin"
- '--------------------------------------------------------------------
- ' TO DO.....
- ' Draw the Grid lines and axis lines
- ' Need to: Create layers for the various items
- ' 1. Grid lines with appropriate color
- ' 2. Graph borders ""
- ' 3. Text ""
- ' 4. Graphed data ""
- '
- ' Need to give the user the option for scaling the
- ' horizontal data?
- '
- ' Label the chart data series
- ' Draw a border around the graph
- ' Added the Title information at the top of the graph
- '
- '--------------------------------------------------------------------
- ' Round up to the nearest 100 to obtain the
- ' extents of the chart border
- MaxX = (100 * Int(MaxX / 100)) * Xscale
- MaxY = (100 * Int(MaxY / 100)) + 100
- ' Size the 'tick' and text
- ticklength = 0.02 * (MaxX / Xscale)
- If ticklength > 10 Then ticklength = 10
|