Break a line into smaller pieces using VBA or VB.NET in AutoCAD

by Will on December 7, 2010

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
End Sub


Enter Your Mail Address

Be Sociable, Share!

Comments on this entry are closed.

Previous post:

Next post: