明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: 翔云95

vba块添加元素,请教大佬

[复制链接]
 楼主| 发表于 7 天前 来自手机 | 显示全部楼层
tiancao100 发表于 2025-5-24 19:34
AppendEntity就不是VBA的,是吧? 转VB。NET也很简单

appendentity是.net的,vba确实没有。
vba只能sendcommand了吧
回复 支持 反对

使用道具 举报

发表于 7 天前 | 显示全部楼层
vba 还可以用 ThisDrawing.CopyObjects
比如:
Sub f_SolAddDiminBlocks()
    'Workaround for Adding dimensions to block AutoCAD
     Dim po_rotDim As AcadDimAligned
     Dim po_block As AcadBlock
     Dim pd_ext1(0 To 2) As Double
     Dim pd_ext2(0 To 2) As Double
     Dim pd_lineLoc(0 To 2) As Double
     Dim po_array(0) As Object

     pd_ext1(0) = 3 : pd_ext1(1) = 3 : pd_ext1(2) = 0
     pd_ext2(0) = 10 : pd_ext2(1) = 3 : pd_ext2(2) = 0
     pd_lineLoc(0) = 5 : pd_lineLoc(1) = 4 : pd_lineLoc(2) = 0
     'create dimeionsion object
     po_rotDim = ThisDrawing.ModelSpace.AddDimAligned(pd_ext1, pd_ext2,
     pd_lineLoc)

     'create a new block by name test
     po_block = ThisDrawing.Blocks.Add(pd_ext1, "test")
     'insert a block reference
     ThisDrawing.ModelSpace.InsertBlock(pd_ext1, "test", 1, 1, 1, 0)
     'copy dimension object
     po_array(0) = po_rotDim
     ThisDrawing.CopyObjects(po_array, po_block)
     po_rotDim.Delete()
     'release the references
     po_block = Nothing
     po_rotDim = Nothing
End Sub
来自:https://adndevblog.typepad.com/a ... ject-using-vba.html
回复 支持 反对

使用道具 举报

 楼主| 发表于 6 天前 | 显示全部楼层
tiancao100 发表于 2025-5-24 23:21
vba 还可以用 ThisDrawing.CopyObjects
比如:
Sub f_SolAddDiminBlocks()

多谢大咖。  这方法OK

在 ThisDrawing.CopyObjects(po_array, po_block) 我改为:
po_block = ThisDrawing.CopyObjects(po_array)

大咖,赞个。多谢多谢
回复 支持 反对

使用道具 举报

发表于 6 天前 来自手机 | 显示全部楼层
有自动化的块应该不复杂吧。换个思路,把块写入外部一个dwg空文件内然后把块清空。在外部处理好后,再重新复制到块内。简单说就是将块内元素全部换新。
回复 支持 反对

使用道具 举报

 楼主| 发表于 6 天前 | 显示全部楼层
回头试试我弄个 AppendEntity函数,按这个vba的CopyObjects方式。单实体,选择集appendentity

回复 支持 反对

使用道具 举报

 楼主| 发表于 6 天前 | 显示全部楼层
longxh28 发表于 2025-5-25 10:30
有自动化的块应该不复杂吧。换个思路,把块写入外部一个dwg空文件内然后把块清空。在外部处理好后,再重新 ...

嗯。 外部dwg方式组块 读进来 , AttachExternalReference()..   

这个思路 也很赞  灵活。多谢大咖提示
回复 支持 反对

使用道具 举报

发表于 6 天前 | 显示全部楼层
~
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 5 天前 | 显示全部楼层
本帖最后由 翔云95 于 2025-5-26 08:48 编辑

                                                                                                       -----        
回复 支持 反对

使用道具 举报

 楼主| 发表于 5 天前 | 显示全部楼层
Bao_lai 发表于 2025-5-25 22:26
这个还要涉及到变换矩阵的哟~

多谢大咖提示
有链接或示例吗?
回复 支持 反对

使用道具 举报

发表于 4 天前 | 显示全部楼层
本帖最后由 Sring65 于 2025-5-27 17:47 编辑

