明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1756|回复: 2

[原创]功能函数返回数组变量数组--执行pline

[复制链接]
发表于 2008-3-17 10:01:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2008-3-17 11:49:48 编辑


此图形是HG20595的一个剖面图形。解决方法是用ADO+数组+poyline方法安成。
程序如下:
  1. Option Explicit
  2. Const Pi = 3.14159265358979
  3. Dim adoCon As New ADODB.Connection
  4. ' 功能:打开指定的数据库(在frmConnectDB中指定)
  5. ' 输入:无
  6. ' 调用:无
  7. ' 返回:如果完成连接,返回True;否则返回False
  8. ' 示例:
  9. '       OpenDB
  10. Public Function OpenDB(InputDataBaseName) As Boolean
  11.     OpenDB = True
  12.    
  13.     ' 如果数据库已打开,不执行任何操作
  14.     If adoCon.State <> 0 Then Exit Function
  15.    
  16.     adoCon.CursorLocation = adUseClient
  17.    
  18.     ' 获得数据库文件的位置
  19.     Dim strDbName As String
  20.     Dim strProject As String
  21.     strProject = Left(ThisDrawing.Application.VBE.activevbProject.FileName, _
  22.                     Len(ThisDrawing.Application.VBE.activevbProject.FileName) - 19)
  23.     strDbName = strProject & "\mdb" & InputDataBaseName & ".mdb"
  24.     adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
  25.         strDbName & ";"
  26. End Function
  27. ' 功能:关闭指定的数据库(在frmConnectDB中指定)
  28. ' 输入:无
  29. ' 调用:无
  30. ' 返回:如果数据库处于打开状态,就关闭它
  31. ' 示例:
  32. '       CloseDB
  33. Public Function CloseDB()
  34.     If adoCon.State <> 0 Then
  35.         adoCon.Close
  36.     End If
  37. End Function
  38. Function HG20595T_Data_Preparation() As Double()
  39.   Dim d1, f2, x, w, c, n1, n, h, k, h1, d, l, PipeOutDiameter, PipeDelta, a1, rr, i, ScheduleWall, SeriesNo
  40.   Dim HG20595(12, 3) As Double
  41. '
  42.   Dim startpoint(0 To 2) As Double
  43.   Dim endpoint(0 To 2) As Double
  44. '
  45. '面域
  46.   Dim curves(0 To 12) As AcadEntity
  47.   Dim regionObj As Variant
  48. '旋转实体
  49.   Dim axisPt(0 To 2) As Double
  50.   Dim axisDir(0 To 2) As Double
  51.   Dim angle As Double
  52. '开孔
  53.   Dim cylinderObj As Acad3DSolid
  54.   Dim radius As Double
  55.   Dim center(0 To 2) As Double
  56.   Dim height As Double
  57.   height = 500
  58.   axisPt(0) = 0: axisPt(1) = 0: axisPt(2) = 0
  59.   axisDir(0) = 0: axisDir(1) = 0: axisDir(2) = 1
  60.   angle = Pi * 2 + 0.2
  61.   Dim solidObj As Acad3DSolid
  62.   Dim Sep_N, Pn As String, Dn As String, SearchCondition
  63.   Pn = 6.3: Dn = 350
  64.   Select Case Pn
  65.     Case 1#, 10#, 16#, 25#
  66.       SearchCondition = Dn & "-" & Trim(Str(Pn) + ".0")
  67.     Case Else
  68.       SearchCondition = Dn & "-" & Trim(Str(Pn))
  69.   End Select
  70.   
  71. '
  72.   ''
  73.   OpenDB ("HG20592")
  74.   '
  75.   Dim rst As New ADODB.Recordset
  76.   Dim Sql As String, ii As Integer
  77.   Sql = "select c.*,a.*,b.* from 带颈对焊法兰  as A,凹凸榫槽密封面 as b ,法兰规格 as c Where " & _
  78.       " c.法兰规格 = '" & SearchCondition & "' and c.法兰规格 = a.法兰规格 and c.法兰规格 = b.法兰规格"
  79.   rst.Open Sql, adoCon, adOpenDynamic, adLockOptimistic
  80.   ''
  81.    
  82.     ScheduleWall = 12: SeriesNo = "B"
  83. '    d1 = rst.Fields("突台外径d"):
  84.     f2 = rst.Fields("台高f2"):
  85.     x = rst.Fields("凸面外径X"):
  86.     w = rst.Fields("榫面内径W")
  87.    
  88.    
  89. '''
  90.     c = rst.Fields("WN法兰厚度C")  '
  91.     Select Case SeriesNo
  92.       Case "A"
  93.         n1 = rst.Fields("WN法兰颈径NA")
  94.         PipeOutDiameter = rst.Fields("钢管外径A")
  95.       Case "B"
  96.         n1 = rst.Fields("WN法兰颈径NB")
  97.         PipeOutDiameter = rst.Fields("钢管外径B")
  98.     End Select
  99.     n = rst.Fields("螺栓数量") 'xxlSheet.cells(ii, 16).Value
  100.     h = rst.Fields("WN法兰高度H")  'xxlSheet.cells(ii, 10).Value
  101.     k = rst.Fields("螺栓孔中心圆直径")  'xxlSheet.cells(ii, 13).Value
  102.     h1 = rst.Fields("WN焊端长度h")
  103.     d = rst.Fields("法兰外径D")  'xxlSheet.cells(ii, 12).Value
  104.     l = rst.Fields("螺栓孔直径")  'xxlSheet.cells(ii, 14).Value
  105.    
  106.     PipeDelta = ScheduleWall
  107.     a1 = PipeOutDiameter
  108.     rr = rst.Fields("WN圆角半径R")
  109.   CloseDB
  110. ' HG20595法兰实体赋值
  111.     ThisDrawing.SendCommand "_fillet" + Chr(10) + "r" & Chr(10) & rr & Chr(10) & Chr(10)
  112.     HG20595(1, 1) = (PipeOutDiameter - 2 * PipeDelta) / 2: HG20595(1, 2) = -f2: HG20595(1, 3) = 0
  113.     HG20595(2, 1) = w / 2: HG20595(2, 2) = -f2: HG20595(2, 3) = 0
  114.     HG20595(3, 1) = w / 2: HG20595(3, 2) = 0: HG20595(3, 3) = 0
  115.     HG20595(4, 1) = x / 2: HG20595(4, 2) = 0: HG20595(4, 3) = 0
  116.     HG20595(5, 1) = x / 2: HG20595(5, 2) = -f2: HG20595(5, 3) = 0
  117.     HG20595(6, 1) = d / 2: HG20595(6, 2) = -f2: HG20595(6, 3) = 0
  118.     HG20595(7, 1) = d / 2: HG20595(7, 2) = -c: HG20595(7, 3) = 0
  119.     HG20595(8, 1) = n1 / 2: HG20595(8, 2) = -c: HG20595(8, 3) = 0
  120.     HG20595(9, 1) = a1 / 2: HG20595(9, 2) = h1 - h: HG20595(9, 3) = 0
  121.     HG20595(10, 1) = a1 / 2: HG20595(10, 2) = -h: HG20595(10, 3) = 0
  122.     HG20595(11, 1) = (PipeOutDiameter - 2 * PipeDelta) / 2: HG20595(11, 2) = -h: HG20595(11, 3) = 0
  123.     HG20595(12, 1) = (PipeOutDiameter - 2 * PipeDelta) / 2: HG20595(12, 2) = -f2: HG20595(12, 3) = 0
  124.     HG20595T_Data_Preparation = HG20595
  125. End Function
  126. Sub lss()
  127.   Dim AA() As Double, bb() As Double, ii, jj, nn
  128.   Debug.Print TypeName(HG20595T_Data_Preparation)
  129.   
  130.   AA = HG20595T_Data_Preparation
  131.   ReDim bb(UBound(AA) * 3 - 1) As Double
  132.   Debug.Print UBound(bb)
  133.   
  134.   For ii = 1 To UBound(AA)
  135.     For jj = 1 To 3
  136.       bb(nn) = AA(ii, jj)
  137.       nn = nn + 1
  138.     Next jj
  139.   Next ii
  140.   Dim ppl As AcadPolyline
  141.   Set ppl = ThisDrawing.ModelSpace.AddPolyline(bb)
  142. End Sub
type ---- type end方法
  1. Option Explicit
  2. Const Pi = 3.14159265358979
  3. Dim adoCon As New ADODB.Connection
  4. Private Type HG20595InitialData
  5.   HG20595 As Variant '(12, 3) As Double
  6.   n As Double
  7.   k As Double
  8.   l As Double
  9. End Type
  10. ' 功能:打开指定的数据库(在frmConnectDB中指定)
  11. ' 输入:无
  12. ' 调用:无
  13. ' 返回:如果完成连接,返回True;否则返回False
  14. ' 示例:
  15. '       OpenDB
  16. Public Function OpenDB(InputDataBaseName) As Boolean
  17.     OpenDB = True
  18.    
  19.     ' 如果数据库已打开,不执行任何操作
  20.     If adoCon.State <> 0 Then Exit Function
  21.    
  22.     adoCon.CursorLocation = adUseClient
  23.    
  24.     ' 获得数据库文件的位置
  25.     Dim strDbName As String
  26.     Dim strProject As String
  27.     strProject = Left(ThisDrawing.Application.VBE.activevbProject.FileName, _
  28.                     Len(ThisDrawing.Application.VBE.activevbProject.FileName) - 19)
  29.     strDbName = strProject & "\mdb" & InputDataBaseName & ".mdb"
  30.     adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
  31.         strDbName & ";"
  32. End Function
  33. ' 功能:关闭指定的数据库(在frmConnectDB中指定)
  34. ' 输入:无
  35. ' 调用:无
  36. ' 返回:如果数据库处于打开状态,就关闭它
  37. ' 示例:
  38. '       CloseDB
  39. Public Function CloseDB()
  40.     If adoCon.State <> 0 Then
  41.         adoCon.Close
  42.     End If
  43. End Function
  44. Function HG20595T_Data_Preparation() As HG20595InitialData
  45.   Dim d1, f2, x, w, c, n1, n, h, k, h1, d, l, PipeOutDiameter, PipeDelta, a1, rr, i, ScheduleWall, SeriesNo
  46. '
  47.   ''
  48.   OpenDB ("HG20592")
  49.   '
  50.   Dim rst As New ADODB.Recordset
  51.   Dim Sql As String, ii As Integer
  52.   
  53. ''
  54.   Dim Sep_N, Pn As String, Dn As String, SearchCondition
  55.   Pn = 6.3: Dn = 350
  56.   Select Case Pn
  57.     Case 1#, 10#, 16#, 25#
  58.       SearchCondition = Dn & "-" & Trim(Str(Pn) + ".0")
  59.     Case Else
  60.       SearchCondition = Dn & "-" & Trim(Str(Pn))
  61.   End Select
  62. ''
  63.   Sql = "select c.*,a.*,b.* from 带颈对焊法兰  as A,凹凸榫槽密封面 as b ,法兰规格 as c Where " & _
  64.       " c.法兰规格 = '" & SearchCondition & "' and c.法兰规格 = a.法兰规格 and c.法兰规格 = b.法兰规格"
  65.   rst.Open Sql, adoCon, adOpenDynamic, adLockOptimistic
  66.   ''
  67.    
  68.     ScheduleWall = 12: SeriesNo = "B"
  69. '    d1 = rst.Fields("突台外径d"):
  70.     f2 = rst.Fields("台高f2"):
  71.     x = rst.Fields("凸面外径X"):
  72.     w = rst.Fields("榫面内径W")
  73.    
  74.    
  75. '''
  76.     c = rst.Fields("WN法兰厚度C")  '
  77.     Select Case SeriesNo
  78.       Case "A"
  79.         n1 = rst.Fields("WN法兰颈径NA")
  80.         PipeOutDiameter = rst.Fields("钢管外径A")
  81.       Case "B"
  82.         n1 = rst.Fields("WN法兰颈径NB")
  83.         PipeOutDiameter = rst.Fields("钢管外径B")
  84.     End Select
  85.     HG20595T_Data_Preparation.n = rst.Fields("螺栓数量") 'xxlSheet.cells(ii, 16).Value
  86.     h = rst.Fields("WN法兰高度H")  'xxlSheet.cells(ii, 10).Value
  87.     HG20595T_Data_Preparation.k = rst.Fields("螺栓孔中心圆直径")  'xxlSheet.cells(ii, 13).Value
  88.    
  89.    
  90.     h1 = rst.Fields("WN焊端长度h")
  91.     d = rst.Fields("法兰外径D")  'xxlSheet.cells(ii, 12).Value
  92.     HG20595T_Data_Preparation.l = rst.Fields("螺栓孔直径")  'xxlSheet.cells(ii, 14).Value
  93.    
  94.     PipeDelta = ScheduleWall
  95.     a1 = PipeOutDiameter
  96.     rr = rst.Fields("WN圆角半径R")
  97.   CloseDB
  98. ' HG20595法兰实体赋值
  99.     Dim HG20595(12, 3) As Double
  100.     ThisDrawing.SendCommand "_fillet" + Chr(10) + "r" & Chr(10) & rr & Chr(10) & Chr(10)
  101.     HG20595(1, 1) = (PipeOutDiameter - 2 * PipeDelta) / 2: HG20595(1, 2) = -f2: HG20595(1, 3) = 0
  102.     HG20595(2, 1) = w / 2: HG20595(2, 2) = -f2: HG20595(2, 3) = 0
  103.     HG20595(3, 1) = w / 2: HG20595(3, 2) = 0: HG20595(3, 3) = 0
  104.     HG20595(4, 1) = x / 2: HG20595(4, 2) = 0: HG20595(4, 3) = 0
  105.     HG20595(5, 1) = x / 2: HG20595(5, 2) = -f2: HG20595(5, 3) = 0
  106.     HG20595(6, 1) = d / 2: HG20595(6, 2) = -f2: HG20595(6, 3) = 0
  107.     HG20595(7, 1) = d / 2: HG20595(7, 2) = -c: HG20595(7, 3) = 0
  108.     HG20595(8, 1) = n1 / 2: HG20595(8, 2) = -c: HG20595(8, 3) = 0
  109.     HG20595(9, 1) = a1 / 2: HG20595(9, 2) = h1 - h: HG20595(9, 3) = 0
  110.     HG20595(10, 1) = a1 / 2: HG20595(10, 2) = -h: HG20595(10, 3) = 0
  111.     HG20595(11, 1) = (PipeOutDiameter - 2 * PipeDelta) / 2: HG20595(11, 2) = -h: HG20595(11, 3) = 0
  112.     HG20595(12, 1) = (PipeOutDiameter - 2 * PipeDelta) / 2: HG20595(12, 2) = -f2: HG20595(12, 3) = 0
  113.     HG20595T_Data_Preparation.HG20595 = HG20595
  114. End Function
  115. Sub lss()
  116.   Dim aa() As Double, bb() As Double, ii, jj, nn
  117.   Dim ee As AcadEntity
  118.   Set ee = DrawingEntityForHG20595T(HG20595T_Data_Preparation.HG20595 _
  119.      , HG20595T_Data_Preparation.l, HG20595T_Data_Preparation.n, HG20595T_Data_Preparation.k)
  120. End Sub
  121. Function DrawingEntityForHG20595T(HG20595 As Variant, l, n, k) As AcadEntity
  122.   'Dim HG20595(12, 3) As Double
  123.   'Dim HG20595
  124.   'HG20595 = HG20595InputData
  125. '
  126.   Dim startpoint(0 To 2) As Double
  127.   Dim endpoint(0 To 2) As Double
  128. '
  129. '面域
  130.   Dim curves(0 To 12) As AcadEntity
  131.   Dim regionObj As Variant
  132. '旋转实体
  133.   Dim axisPt(0 To 2) As Double
  134.   Dim axisDir(0 To 2) As Double
  135.   Dim angle As Double
  136. '开孔
  137.   Dim cylinderObj As Acad3DSolid
  138.   Dim radius As Double
  139.   Dim center(0 To 2) As Double
  140.   Dim height As Double
  141.   height = 500
  142.   axisPt(0) = 0: axisPt(1) = 0: axisPt(2) = 0
  143.   axisDir(0) = 0: axisDir(1) = 0: axisDir(2) = 1
  144.   angle = Pi * 2 + 0.2
  145.   Dim solidObj As Acad3DSolid
  146.    Dim i
  147.    For i = 1 To 11
  148.       startpoint(0) = HG20595(i, 0): startpoint(1) = HG20595(i, 1): startpoint(2) = HG20595(i, 2)
  149.     If i <= 12 Then
  150.          endpoint(0) = HG20595(i + 1, 0): endpoint(1) = HG20595(i + 1, 1): endpoint(2) = HG20595(i + 1, 2)
  151.     End If
  152.     Set curves(i - 1) = ThisDrawing.ModelSpace.AddLine(startpoint, endpoint)
  153.     Next i
  154.     Dim str_handle1, str_handle2
  155.     str_handle1 = curves(6).Handle
  156.     str_handle2 = curves(7).Handle
  157.     ThisDrawing.SendCommand "_fillet" + Chr(10) + "(Handent" & Chr(34) & str_handle1 & Chr(34) & ")" & Chr(10) & "(Handent" & Chr(34) & str_handle2 & Chr(34) & ")" & Chr(10)
  158.     Set curves(11) = ThisDrawing.ModelSpace.Item(ThisDrawing.ModelSpace.Count - 1)
  159.     str_handle1 = curves(7).Handle
  160.     str_handle2 = curves(8).Handle
  161.     ThisDrawing.SendCommand "_fillet" + Chr(10) + "(Handent" & Chr(34) & str_handle1 & Chr(34) & ")" & Chr(10) & "(Handent" & Chr(34) & str_handle2 & Chr(34) & ")" & Chr(10)
  162.     Set curves(12) = ThisDrawing.ModelSpace.Item(ThisDrawing.ModelSpace.Count - 1)
  163. '面域绘制
  164.     regionObj = ThisDrawing.ModelSpace.AddRegion(curves)
  165.     For i = 0 To 12
  166.      curves(i).Delete
  167.     Next i
  168. '实体旋转
  169.          
  170.     Set solidObj = ThisDrawing.ModelSpace.AddRevolvedSolid(regionObj(0), axisPt, axisDir, angle)
  171.     regionObj(0).Delete
  172. '开孔
  173.         
  174.     Dim Alfa
  175.         
  176.     radius = l / 2
  177.    
  178.     Alfa = Pi / n
  179.     For i = 1 To n
  180.       center(0) = k / 2 * Cos(Alfa): center(1) = k / 2 * Sin(Alfa): center(2) = 0
  181.     Set cylinderObj = ThisDrawing.ModelSpace.AddCylinder(center, radius, height)
  182. '剪切
  183.     solidObj.Boolean 2, cylinderObj
  184.     Alfa = Alfa + 2 * Pi / n
  185.     Next i
  186. ''''
  187. End Function
运行结果如图所示,三维HG20595-T型法兰

本帖子中包含更多资源

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

x
发表于 2008-3-18 14:26:00 | 显示全部楼层

谢谢!努力学习中

发表于 2024-5-19 06:24:28 | 显示全部楼层
谢谢,非常好,学习一下!让我们共同努力,共创论坛美好未来!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-25 02:22 , Processed in 0.166873 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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