Wow... the only thing I get when I search this forum for acadblockref is this thread... lol... gues it's time to google it.
|
Wow... the only thing I get when I search this forum for acadblockref is this thread... lol... gues it's time to google it.
Ok... I think I'm getting a little closer... I hope... but it's not returning anything. When I step through the code, it never goes inside the loop that says:
If TypeOf objEnt is AutoCAD.AcadBlockReference Then.....
I also tried:
If TypeName(objEnt) = "IAcadBlockReference" Then...
And:
If TypeName(objEnt) = "AcDbBlockReference" Then...
Code:Dim strBlkName As String Dim strAttTag As String Dim objEnt As AutoCAD.AcadEntity Dim objBlkRef As AutoCAD.AcadBlockReference Dim varAtts As Object Dim objAtt As AutoCAD.AcadAttributeReference Dim objLayout As AutoCAD.AcadLayout Dim objBlock As AutoCAD.AcadBlock Dim I As Long strBlkName = "START" strAttTag = "FILENAME" For Each objLayout In V_AutoCad.ActiveDocument.Layouts If UCase(objLayout.Name) <> "MODEL" Then objBlock = objLayout.Block For Each objEnt In objBlock If TypeOf objEnt Is AutoCAD.AcadBlockReference Then objBlkRef = objEnt If objBlkRef.Name = strBlkName Then varAtts = objBlkRef.GetAttributes For I = 0 To UBound(varAtts) objAtt = (varAtts(I)) If objAtt.TagString = strAttTag Then MsgBox(objAtt.TextString) End If Next I End If End If Next End If Next
Last edited by Ed Jobe; 2009-04-10 at 02:07 PM.
The Block property returns a BlockRef, which is not a collection, but a Block definition. It has an Item method you use to access ents. Try this, put your cursor on the Block method of the layout object and hit F1. Then find the item method, and look at its code example.
I'm not doing VBA... I'm in Visual Studio... so the F1 trick doesn't work. I searched the help file, and I found the item method.. I think.. It's making a collection of the objects, and then getting out layer "0"
I'm not sure how to apply this to reading out block attributes though... I know... I'm hopeless... I really am trying to understand it though.
Am I even heading down the right path for what I'm trying to do?? I need to look through the drawing, find several blocks by name... and in those blocks I need to read stardard information (x insertion point, x scale, etc...) as well as custom information (Sheet Number, Customer, etc..). Is what I'm trying to do even the way to do that??
Yes, you're on the right path, although you could use the .Net lib as well. Anyway, you can find the help on the ActiveX object model in acad_dev.chm in the acad Help folder or in 2008+ there's a link in Help>Additional Resources. Here's the code sample.
Code:Sub Example_Item() ' This example shows two uses of the Item method. ' The first uses Item with an index counter to return an item in a collection. ' The second uses Item with a string to return an item in a collection. ' Iterate thru the model space collection, ' get all the items in the collection ' and store them in an array called newObjs Dim count As Integer count = ThisDrawing.ModelSpace.count ReDim newObjs(count) As AcadEntity Dim index As Integer For index = 0 To count - 1 Set newObjs(index) = ThisDrawing.ModelSpace.Item(index) Next ' Get a particular item, in this case a layer, based on name "0" Dim layerObj As AcadLayer Set layerObj = ThisDrawing.Layers.Item("0") End Sub
Ok... I've finally made some progress... to a point... When I use the code below... it works just like I want it to.. but only if I actually open the drawing in AutoCAD... so apparently something is wrong with how I'm connecting to the objectdbx... or this method just does not work with it or something... so here's my code for the objectdbx as well as my code for getting the blocks.
Code:V_AutoCad = StartAutoCAD() 'Use the function located in PublicFunctions.vb to initialize AutoCAD Dim V_AutoCadInterfaceObject As AxDbDocument V_AutoCadInterfaceObject = V_AutoCad.GetInterfaceObject("ObjectDBX.AxDbDocument") V_AutoCadInterfaceObject.Open(V_FilePathAutoCad) ' ******************************************************************** Public Function StartAutoCAD() As Object On Error Resume Next V_AutoCad = GetObject(, "AutoCad.Application.15") If Err.Number <> 0 Then 'If they don't have AutoCAD open.. then open it Err.Clear() Shell("C:\Program Files\Acad2002\acad.exe", AppWinStyle.NormalFocus) System.Threading.Thread.Sleep(3000) ' Create a new session of AutoCAD using late binding V_AutoCad = GetObject(, "AutoCad.Application.15") End If Return V_AutoCad End Function ' ******************************************************************** Code for getting the block info: Dim strBlkName As String Dim strAttTag As String Dim objEnt As AutoCAD.AcadEntity Dim objBlkRef As AutoCAD.AcadBlockReference Dim varAtts As Object Dim objAtt As AutoCAD.AcadAttributeReference Dim I As Long strBlkName = "START" strAttTag = "FILENAME" For Each objEnt In V_AutoCad.ActiveDocument.ModelSpace If objEnt.ObjectName.ToString = "AcDbBlockReference" Then objBlkRef = objEnt If objBlkRef.Name = strBlkName Then varAtts = objBlkRef.GetAttributes For I = 0 To UBound(varAtts) objAtt = (varAtts(I)) If objAtt.TagString = strAttTag Then MsgBox(objAtt.TextString) End If Next I End If End If Next Next
Last edited by Ed Jobe; 2009-04-12 at 11:11 PM.
I'm a little short on time today. Search this forum for "ObjectDbx" or "oDbx.cls". Also, your StartAutocad sub has an error. The err section should be a call to CreateObject, not GetObject.
Ok... I'll see what I can find searching for those.
I changed that to CreateObject, I don't think that's causing my problem since I've been running this with AutoCAD aready open, so it's not going into that section of code... or shouldn't be anyway.
Thank-you again for helping me figure this out.