Jump to content

Autocad and Access


AKing

Recommended Posts

I'm trying to use automation of Autocad through MSAccess to get attributes out of a cad drawing. The code worked fine when I typed it up in Autocad, but when I moved it over to MSAccess it keeps crashing Access. Yes, I included the reference file.

Here's what it looks like in Autocad:

    Dim blkTitle As AcadBlockReference
   
   For Each oEnt In ThisDrawing.ModelSpace
       If oEnt.EntityType = acBlockReference Then
           If oEnt.HasAttributes Then
               If oEnt.Name = "TITLE" Then
                   Set blkTitle = oEnt
                   If blkTitle.HasAttributes Then
                       aryAttributes = blkTitle.GetAttributes
                       MsgBox aryAttributes(0).TextString
                   End If
               End If
           End If
       End If
   Next oEnt

Here it is in Access:

    Dim oACAD As AutoCAD.AcadApplication
   Dim oDoc As AutoCAD.AcadDocument

   Set oACAD = New AutoCAD.AcadApplication  'Start automation of Acad
   Set oDoc = oACAD.Documents.Open(FilepathInput, False)

   Dim blkTitle As AcadBlockReference
   For Each oEnt In oDoc.ModelSpace
       If oEnt.EntityType = acBlockReference Then
           If oEnt.HasAttributes Then
               If oEnt.Name = "TITLE" Then
                   Set blkTitle = oEnt
                   If blkTitle.HasAttributes Then
                       aryAttributes = blkTitle.GetAttributes
                       MsgBox aryAttributes(0).TextString 'Crashes HERE
                   End If
               End If
           End If
       End If
   Next oEnt

I was wondering if anyone might have any insight into why it would be crashing? I don't get any error messages or anything, just a Send Error Report dialog. Could it have to do with the fact the objects in the Array are typed AcadAttributeReference? Thanks for any help you can provide.

 

Ps. Running Autocad 2008

Link to comment
Share on other sites

You can do it with late or early binding

Here is an quick example of early binding

to AutoCAD application almost not tested

You can see more about late binding on

http://www.excelguru.ca/node/10

and use the same method for AuitoCAD

Make sure that in Acces is set reference

to your current AutoCAD version

Add this code in Access module

Hope this will be works in A2008 also

 

~'J'~

 

Option Compare Text
Option Explicit
Public fullName As String

'//==================//'
Public Sub TitleToAccess()

Dim acApp As AcadApplication
Dim aDoc As AcadDocument
Dim oEnt As AcadEntity
Dim blkTitle As AcadBlockReference
Dim oAttRef As AcadAttributeReference
Dim bnameStr As String
Dim aryAttributes As Variant
Dim ftype(0) As Integer
Dim fdata(0) As Variant
Dim fcode As Variant
Dim fvalue As Variant
Dim i As Integer, j As Integer
Dim info() As String
MsgBox "Be patience...AutoCAD will be" & vbCr & _
       "closed automatically"
fullName = "C:\MyAccess\MyBlocks.dwg" '<-- change the drawing name here
Set acApp = CreateObject("AutoCAD.Application")
Set aDoc = acApp.Documents.Open(fullName)
acApp.Visible = True

   For Each oEnt In aDoc.ModelSpace
       If TypeOf oEnt Is AcadBlockReference Then
       Set blkTitle = oEnt
           If blkTitle.Name = "TITLE" And blkTitle.HasAttributes Then
                       aryAttributes = blkTitle.GetAttributes
                       For i = LBound(aryAttributes) To UBound(aryAttributes)
                       Set oAttRef = aryAttributes(i)
                       ReDim Preserve info(j)
                       info(j) = "Tag: " & oAttRef.TagString & vbCr & "Value: " & oAttRef.TextString
                       j = j + 1
                       Next i
                   End If
               End If
   Next oEnt
   
aDoc.Close 'close w/o changes
acApp.Quit

Set aDoc = Nothing
Set acApp = Nothing
For i = LBound(info) To UBound(info)
MsgBox info(i)
Next

End Sub

Link to comment
Share on other sites

  • 5 years later...

Can you help me with this code to write data from access form to autocad block attributes. Sorry but i have no programing skills.

Link to comment
Share on other sites

Welcome to CT. Firstly, we need to know a few stuff: Which version of Access (some newer versions don't come standard with VBA as per below, so it might need to be translated to VB.Net)? Are you sure you want to do it from the Access side, or would you be willing to have AutoCAD drive the data inside the MDB file (this is a lot simpler to do and might even work faster)?

 

Will both ACad and Access always be on the same PC? Do you want to modify DWG files without ACad? Or do you want to modify data inside MDB without Access?

Link to comment
Share on other sites

I am using autocad 2008 and access 2007, they will always be on on the same pc , i do not intend to modify, if it's easier i wold prefer to brig data from one access form to dwg custom properties or SSM properties then link that data via fields used in title block.

I found a code that works in access and sends data to a word template....http://www.techrepublic.com/blog/msoffice/how-do-i-fill-word-form-fields-with-access-data/164......i want to send data to autocad fields or block attributes.

Link to comment
Share on other sites

i dont intend to modify data without these softwares.....sorry for my english. I want to bring data from one access form to autocad block attributes or dwg's custom properties or ssm properties.

Link to comment
Share on other sites

Not too sure that Access 2007 has VBA, I know I had huge issues with getting a VBA tool I wrote for Outlook 2003 to run in 2007, even after installing MSO's VBA addon-pack.

 

I've not done any programming in DotNet for Access, so I'm not too clued up about that. I have done something similar to the OP's stuff in Access 2003 (though I have to tell you it ran sloooooowwwwwwlyyyy). I ended up rather doing it from AutoCAD using ADOLisp to link to the MDB file - worked 10's of times faster. There are quite a few threads about linking to databases using Lisp, ADOLisp's actually got sample code for linking to MDB files, though it can link to any DB which can be connected to through ADO/ODBC.

 

You might want to look at this thread: http://www.theswamp.org/index.php?topic=42014.0

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...