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
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
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
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
MsgBox "ProcessConstructionBlocks error " & err.Number & vbCrLf & err.Description
Private Function CreateDGNList() As Boolean 'creates an array of filenames
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
ReDim Preserve ListArray(n)
CreateDGNList = False
Unknown said:I did specify the file to be read-write when I opened it
Not here you didn't …
The documentation tells us only that the ReadOnly parameter is optional — it doesn't tell us what it's default value is. So why not be explicit?
Const OpenForWrite As Boolean = False Const OpenReadOnly As Boolean = True Dim oDgn As DesignFile Set oDgn = Application.OpenDesignFileForProgram(temp, OpenForWrite)
My fault, I did have the "false" parameter in my newer code, I posted the incorrect version. I did add it and there are no errors but the construction blocks don't get deleted. Not sure why, I suspect I didn't properly reference the active model.
Unknown said: I suspect I didn't properly reference the active model
It's not clear where referencing the active model comes into this. Reference the active model to what?
When you open a model for programming, by default its models are not loaded. You must explicitly load references.