找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 454|回复: 2

[每日一码] 提取图中属性到外部文件

[复制链接]

已领礼包: 20个

财富等级: 恭喜发财

发表于 2017-5-6 19:29:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
  1. (defun gattex ()
  2.   (setq Blocklist '("Test Blockatt"))  ; ** edit to include block names to
  3.                                        ; select
  4.   (setq TagList '("TAG1"))               ; ** edit to include tag names to
  5.                                        ; extract
  6.                                        ; create block names separated by
  7.                                        ; columns, for selection filter
  8.   (setq Blocknames (List2String BlockList))
  9.   (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 BlockNames))))
  10.   (if (not ss)
  11.     (quit)
  12.   )
  13.   (setq Root (getvar "DWGPREFIX"))
  14.   (setq file (open (strcat Root (substr (getvar "DWGNAME") 1 (-
  15.                                                                 (strlen
  16.                                                                         (getvar "DWGNAME")
  17.                                                                 ) 4
  18.                                                              )
  19.                                 ) "attributes.CSV"
  20.                    ) "w"
  21.              )
  22.         i -1
  23.   )
  24.   (write-line (strcat Root (getvar "DWGNAME") " -found " (itoa
  25.                                                                (sslength ss)
  26.                                                          )
  27.                       " block(s) with attributes"
  28.               ) file
  29.   )
  30.   (repeat (sslength ss)
  31.     (setq TagRow nil
  32.           ValRow nil
  33.     )
  34.     (setq Edata (entget (setq e (ssname ss (setq i (1+ i))))))
  35.     (write-line "" file)
  36.     (write-line (strcat "block name:" "," (Dxf 2 Edata)) file)
  37.     (while (/= (Dxf 0 Edata) "SEQEND")
  38.       (if (and
  39.             (= (Dxf 0 Edata) "ATTRIB")
  40.             (member (dxf 2 Edata) TagList) ; if tag is on list
  41.           )                               ; and
  42.         (progn
  43.           (setq TagRow (cons (Dxf 2 Edata) TagRow))
  44.           (setq valRow (cons (Dxf 1 Edata) ValRow))
  45.         )                               ; progn
  46.       )
  47.       (setq Edata (entget (setq e (entnext e))))
  48.     )                                       ; while
  49.     (write-line (List2String (reverse TagRow)) file)
  50.     (write-line (List2String (reverse ValRow)) file)
  51.   )                                       ; repeat
  52.   (close file)
  53.   (princ (strcat "\nDone writing file " Root "attributes.csv"))
  54.   (princ)
  55. )
  56. ;defun
  57. ;;-------------------------------
  58. (defun List2String (Alist)
  59.   (setq NumStr (length Alist))
  60.   (foreach Item AList
  61.     (if (= Item (car AList))               ; first item
  62.       (setq LongString (car AList))
  63.       (setq LongString (strcat LongString "," Item))
  64.     )
  65.   )
  66.   LongString
  67. )
  68. ;defun
  69. ;;--------------------------------
  70. (defun Dxf (code pairs)
  71.   (cdr (assoc code pairs))
  72. )
  73. (defun c:test ()
  74.   (gattex)
  75.   (princ)
  76. )


下面是VBA代码

[Visual Basic .NET] 纯文本查看 复制代码
Sub Blocks(row, col)
    Dim BlocksSelSet As AcadSelectionSet
    Dim blockref As AcadBlockReference
    Dim AttRef As Variant
    Dim I As Integer
    Dim gpCode(0) As Integer
    Dim dataValue(0) As Variant
    Dim groupCode As Variant, dataCode As Variant
    Dim NumOfBlocks As Integer
    Dim mode
    Dim AttMode As Long
    Dim constant As String
    Dim currentmode As Integer
    Dim attrobject As AcadAttribute
    
    On Error Resume Next
    
    Set BlocksSelSet = ThisDrawing.SelectionSets.Add("All_Blocks")
'define type of selection set (crossing, window, etc, in this case, all)
    
    mode = acSelectionSetAll
'this is a selection set filter
    
    gpCode(0) = 0
    dataValue(0) = "Insert"

    
    groupCode = gpCode
    dataCode = dataValue
    'this collects all blocks into the seletion set
    BlocksSelSet.Select mode, , , groupCode, dataCode
   
    NumOfBlocks = BlocksSelSet.Count
    For x = 1 To NumOfBlocks
        Set blockref = BlocksSelSet(x)
        excelsheet.Cells(row, col).Value = blockref.Name
        excelsheet.Cells(row, col + 1).Value = blockref.layer
        excelsheet.Cells(row, col + 2).Value = Format(blockref.insertionPoint(0), "##,##0.00") & "," & Format(blockref.insertionPoint(1), "##,##0.00")
        row = row + 1
        AttRef = blockref.GetAttributes
        
            For I = LBound(AttRef) To UBound(AttRef)
            Set attrobject = AttRef(I)
                ' Do something with the attribute refs here.
                excelsheet.Cells(row, col).Value = AttRef(I).TagString
                excelsheet.Cells(row, col + 1).Value = AttRef(I).layer
                excelsheet.Cells(row, col + 3).Value = AttRef(I).TextString
                
                constant = Choose(attrobject.mode, "acAttributeModeInvisible", "acAttributeModeConstant", "", "acAttributeModeVerify", "", "", "", "acAttributeModePreset")
                
                
                row = row + 1
            Next
        
        
        
    Next
    row = row + 1
  'clean up
    BlocksSelSet.Clear
    BlocksSelSet.Delete
    rownum = row
End Sub
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 773个

财富等级: 财运亨通

发表于 2017-5-7 20:58:15 | 显示全部楼层
哇,简直酷毙了!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 6056个

财富等级: 富甲天下

发表于 2017-5-8 00:06:08 | 显示全部楼层
谢谢分享。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-24 04:18 , Processed in 0.414070 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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