Author Topic: Datatable to MText (VB.Net)  (Read 3135 times)

0 Members and 1 Guest are viewing this topic.

TREEFINGERS

  • Guest
Datatable to MText (VB.Net)
« on: September 04, 2012, 09:27:38 PM »
Hello,

I've been a lurker for a while and have learned so much from all of your contributions. First let me say thanks! Now, I'd just like to share. I made a short example here of converting a datatable to mtext, which for me will be invaluable, as you can basically insert sql data directly into the drawing. I'm putting it here for any changes or comments you have, or maybe someone might find it useful.

I borrowed some of the mtext code from Kean here: http://through-the-interface.typepad.com/through_the_interface/2010/06/creating-an-autocad-mtext-object-containing-different-colours-using-net.html

I had to use the stringbuilder to convert the datatable to something mtext could handle. Perhaps there's a better way?

Any ideas on formatting columns?

Code - vb.net: [Select]
  1. Imports System.Text
  2. Imports Autodesk.AutoCAD.Runtime
  3. Imports Autodesk.AutoCAD.ApplicationServices
  4. Imports Autodesk.AutoCAD.DatabaseServices
  5. Imports Autodesk.AutoCAD.Geometry
  6. Imports Autodesk.AutoCAD.EditorInput
  7.  
  8. <Assembly: CommandClass(GetType(MTextFromDataTable.MyCommands))>
  9.  
  10. Namespace MTextFromDataTable
  11.     Public Class MyCommands
  12.         <CommandMethod("MTFDT")> Public Sub MTFDT()
  13.             Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  14.             Dim db As Database = doc.Database
  15.             Dim ed As Editor = doc.Editor
  16.  
  17.             Dim table As New System.Data.DataTable
  18.  
  19.             table.Columns.Add("Dosage", GetType(Integer))
  20.             table.Columns.Add("Drug", GetType(String))
  21.             table.Columns.Add("Patient", GetType(String))
  22.             table.Columns.Add("Date", GetType(DateTime))
  23.             table.Rows.Add(25, "Indocin", "David", DateTime.Now)
  24.             table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
  25.             table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
  26.             table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
  27.             table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
  28.  
  29.             Dim ppo As New PromptPointOptions(vbCrLf & "Insertion Point")
  30.             Dim ppr As PromptPointResult
  31.             ppr = ed.GetPoint(ppo)
  32.             If ppr.Status <> PromptStatus.OK Then
  33.                 ed.WriteMessage(vbCrLf & "Cancelled.")
  34.                 Exit Sub
  35.             End If
  36.  
  37.             Dim insertPt As Point3d = ppr.Value
  38.  
  39.             Using tr As Transaction = db.TransactionManager.StartTransaction
  40.                 Dim mt As New MText
  41.                 Dim mtid As ObjectId
  42.                 mt.Location = insertPt
  43.                 Dim sb As New StringBuilder()
  44.                 For i = 0 To table.Rows.Count - 1
  45.                     sb.AppendLine(table.Rows(i).Item(0) & ControlChars.Tab & table.Rows(i).Item(1) & ControlChars.Tab & table.Rows(i).Item(2) & ControlChars.Tab & table.Rows(i).Item(3))
  46.                 Next
  47.                 mt.Contents = sb.ToString
  48.                 Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
  49.                 Dim ms As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
  50.                 mtid = ms.AppendEntity(mt)
  51.                 tr.AddNewlyCreatedDBObject(mt, True)
  52.                 tr.Commit()
  53.             End Using
  54.         End Sub
  55.     End Class
  56. End Namespace

edit:kdub code=vbnet
« Last Edit: September 04, 2012, 09:35:37 PM by Kerry »

TheMaster

  • Guest
Re: Datatable to MText (VB.Net)
« Reply #1 on: September 05, 2012, 08:37:57 AM »
<snip>

Any ideas on formatting columns?


How about using an AutoCAD TABLE ?

If you can get your data into any format supported by AutoCAD's Excel data linking mechanism (e.g., .xls, xlsx, or .csv),  you're pretty much done cause AutoCAD will do rest of the work for you.

I would just output a .CSV file which is really easy to do, and then use AutoCAD's TABLE command to create a table from a data link. That also has the benefit of automatically updating the table when the source data file changes.


fixo

  • Guest
Re: Datatable to MText (VB.Net)
« Reply #2 on: September 05, 2012, 11:41:49 AM »
I'm using multicolumns not often, perhaps
something like this should work for you
Code - vb.net: [Select]
  1.    <CommandMethod("MTFD")> _
  2.         Public Sub testMTX()
  3.             Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  4.             Dim db As Database = doc.Database
  5.             Dim ed As Editor = doc.Editor
  6.             Try
  7.                 Dim table As New System.Data.DataTable
  8.                 table.BeginInit()
  9.                 table.BeginLoadData()
  10.                 table.Columns.Add("Dosage", GetType(Integer))
  11.                 table.Columns.Add("Drug", GetType(String))
  12.                 table.Columns.Add("Patient", GetType(String))
  13.                 table.Columns.Add("Date", GetType(DateTime))
  14.                 table.Rows.Add(25, "Indocin", "David", DateTime.Now)
  15.                 table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
  16.                 table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
  17.                 table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
  18.                 table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
  19.                 table.EndInit()
  20.                 table.AcceptChanges()
  21.                 table.EndLoadData()
  22.                
  23.                 Dim ppo As New PromptPointOptions(vbCrLf & "Insertion Point")
  24.                 Dim ppr As PromptPointResult
  25.                 ppr = ed.GetPoint(ppo)
  26.                 If ppr.Status <> PromptStatus.OK Then
  27.                     ed.WriteMessage(vbCrLf & "Cancelled.")
  28.                     Return
  29.                 End If
  30.  
  31.                 Dim insertPt As Point3d = ppr.Value
  32.  
  33.                 Using tr As Transaction = db.TransactionManager.StartTransaction
  34.                     Dim mt As New MText
  35.                     Dim mtid As ObjectId
  36.                     mt.Location = insertPt
  37.                     Dim sb As New StringBuilder()
  38.                     Dim gap As Double = db.Textsize
  39.                     For i = 0 To table.Rows.Count - 1
  40.                         sb.AppendLine(table.Rows(i).Item(0).ToString() & vbTab & table.Rows(i).Item(1).ToString() & vbTab & table.Rows(i).Item(2).ToString().PadRight(12) & vbTab & table.Rows(i).Item(3).ToString() & "\P")
  41.                     Next
  42.  
  43.                     mt.Contents = sb.ToString().TrimEnd("\P")
  44.  
  45.                     mt.SetStaticColumns(mt.ActualWidth / 4, gap, 4)
  46.  
  47.                     mt.Height = gap * table.Rows.Count * 3
  48.                     Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
  49.                     Dim ms As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
  50.                     mtid = ms.AppendEntity(mt)
  51.                     tr.AddNewlyCreatedDBObject(mt, True)
  52.                     tr.Commit()
  53.                 End Using
  54.             Catch ex As System.Exception
  55.                 ed.WriteMessage(vbLf & ex.StackTrace)
  56.             Finally
  57.                 'do nothing
  58.             End Try
  59.         End Sub

edit:kdub code=vbnet
« Last Edit: September 05, 2012, 08:38:11 PM by Kerry »