Centroid of Region, VBA - need sample code to determine centroid of region

Centroid of Region, VBA - need sample code to determine centroid of region

xxaaron
Participant Participant
1,506 Views
3 Replies
Message 1 of 4

Centroid of Region, VBA - need sample code to determine centroid of region

xxaaron
Participant
Participant

Can someone please add the necessary code to determine the centroid of the Regions region?

 

 

pi = 3.xxx-xxxxxxxx


Dim StartPoint(0 To 2) As Double
Dim EndPoint(0 To 2) As Double
Dim Line(0 To 3) As Object

StartPoint(0) = 0: StartPoint(1) = 0: StartPoint(2) = 0
EndPoint(0) = 1: EndPoint(1) = 0: EndPoint(2) = 0
Set Line(0) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)
StartPoint(0) = 1: StartPoint(1) = 0: StartPoint(2) = 0
EndPoint(0) = 1: EndPoint(1) = 1: EndPoint(2) = 0
Set Line(1) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)
StartPoint(0) = 1: StartPoint(1) = 1: StartPoint(2) = 0
EndPoint(0) = 0: EndPoint(1) = 1: EndPoint(2) = 0
Set Line(2) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)

StartPoint(0) = 0: StartPoint(1) = 0.5: StartPoint(2) = 0

Set Line(3) = ThisDrawing.ModelSpace.AddArc(StartPoint, 0.5, pi / 2, 3 * pi / 2)

Dim Regions As Variant
Regions = ThisDrawing.ModelSpace.AddRegion(Line)

0 Likes
Accepted solutions (3)
1,507 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor
Accepted solution

Have you tried to look up this in VBA document? You do:

 

1. In VBA editor, open "Object Browser"

2. In the left pane, find and select "AcadRegion"

3. In the right pane, select "Centroid" property

4. Click the "?" button in "Object Browser" window, which would lead you to the VBA document about AcadRegion.Centroid with sample code.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

xxaaron
Participant
Participant
Accepted solution

Thanks, the object browser is really useful. I tried to integrate the sample code into my project, but I'm getting the following error: Run-time error 424, object required.  SEE CODE BELOW

 

pi = 3.xxx-xxxxxxxx


Dim StartPoint(0 To 2) As Double
Dim EndPoint(0 To 2) As Double
Dim Line(0 To 3) As Object

StartPoint(0) = 0: StartPoint(1) = 0: StartPoint(2) = 0
EndPoint(0) = 1: EndPoint(1) = 0: EndPoint(2) = 0
Set Line(0) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)
StartPoint(0) = 1: StartPoint(1) = 0: StartPoint(2) = 0
EndPoint(0) = 1: EndPoint(1) = 1: EndPoint(2) = 0
Set Line(1) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)
StartPoint(0) = 1: StartPoint(1) = 1: StartPoint(2) = 0
EndPoint(0) = 0: EndPoint(1) = 1: EndPoint(2) = 0
Set Line(2) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)

StartPoint(0) = 0: StartPoint(1) = 0.5: StartPoint(2) = 0

Set Line(3) = ThisDrawing.ModelSpace.AddArc(StartPoint, 0.5, pi / 2, 3 * pi / 2)


Dim Regions As Variant
Regions = ThisDrawing.ModelSpace.AddRegion(Line)
Dim Centroid As Variant
Centroid = Regions.Centroid
MsgBox "The Centroid for the box is " & Centroid(0) & ", " & Centroid(1), , "Centroid Example"

0 Likes
Message 4 of 4

xxaaron
Participant
Participant
Accepted solution

Never mind, I figured it out. It should be Regions = Regions(0).Centroid

0 Likes