Академический Документы
Профессиональный Документы
Культура Документы
Option Explicit
Sub AddText()
'----------------------------------------------------------------------------------------------
------------------
'This macro adds text in AutoCAD using data - insertion point, text height and text message - f
rom Excel.
'Moreover, it provides some optional parameters, so that the user can adjust the alignment, the
rotation angle,
'the width factor and the color of the text to be inserted (using the Red, Green and Blue param
eters).
'The code uses late binding, so no reference to external AutoCAD (type) library is required.
'It goes without saying that AutoCAD must be installed at your computer before running this cod
e.
'Check if AutoCAD application is open. If is not opened create a new instance and make it visib
le.
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application")
If acadApp Is Nothing Then
Set acadApp = CreateObject("AutoCAD.Application")
acadApp.Visible = True
End If
'Check if the active space is paper space and change it to model space.
If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
acadDoc.ActiveSpace = 1 '1 = acModelSpace in early binding
End If
mACADText - 2
'Loop through all the rows and add the corresponding text in AutoCAD.
With Sheets("Coordinates")
For i = 2 To LastRow
'If the height and the message are not empty, add the text.
If IsEmpty(.Range("D" & i)) = False And IsEmpty(.Range("E" & i)) = False Then
'Set the insertion point.
InsertionPoint(0) = .Range("A" & i).Value
InsertionPoint(1) = .Range("B" & i).Value
InsertionPoint(2) = .Range("C" & i).Value
'Add the text.
Set acadText = acadDoc.ModelSpace.AddText(.Range("E" & i), InsertionPoint, .Range("
D" & i))
'Align the text based on alignment selection. The default is left.
If IsEmpty(.Range("F" & i)) = False Then
If UCase(.Range("F" & i)) = "CENTER" Then
acadText.Alignment = 1
acadText.Move ZeroPoint, InsertionPoint
End If
If UCase(.Range("F" & i)) = "RIGHT" Then
acadText.Alignment = 2
acadText.Move ZeroPoint, InsertionPoint
End If
End If
'If the rotation angle is not empty, rotate the text.
'0.0174532925 is used to convert from degrees to radians.
If IsEmpty(.Range("G" & i)) = False Then acadText.Rotate InsertionPoint, .Range("G"
& i).Value * 0.0174532925
'If the width factor is not empty, apply the width factor.
If IsEmpty(.Range("H" & i)) = False Then acadText.ScaleFactor = .Range("H" & i)
'Use the sheet data to change the color on each line of text (if the corresponding
cells are not empty).
If IsEmpty(.Range("I" & i)) = False And IsEmpty(.Range("J" & i)) = False And IsEmpt
y(.Range("K" & i)) = False Then
Call acadColor.SetRGB(.Range("I" & i), .Range("J" & i), .Range("K" & i))
'Change the color of text.
acadText.TrueColor = acadColor
End If
End If
Next i
End With
End Sub
Sub ClearAll()
'Find the last row and clear all the input data..
With Sheets("Coordinates")
.Activate
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If LastRow > 1 Then
.Range("A2:K" & LastRow).ClearContents
End If
.Range("A2").Select
End With
mACADText - 3
End Sub