乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: witchhero

[编程交流] Excel到Autocad插入属性

[复制链接]

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-6 20:29:57 | 显示全部楼层
你能分享你的代码吗??谢谢你
回复

使用道具 举报

2

主题

6

帖子

4

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 20:30:44 | 显示全部楼层
试试这个
  1. Option Explicit
  2. 'A custom type that holds the scale factors of the block.
  3. Private Type ScaleFactor
  4.     X As Double
  5.     Y As Double
  6.     Z As Double
  7. End Type
  8. Sub InsertBlocks()
  9.     '--------------------------------------------------------------------------------------------------------------------------
  10.     'Inserts blocks in AutoCAD using data - insertion point, block name/full path, scale factors, rotation angle - from Excel.
  11.     'Note that the block name or the block path must already exists, otherwise nothing will be inserted.
  12.     'The code uses late binding, so no reference to external AutoCAD (type) library is required.
  13.     'It goes without saying that AutoCAD must be installed at your computer before running this code.
  14.    
  15.     'Written by:    Christos Samaras
  16.     'Date:          21/04/2014
  17.     'e-mail:        xristos.samaras@gmail.com
  18.     'site:          http://www.myengineeringworld.net
  19.     '--------------------------------------------------------------------------------------------------------------------------
  20.                  
  21.          ' Define the block
  22.     'Declaring the necessary variables.
  23.     Dim acadApp                 As Object
  24.     Dim height                  As Double
  25.     Dim acadDoc                 As Object
  26.     Dim acadBlock               As Object
  27.     Dim attributeObj            As Object
  28.     Dim LastRow                 As Long
  29.     Dim i                       As Long
  30.     Dim InsertionPoint(0 To 2)  As Double
  31.     Dim BlockName               As String
  32.     Dim BlockScale              As ScaleFactor
  33.     Dim RotationAngle           As Double
  34.     Dim tag                     As String
  35.     Dim value                   As String
  36.     Dim prompt                  As String
  37.     Dim varAttributes As Variant
  38.     Dim varBlockProperties As Variant
  39.     Dim Index As Variant
  40.     Dim prop As Variant
  41.     Dim propatr As Variant
  42.    
  43.     'Activate the coordinates sheet and find the last row.
  44.     With Sheets("Coordinates")
  45.         .Activate
  46.         LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  47.     End With
  48.         
  49.     'Check if there are coordinates for at least one circle.
  50.     If LastRow < 2 Then
  51.         MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
  52.         Exit Sub
  53.     End If
  54.    
  55.     'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
  56.     On Error Resume Next
  57.     Set acadApp = GetObject(, "AutoCAD.Application")
  58.     If acadApp Is Nothing Then
  59.         Set acadApp = CreateObject("AutoCAD.Application")
  60.         acadApp.Visible = True
  61.     End If
  62.    
  63.     'Check (again) if there is an AutoCAD object.
  64.     If acadApp Is Nothing Then
  65.         MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
  66.         Exit Sub
  67.     End If
  68.     On Error GoTo 0
  69.    
  70.     'If there is no active drawing create a new one.
  71.     On Error Resume Next
  72.     Set acadDoc = acadApp.ActiveDocument
  73.     If acadDoc Is Nothing Then
  74.         Set acadDoc = acadApp.Documents.Add
  75.     End If
  76.     On Error GoTo 0
  77.     'Check if the active space is paper space and change it to model space.
  78.     If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
  79.         acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
  80.     End If
  81.    
  82.     On Error Resume Next
  83.     'Loop through all the rows and add the corresponding blocks in AutoCAD.
  84.     With Sheets("Coordinates")
  85.         For i = 2 To LastRow
  86.         
  87.         
  88.             'Set the block name.
  89.             BlockName = .Range("D" & i).value
  90.             'If the block name is not empty, insert the block.
  91.             If BlockName <> vbNullString Then
  92.                 'Set the insertion point.
  93.                 InsertionPoint(0) = .Range("A" & i).value
  94.                 InsertionPoint(1) = .Range("B" & i).value
  95.                 InsertionPoint(2) = .Range("C" & i).value
  96.               
  97.               
  98.                 'Initialize the optional parameters.
  99.                 BlockScale.X = 1
  100.                 BlockScale.Y = 1
  101.                 BlockScale.Z = 1
  102.                 RotationAngle = 0
  103.          
  104.                         
  105.                 'Add the block using the sheet data (insertion point, block name, scale factors and rotation angle).
  106.                 'The 0.0174532925 is to convert degrees into radians.
  107.                
  108.                                 
  109.                 Set attributeObj = acadBlock.AddAttribute(height, _
  110.                           prompt, InsertionPoint, tag, value)
  111.                           
  112.                 Set acadBlock = acadDoc.ModelSpace.InsertBlock(InsertionPoint, BlockName, _
  113.                                 BlockScale.X, BlockScale.Y, BlockScale.Z, RotationAngle * 0.0174532925)
  114.                                 
  115.                 varAttributes = acadBlock.GetAttributes
  116.                 varAttributes(0).TextString = .Range("E" & i).value
  117.                 varAttributes(1).TextString = .Range("F" & i).value
  118.                 varAttributes(2).TextString = .Range("G" & i).value
  119.                 varAttributes(3).TextString = .Range("H" & i).value
  120.                 varAttributes(4).TextString = .Range("I" & i).value
  121.                                 
  122.             End If
  123.         Next i
  124.     End With
  125.     'Zoom in to the drawing area.
  126.     acadApp.ZoomExtents
  127.     'Release the objects.
  128.     Set acadBlock = Nothing
  129.     Set acadDoc = Nothing
  130.     Set acadApp = Nothing
  131.    
  132. End Sub
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 14:54 , Processed in 0.351186 second(s), 54 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表