明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 14835|回复: 30

[原创]Auto CAD材料一览表数据到Excel

  [复制链接]
发表于 2007-11-27 16:28:00 | 显示全部楼层 |阅读模式

图示材料一览表是AutoCAD绘制施工图最常见表现形式。有人用属性块的方法处理方法见AutoCAD vba 二次开发教程第317页。

但在实际操作过程中,材料表的数据是任意方法写的,如

idInsertTextInsertPointXInsertPointY
21283572245-322.418415478249.4999997
21283572401.2138.8666674217.4999997
21283572480.037127.4444451217.4999997
21283572568111.8622229217.4999997
21283572643695.00888957217.4999997
2128357272螺母M1643.50000068217.4999997
2128357280GB6170-8617.04704513217.4999997
212835728807RH09-0415.76704513161.4999997
212835729607RH09-0615.76704513153.4999997
212835730407RH09-0615.76704513129.4999997
212835737607RH09-0415.7670451357.49999969
212835738407RH09-0515.7670451349.49999969
212835739207RH09-0515.7670451341.49999969
21283574005-302.418415478233.4999997

hadle,x,y数据是无序排序。

要实现以下目标,需要进行SQL+数组排序处理。

5-107RH09-03固定管板I116Mn/0Cr18Ni10Ti 914.5其中不锈钢66.5kg
5-3GB9948.SHJ405接管 DN20 Sch802200.20.4L=108

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 2威望 +1 明经币 +3 金钱 +20 贡献 +5 激情 +5 收起 理由
3xxx + 1 很给力!
mccad + 1 + 2 + 20 + 5 + 5 【精华】好程序

查看全部评分

发表于 2017-11-8 18:02:23 | 显示全部楼层
谢谢楼主分享!
 楼主| 发表于 2007-11-27 16:35:00 | 显示全部楼层
本帖最后由 作者 于 2008-7-23 14:48:48 编辑

SQL+mdb处理方法
' 从数据库中读取数据
Sub ReadFromMdbFile()
    ' 创建数据库连接
    Dim YCount, nn As Integer
    Dim XDistinct, YDistinct
    Call CreateConnection
    Dim rst As ADODB.Recordset, ii As Integer
    ' 在line表中查询所有的记录
    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseClient
'Distinct Y 统计Y坐标的不重复数量
    rst.Open "SELECT val(format(InsertPointY,'0.00')) FROM EntityText" & _
         " Group By val(Format(InsertPointY,'0.00'))" & _
         " Order By val(Format(InsertPointY,'0.00'))  " & _
         "", cn, adOpenForwardOnly, _
            adLockBatchOptimistic, adCmdText
    rst.MoveFirst
    ReDim YDistinct(rst.RecordCount - 1)
    For ii = 0 To rst.RecordCount - 1
      YDistinct(ii) = rst.Fields(0).Value
      Debug.Print YDistinct(ii)
      rst.MoveNext
    Next ii
    rst.Close
1.5
9.5
17.5
25.5
33.5
41.5
49.5
57.5
65.5
73.5
81.5
89.5
97.5

'Distinct X 统计X坐标的不重复数据的数量
    rst.Open "SELECT val(format(InsertPointX,'0')) FROM EntityText" & _
         " Where  Format(InsertPointY,'0.00') = " & YDistinct(0) & _
         "", cn, adOpenForwardOnly, _
            adLockBatchOptimistic, adCmdText
    rst.MoveFirst
    ReDim XDistinct(rst.RecordCount - 1)
    For ii = 0 To rst.RecordCount - 1
      XDistinct(ii) = rst.Fields(0).Value
      Debug.Print XDistinct(ii)
      rst.MoveNext
    Next ii
    rst.Close
运行结果8列数的X坐标范围。
3
16
44
96
102
137
148

End Sub
相关的ADO帮助文件.


