乐筑天下

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

如何用vba插入块??

[复制链接]

14

主题

27

帖子

13

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
70
发表于 2022-7-6 22:57:25 | 显示全部楼层 |阅读模式
我有一个子例程,它应该定义和声明一个块,向其中添加元素,然后将其插入当前图形中的指定位置。问题是它只在第一次正常工作。之后,在相对于给定插入点的相对位置插入不同数量的块。感谢您的帮助。子例程如下所示:
 
  1. Public Sub BlocCota(pIn As Variant, valCota As Double)
  2. 'Pt hasura
  3.    Dim outerLoop(0 To 0) As AcadEntity
  4.    Dim hasura As AcadHatch
  5.    Dim hasuraPattern As AcPatternType
  6.    Dim hasuraName As String
  7. 'pt restul
  8.    Dim myBloc As AcadBlock
  9.    Dim linie As AcadLine
  10.    Dim myPoly As AcadPolyline
  11.    Dim myText As AcadText
  12.    Dim colPuncte(0 To  As Double
  13.    Dim p1(0 To 2) As Double
  14.    Dim p2(0 To 2) As Double
  15.    Dim s As String
  16.    s = CStr(valCota)
  17. 'Adaugam blocul la desen
  18.    Dim contor As Integer
  19.    Dim b As Boolean
  20.    Dim vX As Integer
  21.    b = True
  22.    contor = ThisDrawing.Blocks.Count - 1
  23.    Dim ind As Integer
  24.    For ind = 0 To contor Step 1
  25.        If ThisDrawing.Blocks.Item(ind).Name = "sageataNivel" Then
  26.            b = False
  27.            vX = ind
  28.        End If
  29.    Next ind
  30.    
  31.    If b = True Then
  32.        Set myBloc = ThisDrawing.Blocks.Add(pIn, "sageataNivel")
  33.    Else
  34.        Set myBloc = ThisDrawing.Blocks.Item(vX)
  35.    End If
  36.    
  37.       
  38.    
  39.    
  40. 'Linia ____ de inceput
  41.    p1(0) = pIn(0)
  42.    p1(1) = pIn(1)
  43.    p1(2) = pIn(2)
  44.    
  45.    p2(0) = p1(0) + 5
  46.    p2(1) = p1(1)
  47.    p2(2) = p1(2)
  48.    
  49.    Set linie = myBloc.AddLine(p1, p2)
  50.    
  51. 'Triunghiul din stanga
  52.    colPuncte(0) = p2(0)
  53.    colPuncte(1) = p2(1)
  54.    colPuncte(2) = p2(2)
  55.    
  56.    colPuncte(3) = colPuncte(0)
  57.    colPuncte(4) = colPuncte(1) + 5
  58.    colPuncte(5) = colPuncte(2)
  59.    
  60.    colPuncte(6) = colPuncte(3) - 5
  61.    colPuncte(7) = colPuncte(4)
  62.    colPuncte( = colPuncte(5)
  63.    
  64.    Set myPoly = myBloc.AddPolyline(colPuncte)
  65.    myPoly.Closed = True
  66.    
  67. 'Triunghiul din dreapta
  68.    colPuncte(0) = colPuncte(0)
  69.    colPuncte(1) = colPuncte(1)
  70.    colPuncte(2) = colPuncte(2)
  71.    
  72.    colPuncte(3) = colPuncte(0)
  73.    colPuncte(4) = colPuncte(1) + 5
  74.    colPuncte(5) = colPuncte(2)
  75.    
  76.    colPuncte(6) = colPuncte(3) + 5
  77.    colPuncte(7) = colPuncte(4)
  78.    colPuncte( = colPuncte(5)
  79.    
  80.    
  81.    
  82.    Set myPoly = myBloc.AddPolyline(colPuncte)
  83.    myPoly.Closed = True
  84.    
  85.    Set outerLoop(0) = myPoly
  86.    hasuraPattern = acHatchPatternTypePreDefined
  87.    hasuraName = "SOLID"
  88.    Set hasura = ThisDrawing.ModelSpace.AddHatch(hasuraPattern, hasuraName, True)
  89.    hasura.AppendOuterLoop (outerLoop)
  90.    hasura.Evaluate
  91.    
  92. 'Linia |
  93.    p1(0) = p2(0)
  94.    p1(1) = p2(1)
  95.    p1(2) = p2(2)
  96.    
  97.    p2(0) = p2(0)
  98.    p2(1) = p2(1) + 15
  99.    p2(2) = p2(2)
  100.    
  101.    Set linie = myBloc.AddLine(p1, p2)
  102. 'Linia ------
  103.    p1(0) = p2(0)
  104.    p1(1) = p1(1) + 5
  105.    p1(2) = p1(2)
  106.    
  107.    p2(0) = p1(0) + 15
  108.    p2(1) = p1(1)
  109.    p2(2) = p1(2)
  110.    Set linie = myBloc.AddLine(p1, p2)
  111.    
  112. 'Textul
  113.    p1(0) = p1(0) + 3
  114.    p1(1) = p1(1) + 3
  115.    p1(2) = p1(2)
  116.    Set myText = myBloc.AddText(s, p1, 7)
  117.    
  118.    
  119. 'Inseram blocul in punctul dat
  120.    Dim myBlocRef As AcadBlockReference
  121.    Set myBlocRef = ThisDrawing.ModelSpace.InsertBlock(pIn, "sageataNivel", 1#, 1#, 1#, 0)
  122.    myBlocRef.Layer = "cote"
  123. End Sub
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 23:28:29 | 显示全部楼层
该例程将相似的几何图形添加到现有块中。这意味着引用该块的任何插入也将包括该新几何体。
 
这可能是一种选择(见下文)。如果每个块参照需要不同的值(valCota),则应将其设置为属性。
 
  1. Public Sub BlocCota(pIn As Variant, valCota As Double)
  2. 'Pt hasura
  3.    Dim outerLoop(0 To 0) As AcadEntity
  4.    Dim hasura As AcadHatch
  5.    Dim hasuraPattern As AcPatternType
  6.    Dim hasuraName As String
  7. 'pt restul
  8.    Dim myBloc As AcadBlock
  9.    Dim linie As AcadLine
  10.    Dim myPoly As AcadPolyline
  11.    Dim myPoly2 As AcadPolyline
  12.    Dim myText As AcadText
  13.    Dim colPuncte(0 To  As Double
  14.    Dim p1(0 To 2) As Double
  15.    Dim p2(0 To 2) As Double
  16.    Dim s As String
  17.    s = CStr(valCota)
  18. 'Adaugam blocul la desen
  19.    Dim contor As Integer
  20.    Dim b As Boolean
  21.    Dim vX As Integer
  22.    b = True
  23.    contor = ThisDrawing.Blocks.Count - 1
  24.    Dim ind As Integer
  25.    For ind = 0 To contor Step 1
  26.        If ThisDrawing.Blocks.Item(ind).Name = "sageataNivel" Then
  27.            b = False
  28.            vX = ind
  29.            Exit For
  30.        End If
  31.    Next ind
  32.    
  33.    'Linia ____ de inceput
  34.    p1(0) = pIn(0)
  35.    p1(1) = pIn(1)
  36.    p1(2) = pIn(2)
  37.    
  38.    p2(0) = p1(0) + 5
  39.    p2(1) = p1(1)
  40.    p2(2) = p1(2)
  41.    
  42.    'Triunghiul din stanga
  43.    colPuncte(0) = p2(0)
  44.    colPuncte(1) = p2(1)
  45.    colPuncte(2) = p2(2)
  46.    
  47.    colPuncte(3) = colPuncte(0)
  48.    colPuncte(4) = colPuncte(1) + 5
  49.    colPuncte(5) = colPuncte(2)
  50.    
  51.    colPuncte(6) = colPuncte(3) - 5
  52.    colPuncte(7) = colPuncte(4)
  53.    colPuncte( = colPuncte(5)
  54.    
  55.    If b = True Then
  56.        Set myBloc = ThisDrawing.Blocks.Add(pIn, "sageataNivel")
  57.         Set linie = myBloc.AddLine(p1, p2)
  58.    
  59.    
  60.      Set myPoly = myBloc.AddPolyline(colPuncte)
  61.      myPoly.Closed = True
  62.      'Linia |
  63.       p1(0) = p2(0)
  64.       p1(1) = p2(1)
  65.       p1(2) = p2(2)
  66.       
  67.       p2(0) = p2(0)
  68.       p2(1) = p2(1) + 15
  69.       p2(2) = p2(2)
  70.       
  71.       Set linie = myBloc.AddLine(p1, p2)
  72.   
  73.      'Linia ------
  74.       p1(0) = p2(0)
  75.       p1(1) = p1(1) + 5
  76.       p1(2) = p1(2)
  77.       
  78.       p2(0) = p1(0) + 15
  79.       p2(1) = p1(1)
  80.       p2(2) = p1(2)
  81.   
  82.       Set linie = myBloc.AddLine(p1, p2)
  83.       
  84.   
  85.      'Textul
  86.       p1(0) = p1(0) + 3
  87.       p1(1) = p1(1) + 3
  88.       p1(2) = p1(2)
  89.       Set myText = myBloc.AddText(s, p1, 7)
  90.    Else
  91.        Set myBloc = ThisDrawing.Blocks.Item(vX)
  92.    End If
  93.    
  94.   Set myPoly2 = ThisDrawing.ModelSpace.AddPolyline(colPuncte)
  95.    myPoly2.Closed = True
  96.    
  97.    Set outerLoop(0) = myPoly2
  98.    hasuraPattern = acHatchPatternTypePreDefined
  99.    hasuraName = "SOLID"
  100.    Set hasura = ThisDrawing.ModelSpace.AddHatch(hasuraPattern, hasuraName, True)
  101.    hasura.AppendOuterLoop (outerLoop)
  102.    hasura.Evaluate
  103.    
  104. 'Inseram blocul in punctul dat
  105.    Dim myBlocRef As AcadBlockReference
  106.    Set myBlocRef = ThisDrawing.ModelSpace.InsertBlock(pIn, "sageataNivel", 1#, 1#, 1#, 0)
  107.    myBlocRef.Layer = "cote"
  108. End Sub
回复

使用道具 举报

0

主题

19

帖子

20

银币

限制会员

铜币
-1
发表于 2022-7-7 00:08:55 | 显示全部楼层
我认为变量“pIn”有不同的位置需求。一个是块“definition”(add)中的插入点
 
设置myBloc=ThisDrawing。阻碍。添加(pIn,“sageataNivel”)
 
例程末端的另一个实例是它在图形中作为块“参考”(插入)的位置
 
设置myBlocRef=ThisDrawing。模型空间。插入块(引脚,“sageataNivel”,1#,1#,1#,0)
 
如果在“blockdefinition”和“blockreference”的调用中,Pin始终为0,0,那么没有问题,
但当所选输入发生变化时,在第二次、第三次、第四次插入时,在放置“块参考”的图形中,在第二个插脚调用插入时,
添加时,它也会相对于原始调用在其“定义”中的位置进行更改(当我假设pIn发生变化时,基本上在第二、第三、第四次调用中重新定义)。
它是否将块“参考”的预期插入的x、y值,甚至可能是x2、第二、第三、第四个序列置换?
 
“pIn\u Def”和“pIn\u Ref”是必需的,我猜是独立的值,pIn\u Def应该是最终的,相对于块“定义”本身。
或者您添加(定义)一次,并且从不重新定义它的“定义”,随后调用在图形中的第二、第三、第四个插入处固定为“参考”
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2024-11-21 21:09 , Processed in 0.142259 second(s), 58 queries .

© 2020-2024 乐筑天下

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