PDA

View Full Version : AutoCAD Custom Exporter using .NET



dtownsend
2007-06-22, 10:11 PM
With the .NET API for AutoCAD can DWG data be exported? Say I wanted to export all the data in a DWG file to an XML file could that be done?

Thanks

Ed Jobe
2007-06-25, 02:07 PM
,Net has xml support built in. What data you want is not clear however.

fixo
2007-06-25, 09:33 PM
With the .NET API for AutoCAD can DWG data be exported? Say I wanted to export all the data in a DWG file to an XML file could that be done?

Thanks

I found this code somewhere on www.codeproject.com
just slightly rewrote it, change them to your needs



Imports System
Imports System.IO
Imports System.Xml
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports acadApp = Autodesk.AutoCAD.ApplicationServices.Application
Namespace XMLUtils
Public Class DrawingToXML
Dim xdoc As New XmlDocument
Dim xroot As XmlElement

<CommandMethod("WXML")> _
Public Sub GetObjects()
Dim doc As Document = acadApp.DocumentManager.MdiActiveDocument
Dim fname As String = doc.Name
MsgBox(fname)
Dim tr As Transaction = doc.Database.TransactionManager.StartTransaction
Dim ed As Editor = doc.Editor
Dim res As PromptSelectionResult
Dim osset As SelectionSet
Dim ObjId As ObjectId
Dim ObjIdColl() As ObjectId

Try
res = ed.SelectAll()
If res.Status <> PromptStatus.OK Then
Return
End If

osset = res.Value
ObjIdColl = osset.GetObjectIds
xdoc.RemoveAll()
xdoc.AppendChild(xdoc.CreateProcessingInstruction("xml", "version='1.0'"))
xroot = xdoc.CreateElement("DrawingRoot")
xroot.SetAttribute("label", fname)
xroot.SetAttribute("description", "Drawing info for " & fname)
xroot.SetAttribute("revisiondata", "Data " & Date.Now.ToString)
xdoc.AppendChild(xroot)

Dim cnt As Integer = 0
For Each ObjId In ObjIdColl

Dim ent As Entity = CType(tr.GetObject(ObjId, OpenMode.ForRead), Entity)

Dim xsingle As XmlElement
xsingle = xdoc.CreateElement("Object")
xsingle.SetAttribute("label", ent.GetType().ToString)
xsingle.SetAttribute("handle", ent.Handle().ToString)
xsingle.SetAttribute("layer", ent.Layer().ToString)
If Not IsNothing(ent.Linetype) Then
xsingle.SetAttribute("linetype", ent.Linetype().ToString)
End If
If Not IsNothing(ent.LineWeight) Then
xsingle.SetAttribute("lineweight", ent.LineWeight().ToString)
End If
If Not IsNothing(ent.ColorIndex) Then
xsingle.SetAttribute("colorIndex", ent.ColorIndex().ToString)
End If
xroot.AppendChild(xsingle)
Next ObjId
xdoc.Save("structure_of_" & (fname.LastIndexOf("\") + 1) & ".xml")
MsgBox("Search for created XML file in this project bin\Release\ folder")
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
tr.Abort()
Finally
tr = Nothing
End Try

End Sub

End Class
End Namespace

dtownsend
2007-07-07, 03:12 AM
Thanks! That worked like magic.

The only problem I am having with the API is when I try to run this in debug mode it dosen't find the command, but when I open autocad and run the netload command it works perfect. I have the problem with all .NET API examples I have tried. I am using AutoCAD 2008. Has anyone had this problem? It's not a big deal, but it would be nice to set some breaks, so I can get a better understand of how this works.

Thanks again!

dtownsend
2007-07-07, 03:17 AM
,Net has xml support built in. What data you want is not clear however.I would like to get any and all data that is able to come from the API.

Basically I am working with over 40 different CAD files from several (5 or so) different sub contractors and I would like to learn more about what kind of data is in the CAD files via the API and how I could come up with some sort of logic that would pull what I need from the files and rebuild the geometry from the CAD files back to native DWG and/or to another format. So overall increase performance and try to build better interoperability into our processes. And also for some other ideas that I have for the data that I'd like to play around with...

fixo
2007-07-07, 06:02 AM
Hi Dustin,
I so sorrowed about I can't help you with it
I am just a code hacker and nothing else
and with my poor english level I couldn't to
explain you something also
Try to start the new thread with this
question

Sorry,

~'J'~