Ian Jackson
Ian Jackson

Reputation: 41

Reading, Writing and controlling Autocad using external VBA

I'm using MS-Access 2010 and Autocad 2012 64bit and work in manufacturing.

I want to be able to at the very least, populate fields in a title block, better still I would like to use data in my access database to write data into a sheet set (the current system works by reading the sheet set values such as sheet title and number into my title block).

The following code is all I have at the moment and it will open autocad and write the date into the command line.

Private Sub OpenAutocad_Click()
    Dim CadApp As AcadApplication
    Dim CadDoc As AutoCAD.AcadDocument

    On Error Resume Next
    Set CadApp = GetObject(, "AutoCAD.Application")

    If Err.Number <> 0 Then
        Set CadApp = CreateObject("AutoCAD.Application")
    End If
    On Error GoTo 0

    CadApp.Visible = True
    CadApp.WindowState = acMax

    Set CadDoc = CadApp.ActiveDocument

    CadDoc.Utility.Prompt "Hello from Access, the time is: " & TheTime

    Set CadApp = Nothing
End Sub

I have no idea where to go from here. What are the commands to control the sheet set manager and change data, and can the .dst file be edited without even opening up autocad? is there a list of all available autocad vba commands and functions?

Upvotes: 0

Views: 4114

Answers (3)

Thomas Ludewig
Thomas Ludewig

Reputation: 725

:) Half the way gone ;) If you has a open autocad with a loaded drawing you can access the whole thing directly.

    Sub block_set_attribute(blo As AcadBlockReference, tagname, tagvalue)
Dim ATTLIST As Variant
If blo Is Nothing Then Exit Sub
If blo.hasattributes Then
    tagname = Trim(UCase(tagname))
    ATTLIST = blo.GetAttributes
    For i = LBound(ATTLIST) To UBound(ATTLIST)

        If UCase(ATTLIST(i).TAGSTRING) = tagname Or UCase(Trim(ATTLIST(i).TAGSTRING)) = tagname & "_001" Then
            'On Error Resume Next
            ATTLIST(i).textString = "" & tagvalue
            Exit Sub
        End If
    Next

  End If
End Sub

Sub findtitleblock(TITLEBLOCKNAME As String, attributename As String, 
attributevalue As String)
Dim entity As AcadEntity
Dim block As acadblcck
Dim blockref As AcadBlockReference
For Each block In ThisDrawing.BLOCKS
For Each entity In block
  If InStr(LCase(entity.objectname), "blockref") > 0 Then
  Set blockref = entity
      If blockref.effectivename = TITLEBLOCKNAME Then
         Call block_set_attribute(blockref, attributename, attributevalue)
         exit for
      End If
       End If
     End If
    Next
 Next
End Sub

call findtitleblock("HEADER","TITLE","Bridge column AXIS A_A")

So assume you has a title block which has the attribute TITLE then it will set the Attribute to the drawing name. it mioght also possible you has to replace the thisdrawing. with your Caddoc. I usually control Access and Excel form autocad and not vice versa ;)

consider also to use "REGEN" and "ATTSYNC" if "nothing happens"

thisdrawing.sendcommens("_attsync" 6 vblf )

Upvotes: 0

Arya S.
Arya S.

Reputation: 131

If I understand your question correctly, you want to automate filling attributes in a drawing title blocks (such as title, drawer, part number, etc) right from MS Access.

Your code can access the Autocad command line already, but Autocad doesn't seem to have the exact command for filling drawing attribute. (command list)

So looks like you need to fill the attributes programatically using the COM API.

The following question appears to be relevant with yours and the accepted answers does provide a sample code:

Is it possible to edit block attributes in AutoCAD using Autodesk.AutoCAD.Interop?

Note that in that question the asker was developing a standalone application in C# .NET, where as you will be using VB Automation from MS Access. Shouldn't be too different since the Component Object Model (COM) being used is the same.

What are the commands to control the sheet set manager and change data and can the .dst file be edited without even opening up autocad?

(sorry can't post more than 2 links)

docs.autodesk.com/ACD/2010/ENU/AutoCAD%202010%20User%20Documentation/files/WS1a9193826455f5ffa23ce210c4a30acaf-7470.htm

No mention about data change, though.

is there a list of all available autocad vba commands and functions?

Yes.
%ProgramFiles%\Common Files\Autodesk Shared\acad_aag.chm - Developer's Guide %ProgramFiles%\Common Files\Autodesk Shared\acadauto.chm - Reference Guide

Online version:

help.autodesk.com/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-36BF58F3-537D-4B59-BEFE-2D0FEF5A4443.htm

help.autodesk.com/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-5D302758-ED3F-4062-A254-FB57BAB01C44.htm

More references here: http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112

Upvotes: 1

mohnston
mohnston

Reputation: 749

If you are declaring CadApp as AcadApplication you must have added a reference to AutoCAD. That means you should be able to see the object model using your Object Browser in your VBA IDE. No? There is also a very helpful site www.theswamp.org which has a whole section devoted to AutoCAD VBA.

Upvotes: 1

Related Questions