2008-7-23
  1. Sub ExcelToCadTable()
  2.   ThisDrawing.ActiveTextStyle.fontFile = "c:\windows\fonts\SIMHEI.ttf"
  3.   Dim colArray, rowBaseData
  4.   colArray = Array(404.55, 424, 455.11, 499, 510.11, 540.11, 550.69, 561.64)
  5.   rowBaseData = 84.71 - 6 - 8
  6.   Dim objText As AcadText
  7.   Dim xlSheet1
  8.   Dim pp(0 To 2) As Double, ppp(0 To 2) As Double, alignmentPoint(0 To 2) As Double
  9.   Set xlSheet1 = ConnectExcel("Sheet1")
  10.   For ii = 40 To 1 Step -1
  11.     For jj = 0 To UBound(colArray) - 1
  12.       pp(0) = colArray(jj) + 2
  13.       pp(1) = rowBaseData + 8
  14.       tt = xlSheet1.Cells(ii, jj + 1)
  15.       Select Case jj
  16.         Case 5, 6
  17.           If Val(tt) < 1 And Val(tt) > 0 Then
  18.             tt = "0" & tt
  19.           End If
  20.       End Select
  21.       Set objText = ThisDrawing.ModelSpace.AddText(tt, pp, 4)
  22.       alignmentPoint(1) = pp(1)
  23.       Select Case jj
  24.         Case 2, 7
  25.           alignmentPoint(0) = colArray(jj) + 2
  26.           objText.Alignment = acAlignmentLeft
  27.           'objText.TextAlignmentPoint = alignmentPoint
  28.         Case Else
  29.           alignmentPoint(0) = colArray(jj) + (colArray(jj + 1) - colArray(jj)) / 2
  30.           objText.Alignment = acAlignmentCenter
  31.           objText.TextAlignmentPoint = alignmentPoint
  32.       End Select
  33.       '
  34.     Next jj
  35.     rowBaseData = rowBaseData + 8
  36.   Next ii
  37. End Sub

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2007-11-28 08:44:00 | 显示全部楼层
本帖最后由 作者 于 2007-12-9 21:32:46 编辑

AutoCAD2006以上版本使用属性块的方法如下

' 导出到Word中
Public Sub OutputToWord(ByVal SSetObj As AcadSelectionSet, ByVal LBObj As ListBox)
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdTable As Word.Table
   
    Dim EntObj As AcadEntity
    Dim AttRefObjs As Variant
    Dim n As Integer
    Dim i As Integer
    Dim j As Integer
   
    On Error Resume Next
    ' 连接Word
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Err.Clear
        Set wdApp = CreateObject("Word.Application")
        If Err Then
            Err.Clear
            MsgBox "无法启动Word,请检查是否正确安装!"
            Exit Sub
        End If
    End If
    wdApp.Visible = True
   
    On Error GoTo ErrTrap
    ' 返回新创建的文档
    Set wdDoc = wdApp.Documents.Add
    ' 返回在段落一之后新创建的表格
    Set wdTable = wdDoc.Tables.Add(wdDoc.Paragraphs(1).Range, 1, LBObj.ListCount)
   
    n = 0
    ' 遍历选择集
    For Each EntObj In SSetObj
        ' 增加行
        wdTable.Rows.Add
        ' 返回属性数据
        AttRefObjs = EntObj.GetAttributes
        n = n + 1
        For i = 0 To UBound(AttRefObjs)
            For j = 0 To LBObj.ListCount - 1
                If AttRefObjs(i).TagString = LBObj.List(j) And LBObj.Selected(j) = True Then
                    If n = 1 Then
                        ' 首行,标签做为表格的列标题
                        wdTable.Cell(n, j + 1).Range.Text = AttRefObjs(i).TagString
                        wdTable.Cell(n + 1, j + 1).Range.Text = AttRefObjs(i).TextString
                    Else
                        wdTable.Cell(n + 1, j + 1).Range.Text = AttRefObjs(i).TextString
                    End If
                End If
            Next
        Next
    Next
   
    ' 删除表格中的空列
    For i = LBObj.ListCount - 1 To 0 Step -1
        If wdTable.Cell(1, i + 1).Range.Text = vbCr + Chr(7) Then
            wdTable.Columns(i + 1).Delete
        End If
    Next
   
    ' 按序号排序
    wdTable.Sort True, "列 1"
    ' 自动调整列宽
    wdTable.AutoFitBehavior 1
   
    ' 释放Word对象
    Set wdTable = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing
    Exit Sub
   
ErrTrap:
    On Error GoTo 0
End Sub

