明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2600|回复: 6

VBA如何用选择集定义块

[复制链接]
发表于 2007-8-26 10:30:00 | 显示全部楼层 |阅读模式
求助:VBA可以用block.add的方法定义块中的实体,请教:VBA方法如何用图形中已有的实体即选择集来确定块中的对象?
发表于 2007-8-26 21:44:00 | 显示全部楼层
copyobjects
 楼主| 发表于 2007-8-27 22:58:00 | 显示全部楼层
难道还要通过一个转换成XRef块的过程?
发表于 2007-8-28 10:03:00 | 显示全部楼层

查查帮助文件Copyobjects方法的用法。你的要求只是把模型或图纸空间里的对象拷贝到图块空间里。

发表于 2007-8-28 15:48:00 | 显示全部楼层
本帖最后由 作者 于 2010-5-21 10:39:17 编辑

下面的为vb中的源码,稍加改动就可在vba中使用。附件为vb源文件
  1. Option Explicit
  2. Dim SSet As Object
  3. Dim ptBase As Variant
  4. Dim strPath As String
  5. Private Sub CmdCancle_Click()
  6.     Unload Me
  7. End Sub
  8. Private Sub CmdOK_Click()
  9.     If Trim(TxtBlockName.Text) = "" Then
  10.         If MsgBox("请输入图块名称", vbCritical + vbOKOnly, AppName) = vbOK Then Exit Sub
  11.     End If
  12.    
  13.     Me.Hide
  14.    
  15.     ' 提示用户输入块定义的名称
  16.     'Dim strName As String
  17.     'strName = ThisDrawing.Utility.GetString(True, vbCrLf & "输入块的名称:")
  18.         
  19.     ' 获得相对路径
  20.     strPath = App.Path & "\BlockLib" & ComFolderName & "" & Trim(TxtBlockName) & ".dwg"
  21.     'strPath = App.Path & "\BlockLib" & Trim(TxtBlockName) & ".dwg"
  22.    
  23.     ' 将所有的实体移动到原点附近,确保块定义的插入点无误
  24.     'Dim ptOrigin(0 To 2) As Double
  25.     'ptOrigin(0) = 0: ptOrigin(1) = 0: ptOrigin(2) = 0
  26.     'Dim Ent As OBJECT
  27.     'For Each Ent In SSet
  28.     '    Ent.Move ptBase, ptOrigin
  29.     'Next
  30.    
  31.     ' 将块定义导出
  32.     'ThisDrawing.Wblock strPath, SSet   ' 使用此方法创建的块没有浏览缩略图
  33.    
  34.     ThisDrawing.SetVariable "FILEDIA", 0
  35.    
  36.     ' 将块定义导出
  37.     ThisDrawing.SendCommand "-WBLOCK" & vbLf & strPath & vbLf & vbLf & axPoint2lspPoint(ptBase) & vbLf & axSSet2lspEnts(SSet) & vbLf & vbLf
  38.    
  39.     Call CmdOKNextCode
  40. End Sub
  41. Private Sub CmdOKNextCode()
  42.     ThisDrawing.SetVariable "FILEDIA", 1
  43.    
  44.     If OptNoChange.Value Then
  45.         'For Each Ent In SSet
  46.         '    Ent.Move ptOrigin, ptBase
  47.         'Next
  48.     End If
  49.    
  50.     If OptDelect.Value Then
  51.         ' 删除图形中绘制的所有对象
  52.         SSet.Delete
  53.     End If
  54.    
  55.     If OptBlock.Value Then
  56.    
  57.         ' 删除图形中绘制的所有对象
  58.         'SSet.Erase
  59.         SSet.Delete
  60.         
  61.         Dim ObjBlock As Object
  62.         Set ObjBlock = ThisDrawing.ModelSpace.InsertBlock(ptBase, strPath, 1, 1, 1, 0)
  63.     End If
  64.    
  65.     Set SSet = Nothing
  66.    
  67.     Unload Me
  68. End Sub
  69. Private Sub Command1_Click()
  70.     Unload Me
  71. End Sub
  72. Private Sub CmdPickPoint_Click()
  73.     Me.Hide
  74.    
  75.     ' 提示用户输入块定义的基点
  76.     ptBase = ThisDrawing.Utility.GetPoint(, vbCrLf & "拾取基点:")
  77.    
  78.     Me.Show
  79. End Sub
  80. Private Sub CmdSelectObjects_Click()
  81.     Me.Hide
  82.     Set SSet = GetSelectionSetObject
  83.    
  84.     Me.Show
  85. End Sub
  86. Private Sub Form_Load()
  87.     On Error GoTo ErrHandle
  88.    
  89.     Dim Fso As Object
  90.     Dim Fols As Object
  91.     Dim Fol As Object
  92.    
  93.     Set Fso = CreateObject("Scripting.FileSystemObject")
  94.     Set Fols = Fso.GetFolder(App.Path & "\BlockLib")
  95.     For Each Fol In Fols.SubFolders
  96.         ComFolderName.AddItem Fol.Name
  97.     Next
  98.    
  99.     ComFolderName.ListIndex = 0
  100.    
  101.     Call GetAutoCADApplication(Me)
  102.    
  103.     Call MoveXWindowsCenter(Me)
  104.    
  105.     Set Fso = Nothing
  106.     Set Fols = Nothing
  107.    
  108.     Exit Sub
  109. ErrHandle:
  110.     MsgBox Err.Description, vbCritical + vbOKOnly, AppName
  111.     Err.Clear
  112.     Unload Me
  113. End Sub
  114. Private Sub Form_KeyPress(KeyAscii As Integer)
  115.     If KeyAscii = 27 Then '用户按了ESC键,退出
  116.         Unload Me
  117.     End If
  118. End Sub
  119. Private Sub TextFocus(ctl As Control) '定义过程
  120.     ctl.SelStart = 0
  121.     ctl.SelLength = Len(ctl.Text)
  122. End Sub
  123. Private Sub Text1_GotFocus()
  124.     TextFocus Text1 '过程调用
  125. End Sub
  126. Public Property Set Application(ByVal vNewApplication As Object)
  127.     Set AcadApp = vNewApplication
  128. End Property
  129. '转换点的函数
  130. Public Function axPoint2lspPoint(Pnt As Variant) As String
  131.     axPoint2lspPoint = Pnt(0) & "," & Pnt(1) & "," & Pnt(2)
  132. End Function
  133. '转换图元函数
  134. Public Function axEnt2lspEnt(EntObj As Object) As String
  135.     Dim entHandle As String
  136.     entHandle = EntObj.Handle
  137.     axEnt2lspEnt = "(handent " & Chr(34) & entHandle & Chr(34) & ")"
  138. End Function
  139. ' 转换多个图元的函数
  140. Public Function axSSet2lspEnts(ByVal SSet As Object) As String
  141.     If SSet.Count = 0 Then Exit Function
  142.     Dim entHandle As String
  143.     Dim strEnts As String
  144.     entHandle = SSet.Item(0).Handle
  145.     strEnts = "(handent " & Chr(34) & entHandle & Chr(34) & ")"
  146.    
  147.     If SSet.Count > 1 Then
  148.         Dim i As Integer
  149.         For i = 1 To SSet.Count - 1
  150.             entHandle = SSet.Item(i).Handle
  151.             strEnts = strEnts & vbCr & "(handent " & Chr(34) & entHandle & Chr(34) & ")"
  152.         Next i
  153.     End If
  154.    
  155.     axSSet2lspEnts = strEnts
  156. End Function

本帖子中包含更多资源

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

x
发表于 2007-8-29 09:05:00 | 显示全部楼层
 楼主| 发表于 2007-8-31 23:38:00 | 显示全部楼层
多谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-26 12:33 , Processed in 0.152527 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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