Replace Spline to Circle - False size

Replace Spline to Circle - False size

erhan.lale
Contributor Contributor
1,350 Views
3 Replies
Message 1 of 4

Replace Spline to Circle - False size

erhan.lale
Contributor
Contributor

Hello,

 

i wrote a VBA, which should replace all spline with circles. I get size of spline with GetBoundingBox, calculate center point and create circle on the same place.

Sub spline2circle()

    Dim pt(2) As Double
    Dim rd As Double
    Dim min As Variant
    Dim max As Variant
    Dim cr As AcadCircle
    Dim curSpline As AcadSpline
    Dim ent As AcadEntity
    Dim key As Variant
    
    For Each ent In ThisDrawing.ModelSpace
        If TypeOf ent Is AcadSpline Then
            Set curSpline = ent
            curSpline.GetBoundingBox min, max
            pt(0) = min(0) + (max(0) - min(0)) / 2
            pt(1) = min(1) + (max(1) - min(1)) / 2
            pt(2) = 0
            rd = (Abs(((max(1) - min(1)) * 0.5)) + Abs(((max(0) - min(0)) * 0.5))) * 0.5
            Set cr = ThisDrawing.ModelSpace.AddCircle(pt, rd)
        End If
    Next
End Sub

 

You can see screenshot;

green=spline

white= circle

Circle is bigger than spline and aligned to top-right corner of them. Why?

 

spine2circle.jpg

0 Likes
1,351 Views
3 Replies
Replies (3)
Message 2 of 4

erhan.lale
Contributor
Contributor

GetBoundingBox method works perfect with lines or shapes with corner but it is not precise with curves. See screenshot:

getboundingbox.jpg

 

code is:

Sub spline2rect()

    Dim pt(2) As Double
    Dim rd As Double
    Dim min As Variant
    Dim max As Variant
    Dim points(0 To 11) As Double
    Dim plineRect As AcadPolyline
    Dim curSpline As AcadSpline
    Dim ent As AcadEntity
    
    For Each ent In ThisDrawing.ModelSpace
        If TypeOf ent Is AcadSpline Then
            Set curSpline = ent
            curSpline.GetBoundingBox min, max
            
            points(0) = min(0): points(1) = min(1): points(2) = 0:
            points(3) = max(0): points(4) = min(1): points(5) = 0:
            points(6) = max(0): points(7) = max(1): points(8) = 0:
            points(9) = min(0): points(10) = max(1): points(11) = 0:
            
            Set plineRect = ThisDrawing.ModelSpace.AddPolyline(points)
            plineRect.Closed = True
        End If
    Next
End Sub

 

Is there any other way to coreate circles based on spline?

0 Likes
Message 3 of 4

erhan.lale
Contributor
Contributor

I can do it even with CorelDraw with really short code. Why is it not possible with autocad?

Corel-VBA-Example:

Sub replace_shapes_with_ellipses()
Dim sr As ShapeRange
Dim srCreated As New ShapeRange
Dim s As Shape
    For Each s In ActiveSelectionRange
        srCreated.Add ActiveLayer.CreateEllipse(s.LeftX, s.TopY, s.RightX, s.BottomY)
        s.Delete
    Next s
    srCreated.CreateSelection
End Sub

 

0 Likes
Message 4 of 4

Ed__Jobe
Mentor
Mentor

@erhan.lale wrote:

GetBoundingBox method works perfect with lines or shapes with corner but it is not precise with curves. See screenshot:

getboundingbox.jpg

 


I don't have an answer of how to get around this problem, but perhaps the reason why the bb is off could be due to the splines control points, which are not shown. I suspect that vba isn't able to do this. The .net api has a lot more curve functions available.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature