Hi
I'm trying to program an application that will access the database in AutoCAD. The application is to extract all text fields and their coordinates (perhaps blocks and attributes as well).
I use AutoCAD 2007, VB.NET and Visual Studio 2005.
From the net, I gather that .NET is not the best way to perform inter proces programming, yet (the data will be forwarded to Excel).
Instead using COM should be the easiest way.
I have figured out how to start AutoCAD, with a specific dwg file, using COM, with the following code attached to a buttons click event (referencing AutoCAD 2007 type library and AutoCAD ObjectDBX Common 17.0 type library) :
Imports Autodesk.AutoCAD.Interop
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filepath As String
filepath = "C:\test.dwg"
OpenACADFile(filepath)
End Sub
Sub OpenACADFile(ByVal filepath As String)
Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument
On Error Resume Next
'test for existing cad application
AcadApp = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then
Err.Clear()
'opens a new application if none is available
AcadApp = CreateObject("AutoCAD.Application")
If Err.Number <> 0 Then
MsgBox("Error opening AutoCAD")
Exit Sub
End If
End If
AcadApp.WindowState = 3 'acMax
acadDoc = AcadApp.Documents.Open(filepath)
AcadApp.Application.Visible = True
'Not part of opening the file! Used to call code from lab 3.
CreateEmployee()
acadDoc = Nothing
AcadApp = Nothing
End Sub
Working with the AutoCAD database should be possible using VB.NET, however.
I would like to access the database of the file which has been opened, with the code above.
Autodesk has some labs showing how to access the database, using VB.NET
(
http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627)
However, the labs only operate with the <CommandMethod("name"")>
method which requires the building of a dll and use of NETLOAD from
within AutoCAD.
I tried adding an example from the labs, to my code, but it does not seem to
work (no circle is added). The code works with <CommandMethod>.
What I did, was to add references to acdbmgd.dll and acmgd.dll.
Furthermore I added the following imports (as in lab 3):
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
The code, called from the sub above was:
Public Function CreateEmployee()
Dim db As Database = HostApplicationServices.WorkingDatabase()
Dim trans As Transaction = db.TransactionManager.StartTransaction()
Try
'First declare the objects that we will use
Dim circle As Circle 'This will be the circle we add to Modelspace
Dim btr As BlockTableRecord 'To Add that circle, we must open ModelSpace up
Dim bt As BlockTable 'To open ModelSpace, we must access it through the BlockTable
'Now create the circle...look carefully at these arguments - Notice 'New' for the
'Point3d and the static ZAxis
circle = New Circle(New Point3d(10, 10, 0), Vector3d.ZAxis, 2.0)
'We need to get object references for the BlockTable and ModelSpace:
'Notice here that we obtain them with the transactions GetObject member!
bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
'Now, we declare an ID to represent the ModelSpace block table record...
'Dim btrId As ObjectId = bt.Item(BlockTableRecord.ModelSpace)
'and use it to obtain the object reference - notice we open for Write
btr = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
'Now, use the btr reference to add our circle
btr.AppendEntity(circle)
trans.AddNewlyCreatedDBObject(circle, True) 'and make sure that the transaction knows about it
trans.Commit() 'Once we're done, we commit the transaction, so that all our changes are saved...
Catch
MsgBox("Error handling")
Finally
trans.Dispose() '... and dispose of the transaction, since we're all done...
'...(this is not a DB-resident object)
End Try
Return Nothing
End Function
Does anyone know how to extract all text fields from a dwg file, using .NET?
Does COM and .NET mix, as I tried?
Any help would be much appreciated (sorry about the long post).
Best regards
Martin
edit: added code tags Jonesy