Hello all (first post) (Some experience with .net c# and VB, but fairly new to AutoCAD)
I am using VS 2008 and ACad 2009
I have a similar requirment to this thread and I learned a lot from this thread, Thanks for posting lots of code!
Now in my application I have to read many title blocks from many files, allow the user to edit any and all attributes and then if they do edit them, udate the attributes in the files.
You can see from the code, that I am getting all the title blocks, even though they can be in model space or in one or more paper space blocks. So far so good. What I want to do is save the location of the title blocks from each file later updates (I don't keep the drawings open between reading and updating), so I want to track where they were found, either in model space or paper space, and if in paper space which paper space block they were in. I can do this using tblock.InsertSpace. The only problem is if one of the paper spaces is the current space, I can only get "*paper_space" instead of the "*paper_space##" name that I need. Theoretically the file could be opened between the read and update and saved with another paper space as the current space.
So does anyone know of a way to get the actual name of a paper space when it is the current space?
Also I thought I saw somewhere how to get the name on the space tab instead of the paper space name, does anyone know how to get that?
TIA
Public Function AddDrawing(ByVal fName As String) As Integer
' Read all title blocks in the current drawing and add to the drawings collection
Dim tblock As sida_TBAttributes
Dim iCount As Integer ' The number of drawings added to the collection
' mark if we found anything or not
Dim tbFound As Boolean = False
If fName = "" Then
AddDrawing = 0
Else
' Create a document object
Dim doc As Document
doc = Application.DocumentManager.MdiActiveDocument
' create an editor object
Dim ed As Editor = doc.Editor
' create a database and try to load the selected file
Dim db As Database = New Database(False, True)
Using (db)
Try
' open the file for reading
db.ReadDwgFile(fName, FileOpenMode.OpenForReadAndAllShare, False, "")
' start a transaction
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Using (tr)
Dim bFoundTB As Boolean = False
Dim btr As BlockTableRecord
' open the block table
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
' If a title block exists then open the blocktable record
If bt.Has("Titleblock_Type1") Then
btr = tr.GetObject(bt("Titleblock_Type1"), OpenMode.ForRead)
tbFound = True
ElseIf bt.Has("Titleblock_Type2") Then
btr = tr.GetObject(bt("Titleblock_Type2"), OpenMode.ForRead)
tbFound = True
ElseIf bt.Has("Titleblock_Type3") Then
btr = tr.GetObject(bt("Titleblock_Type3"), OpenMode.ForRead)
tbFound = True
End If
If tbFound Then
Dim objIds As ObjectIdCollection = btr.GetBlockReferenceIds(False, False)
Dim objId As ObjectId
' Get the info on all the titleblocks in a file
For Each objId In objIds
Dim i As Integer
Dim ac As Integer
Dim blockref As BlockReference = tr.GetObject(objId, OpenMode.ForRead)
' A new attribute class for the new title block
tblock = New sida_TBAttributes()
iCount = 0
' Save the drawing file name
tblock.DWGFilePathName = fName
' Save the paper/model space the title block is on
'**** Current paperspace only returns "*paper_space" not actual name
'**** like "*paper_space12"
'tblock.InsertSpace = blockref.BlockName
' get the attributes
ac = blockref.AttributeCollection.Count
For i = 0 To ac - 1
Dim atr As AttributeReference
' Cast the enumerated type to an attribute reference
atr = CType(tr.GetObject(blockref.AttributeCollection(i), OpenMode.ForRead, False, True), AttributeReference)
tblock.AddTBlockAttInfo(blockref.Name, i, atr.TextString)
Next
' Add the TB to the collection
Try
Me.Drawings.Add(tblock, tblock.attProject.OrderNumber + _
tblock.attSheet.Number + tblock.attRev.CurrentRevisionLevel)
' Update the count of TB's we have found
iCount += 1
Catch
' It already exists so we don't add, but also don't need to do anything here
End Try
Next
End If
End Using
Catch
End Try
End Using
End If
AddDrawing = iCount
End Function
Eric