明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4402|回复: 4

大家一起完善一个粗糙度标注的程序

[复制链接]
发表于 2003-11-18 22:13:00 | 显示全部楼层 |阅读模式
刚写完了一个函数,在想是用属性块用呢还是用匿名块好呢。
因为写的时候用了匿名块,写完了了现没有必须用匿名块,好象属性块好,建立四个图块(也用程序生成),在生成时先查看是否有该名称图块存在,如果存在则直接引用,如果不存在则引用新建块函数新建一个相应的属性块。

现在把已经写好的部分贴出来。是用匿名块方法,而且也只写了一部分,还没有写到交互操作部分。
  1. Sub AddCCD()
  2. Dim pnt As Variant
  3. pnt = ThisDrawing.Utility.GetPoint(, "插入点:")
  4. CreateCCD 0, pnt, Radians(150), 3.5, 1
  5. End Sub

  6. ' 粗糙度符号标注函数
  7. ' Mode为粗糙度模式,0代表表面未加工,1代表表面加工
  8. ' InsertPoint为插入点位置
  9. ' Angle为插入的角度
  10. ' Value粗糙度值
  11. ' Factor为插入的比例因子
  12. Function CreateCCD(Mode As Integer, InsertPoint As Variant, Angle As Double, Value As String, Factor As Double) As AcadBlockReference
  13.     Dim objBlock As AcadBlock
  14.     Dim InsPnt(2) As Double
  15.     InsPnt(0) = 0: InsPnt(1) = 0: InsPnt(2) = 0
  16.     Set objBlock = ThisDrawing.Blocks.Add(InsPnt, "*U")
  17.     Dim Pnt2 As Variant
  18.     Dim Pnt3 As Variant
  19.     Dim Pnt4 As Variant
  20.     Dim Pnt5 As Variant
  21.     Dim Pnt6 As Variant
  22.     Dim Pnt7 As Variant
  23.     Dim r As Variant
  24.     Pnt2 = ThisDrawing.Utility.PolarPoint(InsPnt, Radians(60), 12)
  25.     Pnt3 = ThisDrawing.Utility.PolarPoint(InsPnt, Radians(120), 6)
  26.     Pnt4 = ThisDrawing.Utility.PolarPoint(Pnt3, 0, 6)
  27.     Pnt5 = ThisDrawing.Utility.PolarPoint(InsPnt, Radians(90), 3 / Cos(Radians(30)))
  28.     Pnt6 = ThisDrawing.Utility.PolarPoint(Pnt4, Radians(90), 0.7)
  29.     Pnt7 = ThisDrawing.Utility.PolarPoint(Pnt4, Radians(90), 4.2)
  30.     r = 3 * Tan(Radians(30))
  31.     Dim objLine As AcadLine
  32.     Dim objCircle As AcadCircle
  33.     Set objLine = objBlock.AddLine(InsPnt, Pnt2)
  34.     objLine.color = acByBlock
  35.     Set objLine = objBlock.AddLine(InsPnt, Pnt3)
  36.     objLine.color = acByBlock
  37.     If Mode = 1 Then
  38.         Set objLine = objBlock.AddLine(Pnt3, Pnt4)
  39.         objLine.color = acByBlock
  40.     ElseIf Mode = 0 Then
  41.         Set objCircle = objBlock.AddCircle(Pnt5, r)
  42.         objCircle.color = acByBlock
  43.     End If
  44.     Dim objText As AcadText
  45.     If Angle > Radians(90) And Angle <= Radians(270) Then
  46.         Set objText = objBlock.AddText(Value, Pnt6, 3.5)
  47.         objText.Alignment = acAlignmentTopLeft
  48.         objText.Rotate InsPnt, Radians(180)
  49.         objText.Move InsPnt, Pnt6
  50.     Else
  51.         Set objText = objBlock.AddText(Value, Pnt6, 3.5)
  52.         objText.Alignment = acAlignmentBottomRight
  53.         objText.Move InsPnt, Pnt6
  54.     End If
  55.     objText.color = acByBlock
  56.     Dim blkname As String
  57.     blkname = objBlock.Name
  58.     Dim objBlockRef As AcadBlockReference
  59.     Set objBlockRef = ThisDrawing.ModelSpace.InsertBlock(InsertPoint, blkname, Factor, Factor, Factor, Angle)
  60.     Set CreateCCD = objBlockRef
  61. End Function

  62. Public Function PI() As Double
  63.   PI = Atn(1) * 4
  64. End Function

  65. Private Function Degrees(Radians As Double) As Double
  66.   Degrees = Radians / PI * 180
  67. End Function

  68. Private Function Radians(Degrees As Double) As Double
  69.   Radians = Degrees / 180 * PI
  70. End Function
发表于 2003-11-19 10:10:00 | 显示全部楼层
属性块应该更好,直接调用也方便
 楼主| 发表于 2003-11-19 11:46:00 | 显示全部楼层
改成了使用属性块,程序中包括了属性块的生成方法和属性块插入后的属性修改