可以试试WBlock导出文件后在块里面再插入为块
  1. Sub Example_WBlock()
  2.     ' This example creates several objects in model space and
  3.     ' adds them to a selection set. This selection set is then
  4.     ' output as a new drawing file.
  5.    
  6.     ' Create a Ray object in model space
  7.     Dim rayObj As AcadRay
  8.     Dim basePoint(0 To 2) As Double
  9.     Dim SecondPoint(0 To 2) As Double
  10.     basePoint(0) = 3#: basePoint(1) = 3#: basePoint(2) = 0#
  11.     SecondPoint(0) = 1#: SecondPoint(1) = 3#: SecondPoint(2) = 0#
  12.     Set rayObj = ThisDrawing.ModelSpace.AddRay(basePoint, SecondPoint)
  13.    
  14.     ' Create a polyline object in model space
  15.     Dim plineObj As AcadLWPolyline
  16.     Dim points(0 To 5) As Double
  17.     points(0) = 3: points(1) = 7
  18.     points(2) = 9: points(3) = 2
  19.     points(4) = 3: points(5) = 5
  20.     Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
  21.     plineObj.Closed = True

  22.     ' Create a line object in model space
  23.     Dim lineObj As AcadLine
  24.     Dim startPoint(0 To 2) As Double
  25.     Dim endPoint(0 To 2) As Double
  26.     startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0
  27.     endPoint(0) = 2: endPoint(1) = 2: endPoint(2) = 0
  28.     Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
  29.    
  30.     ' Create a circle object in model space
  31.     Dim circObj As AcadCircle
  32.     Dim centerPt(0 To 2) As Double
  33.     Dim radius As Double
  34.     centerPt(0) = 20: centerPt(1) = 30: centerPt(2) = 0
  35.     radius = 3
  36.     Set circObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius)

  37.     ' Create an ellipse object in model space
  38.     Dim ellObj As AcadEllipse
  39.     Dim majAxis(0 To 2) As Double
  40.     Dim center(0 To 2) As Double
  41.     Dim radRatio As Double
  42.     center(0) = 5#: center(1) = 5#: center(2) = 0#
  43.     majAxis(0) = 10: majAxis(1) = 20#: majAxis(2) = 0#
  44.     radRatio = 0.3
  45.     Set ellObj = ThisDrawing.ModelSpace.AddEllipse(center, majAxis, radRatio)

  46.     ZoomAll
  47.    
  48.     ' Create a selection set
  49.     Dim ssetObj As AcadSelectionSet
  50.     Set ssetObj = ThisDrawing.SelectionSets.Add("WBLOCKSET")
  51.    
  52.     ' Iterate through the model space collection and add
  53.     ' each item found to an array of objects
  54.     ReDim objsInModelSpace(0 To ThisDrawing.ModelSpace.count - 1) As AcadEntity
  55.     Dim I As Integer
  56.     For I = 0 To ThisDrawing.ModelSpace.count - 1
  57.         Set objsInModelSpace(I) = ThisDrawing.ModelSpace.Item(I)
  58.     Next
  59.    
  60.     ' Add the array of objects into the selection set
  61.     ssetObj.AddItems objsInModelSpace
  62.    
  63.     ' Output the selection set to a new file
  64.     ThisDrawing.Wblock "C:\AutoCAD\WBlock_example.dwg", ssetObj
  65.    
  66. End Sub
  1. Sub Example_AttachExternalReference()
  2.     ' This example displays all the blocks in the current drawing
  3.     ' before and after adding an external reference.
  4.     '
  5.     ' This example uses the "city map.dwg" found in the Sample
  6.     ' directory. If you do not have this drawing, or if it is
  7.     ' in a different directory, insert a valid path and file name
  8.     ' for the PathName variable below.
  9.    
  10.     Dim InsertPoint(0 To 2) As Double
  11.     Dim insertedBlock As AcadExternalReference
  12.     Dim tempBlock As AcadBlock
  13.     Dim msg As String, PathName As String
  14.    
  15.     ' Define external reference to be inserted
  16.     InsertPoint(0) = 1: InsertPoint(1) = 1: InsertPoint(2) = 0
  17.     PathName = "c:\program files\autocad\sample\city map.dwg"
  18.    
  19.     ' Display current Block information for this drawing
  20.     GoSub ListBlocks
  21.    
  22.     ' Add the external reference to the drawing
  23.     Set insertedBlock = ThisDrawing.ModelSpace.AttachExternalReference(PathName, "XREF_IMAGE", InsertPoint, 1, 1, 1, 0, False)
  24.         
  25.     ThisDrawing.Application.ZoomAll
  26.    
  27.     ' Display new Block information for this drawing
  28.     GoSub ListBlocks
  29.    
  30.     Exit Sub

  31. ListBlocks:
  32.     msg = vbCrLf    ' Reset message
  33.    
  34.     For Each tempBlock In ThisDrawing.Blocks
  35.         msg = msg & tempBlock.name & vbCrLf     ' Add Block to list
  36.     Next
  37.    
  38.     MsgBox "The current blocks in this drawing are: " & msg
  39.    
  40.     Return
  41. End Sub
  1. Sub Example_Bind()
  2.     On Error GoTo ERRORHANDLER
  3.                           
  4.     ' Define external reference to be inserted
  5.     Dim xrefHome As AcadBlock
  6.     Dim xrefInserted As AcadExternalReference
  7.     Dim insertionPnt(0 To 2) As Double
  8.     Dim PathName As String
  9.     insertionPnt(0) = 1
  10.     insertionPnt(1) = 1
  11.     insertionPnt(2) = 0
  12.     PathName = "c:/AutoCAD/sample/City map.dwg"
  13.    
  14.     ' Add the external reference
  15.     Set xrefInserted = ThisDrawing.ModelSpace. _
  16.             AttachExternalReference(PathName, "XREF_IMAGE", _
  17.             insertionPnt, 1, 1, 1, 0, False)
  18.     ZoomAll
  19.     MsgBox "The external reference is attached."
  20.    
  21.     ' Bind the external reference definition
  22.     ThisDrawing.Blocks.Item(xrefInserted.name).Bind False
  23.     MsgBox "The external reference is bound."
  24.     Exit Sub
  25. ERRORHANDLER:
  26.     MsgBox Err.Description
  27. End Sub

回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-31 09:32 , Processed in 0.147714 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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