I'm trying to rewrite some old VBA code into VB.net (which I'm not even Novice level yet), and I'm stumped on what would be a fairly simple principle (in VBA)..
For Each BlockDef in ThisDrawing.blocks If Left(BlockDef.name, 12) = "*Paper_Space" then Select Case True Case BlockDef.Layout.Name Like "*Test*" blah blah End Select End If Next
Essentially conditional manipulation of paperspace elements depending on the layout name.
The following fragment allows me to recurse through entities in paperspace, but I've no idea which layout tab..
Dim BlkPaperSpc As BlockTableRecord = ScanTrans.GetObject(BlkTab _ (BlockTableRecord.PaperSpace), OpenMode.ForWrite) For Each AcObjId As ObjectId In BlkPaperSpc Dim PspaceObj As String = AcObjId.ObjectClass.Name Next
What I need to do is to be able to identify a type of object (a table), confirm it's location, delete it and replace it with another one, depending on which layout tab it's on.
Any pointers or tips for the complete novice??
G
This is not the best solution but I hope
it will to get you started
'' get the real block name. '' Public Shared Function EffectiveName(tr As Transaction, bref As BlockReference) As String Dim btr As BlockTableRecord = Nothing If (bref.IsDynamicBlock) Or (bref.Name.StartsWith("`*U", StringComparison.InvariantCultureIgnoreCase)) Then btr = TryCast(tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead), BlockTableRecord) Else btr = TryCast(tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead), BlockTableRecord) End If Return btr.Name End Function <CommandMethod("cblk")> _ Public Sub iterateThroughLayouts() Dim layts As New List(Of String) Dim doc As Document = acApp.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Dim blkdict As New Dictionary(Of ObjectId, String()) Try Dim tr As Transaction = db.TransactionManager.StartTransaction Using tr Dim ldict As DBDictionary = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) For Each dicent As DBDictionaryEntry In ldict Dim lay As Layout = CType(dicent.Value.GetObject(OpenMode.ForRead), Layout) Dim layname As String = lay.LayoutName ''do your stuffs in the every layout If layname <> "Model" Then Dim btr As BlockTableRecord = tr.GetObject(lay.BlockTableRecordId, OpenMode.ForRead) For Each id As ObjectId In btr Dim ent As Entity = tr.GetObject(id, OpenMode.ForRead, False) If TypeOf (ent) Is BlockReference Then 'get block instance Dim bref As BlockReference = DirectCast(ent, BlockReference) 'get block name Dim blkname = EffectiveName(tr, bref) ' get block instance owner blkdict.Add(bref.ObjectId, New String() {blkname, layname}) 'we have to gather all desired values to Dictionary (of string, string()) End If Next End If Next tr.Commit() End Using 'display info in the command line after For Each kvp As KeyValuePair(Of ObjectId, String()) In blkdict ed.WriteMessage(vbLf + "{0} {1} {2}" + vbLf, kvp.Key, kvp.Value(0), kvp.Value(1)) Next Catch ex As System.Exception acApp.ShowAlertDialog(Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace) End Try End Sub
Hi, Thanks for the code fragment - unfortunately I've not had time today to read through it and understand it completely, but I didn't want your effort to go without response.
I'm sure I'll be back with more questions, hopefully on other topics 🙂
G