<?xml encoding=”UTF-8″>By Adam Nagy
When interacting with a DWG type drawing document, you can also use the AutoCAD API in it through the ObjectDBX type library.
From the DrawingDocument.ContainingDWGDocument you’ll get back an AcadDatabase object that you can use to drill down into the drawing’s contents.
As an example for showing how to achieve something using the Inventor API vs the AutoCAD API, here are two functions which are supposed to be doing the same thing: iterating through the Sheets (Layouts in AutoCAD) and set the Prompt Text values (Attribute Text in AutoCAD) for a given AutoCADBlock (Block Reference in AutoCAD).
Inventor code:
Sub SetAttributesOnSheets()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim Blockname As String
Blockname = "Testblock"
Dim oSheet As Sheet
For Each oSheet In oDrawDoc.Sheets
Dim oBlock As AutoCADBlock
For Each oBlock In oSheet.AutoCADBlocks
If oBlock.Name = Blockname Then
Dim tagStrings() As String
Dim textStrings() As String
oBlock.GetPromptTextValues tagStrings, textStrings
Dim i As Integer
For i = LBound(tagStrings) To UBound(tagStrings)
If tagStrings(i) = "TEXT1" Then
textStrings(i) = "text1text"
ElseIf tagStrings(i) = "TEXT2" Then
textStrings(i) = "text2text"
End If
Next
' If multiple tag strings are the same then it will
' only set the value for the first one of them
' That's one of the reasons for this article
' to show how to do it using the AutoCAD API instead
' which seems to be working fine and can be used as
' a workaround
oBlock.SetPromptTextValues tagStrings, textStrings
End If
Next
Next
End Sub
AutoCAD/ObjectDBX code:
Sub SetAttributesOnLayouts()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim Blockname As String
Blockname = "Testblock"
' Using AutoCAD API
' If you want to declare variables using their
' exact type, which will provide you with intelli-sense
' as well, then you need to reference the
' "AutoCAD/ObjectDBX Common Type Library"
' through "Tools >> References"
' You can also declare the variables as "Object" instead, in
' which case you are doing so called "late-binding"
' which does not require you to reference the type library:
' e.g.: Dim oDb As Object
Dim oDb As AcadDatabase
Set oDb = oDrawDoc.ContainingDWGDocument
' This way we can set the attribute values even inside
' the model space layout which does not seem to be
' possible even through the Inventor User Interface
Dim oLayout As AcadLayout
For Each oLayout In oDb.Layouts
Dim oBlock As AcadBlock
Set oBlock = oLayout.Block
Dim oEnt As AcadEntity
For Each oEnt In oBlock
If TypeOf oEnt Is AcadBlockReference Then
Dim oBR As AcadBlockReference
Set oBR = oEnt
If oBR.Name = Blockname Then
Dim oAtts As Variant
oAtts = oBR.GetAttributes()
Dim i As Integer
For i = LBound(oAtts) To UBound(oAtts)
Dim oRef As AcadAttributeReference
Set oRef = oAtts(i)
If oRef.TagString = "TEXT1" Then
oRef.TextString = "text1text"
ElseIf oRef.TagString = "TEXT2" Then
oRef.TextString = "text2text"
End If
Next
End If
End If
Next
Next
Call oDrawDoc.Update
End Sub
Result:


Leave a Reply