乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 84|回复: 2

[编程交流] Excel VBA:添加工字钢截面

[复制链接]

7

主题

30

帖子

36

银币

初来乍到

Rank: 1

铜币
22
发表于 2022-7-6 19:35:07 | 显示全部楼层 |阅读模式
基于已知尺寸绘制工字钢截面图的Excel函数
重点是与表值匹配的截面积。
 
请注意,基于我的知识和使用,excel中的I-Beam数据收集,
我对任何错误或遗漏信息概不负责
 
Phh公司
 
 
 
203514mwjl02vfslywjmwj.png
 
  1. Option Explicit
  2. Sub AddIbeamToDwg()
  3.     On Error Resume Next
  4.    
  5.     'Connect to AutoCad application
  6.     Dim acadApp As AcadApplication
  7.     Set acadApp = GetObject(, "AutoCad.Application")
  8.     If Err <> 0 Then
  9.         Err.Clear
  10.         MsgBox "Open the AutoCad application first and then execute!"
  11.         Exit Sub
  12.     End If
  13.    
  14.     'Connect to AutoCad drawing document
  15.     Dim acadDoc As AcadDocument
  16.     Set acadDoc = acadApp.ActiveDocument
  17.    
  18.     'Call excel to get data
  19.     Dim excel As Object
  20.     Set excel = GetObject(, "Excel.Application")
  21.     Dim excelSheet As Object
  22.     Set excelSheet = excel.ActiveWorkbook.Sheets(ActiveSheet.Name)
  23.    
  24.     'Setup i-beam variables
  25.     Dim ibStr As String
  26.     Dim insertionPoint(0 To 2) As Double
  27.     Dim height As Double
  28.     Dim ibDepth As Double
  29.     Dim ibWidth As Double
  30.     Dim ibtw As Double
  31.     Dim ibtf As Double
  32.     Dim ibSc As Double
  33.    
  34.     Dim dblPi As Double
  35.     dblPi = WorksheetFunction.Pi()
  36.    
  37.     Dim rw As Integer
  38.     rw = ActiveCell.Row
  39.    
  40.     'Check if row & 1st cell empty then stop
  41.     If ActiveCell.Value = 0 Or excelSheet.Cells(rw, 1) = 0 Then
  42.         MsgBox "No I-Beam data selected, please select row that contains data!"
  43.         Exit Sub
  44.         End If
  45.         
  46.     If excelSheet.Cells(rw, 1) Then
  47.     ibStr = excelSheet.Cells(rw, 2)
  48.     ibDepth = excelSheet.Cells(rw, 3)
  49.     ibWidth = excelSheet.Cells(rw, 4)
  50.     ibtw = excelSheet.Cells(rw, 5)
  51.     ibtf = excelSheet.Cells(rw, 6)
  52.     ibSc = excelSheet.Cells(rw, 7)
  53.     End If
  54.    
  55.       Dim ibeamName As AcadText
  56.       Dim plineObj As AcadLWPolyline
  57.       Dim plineObj1 As AcadLWPolyline
  58.       Dim ibRad As Double
  59.       Dim ibRad1 As Double
  60.       ibRad = 0
  61.       
  62.       Dim points(0 To 37) As Double
  63.       'Create a temporary lwPolyline for calculating the area
  64.       '4 corner area ratio = 3.65979236632549
  65.       points(0) = 0: points(1) = ibDepth
  66.       points(2) = (ibWidth / 2): points(3) = ibDepth
  67.       points(4) = (ibWidth / 2): points(5) = (ibDepth - ibtf)
  68.       points(6) = (ibtw / 2 + ibRad): points(7) = (ibDepth - ibtf)
  69.       points(8) = (ibtw / 2): points(9) = (ibDepth - (ibtf + ibRad))
  70.       points(10) = (ibtw / 2): points(11) = (ibtf + ibRad)
  71.       points(12) = (ibtw / 2 + ibRad): points(13) = ibtf
  72.       points(14) = (ibWidth / 2): points(15) = ibtf
  73.       points(16) = (ibWidth / 2): points(17) = 0
  74.       points(18) = 0: points(19) = 0
  75.       points(20) = (ibWidth / 2) * (-1): points(21) = 0
  76.       points(22) = (ibWidth / 2) * (-1): points(23) = ibtf
  77.       points(24) = (ibtw / 2 + ibRad) * (-1): points(25) = ibtf
  78.       points(26) = (ibtw / 2) * (-1): points(27) = (ibtf + ibRad)
  79.       points(28) = (ibtw / 2) * (-1): points(29) = (ibDepth - (ibtf + ibRad))
  80.       points(30) = ((ibtw / 2) + ibRad) * (-1): points(31) = (ibDepth - ibtf)
  81.       points(32) = (ibWidth / 2) * (-1): points(33) = (ibDepth - ibtf)
  82.       points(34) = (ibWidth / 2) * (-1): points(35) = ibDepth
  83.       points(36) = 0: points(37) = ibDepth
  84.       Set plineObj = acadDoc.ModelSpace.AddLightWeightPolyline(points)
  85.       plineObj.Closed = True
  86.       'ibArea = plineObj.Area
  87.       ibRad1 = VBA.Sqr(((ibSc - plineObj.Area) * 3.65979236632549) / dblPi)
  88.       plineObj.Delete
  89.             
  90.       Dim vertices(0 To 37) As Double
  91.       
  92.       'I-beam drawn after calculate radius base on lwPolyline above
  93.       vertices(0) = 0: vertices(1) = ibDepth
  94.       vertices(2) = (ibWidth / 2): vertices(3) = ibDepth
  95.       vertices(4) = (ibWidth / 2): vertices(5) = (ibDepth - ibtf)
  96.       vertices(6) = (ibtw / 2 + ibRad1): vertices(7) = (ibDepth - ibtf)
  97.       vertices(8) = (ibtw / 2): vertices(9) = (ibDepth - (ibtf + ibRad1))
  98.       vertices(10) = (ibtw / 2): vertices(11) = (ibtf + ibRad1)
  99.       vertices(12) = (ibtw / 2 + ibRad1): vertices(13) = ibtf
  100.       vertices(14) = (ibWidth / 2): vertices(15) = ibtf
  101.       vertices(16) = (ibWidth / 2): vertices(17) = 0
  102.       vertices(18) = 0: vertices(19) = 0
  103.       vertices(20) = (ibWidth / 2) * (-1): vertices(21) = 0
  104.       vertices(22) = (ibWidth / 2) * (-1): vertices(23) = ibtf
  105.       vertices(24) = (ibtw / 2 + ibRad1) * (-1): vertices(25) = ibtf
  106.       vertices(26) = (ibtw / 2) * (-1): vertices(27) = (ibtf + ibRad1)
  107.       vertices(28) = (ibtw / 2) * (-1): vertices(29) = (ibDepth - (ibtf + ibRad1))
  108.       vertices(30) = ((ibtw / 2) + ibRad1) * (-1): vertices(31) = (ibDepth - ibtf)
  109.       vertices(32) = (ibWidth / 2) * (-1): vertices(33) = (ibDepth - ibtf)
  110.       vertices(34) = (ibWidth / 2) * (-1): vertices(35) = ibDepth
  111.       vertices(36) = 0: vertices(37) = ibDepth
  112.       
  113.       'Create a light weight Polyline object and draw in AutoCAD application
  114.       Set plineObj1 = acadDoc.ModelSpace.AddLightWeightPolyline(vertices)
  115.       plineObj1.Closed = True
  116.       
  117.       'Add a bulge to segment 3
  118.       plineObj1.SetBulge 3, Tan(dblPi / 8)
  119.       plineObj1.SetBulge 5, Tan(dblPi / 8)
  120.       plineObj1.SetBulge 12, Tan(dblPi / 8)
  121.       plineObj1.SetBulge 14, Tan(dblPi / 8)
  122.       
  123.       insertionPoint(0) = 0: insertionPoint(1) = -2: insertionPoint(2) = 0
  124.       height = 0.5
  125.       
  126.       Set ibeamName = acadDoc.ModelSpace.AddText(ibStr, insertionPoint, height)
  127.       ibeamName.Alignment = acAlignmentCenter
  128.       
  129.       ibeamName.Update
  130.       plineObj1.Update
  131.    
  132. End Sub
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 20:09:53 | 显示全部楼层
对于我来说,看看WiseySteelShapes吧
 
回复

使用道具 举报

7

主题

30

帖子

36

银币

初来乍到

Rank: 1

铜币
22
发表于 2022-7-6 20:58:11 | 显示全部楼层
谢谢你的信息,
 
欢呼
 
Phh公司
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 14:33 , Processed in 0.457797 second(s), 61 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表