PDA

View Full Version : Solved: Scan for a block in AutoCAD



seeking_help
08-05-2008, 12:46 PM
Hi all,

I am trying to scan each drawing and read certain blocks such as the sheet #, page title, etc. so I can put them into an excel sheet.

This is what I have so far...


Option Explicit
Private Sub ScanBlock()
Dim strString As String
Dim obj As Object
Dim obj2 As Object
'On Error Resume Next
For Each obj In ThisDrawing.PaperSpace
If obj.ObjectName = "AcDbBlockReference" Then
If obj.Name = "tbD2" Then
For Each obj2 In obj
If obj2.ObjectName = ?AcDbMtext? Then
strString = obj2.Value
MsgBox strString
'Return strString
End If
Next obj2
End If
End If
Next obj
End Sub


It breaks at the red section (?AcDbMtext?) saying "variable not defined"
Any ideas on why this is happening and how to fix it? I do have Mtext in the drawing. Please let me know if you need more info on this also.

Thanks for the help,
S_H

Tommy
08-05-2008, 02:33 PM
Instead of dimming the variable as an object try AcadObject. :)

mdmackillop
08-05-2008, 02:48 PM
I know nil about autocad, but it looks here
For Each obj2 In obj

that you are saying
For Each Object in Object
as opposed to
For Each Object in Objects

Tommy
08-05-2008, 03:13 PM
md,
The obj is "supposed" to be a block, which can have multiple items. But you may be correct, it may need to be dimmed as an AcadBlock.

seeking_help,
md has brought up a point that I originally missed. You are looking for a block when you may need to be looking for a blockreference. The difference is a block is inserted once, whereas the blockreference is inserted multiple times. But knowing this is a title block it should only be "referenced" once.

ska67can
08-06-2008, 05:06 AM
try this

Dim objtext as AcadText
Dim objBlock as AcadBlockReference

For Each obj In ThisDrawing.PaperSpace
If TypeOf obj Is AcDbBlockReference And obj.Name = "tbD2" Then
Set objBlock = obj
For Each obj2 In objBlock
If TypeOf obj2 Is AcadText Then
Set objtext = obj2
strString = objtext.TextString
MsgBox strString

seeking_help
08-06-2008, 06:40 AM
Thanks for all the replies,

Ok thats weird, I sent this code to a co-worker yesterday and it worked on his computer so I just re-typed that line this morning and no more error there. :dunno

md, the obj2 is trying to look for Mtext inside the title block.

Tommy, yes the block ("tbD2") is only "referenced" once in a drawing and it will now read that block.

sca67can, I changed AcDbBlockReference to AcadBlockReference because it gave me a compile error "user-defined type not defined" on that TypeOf. It now tells me a run time error "object doesn't support this property or method." My code gives me the same error on the >>> For Each obj2 in obj <<<< line (I tried to change obj to --> obj.entity still nothing)



It will scan through the blocks and it will find "tbD2" but I guess it does not like how I am looking for the Mtext inside that block. Any suggestions to fix these errors and to get the Mtext inside the Block?

Thanks for all the help so far and for future help, I really appreciate it!

Regards,
S_H

Tommy
08-06-2008, 07:42 AM
Option Explicit
Private Sub ScanBlock()
Dim strString As String
Dim obj As AcadBlock
Dim obj2 As AcadEntity
For Each obj In ThisDrawing.Blocks
If obj.Name = "tbD2" Then
For Each obj2 In obj
If obj2.ObjectName = "AcDbMText" Then 'this was the problem, notice the capital T
strString = obj2.TextString
MsgBox strString
'Return strString
End If
Next obj2
End If
Next obj
End Sub

seeking_help
08-06-2008, 08:17 AM
Ahh, got to love the minor details. Thanks for the catch Tommy, and now it works great.

Next step is now to figure out how to send that string to an Excel Spreadsheet. Any ideas? I am just now to this step so have yet to research on it but thought i would ask.

Thanks again with the help thus far!!

Regards;
S_H

Tommy
08-06-2008, 08:56 AM
Sub PlaceInExcel(iStrInfo As String)
Dim xL As Excel.Application
Dim xLWrkBk As Workbook
Dim xLSheet As Worksheet
' this will create a new instance every time you may want to check for an existing instance
Set xL = CreateObject("Excel.Application")
xL.Visible = True
Set xLWrkBk = xL.Workbooks.Add
Set xLSheet = xLWrkBk.Sheets("Sheet1")
xLSheet.Cells(1, 1).Value = iStrInfo
' this leaves excel open and active if you need to do something else let me know
Set xLSheet = Nothing
Set xLWrkBk = Nothing
Set xL = Nothing
End Sub

seeking_help
08-06-2008, 09:35 AM
Wow, I was figuring a few lines at most http://www.vbaexpress.com/forum/images/icons/icon7.gif

Once again...perfect!! Gave me a compile error at first but that was because I did not have the correct reference.

Few notes thought if anyone else needs help on this subject and reads this.

1.) Make sure you have the "Microsoft Excel x.x Object Library" active
2.) Have to add a simple line of code to get into the PlaceInExcel Sub. I simply used a vbYesNo + vbQuestion Msgbox to get into it.

Thanks again!! This works great! SOLVED!

Extreme gratitude,
S_H