Deleting an AutoCad 2013 region in VBA that was just created

I have a routine that calculates the area of a Polyline.  Then it creates a region from the polyline to extract the centroid.  Works fine.

Except when the routine is done, I don't really want the region in my drawing anymore.  How do I get the code to delete the region that was created in order to get the centroid ?  (I'd like to let AutoCad give me the centroid than put all the code in there to do it myself...)

Public Sub GetEntity(PLArea, PLCentroid)
Dim objEnt As AcadEntity
Dim varPick As Variant
Dim Pline As AcadLWPolyline
Dim loops(0) As AcadEntity, region
Dim CenPt As Variant

On Error Resume Next
    With ThisDrawing.Utility
        .GetEntity objEnt, varPick, vbCr & "Pick a Polyline: "
        If objEnt Is Nothing Then 'check if object was picked.
            .Prompt vbCrLf & "You did not pick as entity"
            Exit Sub
        End If
        
        If objEnt.ObjectName <> "AcDbPolyline" Then 'check if object was picked.
            MsgBox "That is not a PolyLine.  It is a(n) " & objEnt.ObjectName & ".  Please start over and try again."
            End
        End If
    End With
'cast Entity to Polyline proper
    Set Pline = objEnt
    PLArea = Pline.Area / 43560
    ThisDrawing.SetVariable "DELOBJ", 0 ' this will prevent deleting the polyline
    Set loops(0) = objEnt
    region = ThisDrawing.ModelSpace.AddRegion(loops)
    CenPt = region(0).Centroid
' now that I have the centroid how do I delete the region I just created
End Sub

Open in new window

KevinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

insidetechCommented:
several ways come to mind but the simplest one:

Erase LAST
0
KevinAuthor Commented:
Thanks.  I put the

Erase Last 

Open in new window


command right after

CenPt = region(0).Centroid

Open in new window


but the region is still there after I exit...
0
insidetechCommented:
Notice that you set the Variable Delobj=0.

In this case the plines will stay if you do the erase last. ( the region will be erased)

So... if you wan it all gone set back the delobj=3 and follow by the Erase last.
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

KevinAuthor Commented:
I am afraid that I don't follow, sorry.

The polyline needs to stay, so that was why I set DELOBJ to zero. But I want the region to disappear, which it doesn't when I put in the Erase Last command.

Any other ways that you can think of that will keep the Polyline?
0
insidetechCommented:
What version of AutoCAD are you using?
In my version 2012 the erase last erases the region and leaves the poly's alone, when delobj set to 0.
Try it manually not in the script, perhaps it behaves differently in the script, but using command line it behaves for me exactly the way you want it. The erase last removes the area only, leaving polylines alone.
0
KevinAuthor Commented:
I am using 2013.  Will give it a shot when I get back to the office in the morning...
0
insidetechCommented:
How is your 2013 behaving?
0
KevinAuthor Commented:
Sorry - I thought that i had replied from my phone, but see that it didn't post.

Manually, the "erase last" command works properly and will erase the recently created region leaving the polyline unaffacted, but not in the VBA code - the code does not delete the region...
0
Saqib Husain, SyedEngineerCommented:
Are you still waiting for a solution?
0
Saqib Husain, SyedEngineerCommented:
Try

    For i = UBound(region) To 0 Step -1
    region(i).Delete
    Next i
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CAD/Architecture Software

From novice to tech pro — start learning today.