Break a line into smaller pieces using VBA or VB.NET in AutoCAD®
Here's a simple example of some VBA code that might come in handy either as a learning exercise, or if you happen to be trying to break a line programatically.
The code fistly asks the user to select a line, and the object is stored in the Line variable. Then, I've hard-coded the NumberOfSegments variable to equal 5. Next, we calculate what the "offset" would be for each line if we were to divide the original line up into 5 segments. Using this value, we then set the position of the existing line to be the length of the first line in the new set of lines. Then, using the offset calculated earlier, we can copy the line, and move by the amount defined in the Offset variable.
Option Explicit
Sub ExplodeLine()
Dim NumberOfSegments As Long
Dim Line As AcadLine
Dim NewEndPoint(2) As Double
Dim BasePoint(2) As Double
Dim Offset(2) As Double
Dim n As Long
Dim p As Variant
ThisDrawing.Utility.GetEntity Line, p, "Pick a Line..."
NumberOfSegments = 5
Offset(0) = (Line.EndPoint(0) - Line.StartPoint(0)) / NumberOfSegments
Offset(1) = (Line.EndPoint(1) - Line.StartPoint(1)) / NumberOfSegments
NewEndPoint(0) = Line.StartPoint(0) + Offset(0)
NewEndPoint(1) = Line.StartPoint(1) + Offset(1)
Line.EndPoint = NewEndPoint
For n = 2 To NumberOfSegments
' Loop (NumberOfSegments - 1) times as the first already exists
Set Line = Line.Copy
Line.Move BasePoint, Offset
Next
End Sub
Regards,
Will
Comments
Daan
2015-03-09 15:30:17
Hi there Will, hope you are still there, I am an indpendent tool-developer with a ton of AutoCAD®-VBA-code for one of my customers. I am trying to weigh the consequences of porting all these tools to VB.NET The code you wrote above looks like VBA to me, and my first question is: what does this look like in VB.NET ?
My second question would be something like: how easy or how hard is it to rewrite VBA-code for .NET , and is there a tool for that? Kindest regards, Daniel
Manoucher
2016-04-04 08:56:21
Good idea