Edit blocks using ObjectDBX in VBA

Edit blocks using ObjectDBX in VBA

Anonymous
Not applicable
4,614 Views
3 Replies
Message 1 of 4

Edit blocks using ObjectDBX in VBA

Anonymous
Not applicable

Correct me if I'm wrong, but if I want to edit drawings with a VBA program without opening each one of them in AutoCAD, I need to use objectDBX. I'm having a bit of trouble finding enough examples of objectDBX use with VBA and confused on how use it.

 

 

Could anyone help me convert this code snippet (taken from @Anonymous: http://forums.autodesk.com/t5/visual-basic-customization/edit-attributes-for-all-blocks-in-drawing/m... ) to use objectDBX, enabling it to be used in a batch process without opening the specific drawings?

 

The code is basically checking all the blocks in drawings for attributes with tag "My_String" and setting the text to "New Text":

Dim item As Variant
For Each item In ThisDrawing.ModelSpace
    If TypeOf item Is AcadBlockReference Then
        Dim att As Variant
  
        For Each att In item.GetAttributes
            If att.Visible = False Then
                If att.TagString = "My_String" Then
                    att.TextString = "New Text"
                End If
            End If
            
        Next
    End If
Next

 

(I am not even sure if I'm using the term objectDBX correctly)

 

Thank you in advance!

0 Likes
4,615 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

If you did a search in this forum for "AxDbDocument", or "DBX", you could have found quite a few discussion on the topic of using ObjectDBX to open "side" drawing with AutoCAD (i.e. read drawing into AutoCAD process' memory without opening in AutoCAD editor).

 

The key step is to create a AxDbDocument and read *.dwg file into it. Then you can access data in the drawing file in the same way as VBA's ThisDrawing object.

 

Here is a snippet of very simple code of doing it (I use Acad2015, thus the version code (xxxxx.20):

 

Option Explicit

Public Sub DbxTest()

    Dim dwgFiles(0 To 1) As String
    dwgFiles(0) = "C:\Temp\DbxTest01.dwg"
    dwgFiles(1) = "C:\Temp\DbxTest02.dwg"
    
    Dim doc As AXDBLib.AxDbDocument
    
    Dim i As Integer
    
    For i = 0 To 1
    
        Set doc = ThisDrawing.Application.GetInterfaceObject("ObjectDBX.AxDbDocument.20")
        doc.Open dwgFiles(i)

        DoWorkWithDwg doc
        
    Next

End Sub

Private Sub DoWorkWithDwg(dwg As AXDBLib.AxDbDocument)
    
    ''MsgBox dwg.Name
    
    Dim count As Integer
    Dim ent As AcadEntity
    For Each ent In dwg.ModelSpace
        ''MsgBox ent.ObjectName
        count = count + 1
    Next
    
    MsgBox "Drawing: " & dwg.Name & vbCrLf & "Entity Count: " & count
    
End Sub

HTH

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4

Anonymous
Not applicable

Thank you. That's probably exactly what I need!

 

I'm already confused though, because by these lines:

 

Dim doc As AXDBLib.AxDbDocument
Set doc = ThisDrawing.Application.GetInterfaceObject("ObjectDBX.AxDbDocument.20")

I get a type mismatch error.

 

I'm using civil3D 2016 and I think it should still be "20"?

 

 

 

0 Likes
Message 4 of 4

Anonymous
Not applicable

Well nevermind, I fixed that by changing:

 

Dim doc As AXDBLib.AxDbDocument
Set doc = ThisDrawing.Application.GetInterfaceObject("ObjectDBX.AxDbDocument.20")

to:

 

Dim doc As AxDbDocument
Set doc = Application.GetInterfaceObject("ObjectDBX.AxDbDocument.20")

Not sure why this works instead.

 

Running into new problems now,but thank you for putting me in the right direction!

 

 

 

 

0 Likes