CAD VBA 导出cass扩展数据到excel

news/2024/7/21 5:14:06 标签: excel, CAD, cass, vba

        cass中往往会写入扩展数据,但获取扩展数据较为麻烦,此例我们通过getxdata函数获取实体的扩展数据,然后逐要素循环写入excel中,代码如下:

版本1:要素扩展数据字段不同


Sub 导出扩展数据到excel()
    Dim Excel As Object
    Dim elem As Object
    Dim excelSheet As Object
    Dim Array1 As Variant
    Dim xdataOut As Variant
    Dim xtypeOut As Variant
    Dim Count, RowNum As Integer
    Dim NumberOfAttributes As Integer
    
    ' Start Excel
    On Error Resume Next
    
    Set Excel = GetObject(, "Excel.Application")
    
    If Err <> 0 Then
        Err.Clear
        Set Excel = CreateObject("Excel.Application")
            
        If Err <> 0 Then
            MsgBox "Could not load Excel.", vbExclamation
            End
        End If
    End If
    
   
    
    Excel.Visible = True
    Excel.Workbooks.Add
    Excel.Sheets("Sheet1").Select
    Set excelSheet = Excel.ActiveWorkbook.Sheets("Sheet1")
    
    RowNum = 1
   
   For Each elem In ThisDrawing.ModelSpace
        If StrComp(elem.EntityName, "AcDbPolyline", 1) = 0 Then
            If elem.HasAttributes Then
 ''通过getattributes函数我们把块的属性放入数组中,下图可见数组有3个项目
 ''每个项目都有tagstring和textstring,然后把数组中值输出到excel,至此
 ''我们提取出了块中的全部属性
                elem.Highlight (True)
                elem.GetXData "", xtypeOut, xdataOut
                
                For Count = LBound(xtypeOut) To UBound(xtypeOut)
                   
                        If StrComp(xtypeOut(Count).EntityName, "AcDbAttribute", 1) = 0 Then
                        
                            excelSheet.Cells(RowNum, Count + 1).Value = xtypeOut(Count)
                      
                        End If
                  
                Next Count
                RowNum = RowNum + 1
                For Count = LBound(xdataOut) To UBound(xdataOut)
                    excelSheet.Cells(RowNum, Count + 1).Value = xdataOut(Count)
                Next Count
              
            End If
             RowNum = RowNum + 1
        End If
       
    Next elem
    
    NumberOfAttributes = RowNum - 1
    
    If NumberOfAttributes > 0 Then
        excelSheet.UsedRange.Font.Bold = True
        
        'For a specific set of attribute information this could
        'be set to fit the exact number of columns.
        excelSheet.Columns("A:G").AutoFit

    Else
        MsgBox "未发现扩展数据" & Space(50) & vbCr & _
 "写代码qq:443440204", vbInformation, "版权所有qq:443440204"
        ''Excel.Quit
    End If
MsgBox "OK" & Space(50) & vbCr & _
 "vba代码二次开发qq:443440204", vbInformation, "版权所有qq:443440204"
End Sub



版本2:要素扩展数据字段相同


Sub 导出扩展数据到excel()
    Dim Excel As Object
    Dim elem As Object
    Dim excelSheet As Object
     Dim xdataOut As Variant
    Dim xtypeOut As Variant
    Dim Count, RowNum As Integer
    Dim NumberOfAttributes As Integer
    
    ' Start Excel
    On Error Resume Next
    
    Set Excel = GetObject(, "Excel.Application")
    
    If Err <> 0 Then
        Err.Clear
        Set Excel = CreateObject("Excel.Application")
            
        If Err <> 0 Then
            MsgBox "不能加载Excel.", vbExclamation
            End
        End If
    End If
    

    
    Excel.Visible = True
    Excel.Workbooks.Add
    Excel.Sheets("Sheet1").Select
    Set excelSheet = Excel.ActiveWorkbook.Sheets("Sheet1")
    
    RowNum = 1
    Dim Header As Boolean
    For Each elem In ThisDrawing.ModelSpace
        
            If elem.HasAttributes Then
                 elem.Highlight (True)
                elem.GetXData "", xtypeOut, xdataOut
                For Count = LBound(xtypeOut) To UBound(xtypeOut)
                    If Header = False Then
                        If StrComp(xtypeOut(Count).EntityName, "AcDbAttribute", 1) = 0 Then
                            excelSheet.Cells(RowNum, Count + 1).Value = xtypeOut(Count)
                        End If
                    End If
                Next Count
                RowNum = RowNum + 1
                For Count = LBound(xdataOut) To UBound(xdataOut)
                    excelSheet.Cells(RowNum, Count + 1).Value = xdataOut(Count)
                Next Count
                Header = True
            End If
        
    Next elem
    
    NumberOfAttributes = RowNum - 1
    
    If NumberOfAttributes > 0 Then
        excelSheet.UsedRange.Font.Bold = True
        
        'For a specific set of attribute information this could
        'be set to fit the exact number of columns.
        excelSheet.Columns("A:G").AutoFit

    Else
        MsgBox "未发现扩展数据" & Space(50) & vbCr & _
 "写代码qq:443440204", vbInformation, "版权所有qq:443440204"
        ''Excel.Quit
    End If
