How to Remove element using OpenDesignFileForProgram?

I am using OpenDesignFileForProgram for counting elements.

I would like to Remove Element also, can this be done? My experiments all seem to result in "Model is Read-Only".

Thanks,

Paul

Parents
  •   Set DesignFile = object.OpenDesignFileForProgram (DesignFileName [, ReadOnly]) 

    The last parameter is optional. You need to specify False for write access …

    Const OpenForWrite As Boolean = False
    Const OpenForRead As Boolean = True
    Set DesignFile = object.OpenDesignFileForProgram ("C:\temp\paul.dgn", OpenForWrite) 

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • Jon,

    Thanks.

    I should of been more clear, I did specify the file to be read-write when I opened it.

    The model is read-only, I think.

    Here is my code:

    Option Explicit

    Dim model As ModelReference

    Dim BlocksFound As Long

    Dim FilePath As String      ' fullpath of current design file

    Dim ListArray()             ' holds list of drawings to process

    Dim n As Long               ' holds current file in array list

    Dim i As Long               ' general counter

    Dim temp As String

    Dim ret

    Public Sub TestScanForConstructionBlocks()

    On Error GoTo err

       FilePath = "c:\dgn\Detroit_16"

       i = 0

       ReDim ListArray(100)

       If CreateDGNList = False Then End

       For n = LBound(ListArray) To UBound(ListArray) - 1

           temp = FilePath & "\" & ListArray(n)

           Application.ShowTempMessage msdStatusBarAreaLeft, "scanning for construction blocks " & n & " of " & UBound(ListArray) - 1

           DoEvents

           Dim oDgn As DesignFile                                          ' contains Microstation file object

           Set oDgn = Application.OpenDesignFileForProgram(temp)

           'Set model = oDgn.Models(1)

           ret = CountConstructionBlocks(temp)

           Debug.Print ret; "  "; temp

       Next n

       Application.ShowTempMessage msdStatusBarAreaLeft, " "

       MsgBox "done"

    Exit Sub

    err:

       MsgBox "TestScanForBlocksMain Error # " & Str(err.Number), vbCritical

    End Sub

    Private Function CountConstructionBlocks(infile As String) As Long

       Dim oFile As DesignFile

       Set oFile = Application.OpenDesignFileForProgram(infile, False)

       Dim oScan As New ElementScanCriteria

       Dim oElEnum As ElementEnumerator

       oScan.ExcludeAllClasses: oScan.ExcludeAllTypes

       oScan.IncludeType msdElementTypeShape: oScan.IncludeClass msdElementClassConstruction

       Set model = oFile.Models(1)

       Set oElEnum = model.Scan(oScan)

       CountConstructionBlocks = ProcessConstructionBlocks(oFile, oElEnum)

       oFile.Close

    End Function

    Private Function ProcessConstructionBlocks(oFile As DesignFile, oElEnum As ElementEnumerator) As Long

    On Error GoTo err

    Dim oEl As Element

       ProcessConstructionBlocks = 0

       Do While oElEnum.MoveNext

           Set oEl = oElEnum.Current

                ' ActiveModelReference.RemoveElement oEl

               If Not model.IsReadOnly Then

                   model.RemoveElement oEl

               End If

           ProcessConstructionBlocks = ProcessConstructionBlocks + 1

       Loop

       Exit Function

    err:

       MsgBox "ProcessConstructionBlocks error " & err.Number & vbCrLf & err.Description

    End Function

    Private Function CreateDGNList() As Boolean 'creates an array of filenames

    On Error GoTo err

    CreateDGNList = True

    Dim MyName

       n = 0

       MyName = Dir(FilePath & "\" & "*.DGN")    ' Get the first file

       Do While MyName <> ""

           ListArray(n) = MyName

           MyName = Dir                    ' Get next file

           n = n + 1

       Loop

       ReDim Preserve ListArray(n)

    Exit Function

    err:

       CreateDGNList = False

    End Function

Reply
  • Jon,

    Thanks.

    I should of been more clear, I did specify the file to be read-write when I opened it.

    The model is read-only, I think.

    Here is my code:

    Option Explicit

    Dim model As ModelReference

    Dim BlocksFound As Long

    Dim FilePath As String      ' fullpath of current design file

    Dim ListArray()             ' holds list of drawings to process

    Dim n As Long               ' holds current file in array list

    Dim i As Long               ' general counter

    Dim temp As String

    Dim ret

    Public Sub TestScanForConstructionBlocks()

    On Error GoTo err

       FilePath = "c:\dgn\Detroit_16"

       i = 0

       ReDim ListArray(100)

       If CreateDGNList = False Then End

       For n = LBound(ListArray) To UBound(ListArray) - 1

           temp = FilePath & "\" & ListArray(n)

           Application.ShowTempMessage msdStatusBarAreaLeft, "scanning for construction blocks " & n & " of " & UBound(ListArray) - 1

           DoEvents

           Dim oDgn As DesignFile                                          ' contains Microstation file object

           Set oDgn = Application.OpenDesignFileForProgram(temp)

           'Set model = oDgn.Models(1)

           ret = CountConstructionBlocks(temp)

           Debug.Print ret; "  "; temp

       Next n

       Application.ShowTempMessage msdStatusBarAreaLeft, " "

       MsgBox "done"

    Exit Sub

    err:

       MsgBox "TestScanForBlocksMain Error # " & Str(err.Number), vbCritical

    End Sub

    Private Function CountConstructionBlocks(infile As String) As Long

       Dim oFile As DesignFile

       Set oFile = Application.OpenDesignFileForProgram(infile, False)

       Dim oScan As New ElementScanCriteria

       Dim oElEnum As ElementEnumerator

       oScan.ExcludeAllClasses: oScan.ExcludeAllTypes

       oScan.IncludeType msdElementTypeShape: oScan.IncludeClass msdElementClassConstruction

       Set model = oFile.Models(1)

       Set oElEnum = model.Scan(oScan)

       CountConstructionBlocks = ProcessConstructionBlocks(oFile, oElEnum)

       oFile.Close

    End Function

    Private Function ProcessConstructionBlocks(oFile As DesignFile, oElEnum As ElementEnumerator) As Long

    On Error GoTo err

    Dim oEl As Element

       ProcessConstructionBlocks = 0

       Do While oElEnum.MoveNext

           Set oEl = oElEnum.Current

                ' ActiveModelReference.RemoveElement oEl

               If Not model.IsReadOnly Then

                   model.RemoveElement oEl

               End If

           ProcessConstructionBlocks = ProcessConstructionBlocks + 1

       Loop

       Exit Function

    err:

       MsgBox "ProcessConstructionBlocks error " & err.Number & vbCrLf & err.Description

    End Function

    Private Function CreateDGNList() As Boolean 'creates an array of filenames

    On Error GoTo err

    CreateDGNList = True

    Dim MyName

       n = 0

       MyName = Dir(FilePath & "\" & "*.DGN")    ' Get the first file

       Do While MyName <> ""

           ListArray(n) = MyName

           MyName = Dir                    ' Get next file

           n = n + 1

       Loop

       ReDim Preserve ListArray(n)

    Exit Function

    err:

       CreateDGNList = False

    End Function

Children