Author Topic: Using AcadRegion.Copy  (Read 121 times)

0 Members and 1 Guest are viewing this topic.


  • Newt
  • Posts: 25
Using AcadRegion.Copy
« on: December 11, 2018, 09:00:57 AM »
I have a routine that works out the length of the longest line in an AcadRegion object.  I expect the code below to create a copy of the Region objects on a specific layer, explode the copy and then when finished, remove the copy, but it doesn' removes both the copy and the original Region object when I run it a second time:

Code: [Select]
    Public Function GetInsituConcretePolyInsertCount(ByRef myDocuments As AcadDocumentManagerExample) As Integer

        Dim polyInsertCount As Integer = 0
        Dim filterType(1) As Short
        Dim filterData(1) As Object
        Dim selectionSet As AcadSelectionSet
        Dim i As Integer
        Dim inSituConcreteRegion As AcadRegion
        Dim copyOfInSituConcreteRegion As AcadRegion
        Dim insituLength As Double
        Dim explodedRegion As Object
        Dim j As Integer
        Dim line As AcadLine
        Dim regionLength As Double


            insituLength = 0

            selectionSet = CreateSelSet(myDocuments.ThisDrawing, "INSITUCONCRETE")

            filterType(0) = 0
            filterType(1) = 8

            filterData(0) = "REGION"
            filterData(1) = EASICAD_LAYERS.INSITU_INFILL

            selectionSet.Select(AcSelect.acSelectionSetAll, , , filterType, filterData)

            For i = 0 To selectionSet.Count - 1
                inSituConcreteRegion = TryCast(selectionSet.Item(i), AcadRegion)

                If Not IsNothing(inSituConcreteRegion) Then

                    'regionLength = GetLengthOfRegion(inSituConcreteRegion, myDocuments)

                    regionLength = 0

                    copyOfInSituConcreteRegion = TryCast(inSituConcreteRegion.Copy, AcadRegion)

                    If IsNothing(copyOfInSituConcreteRegion) Then
                        Continue For
                    End If

                    explodedRegion = copyOfInSituConcreteRegion.Explode

                    For j = 0 To UBound(explodedRegion)
                            line = TryCast(explodedRegion(j), AcadLine)

                            If Not IsNothing(line) Then
                                If line.Length > regionLength Then
                                    regionLength = line.Length
                                End If

                            End If

                        Catch ex As Exception
                            'Do nothing it's not a line
                        End Try



                    insituLength += regionLength

                End If

            If insituLength > 0 Then
                polyInsertCount = Math.Round(insituLength / 1200, 0, MidpointRounding.AwayFromZero)
            End If

        Catch ex As System.Exception
            MsgBox("Reusable.GetInsituConcretePolyInsertCount : " & ex.Message, MsgBoxStyle.OkOnly, EasiCADException.EasiCADErrorTitle)
        End Try

        Return polyInsertCount
    End Function

The first time I run this routine, it processes fine and the drawing contains the original Region(s).  However, if I run the routine a second time it removes both the copy and the original.  If I use the routine once, save the drawing, close it, reopen it and run the routine again, it works fine.  It's only when the routine is run back to back in the same session, regardless if I save or not.

I'm sure someone will tell me to use another artefact like a Polyline.  I can, but that involves an enormous amount of work elsewhere....and the bulk of the system I am custodian of uses Regions buried in Blocks that get exploded all the time (and not using a Copy either) and these don't get removed.  I could bury these Regions in blocks I suppose, but I'm back to an enormous amount of work elsewhere.  And I simply don't understand why the Copy command doesn't work that second time.

The myDocuments object is essentially an AcadDocumentManager object.

Kind regards, Paul


  • Newt
  • Posts: 25
Re: Using AcadRegion.Copy
« Reply #1 on: December 11, 2018, 11:31:18 AM »
False alarm.  The issue was being caused by the CreateSelSet procedure call at the top that incorrectly Erased the selection set contents rather than Clear(ed) them.