以下为简单的程序调用部分,大家可以继续完善
Sub ic()
On Error Resume Next
Dim pnt As Variant
pnt = ThisDrawing.Utility.GetPoint(, " 选择插入点:")
Dim angle As Double
angle = ThisDrawing.Utility.GetAngle(pnt, " 选择选择角度:")
Dim txt As String
txt = ThisDrawing.Utility.GetString(0, " 请输入粗糙度大小:")
Dim mode As Integer
mode = ThisDrawing.Utility.GetInteger(" 选择粗糙度样式[表面非加工0/表面加工1]<表面非加工>:")
If Err Then
    mode = 0
    Err.Clear
End If
CreateCCD mode, pnt, angle, txt, 1
End Sub

以下为生成粗糙度的函数部分:

  1. ' 粗糙度符号标注函数
  2. ' Mode为粗糙度模式,0代表表面未加工,1代表表面加工
  3. ' InsertPoint为插入点
  4. ' Angle为插入的角度
  5. ' Value粗糙度值
  6. ' Factor为插入的比例因子
  7. Function CreateCCD(mode As Integer, InsertPoint As Variant, angle As Double, Value As String, Factor As Double) As AcadBlockReference
  8.     Dim objBlock As AcadBlock
  9.     Dim InsPnt(2) As Double
  10.     InsPnt(0) = 0: InsPnt(1) = 0: InsPnt(2) = 0
  11.     Dim BlkName As String
  12.     BlkName = "mc_ccd_" & mode
  13.     On Error Resume Next
  14.     Set objBlock = ThisDrawing.Blocks(BlkName)
  15.     If Err Then
  16.         Err.Clear
  17.         Set objBlock = ThisDrawing.Blocks.Add(InsPnt, BlkName)
  18.    
  19.         Dim Pnt2 As Variant
  20.         Dim Pnt3 As Variant
  21.         Dim Pnt4 As Variant
  22.         Dim Pnt5 As Variant
  23.         Dim Pnt6 As Variant
  24.         Dim r As Variant
  25.         Pnt2 = ThisDrawing.Utility.PolarPoint(InsPnt, Radians(60), 12)
  26.         Pnt3 = ThisDrawing.Utility.PolarPoint(InsPnt, Radians(120), 6)
  27.         Pnt4 = ThisDrawing.Utility.PolarPoint(Pnt3, 0, 6)
  28.         Pnt5 = ThisDrawing.Utility.PolarPoint(InsPnt, Radians(90), 3 / Cos(Radians(30)))
  29.         Pnt6 = ThisDrawing.Utility.PolarPoint(Pnt4, Radians(90), 0.7)
  30.         r = 3 * Tan(Radians(30))
  31.         Dim objLine As AcadLine
  32.         Dim objCircle As AcadCircle
  33.         Set objLine = objBlock.AddLine(InsPnt, Pnt2)
  34.         objLine.color = acByBlock
  35.         Set objLine = objBlock.AddLine(InsPnt, Pnt3)
  36.         objLine.color = acByBlock
  37.         If mode = 1 Then
  38.             Set objLine = objBlock.AddLine(Pnt3, Pnt4)
  39.             objLine.color = acByBlock
  40.         ElseIf mode = 0 Then
  41.             Set objCircle = objBlock.AddCircle(Pnt5, r)
  42.             objCircle.color = acByBlock
  43.         End If
  44.         Dim objAtt As AcadAttribute
  45.         Set objAtt = objBlock.AddAttribute(3.5, acAttributeModeNormal, "粗糙度值", Pnt6, "CCD", "")
  46.         objAtt.Alignment = acAlignmentBottomRight
  47.         objAtt.Move InsPnt, Pnt6
  48.         objAtt.color = acByBlock
  49.     End If
  50.     Dim objBlockRef As AcadBlockReference
  51.     Set objBlockRef = ThisDrawing.ModelSpace.InsertBlock(InsertPoint, BlkName, Factor, Factor, Factor, angle)
  52.     Dim objAtts As Variant
  53.     objAtts = objBlockRef.GetAttributes
  54.     Dim objAttRef As AcadAttributeReference
  55.     Set objAttRef = objAtts(0)
  56.     objAttRef.TextString = Value
  57.     If angle > Radians(90) And angle <= Radians(270) Then
  58.         objAttRef.Alignment = acAlignmentTopLeft
  59.         objAttRef.Rotate objAttRef.TextAlignmentPoint, Radians(180)
  60.     End If
  61. End Function

  62. Public Function PI() As Double
  63.   PI = Atn(1) * 4
  64. End Function

  65. Private Function Degrees(Radians As Double) As Double
  66.   Degrees = Radians / PI * 180
  67. End Function

  68. Private Function Radians(Degrees As Double) As Double
  69.   Radians = Degrees / 180 * PI
  70. End Function
发表于 2003-11-23 17:36:00 | 显示全部楼层
呵呵,有些难度,看着累死俺村人了:)
发表于 2009-5-2 10:36:00 | 显示全部楼层

楼主好啊,我还是看的不是很懂

McCAD是什么意思啊?

我做的毕业设计是用VB实现粗糙度的快速标注,你说的这些事不是对我的很有用处啊?

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

本版积分规则

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

GMT+8, 2024-11-25 14:28 , Processed in 0.152413 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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