MsgBox "OK" & Space(50) & vbCr & _
 "vba代码二次开发qq:443440204", vbInformation, "版权所有qq:443440204"
End Sub



 


http://www.niftyadmin.cn/n/5271155.html

相关文章

电脑系统清理缓存怎么清理 电脑系统清理软件哪个好 CCleaner 专业版

在日常生活中&#xff0c;电脑已经成为我们工作和娱乐的主要工具。然而&#xff0c;电脑用久了&#xff0c;文件越来越多&#xff0c;系统运行容易被拖慢。因此&#xff0c;定期清理缓存是非常必要的。今天&#xff0c;我们就来聊聊电脑系统缓存清理后对软件有影响吗&#xff0…

记一次jar冲突的问题

问题 业务中需要在spark中链接redis作为服务缓存&#xff0c;spark程序中引入redis的jar包后上传spark集群运行是报java.lang.NoSuchMethodError: com.xxx.common.pool.ConnectionPool.startAsync()Lcom/google/common/util/concurrent/Service;根据报错信息发现是jar包冲突造…

【深度强化学习】确定性策略梯度算法 DDPG

前面讲到如 REINFORCE&#xff0c;Actor-Critic&#xff0c;TRPO&#xff0c;PPO 等算法&#xff0c;它们都是随机性策略梯度算法&#xff08;Stochastic policy&#xff09;&#xff0c;在广泛的任务上表现良好&#xff0c;因为这类方法鼓励了算法探索&#xff0c;给出的策略是…

jQuery Ajax 缓存

在jQuery中&#xff0c;Ajax请求的默认行为可能会根据浏览器和数据类型的不同而有所差异。通常&#xff0c;对于GET类型的请求&#xff0c;浏览器会缓存响应结果以提高性能。然而&#xff0c;在某些情况下&#xff0c;我们可能不希望使用缓存&#xff0c;特别是在需要获取实时数…

05 动态渲染数据

概述 实际上动态渲染数据&#xff0c;在《使用CDN开发Vue3项目》中就已经学习过了&#xff0c;核心代码如下&#xff1a; <div id"vue-app">{{text}}</div> <script src"https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></sc…

Flink系列之:监控反压

Flink系列之&#xff1a;监控反压 一、反压二、Task 性能指标三、示例四、反压状态 Flink Web 界面提供了一个选项卡来监控正在运行 jobs 的反压行为。 一、反压 如果你看到一个 task 发生 反压警告&#xff08;例如&#xff1a; High&#xff09;&#xff0c;意味着它生产数…

你如何看待“前端已死”

随着人工智能和低代码的崛起&#xff0c;“前端已死”的声音逐渐兴起。前端已死&#xff1f;尊嘟假嘟&#xff1f;快来发表你的看法吧&#xff01; 一、你如何看待“前端已死” 对于Java是否已经死亡以及前端是否已经凉凉&#xff0c;我认为这样的说法是不准确的。 首先&#…

格密码:离散高斯与子高斯分布

高斯分布我们都很熟悉&#xff0c;但在格密码中会用到一种特殊的高斯分布&#xff0c;将其取名离散高斯分布&#xff08;discrete Gaussian)。 一. N维连续高斯分布 给定一个正整数n&#xff0c;代表维度。一个正实数&#xff0c;代表标准差&#xff08;高斯分布的标准差决定着…