' 导出到Excel中
Public Sub OutputToExcel(ByVal SSetObj As AcadSelectionSet, ByVal LBObj As ListBox)
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
   
    Dim EntObj As AcadEntity
    Dim AttRefObjs As Variant
    Dim n As Integer
    Dim i As Integer
    Dim j As Integer
   
    On Error Resume Next
    ' 连接Excel
    Set xlApp = GetObject(, "Excel.Application")
    If Err Then
        Err.Clear
        Set xlApp = CreateObject("Excel.Application")
        If Err Then
            Err.Clear
            MsgBox "无法启动Word,请检查是否正确安装!"
            Exit Sub
        End If
    End If
    xlApp.Visible = True
   
    On Error GoTo ErrTrap
    ' 返回新创建的工作簿
    Set xlBook = xlApp.Workbooks.Add
    ' 返回新增加的工作表,并移动到最后一个
    Set xlSheet = xlBook.Worksheets.Add
    xlSheet.Move , xlBook.Worksheets(xlBook.Worksheets.Count)
   
    n = 0
    ' 遍历选择集
    For Each EntObj In SSetObj
        ' 返回属性数据
        AttRefObjs = EntObj.GetAttributes
        n = n + 1
        For i = 0 To UBound(AttRefObjs)
            For j = 0 To LBObj.ListCount - 1
                If AttRefObjs(i).TagString = LBObj.List(j) And LBObj.Selected(j) = True Then
                    If n = 1 Then
                        ' 首行,标签做为表格的列标题
                        xlSheet.Cells(n, j + 1).Value = AttRefObjs(i).TagString
                        xlSheet.Cells(n + 1, j + 1).Value = AttRefObjs(i).TextString
                    Else
                        xlSheet.Cells(n + 1, j + 1).Value = AttRefObjs(i).TextString
                    End If
                End If
            Next
        Next
    Next
   
    ' 删除表格中的空列
    For i = LBObj.ListCount - 1 To 0 Step -1
        If xlSheet.Cells(1, i + 1).Value = "" Then
            xlSheet.Columns(i + 1).Delete
        End If
    Next
   
    ' 按序号排序
    xlSheet.UsedRange.Sort Key1:=Range("A2"), Header:=xlYes
    ' 自动调整列宽
    xlSheet.Columns.AutoFit

    ' 释放Exccel对象
    Set xlSheet = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing
    Exit Sub
   
ErrTrap:
    On Error GoTo 0
End Sub

Excel VBA 范例文件代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2007-12-5 15:43:00 | 显示全部楼层
本帖最后由 作者 于 2007-12-9 21:37:01 编辑

select选择集+图层Layer

Sub ls()
  Dim ss1 As AcadSelectionSet
  Dim layername As String
  Dim AcadEnt As AcadEntity
  'Dim pp1 As AcadPoint, pp2 As AcadPoint
  '指定图层名称
  'Set pp1 = ThisDrawing.Utility.GetPoint
  'Set pp2 = ThisDrawing.Utility.GetPoint
  layername = "件号"
  Dim tt As AcadText, MTt As AcadMText
  '得到选择集

  Dim gpCode(0) As Integer
  Dim dataValue(0) As Variant
  gpCode(0) = 8
  dataValue(0) = layername

  Set ss1 = ThisDrawing.SelectionSets.Add("ss3")
  ss1.Select acSelectionSetAll, , , gpCode, dataValue
  'ss1.Select acSelectionSetCrossing, pp1, pp2, , dataValue
For Each AcadEnt In ss1
  'Debug.Print AcadEnt.ObjectName
  Select Case AcadEnt.ObjectName
    Case "AcDbText"
      Set tt = AcadEnt
      Debug.Print tt.TextString
    Case "AcDbMText"
      Set MTt = AcadEnt
      Debug.Print "MText---", MTt.TextString
  End Select
Next
  ss1.Delete
  'ss1.Clear
End Sub

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2007-12-30 16:32:00 | 显示全部楼层

支持兰州人!

发表于 2008-1-13 12:32:00 | 显示全部楼层

好资料

发表于 2008-1-31 22:46:00 | 显示全部楼层

高人阿!谢谢

发表于 2008-2-25 19:01:00 | 显示全部楼层

收获了很多资料

发表于 2008-4-2 20:36:00 | 显示全部楼层
本人前不久也刚刚编写了一个cad表格自动输出到EXCEL的vba代码,基本思路就是把选择到的所有文字实体先按y坐标排序,由此可以把这些字分成若干行,每行再按纵坐标排序,确定先后顺序,缺点就是无法判断空的单元格,会导致后面的单元格内容串列.
发表于 2009-12-12 10:39:00 | 显示全部楼层

很好的资料,学习,收藏

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 07:50 , Processed in 0.328887 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表