TheSwamp
Code Red => .NET => Topic started by: Martin_2 on January 23, 2007, 05:01:45 PM
-
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
-
Martin_2
Would you please Zip the Solution and post/attach the Zip .. to make it easier for any VB.NET users to understand your problem.
Have you used selectionSets in the NET API ?
-
Welcome to the swamp Martin. I really don’t do VB that well but… :ugly:
I could be wrong on this, but I don’t think you can access Cad’s .NET API without loading your assembly via netload,
if you are working out of process you’re stuck with using COM.
Dan
-
Hi again
Thank you for replying.
I'm not entirely sure of what you are looking for. Please let me know if the attachments are not sufficient.
I have attached the file ClassLibrary1.zip, which is the entire Visual Studio 2005 project folder for lab3.
The project has been build, so ClassLibrary1.dll should be in ClassLibrary1\bin\release
Type NETLOAD, in AutoCAD 2007 to load the dll.
Type create, to run the program.
Zoom to extend, to show the circle.
This is the program I tried to integrate into the program that open a specific AutoCAD file.
The 'AutoCAD open' program has been attached as WindowsApplication4.zip
The zip file contains the entire Visual Studio 2005 project folder, for WindowsApplication4.
To run the program, open the project from Visual Studio 2005. Add the path to a dwg file, in the code (at the commented position), build and run debug (F5).
Press the button and AutoCAD should open with the file you have specified.
With regards to 'selectionSets', I don't know what it is.
How does it work?
Best Regards
Martin
Martin_2
Would you please Zip the Solution and post/attach the Zip .. to make it easier for any VB.NET users to understand your problem.
Have you used selectionSets in the NET API ?