乐筑天下

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

[编程交流] VBA - Measuring the area of a

[复制链接]

1

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 14:17:43 | 显示全部楼层 |阅读模式
Hello everybody!
 
I use VBA with Excel for some years but I have been asked to create a VBA macro for AutoCAD. I have to draw the shape of a storage pile from the table of the measurement in Excel. Then I have to measure the area of each profile. My macro runs well for the drawing, but I can't read the area of the region I created.
 
I have used the same codes as the help examples (I think ^^) but I have this message: "Object doesn't support this property or method". But when I use the 'Watch' tool, the region have an area... .
 
Here is an extract of the code. The total Excel worksheet is attached:
 
  1. Sub Calculation()      Dim ThisDrawing As AutoCAD.AcadDocument   Set ThisDrawing = AutoCAD.ActiveDocument      Dim Measurement As Variant   Dim NbStep As Integer   Dim NbProfile As Integer   Dim DimStep As Single   Dim DimProfile As Single   Dim Elev As Single   Dim Density As Single   Dim Col As Integer   Dim Row As Integer   Dim SCol As Integer   Dim SRow As Integer   Dim Line As Integer   Dim Value As Single          Dim CommandSent As String      Dim zStart As Single   Dim zEnd As Single      Dim ProfileEntity(0 To 3) As AcadEntity   Dim ProfileRegion As Variant         Line = 1      'save the informations   DimStep = 3   DimProfile = 9.75   Elev = 20.12   Density = 1500      With Application   .Calculation = xlCalculationManual   '---------------------------------------------------------'---(deleted - see the attached file)-----------------------'---------------------------------------------------------      'create the command for AutoCAD. First it creates the top spline and then the lines to close the profile.   'after each profile, the y value is increased to separate the profiles.   For Col = 0 To NbProfile - 1          'creates the spline       Dim Points() As Double       Dim i As Integer       Dim SplineTangent(0 To 2) As Double              ReDim Points(1 To (NbStep + 1) * 3)              SplineTangent(0) = 0: SplineTangent(1) = 0: SplineTangent(2) = 0              zStart = Format(Elev - Worksheets("Measurement Table").Cells(SRow, Col + SCol).Value, "#0.000")              'enters the points for the spline       For Row = 0 To NbStep           zEnd = Format(Elev - Worksheets("Measurement Table").Cells(Row + SRow, Col + SCol).Value, "#0.000")                                  Points((Row * 3) + 1) = Format(Row * DimStep, "#0.000")           Points((Row * 3) + 2) = Format(Col * DimProfile, "#0.000")           Points((Row + 1) * 3) = zEnd                  Next Row              'sends the command       Set ProfileEntity(0) = ThisDrawing.ModelSpace.AddSpline(Points, SplineTangent, SplineTangent)                     'closes the profile       Dim LinePoint0(0 To 2) As Double       Dim LinePoint1(0 To 2) As Double       Dim LinePoint2(0 To 2) As Double       Dim LinePoint3(0 To 2) As Double              LinePoint0(0) = Format(NbStep * DimStep, "#0.000"): LinePoint0(1) = Format(Col * DimProfile, "#0.000"): LinePoint0(2) = zEnd       LinePoint1(0) = Format(NbStep * DimStep, "#0.000"): LinePoint1(1) = Format(Col * DimProfile, "#0.000"): LinePoint1(2) = 0       LinePoint2(0) = 0: LinePoint2(1) = Format(Col * DimProfile, "#0.000"): LinePoint2(2) = 0       LinePoint3(0) = 0: LinePoint3(1) = Format(Col * DimProfile, "#0.000"): LinePoint3(2) = zStart                     'sends the command       Set ProfileEntity(1) = ThisDrawing.ModelSpace.AddLine(LinePoint0, LinePoint1)       Set ProfileEntity(2) = ThisDrawing.ModelSpace.AddLine(LinePoint1, LinePoint2)       Set ProfileEntity(3) = ThisDrawing.ModelSpace.AddLine(LinePoint2, LinePoint3)                             'measures the area       'On Local Error GoTo NoRegion -> will be enabled when the error is corrected       ProfileRegion = ThisDrawing.ModelSpace.AddRegion(ProfileEntity)              ThisDrawing.Regen acAllViewports                      Worksheets("Results").Cells(Col + 2, 2).Value = ThisDrawing.ModelSpace.ProfileRegion.Area '-> Here is the error   Next Col'---------------------------------------------------------'---(deleted - see the attached file)-----------------------'---------------------------------------------------------   'changes the viewpoint   ThisDrawing.SendCommand "vpoint" & vbCr & "1,-1,1" & vbCr      .Calculation = xlCalculationAutomatic   End WithExit SubNoRegion:   Worksheets("Results").Cells(Col + 2, 2).Value = "ERROR"Resume NextEnd Sub
 
The code is not optimized at all: I adapted it from an old procedure, so it can seem quite strange...!
 
If somebody could help me finding a solution... I'm stuck on it for hours!
Thanks.
Drawing sheet.zip
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 14:37:24 | 显示全部楼层
Change line to this:
 
Worksheets("Results").Cells(Col + 2, 2).Value = ProfileRegion(0).Area
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 14:41:57 | 显示全部楼层
Incidentally, some of the cross sections are not converting to Regions due to self-intersecting geometry.
回复

使用道具 举报

1

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 14:54:24 | 显示全部楼层
Now it works! Thank you very much:thumbsup:!
 
-> I know this, that is why I prepared the "On Error GoTo NoRegion"
 
After that, it could be manually corrected: I use the 'Break at point' command and I create 2 different regions (Z > 0, I delete the little other one)... If you have another solution, I'm interested !
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 15:12:25 | 显示全部楼层
 
Ah, yes.  I overlooked that.
 
I suppose one automated solution could be to extend the lines for each region a sensible distance below Z = 0.  The area could then be derived mathematically by area of region – (region width * sensible distance)
回复

使用道具 举报

1

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 15:24:48 | 显示全部楼层
Thank you for this idea!
Just one comment: The area is not exactly the same as expected. You also substract the area which is over the spline and under Z=0. So you have a little difference. But as the difference is limited, I think I will use this method .
 
Have a nice day!
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-5 03:08 , Processed in 0.477070 second(s), 64 queries .

© 2020-2025 乐筑天下

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