Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I wrote a tool to perform a find and replace on the active document. I’ve been attempting to modify it to allow me to pass the sub a file name so that I can perform the edit to multiple files. As best I can tell the editor variable I set points to the active document. I simply can’t figure out how to edit the passed document, and not the open one.
Private Sub test(fn As String)
Dim db As Database = New Database(False, True),
dwgDir As String = System.IO.Path.GetDirectoryName(fn),
dwgName As String = System.IO.Path.GetFileName(fn),
DocMgr As DocumentCollection = Application.DocumentManager,
Doc As Document = DocumentCollectionExtension.Add(DocMgr, fn),
ed As Editor = Doc.Editor
Using db
Try
db.ReadDwgFile(fn, FileOpenMode.OpenForReadAndWriteNoShare, False, "")
Catch __unusedException1__ As System.Exception
Return
End Try
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "TEXT")},
Filter As SelectionFilter = New SelectionFilter(filList),
Res As PromptSelectionResult = ed.SelectAll(Filter),
selSet As SelectionSet = Res.Value
If selSet IsNot Nothing Then
Dim idArray As ObjectId() = selSet.GetObjectIds()
Try
For Each txtID As ObjectId In idArray
Dim txt As DBText = CType(tr.GetObject(txtID, OpenMode.ForWrite), DBText),
wksp As String = txt.BlockName.ToString,
hand As String = txt.Handle.ToString,
val As String = txt.TextString,
locX As String = txt.Position.X.ToString,
locY As String = txt.Position.Y.ToString,
locZ As String = txt.Position.Z.ToString
If hand <> Nothing And val <> Nothing And val.Contains(strFind) Then
txt.TextString = val.Replace(strFind, strReplace)
txt.RecordGraphicsModified(True)
tbl.Rows.Add(txt.TextString, val, "DBText", dwgName, hand, wksp, locX, locY, locZ)
End If
Next
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(("Exception: " & ex.Message))
Finally
tr.Dispose()
End Try
End If
End Using
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "INSERT")},
filter As SelectionFilter = New SelectionFilter(filList),
Res As PromptSelectionResult = ed.SelectAll(filter),
selSet As SelectionSet = Res.Value
If selSet IsNot Nothing Then
Dim idArray As ObjectId() = selSet.GetObjectIds()
For Each blkId As ObjectId In idArray
Dim blkRef As BlockReference = CType(tr.GetObject(blkId, OpenMode.ForRead), BlockReference)
Dim BLK_n As String = blkRef.Name,
BLK_hand As String = blkRef.Handle.ToString
Dim btr As BlockTableRecord = CType(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
btr.Dispose()
Dim attCol As AttributeCollection = blkRef.AttributeCollection
If attCol IsNot Nothing Then
For Each attId As ObjectId In attCol
Dim att As AttributeReference = CType(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference),
wksp As String = att.BlockName.ToString,
hand As String = att.Handle.ToString,
val As String = att.TextString,
locX As String = att.Position.X.ToString,
locY As String = att.Position.Y.ToString,
locZ As String = att.Position.Z.ToString
If hand <> Nothing And val <> Nothing And BLK_hand <> Nothing And BLK_n <> Nothing And val.Contains(strFind) = True Then
att.TextString = val.Replace(strFind, strReplace)
att.RecordGraphicsModified(True)
tbl.Rows.Add(att.TextString, val, "Attribute", dwgName, hand, wksp, locX, locY, locZ)
End If
Next
End If
Next
tr.Commit()
End If
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(("Exception: " & ex.Message))
Finally
tr.Dispose()
End Try
End Using
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "MTEXT")},
Filter As SelectionFilter = New SelectionFilter(filList),
Res As PromptSelectionResult = ed.SelectAll(Filter),
selSet As SelectionSet = Res.Value
If selSet IsNot Nothing Then
Dim idArray As ObjectId() = selSet.GetObjectIds()
Try
For Each txtID As ObjectId In idArray
Dim mtxt As MText = CType(tr.GetObject(txtID, OpenMode.ForWrite), MText),
wksp As String = mtxt.BlockName.ToString,
hand As String = mtxt.Handle.ToString,
val As String = mtxt.Contents.ToString,
locX As String = mtxt.Location.X.ToString,
locY As String = mtxt.Location.Y.ToString,
locZ As String = mtxt.Location.Z.ToString
If hand <> Nothing And val <> Nothing And val.Contains(strFind) = True Then
mtxt.Contents = val.Replace(strFind, strReplace)
mtxt.RecordGraphicsModified(True)
tbl.Rows.Add(mtxt.Contents, val, "MText", dwgName, hand, wksp, locX, locY, locZ)
End If
Next
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(("Exception: " & ex.Message))
Finally
tr.Dispose()
End Try
End If
End Using
End Using
End SubI attempted to approach it differently hoping to resolve the issue, but this code failed too. It goes through the files but won't update any of them.
Private Sub test(fn As String)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument,
ed As Editor = doc.Editor,
db As Database = New Database(False, True),
dwgDir As String = System.IO.Path.GetDirectoryName(fn),
dwgName As String = System.IO.Path.GetFileName(fn)
Using db
Try
db.ReadDwgFile(fn, System.IO.FileShare.Read, False, "")
Catch __unusedException1__ As System.Exception
ed.WriteMessage(vbLf & "Unable to read drawing file.")
Return
End Try
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Using tr
Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
Dim btr As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead), BlockTableRecord)
For Each objId As ObjectId In btr
Dim objtyp As String = objId.GetType().ToString,
objent As Entity = CType(tr.GetObject(objId, OpenMode.ForWrite), Entity),
OBJDXF As String = objent.GetRXClass().DxfName
Select Case OBJDXF
Case "TEXT"
Dim txt As DBText = CType(tr.GetObject(objId, OpenMode.ForWrite), DBText),
wksp As String = txt.BlockName.ToString,
hand As String = txt.Handle.ToString,
val As String = txt.TextString,
locX As String = txt.Position.X.ToString,
locY As String = txt.Position.Y.ToString,
locZ As String = txt.Position.Z.ToString
If hand <> Nothing And val <> Nothing And val.Contains(strFind) Then
txt.TextString = val.Replace(strFind, strReplace)
txt.RecordGraphicsModified(True)
tbl.Rows.Add(txt.TextString, val, "DBText", dwgName, hand, wksp, locX, locY, locZ)
End If
Case "MTEXT"
Dim mtxt As MText = CType(tr.GetObject(objId, OpenMode.ForWrite), MText),
wksp As String = mtxt.BlockName.ToString,
hand As String = mtxt.Handle.ToString,
val As String = mtxt.Contents.ToString,
locX As String = mtxt.Location.X.ToString,
locY As String = mtxt.Location.Y.ToString,
locZ As String = mtxt.Location.Z.ToString
If hand <> Nothing And val <> Nothing And val.Contains(strFind) = True Then
mtxt.Contents = val.Replace(strFind, strReplace)
mtxt.RecordGraphicsModified(True)
tbl.Rows.Add(mtxt.Contents, val, "MText", dwgName, hand, wksp, locX, locY, locZ)
End If
Case "Attribute"
Dim att As AttributeReference = CType(tr.GetObject(objId, OpenMode.ForWrite), AttributeReference),
wksp As String = att.BlockName.ToString,
hand As String = att.Handle.ToString,
val As String = att.TextString,
locX As String = att.Position.X.ToString,
locY As String = att.Position.Y.ToString,
locZ As String = att.Position.Z.ToString
If hand <> Nothing And val <> Nothing And hand <> Nothing And val.Contains(strFind) = True Then
att.TextString = val.Replace(strFind, strReplace)
att.RecordGraphicsModified(True)
tbl.Rows.Add(att.TextString, val, "Attribute", dwgName, hand, wksp, locX, locY, locZ)
End If
End Select
Next
tr.Commit()
tr.Dispose()
End Using
End Using
End Sub
Solved! Go to Solution.