乐筑天下

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

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

[复制链接]

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 19:30:15 | 显示全部楼层 |阅读模式
样品rar公司
嗨,朋友们,
我从网上找到了一个VBA代码。根据AutoCAD中的定位坐标对块进行编码。
代码正在工作,但用于简单的块。
我想更改属性块,但我做不到。
 
谁能帮忙?
 
我希望我能理解。因为我的英语太差了
 
  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.    tag = "ATT1"
  38.    value = Range("E3")
  39.    height = 250
  40.    
  41.    'Activate the coordinates sheet and find the last row.
  42.    With Sheets("Coordinates")
  43.        .Activate
  44.        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  45.    End With
  46.       
  47.    'Check if there are coordinates for at least one circle.
  48.    If LastRow < 2 Then
  49.        MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
  50.        Exit Sub
  51.    End If
  52.    
  53.    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
  54.    On Error Resume Next
  55.    Set acadApp = GetObject(, "AutoCAD.Application")
  56.    If acadApp Is Nothing Then
  57.        Set acadApp = CreateObject("AutoCAD.Application")
  58.        acadApp.Visible = True
  59.    End If
  60.    
  61.    'Check (again) if there is an AutoCAD object.
  62.    If acadApp Is Nothing Then
  63.        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
  64.        Exit Sub
  65.    End If
  66.    On Error GoTo 0
  67.    
  68.    'If there is no active drawing create a new one.
  69.    On Error Resume Next
  70.    Set acadDoc = acadApp.ActiveDocument
  71.    If acadDoc Is Nothing Then
  72.        Set acadDoc = acadApp.Documents.Add
  73.    End If
  74.    On Error GoTo 0
  75.    'Check if the active space is paper space and change it to model space.
  76.    If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
  77.        acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
  78.    End If
  79.    
  80.    On Error Resume Next
  81.    'Loop through all the rows and add the corresponding blocks in AutoCAD.
  82.    With Sheets("Coordinates")
  83.        For i = 2 To LastRow
  84.       
  85.       
  86.            'Set the block name.
  87.            BlockName = .Range("D" & i).value
  88.            'If the block name is not empty, insert the block.
  89.            If BlockName <> vbNullString Then
  90.                'Set the insertion point.
  91.                InsertionPoint(0) = .Range("A" & i).value
  92.                InsertionPoint(1) = .Range("B" & i).value
  93.                InsertionPoint(2) = .Range("C" & i).value
  94.             
  95.             
  96.                'Initialize the optional parameters.
  97.                BlockScale.X = 1
  98.                BlockScale.Y = 1
  99.                BlockScale.Z = 1
  100.                RotationAngle = 0
  101.          
  102.                        
  103.                'Add the block using the sheet data (insertion point, block name, scale factors and rotation angle).
  104.                'The 0.0174532925 is to convert degrees into radians.
  105.                
  106.                               
  107.                Set attributeObj = acadBlock.AddAttribute(height, _
  108.                          prompt, InsertionPoint, tag, value)
  109.                         
  110.                Set acadBlock = acadDoc.ModelSpace.Insertblock(InsertionPoint, BlockName, _
  111.                                BlockScale.X, BlockScale.Y, BlockScale.Z, RotationAngle * 0.0174532925)
  112.                               
  113.            End If
  114.        Next i
  115.    End With
  116.    'Zoom in to the drawing area.
  117.    acadApp.ZoomExtents
  118.    'Release the objects.
  119.    Set acadBlock = Nothing
  120.    Set acadDoc = Nothing
  121.    Set acadApp = Nothing
  122.    
  123. End Sub
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 19:36:46 | 显示全部楼层
没有人回答吗?
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 19:46:26 | 显示全部楼层
下面是一些代码更新属性值的示例
 
  1. attribs = SS.Item(Cntr).GetAttributes
  2.       
  3.     If attribs(0).TextString = pitname Then
  4.       pt1 = ThisDrawing.Utility.GetPoint(, " pick first point")
  5.       txtx1 = CStr(FormatNumber(pt1(0), 3))
  6.       TXTY1 = CStr(FormatNumber(pt1(1), 3))
  7.       
  8.        attribs(1).TextString = txtx1
  9.        attribs(2).TextString = TXTY1
  10.       
  11.        attribs(1).Update
  12.        attribs(2).Update
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 19:52:04 | 显示全部楼层
嗨,比格尔,
非常感谢你为我花的时间。
代码由我如何集成自己的代码给出?我不太擅长编码,你能帮我吗?
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 19:54:35 | 显示全部楼层
我已经停止使用VBA了,最好看一下VLISP或纯lisp的很多例子来做同样的事情。
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 20:01:28 | 显示全部楼层
非常感谢。我的搜索还在继续,我希望我的权利
回复

使用道具 举报

2

主题

9

帖子

7

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 20:05:34 | 显示全部楼层
我和你在一起。
首先,您需要能够在excel工作表中列出属性值。在表格中的样本中(“坐标”)
然后您将使用;
 
Dim varAttributes作为变体
 
varAttributes=acadblock。获取属性
 
对于L=LBound(varAttributes)到UBound(varAttributes)
varAttributes(L)。TextString=devrekesici(k+1,L+1)。价值
下一个
 
上面的“devrekesici”是我指定的范围。它将是你想要的任何东西。
 
希望这对别人有帮助。
回复

使用道具 举报

1

主题

6

帖子

5

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 20:11:58 | 显示全部楼层
非常感谢piscopatos,它起作用了!!
回复

使用道具 举报

2

主题

9

帖子

7

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 20:18:41 | 显示全部楼层
我很高兴它对你有用。试着进入。NET API。这有点难,但要强大得多。
回复

使用道具 举报

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-6 20:21:12 | 显示全部楼层
[尺寸=4]witchhero,你能在这里找到新的重拍代码吗:),谢谢![/尺寸]
 
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 14:48 , Processed in 0.407719 second(s), 72 queries .

© 2020-2025 乐筑天下

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