- 积分
- 25756
- 明经币
- 个
- 注册时间
- 2011-10-3
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 springwillow 于 2020-12-29 14:30 编辑
- Sub ExportToACAD()
- On Error Resume Next
- Dim objAcad As Object ''AcadApplication
- Dim objAcadDoc As Object
- ''AcadDocument
- Dim objModelSpace As Object ''AcadModelSpace
-
- Dim msgResult As Integer
- Dim a As Range
- If Selection Is Nothing Then MsgBox "Nothing Selected!": Exit Sub
- msgResult = MsgBox("您共选择了" & Selection.Rows.Count & "行 " & Selection.Columns.Count & "列," & Chr(13) & "请注意一些对齐方式可能被忽略!" & Chr(13) & "继续吗 ", vbOKCancel, "选择")
- If msgResult = vbCancel Then Exit Sub
- Err.Clear
- Set objAcad = GetObject(, "AutoCAD.application")
- If Err.Number = 0 Then GoTo Finish
- Err.Clear
- Set objAcad = CreateObject("autocad.application")
- Finish:
- If Err.Number <> 0 Then
- MsgBox "You must have AutoCAD installed to run this Macro!", vbCritical, "Export to ACAD"
- Exit Sub
- End If
- On Error GoTo errHandler
- Set objAcadDoc = objAcad.Documents.Add
- Set objModelSpace = objAcadDoc.ModelSpace
- Dim textObj As Object
- ''AcadText
- Dim lineObj As Object ''AcadLine
- Dim insPnt(0 To 2) As Double
- Dim stPnt(0 To 2) As Double
- Dim edPnt(0 To 2) As Double
- Dim txtHeight As Double
- Const txtClearance As Double = 2
- Static startY As Double
- startY = Selection.Rows(Selection.Rows.Count).Top - Selection.Rows(1).Top
- For Each a In Selection
- If a.Borders(xlEdgeTop).LineStyle = xlContinuous Then
- stPnt(0) = a.Left: stPnt(1) = startY - a.Top: stPnt(2) = 0
- edPnt(0) = a.Left + a.Width: edPnt(1) = stPnt(1): edPnt(2) = 0
- Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
-
- End If
- If a.Borders(xlEdgeLeft).LineStyle = xlContinuous Then
- stPnt(0) = a.Left: stPnt(1) = startY - a.Top: stPnt(2) = 0
- edPnt(0) = stPnt(0): edPnt(1) = startY - a.Top - a.Height:
- edPnt(2) = 0
- Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
- End If
- txtHeight = a.Font.Size / 1.5
- If Trim(a.Text) <> "" Then
- If a.HorizontalAlignment = xlCenter Then
- insPnt(0) = a.Left + a.Width / 2
-
- insPnt(1) = startY - a.Top - a.Height / 2
- insPnt(2) = 0
- Set textObj = objModelSpace.AddText(a.Text, insPnt, txtHeight)
- textObj.Alignment = 10
- 'acAlignmentMiddleCenter
- textObj.TextAlignmentPoint = insPnt
- ElseIf a.HorizontalAlignment = xlLeft Or (a.HorizontalAlignment = xlGeneral And _
- Not IsNumeric(a.Text)) Then
- insPnt(0) = a.Left + txtClearance
- insPnt(1) = startY - a.Top - a.Height / 2
- insPnt(2) = 0
- Set textObj = objModelSpace.AddText(a.Text, insPnt, txtHeight)
-
- textObj.Alignment = 9 'acAlignmentMiddleLeft
-
- textObj.TextAlignmentPoint = insPnt
- Else
-
- insPnt(0) = a.Left + a.Width - txtClearance
- insPnt(1) = startY - a.Top - a.Height / 2
- insPnt(2) = 0
- Set textObj = objModelSpace.AddText(a.Text, insPnt, txtHeight)
- textObj.Alignment = 11
- 'acAlignmentMiddleRight
- textObj.TextAlignmentPoint = insPnt
- End If
- End If
- Next a
- For Each a In Selection.Offset(Selection.Rows.Count - 1, 0). _
- Resize(1, Selection.Columns.Count)
- If a.Borders(xlEdgeBottom).LineStyle = xlContinuous Then
- stPnt(0) = a.Left: stPnt(1) = startY - a.Top - a.Height: stPnt(2) = 0
- edPnt(0) = a.Left + a.Width: edPnt(1) = stPnt(1): edPnt(2) = 0
- Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
- End If
- Next
- For Each a In Selection.Offset(0, Selection.Columns.Count - 1). _
- Resize(Selection.Rows.Count, 1)
- If a.Borders(xlEdgeRight).LineStyle = xlContinuous Then
- stPnt(0) = a.Left + a.Width: stPnt(1) = startY - a.Top:
- stPnt(2) = 0
- edPnt(0) = stPnt(0): edPnt(1) = startY - a.Top - a.Height: edPnt(2) = 0
- Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
- End If
- Next
- Application.WindowState = xlMinimized
- objAcad.WindowState = 3 'acMax
- objAcad.Visible = True
- objAcad.ZoomAll
- 'objAcad.Close
- Set objAcad = Nothing
- Set objAcadDoc = Nothing
- Set objModelSpace = Nothing
- Exit Sub
- errHandler: MsgBox "在该系统中不能正常运行! " & Chr(10) & Err.Description, vbCritical, "Export to ACAD"
- End Sub
使用方法是打开Excel程序,找到开发工具(如果没有可以在选项-自定义功能区中填加),点击Visual Basic按钮调出VBA编辑器,把代码粘贴到代码窗口。回到Excel,选中要插入CAD的单元格,设置好单元格的边框,执行宏即可。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
查看全部评分
|