明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1930|回复: 5

[讨论]Bug??炸开插入文件块后MText也被炸开。

[复制链接]
发表于 2003-12-16 09:26:00 | 显示全部楼层 |阅读模式
我用VBA将一个DWG文件插入当前文件中,代码如下:
插入结果是MText也被炸开称为Text,但是PLine等却没有被炸开,请问是不是CAD的bug?
我用的是AutoCAD2000中文版。

Public Sub insertFile()
  Dim insertPoint(0 To 2) As Double
  Dim block As AcadBlockReference
  Dim FileToInsert As String
  insertPoint(0) = 0
  insertPoint(1) = 0
  insertPoint(2) = 0
  
  FileToInsert = "C:\MText.dwg"
  Set block = ThisDrawing.ModelSpace.InsertBlock(insertPoint, FileToInsert, 1, 1, 1, 0)
  block.Explode
  'block.Delete
End Sub
发表于 2003-12-16 09:58:00 | 显示全部楼层
呵呵,奇怪了,居然跟用Explode命令的效果不同?

实在不行的话就用thisdrawing.sendcommand "Explode last  "
来代替block.Explode咯
 楼主| 发表于 2003-12-16 10:17:00 | 显示全部楼层
谢谢回复!
看来我只有用sendcommand代替了。
 楼主| 发表于 2003-12-16 15:41:00 | 显示全部楼层
刚刚实际应用时发现一点小问题:当被插入的文件中有图层被关闭时用"Explode last  "时不会炸开图形,修改为直接使用句柄作为Explode的参数即可。完整代码如下:


Public Sub insertFile()
  Dim insertPoint(0 To 2) As Double
  Dim block As AcadBlockReference
  Dim FileToInsert As String
  insertPoint(0) = 0
  insertPoint(1) = 0
  insertPoint(2) = 0
  
  FileToInsert = "C:\MText.dwg"
  Set block = ThisDrawing.ModelSpace.InsertBlock(insertPoint, FileToInsert, 1, 1, 1, 0)
  'block.Explode
  'block.Delete
'  ThisDrawing.SendCommand "explode " & axEnt2lspEnt(block) & "  "
   ThisDrawing.SendCommand "explode last  "

End Sub
Public Function axEnt2lspEnt(entObj As AcadEntity) As String
    Dim entHandle As String
    entHandle = entObj.Handle
    axEnt2lspEnt = "(handent " & Chr(34) & entHandle & Chr(34) & ")"
End Function
发表于 2003-12-17 15:11:00 | 显示全部楼层
以下函数可解决该问题,该函数使用了大家比较少认识的CopyObjects方法来直接将图块中的对象复制到当前的空间中并按照插入点的位置移动到图块插入的位置。
  1. Sub BlkExp()
  2.     Dim ent As AcadEntity
  3.     Dim pnt As Variant
  4.     On Error Resume Next
  5.     Do
  6.         ThisDrawing.Utility.GetEntity ent, pnt, "选择要分解的图块参照对象:"
  7.         If Err <> 0 Then
  8.             Err.Clear
  9.         Else
  10.             If ent.ObjectName = "AcDbBlockReference" Then Exit Do
  11.         End If
  12.     Loop
  13.     MsgBox "选定的图块被分解后共有" & UBound(BlockRefExplode(ent)) & "个图元。", , "明经通道VBA示例"
  14. End Sub

  15. ' 该函数用于代替ActiveX方法中图块的Explode方法, _
  16.   因为原先的Explode方法带有BUG, _
  17.   分解带MText时连MText都被分解成Text。
  18.   
  19. Function BlockRefExplode(BlockRef As AcadBlockReference) As Variant

  20.     Dim Space As AcadBlock
  21.     Dim BlockName As String
  22.     Dim InsertPoint As Variant
  23.     Dim OriginPoint(2) As Double
  24.     Dim Block As AcadBlock

  25.     BlockName = BlockRef.Name
  26.     InsertPoint = BlockRef.InsertionPoint
  27.     Set Space = ThisDrawing.ObjectIdToObject(BlockRef.OwnerID)
  28.     Set Block = ThisDrawing.Blocks(BlockName)

  29.     Dim BlkEnt() As AcadEntity
  30.     ReDim BlkEnt(Block.Count - 1)
  31.     Dim i As Long
  32.     For i = 0 To Block.Count - 1
  33.         Set BlkEnt(i) = Block(i)
  34.     Next

  35.     Dim SpaceCount As Long
  36.     SpaceCount = Space.Count
  37.     ThisDrawing.CopyObjects BlkEnt, Space
  38.    
  39.     Dim TotalCount As Long
  40.     TotalCount = Space.Count - SpaceCount
  41.    
  42.     Dim BlkRefEnt() As AcadEntity
  43.     ReDim BlkRefEnt(TotalCount)
  44.     For i = 0 To TotalCount - 1
  45.         Space(i + SpaceCount).Move OriginPoint, InsertPoint
  46.         Set BlkRefEnt(i) = Space(i + SpaceCount)
  47.     Next

  48.     BlockRef.Delete
  49.     BlockRefExplode = BlkRefEnt
  50. End Function
 楼主| 发表于 2003-12-18 11:40:00 | 显示全部楼层
谢谢老大!
我认为直接用Sendcommand运行CAD的命令不是太好,但是有时候又不知道如何直接用VBA解决一些问题,下面的代码也是直接用CAD的Pedit命令将一些首尾相连的直线连接成一个PLine,老大可不可以转换为直接使用VBA函数实现同样功能?

'合并选择集中分离的线为PLine
Public Sub JoinLineToPline(sel As AcadSelectionSet)
   Dim strCommand As String
    strCommand = "_Pedit" & vbCr & axEnt2lspEnt(sel.Item(0)) & " Y J "
    Dim i As Integer
      For i = 0 To sel.Count - 1
        strCommand = strCommand + axEnt2lspEnt(sel.Item(i)) + vbCr
      Next
      strCommand = strCommand + vbCr + vbCr
      ThisDrawing.SendCommand strCommand
End Sub
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-28 12:53 , Processed in 0.167976 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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