明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2466|回复: 6

如何在vba中实现象autocad界面上的点选生成阴影呢

[复制链接]
发表于 2003-12-29 00:11:00 | 显示全部楼层 |阅读模式
各位,我有一个问题请教,如和在vba中实现如同AutoCAD界面中的那样点选一个区域从而在该区域生成阴影,譬如一个圆,过圆心有两条相互垂直的线,现在我希望在1,3象限生成阴影,该如何呢
发表于 2003-12-29 10:02:00 | 显示全部楼层
这个行不行?
  1. Sub test()
  2. Dim entry As AcadEntity
  3. Dim pickedp As Variant


  4. Dim cirentry As AcadCircle

  5.     Dim center(0 To 2) As Double
  6.     Dim radius As Double
  7.     Dim startAngle As Double
  8.     Dim endAngle As Double
  9.     Dim outerLoop(0 To 3) As AcadEntity

  10. On Error Resume Next
  11. ThisDrawing.Utility.GetEntity entry, pickedp, "选择一个圆:"
  12. If Err Then
  13. Err.Clear
  14. End
  15. End If
  16. 'MsgBox entry.ObjectName
  17. If entry.ObjectName = "AcDbCircle" Then

  18. Set cirentry = entry

  19. pickedp = cirentry.center

  20.     center(0) = pickedp(0): center(1) = pickedp(1): center(2) = pickedp(2)
  21.     radius = cirentry.radius
  22.     startAngle = 0
  23.     endAngle = 3.141592 / 2
  24.     Set outerLoop(0) = ThisDrawing.ModelSpace.AddArc(center, radius, startAngle, endAngle)
  25.     startAngle = 3.141592
  26.     endAngle = 3.141592 * 1.5
  27.     Set outerLoop(1) = ThisDrawing.ModelSpace.AddArc(center, radius, startAngle, endAngle)
  28.    
  29.     Set outerLoop(2) = ThisDrawing.ModelSpace.AddLine(outerLoop(0).StartPoint, outerLoop(1).StartPoint)
  30.     Set outerLoop(3) = ThisDrawing.ModelSpace.AddLine(outerLoop(1).EndPoint, outerLoop(0).EndPoint)
  31.    
  32.    
  33.      Dim hatchObj As AcadHatch
  34.     Dim patternName As String
  35.     Dim PatternType As Long
  36.     Dim bAssociativity As Boolean
  37.    
  38.     ' Define the hatch
  39.     patternName = "ANSI31"
  40.     PatternType = 0
  41.     bAssociativity = True
  42.    
  43.     ' Create the associative Hatch object

  44.     Set hatchObj = ThisDrawing.ModelSpace.AddHatch(PatternType, patternName, bAssociativity)
  45.     hatchObj.AppendOuterLoop (outerLoop)
  46.     hatchObj.PatternScale = 0.1
  47.     hatchObj.Evaluate
  48.    
  49.    
  50.     outerLoop(0).Delete
  51.     outerLoop(1).Delete
  52.     outerLoop(2).Delete
  53.     outerLoop(3).Delete
  54. End If
  55. End Sub
