明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1942|回复: 0

[原创]不规则多边形区域填充的代码

[复制链接]
发表于 2008-1-18 22:19:00 | 显示全部楼层 |阅读模式

在论坛学到了不少知识,也应该做点回报。

填充不是很快,别人画的偶然会出现程序无影响的现象,几百个填了近一个小时。发出来大家看看。但我自己用多条多段线画的不规则多边形可以很快填充。其中有几个窗口界面上的就不发了,只发了关键代码。

其中,Listbox1是从Excel取过来的数据。

程序可以利用查找图中的文字内容与EXcel取的数据对比,如相符,则填充文字所在区域周围的不规则的多边形。多边形里面的孤岛不填充。

代码如下:

Dim layerName As String
Dim entry As AcadLayer
Dim ghtc As Boolean
Dim colo As Integer

layerName = ""

For Each entry In ThisDrawing.Layers
layerName = entry.Name
If layerName = "填充" Then
ghtc = True
ThisDrawing.ActiveLayer = entry
Exit For
End If
Next

If ghtc <> True Then

Set entry = ThisDrawing.Layers.Add("填充")
ThisDrawing.ActiveLayer = entry

End If
entry.color = acGreen ' 指定"填充"图层的颜色为绿色
colo = 3
ThisDrawing.SendCommand "_-color" & vbCr & colo & vbCr
                Do
                If COMMANDNAME = "" Then
                Sleep 50
                Exit Do
                End If
                Loop

On Error GoTo errHandle

Dim Found
'Dim pt As Variant
Dim MyObject As AcadObject
Dim MyCollection As AcadModelSpace

UserForm1.TextBox1.text = ""
UserForm1.Hide
UserForm3.Show
  
Found = False    ' 设置变量初始值。

ZoomExtents

Set MyCollection = ThisDrawing.ModelSpace

For i = 0 To ListBox1.ListCount - 1

    DoEvents
   
    For Each MyObject In MyCollection    ' 对每个成员作一次迭代。
    'MsgBox MyObject.ObjectName
   
        If MyObject.ObjectName = "AcDbText" Then
            DoEvents
            Found = False
   
            If Left(MyObject.textString, 13) = ListBox1.List(i, 0) Then
     ' 如果 Text 属性值等于设定值则。
                Found = True    ' 将变量 Found 的值设成 True。
                MyObject.GetBoundingBox , MinPoint
                magnification = 2500

                ThisDrawing.Application.ZoomCenter pt, magnification

                ThisDrawing.SendCommand "_-bhatch" & vbCr & "p" & vbCr & "solid" & vbCr & MinPoint(0) & "," & MinPoint(1)&   vbCr & vbCr               
               
                Do
                If COMMANDNAME = "" Then
                Sleep 50
                'MsgBox "正在填充" & ListBox1.List(i, 0) & "!"
                Exit Do
                End If
                Loop

                ZoomExtents

                Exit For
       
            Else
                Found = False
            End If
        End If
    UserForm3.Label1.Caption = ListBox1.List(i, 0) & vbCrLf & "完成" & Round((i + 1) / ListBox1.ListCount, 2) * 100 & "%"
    Next
   
                colo = 70 + (i Mod 4) * 10
                ThisDrawing.SendCommand "_-color" & vbCr & colo & vbCr
                Do
                    If COMMANDNAME = "" Then
                        Sleep 50
                        Exit Do
                    End If
                Loop
   
  If Found = False Then
    Dim xbtext As String
    xbtext = TextBox1.text
    TextBox1.text = xbtext & ListBox1.List(i, 0) & vbCrLf
  End If

DoEvents
Next

errHandle:
    If Err.Number = -2145386493 Then
        MsgBox "填充定义边界未闭合!", vbCritical
    End If
    Err.Clear
   
  UserForm3.Hide
  UserForm1.Show
 

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

本版积分规则

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

GMT+8, 2025-5-1 19:04 , Processed in 0.170813 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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