发表于 2003-12-29 10:41:00 | 显示全部楼层
完善一下,可以批量处理:
  1. Const PI = 3.1415926
  2. Sub addhatch_1_3forcircles() '批量处理
  3. Dim ss As AcadSelectionSet
  4. Dim cirentry As AcadCircle
  5. Dim patternName As String '填充图案名
  6. Dim PatternScale As Double '填充图案比例
  7. patternName = "ANSI31"
  8. PatternScale = 0.02

  9. On Error Resume Next
  10. Set ss = ThisDrawing.SelectionSets("sscircle")
  11. If Err Then
  12. Err.Clear
  13. Set ss = ThisDrawing.SelectionSets.Add("sscircle")
  14. End If
  15. ss.Clear

  16. Dim filtype(0) As Integer
  17. Dim fildata(0) As Variant
  18. filtype(0) = 0
  19. fildata(0) = "Circle"
  20. ss.SelectOnScreen filtype, fildata


  21. If ss.Count <> 0 Then
  22. Dim i As Integer

  23. For i = 0 To ss.Count - 1 Step 1
  24. If ss.Item(i).ObjectName = "AcDbCircle" Then
  25. Set cirentry = ss.Item(i)
  26. addhatch_1_3_of_circle cirentry, patternName, PatternScale
  27. End If
  28. Next

  29. End If

  30. End Sub


  31. Function addhatch_1_3_of_circle(cirentry As AcadCircle, patternName As String, PatternScale As Double) '单个处理


  32.     Dim pickedp As Variant
  33.    

  34.     Dim center(0 To 2) As Double
  35.     Dim radius As Double
  36.     Dim startAngle As Double
  37.     Dim endAngle As Double
  38.     Dim outerLoop(0 To 3) As AcadEntity

  39. On Error Resume Next

  40.     pickedp = cirentry.center

  41.     center(0) = pickedp(0): center(1) = pickedp(1): center(2) = pickedp(2)
  42.     radius = cirentry.radius
  43.     startAngle = 0
  44.     endAngle = PI / 2
  45.     Set outerLoop(0) = ThisDrawing.ModelSpace.AddArc(center, radius, startAngle, endAngle) '添加1象限弧
  46.     startAngle = PI
  47.     endAngle = PI * 1.5
  48.     Set outerLoop(1) = ThisDrawing.ModelSpace.AddArc(center, radius, startAngle, endAngle) '添加3象限弧
  49.    
  50.     Set outerLoop(2) = ThisDrawing.ModelSpace.AddLine(outerLoop(0).StartPoint, outerLoop(1).StartPoint) '添加直线
  51.     Set outerLoop(3) = ThisDrawing.ModelSpace.AddLine(outerLoop(1).EndPoint, outerLoop(0).EndPoint) '添加直线
  52.    
  53.    
  54.      Dim hatchObj As AcadHatch
  55.    
  56.     Dim PatternType As Long
  57.     Dim bAssociativity As Boolean
  58.    
  59.     ' 定义填充
  60.    
  61.     PatternType = 0
  62.     bAssociativity = True
  63.    
  64.     ' 创建填充

  65.     Set hatchObj = ThisDrawing.ModelSpace.AddHatch(PatternType, patternName, bAssociativity)
  66.     hatchObj.AppendOuterLoop (outerLoop)
  67.     hatchObj.PatternScale = PatternScale
  68.     hatchObj.Color = 3
  69.     hatchObj.Evaluate
  70.    
  71.    
  72.   '  outerLoop(0).Delete '删除1象限弧
  73.   '  outerLoop(1).Delete '删除3象限弧
  74.   '  outerLoop(2).Delete '删除直线
  75.   '  outerLoop(3).Delete '删除直线

  76. End Function
发表于 2003-12-29 11:01:00 | 显示全部楼层
疑问?为什么填充图案名patternName = "SOLID"的时候结果显示会不同的呢?!请高手解答
发表于 2003-12-29 12:49:00 | 显示全部楼层
显示什么结果?贴张图看一下。
发表于 2003-12-29 13:09:00 | 显示全部楼层
看看图像:

本帖子中包含更多资源

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

x
 楼主| 发表于 2003-12-29 18:08:00 | 显示全部楼层
谢谢二位大侠,很好。
我还有一个问题,就是在vba中加入扩展数据的问题,似乎有setxdata和dictionary两中方法,但是我发现,在autocad vba中,所有的对象均有setxdata方法,那么我想问一下,dictionary对象的setxdata方法有和作用。此外,dictionaries集合一开始就有5个预定义的词典,而且用lisp可以看到,但是在使用item方法时,这几个预定义的词典对象竟然不支持name属性,why?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-28 10:40 , Processed in 0.172588 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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