Error on create table style

Error on create table style

Anonymous
Not applicable
737 Views
3 Replies
Message 1 of 4

Error on create table style

Anonymous
Not applicable

Hi,

I am working on a code for AutoCAD 2015 using VS 2010 with VB.NET.

I complile with Framework 4.0 and I use the ObjectARX2014 for x64.

 

My code is to make a table with some data from a listview. It works just fine but I want to make a new custom table style to use it for my table.

 Using the code that I found on Kean Walmsley blog, it works fine but I face with a problem: I want to use also the Title - first row of table with the cells unmerged.

As default, Title have option "Merge cells on row/column creation" cheked and I do not know how to uncheck this by code.

 

Digging on internet I see what my code it is not exaclty as should be, I mean define of the table style variable is not declared ok.

 

On Kean Walmsley blog "ts" is defined like this.

 

 TableStyle ts = new TableStyle();

 That means in VB.NET this is equal to 

 

 

Dim ts As New AcadTableStyle

 

Then I have the option to uncheck the merge of title with command:

 

 

ts.EnableMergeAll("Title", False)

It compiles just fine but when I run it in AutoCAD I get this error that reffer to line: Dim ts As New AcadTableStyle

 

 

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {29B93DFA-D02F-4A09-852F-E6B99E0BBAC2} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

 

I look on my computer registry and it seems it reffer to AcadTableStyle registry but more than this I do not understand.

 

After this, I define the table style "ts" as:

 

 

Dim ts As New Autodesk.AutoCAD.DatabaseServices.TableStyle

And the code is compile ok, I do not get any error, table is created with all other settings ( color, thickness, etc ) but in this case I do not know the command to uncheck merge of the Title, because the properties tree is changed now ( are not same class of commands ) and the Title - first row - appear merged.

 

I tried also to suppres the Title but still not any result.

 

 Does anyone can help me to make the table style  with the Title unmerged ?

 

 My all code is this one:

 

 Private Sub btnTabel_Click(sender As System.Object, e As System.EventArgs) Handles btnTabel.Click
        'CONFIGURAREA STILULUI PENTRU TABEL.

        If drawings_type = 1 Then 'daca este sistemul de ventilatie
            scale_drawing = Val(Microsoft.VisualBasic.Right(scale_dwg, 3))
            lblText_heigh.Text = "Cell height= " & scale_drawing * 2.5
            Dim table_rows As Integer
            table_rows = lstName.Items.Count
            Dim table_str(table_rows, 9) As String
            Dim table_row As Integer
            Dim table_col As Integer
            Dim table_row_real As Integer
            Dim checkpoint As Integer

            Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim acDb As Database = acDoc.Database
            Dim acEd As Editor = acDoc.Editor
            Using acLckDoc As DocumentLock = acDoc.LockDocument()
                Using acTable_tran As Transaction = acDoc.TransactionManager.StartTransaction
                    Dim acPoint As PromptPointResult = acEd.GetPoint("\nPick table insertion point. ")
                    If acPoint.Status = PromptStatus.OK Then
                        Dim styleName = "MT-tablestyle"
                        Dim tsId As ObjectId = Nothing
                        Dim sd As DBDictionary = acTable_tran.GetObject(acDb.TableStyleDictionaryId, OpenMode.ForRead)
                        If sd.Contains(styleName) Then
                            tsId = sd.GetAt(styleName)
                        Else
                            Dim ts As New AcadTableStyle
                            'Dim ts As New Autodesk.AutoCAD.DatabaseServices.TableStyle
                           

                            'ts.IsHeaderSuppressed = True
                            'ts.IsHeaderSuppressed = True
                            'ts.SetBackgroundColor(Color.FromColorIndex(ColorMethod.ByAci, 1), RowType.DataRow)
                            'ts.HeaderSuppressed = True
                            'ts.TitleSuppressed = True

                            ts.EnableMergeAll("Title", False)
                            'ts.SetCellClass(RowType.DataRow, MergeCellStyleOption.None)
                            'ts.SetCellClass(RowType.HeaderRow, MergeCellStyleOption.None)
                            sd.UpgradeOpen()
                            tsId = sd.SetAt(styleName, ts)
                            acTable_tran.AddNewlyCreatedDBObject(ts, True)
                        End If
                        Dim acBlockTbl As BlockTable = acTable_tran.GetObject(acDoc.Database.BlockTableId, OpenMode.ForRead)
                        Dim acBlockRec As BlockTableRecord = acTable_tran.GetObject(acBlockTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                        Dim acTable As Table = New Table
                        table_rows = lstName.Items.Count
                        table_str(0, 0) = " TANK NO."
                        table_str(0, 1) = " VENT NO."
                        table_str(0, 2) = " FILL DIM"
                        table_str(0, 3) = " VENT DIM"
                        table_str(0, 4) = " MATERIAL"
                        table_str(0, 5) = " TO DECK"
                        table_str(0, 6) = " TYPE"
                        table_str(0, 7) = " REMARK"
                        table_str(0, 8) = " FLUID"
                        table_row_real = table_rows
                        checkpoint = 0
                        For table_row = 1 To table_rows
                            If table_row + checkpoint = table_rows Then
                                table_str(table_row, 0) = lstName.Items(table_row - 1 + checkpoint).SubItems(0).Text
                                table_str(table_row, 1) = lstName.Items(table_row - 1 + checkpoint).SubItems(1).Text
                                table_str(table_row, 2) = "-"
                                table_str(table_row, 3) = lstName.Items(table_row - 1 + checkpoint).SubItems(3).Text
                                table_str(table_row, 4) = "-"
                                table_str(table_row, 5) = "-"
                                table_str(table_row, 6) = lstName.Items(table_row - 1 + checkpoint).SubItems(2).Text
                                table_str(table_row, 7) = lstName.Items(table_row - 1 + checkpoint).SubItems(7).Text
                                table_str(table_row, 8) = lstName.Items(table_row - 1 + checkpoint).SubItems(5).Text
                                GoTo last_line
                            End If
                            If table_row + checkpoint > table_rows Then
                                GoTo last_line
                            End If
                            If lstName.Items(table_row - 1 + checkpoint).SubItems(0).Text.ToString = lstName.Items(table_row + checkpoint).SubItems(0).Text.ToString Then
                                table_str(table_row, 0) = lstName.Items(table_row - 1 + checkpoint).SubItems(0).Text
                                table_str(table_row, 1) = lstName.Items(table_row - 1 + checkpoint).SubItems(1).Text & "/" & lstName.Items(table_row + checkpoint).SubItems(1).Text
                                table_str(table_row, 2) = "-"
                                table_str(table_row, 3) = lstName.Items(table_row - 1 + checkpoint).SubItems(3).Text
                                table_str(table_row, 4) = "-"
                                table_str(table_row, 5) = "-"
                                table_str(table_row, 6) = lstName.Items(table_row - 1 + checkpoint).SubItems(2).Text
                                table_str(table_row, 7) = lstName.Items(table_row - 1 + checkpoint).SubItems(7).Text
                                table_str(table_row, 8) = lstName.Items(table_row - 1 + checkpoint).SubItems(5).Text
                                table_row_real = table_row_real - 1
                                checkpoint = checkpoint + 1
                            Else
                                table_str(table_row, 0) = lstName.Items(table_row - 1 + checkpoint).SubItems(0).Text
                                table_str(table_row, 1) = lstName.Items(table_row - 1 + checkpoint).SubItems(1).Text
                                table_str(table_row, 2) = "-"
                                table_str(table_row, 3) = lstName.Items(table_row - 1 + checkpoint).SubItems(3).Text
                                table_str(table_row, 4) = "-"
                                table_str(table_row, 5) = "-"
                                table_str(table_row, 6) = lstName.Items(table_row - 1 + checkpoint).SubItems(2).Text
                                table_str(table_row, 7) = lstName.Items(table_row - 1 + checkpoint).SubItems(7).Text
                                table_str(table_row, 8) = lstName.Items(table_row - 1 + checkpoint).SubItems(5).Text
                            End If
                        Next
last_line:

                        acTable.TableStyle = tsId
                        acTable.SetSize(table_row_real + 1, 9)
                        acTable.SetRowHeight(2.5 * scale_drawing)
                        acTable.SetColumnWidth(17.5 * scale_drawing)
                        acTable.Position = acPoint.Value
                        For table_row = 0 To table_row_real
                            For table_col = 0 To 8
                                acTable.Cells(table_row, table_col).TextString = table_str(table_row, table_col)
                                acTable.Cells(table_row, table_col).TextHeight = 1.5 * scale_drawing
                                acTable.Cells(table_row, table_col).Alignment = CellAlignment.MiddleCenter
                            Next
                        Next

                        acTable.GenerateLayout()

                        acBlockRec.AppendEntity(acTable)
                        acTable_tran.AddNewlyCreatedDBObject(acTable, True)
                        acTable_tran.Commit()

                    End If
                End Using
            End Using
        End If

 

 

0 Likes
738 Views
3 Replies
Replies (3)
Message 2 of 4

kean_walmsley
Autodesk
Autodesk

Hi Daniel,

 

Here's some C# code I put together to take an existing table and unmerge the title row. (It could easily be changed to unmerge a user-selected cell range, of course.)

 

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

 

namespace TableStyleEditing

{

  public class Commands

  {

    [CommandMethod("UTT")]

    public void UnmergeTableTitle()

    {

      var doc = Application.DocumentManager.MdiActiveDocument;

      if (doc == null) return;

      var ed = doc.Editor;

 

      // Select a table

 

      var peo = new PromptEntityOptions("\nSelect table");

      peo.SetRejectMessage("\nMust be a table.");

      peo.AddAllowedClass(typeof(Table), false);

      var per = ed.GetEntity(peo);

      if (per.Status != PromptStatus.OK)

        return;

 

      using (var tr = doc.TransactionManager.StartTransaction())

      {

        var table = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Table;

        if (table != null)

        {

          // Get the first row

 

          var row = table.Rows[0];

 

          // If it is merged, unmerge it

 

          if (row.IsMerged.HasValue && row.IsMerged.Value)

          {

            table.UnmergeCells(row);

            ed.WriteMessage("\nUnmerged first row.");

          }

          else

          {

            ed.WriteMessage("\nFirst row is not merged.");

          }

        }

        tr.Commit();

      }

    }

  }

}

 

Kean

 



Kean Walmsley

Platform Architect & Evangelist, Autodesk Research

Blog | Twitter
0 Likes
Message 3 of 4

Anonymous
Not applicable

Thanks Kean.

Idea to have this inside of a Table style, not as a command, is because this API that I develop will be used by all people from my department that are using Autocad and that people are not so skiled to do this things for this I made my little program just to help them to not make mistakes and make tings correct.

 

0 Likes
Message 4 of 4

kean_walmsley
Autodesk
Autodesk

Of course: you should take the code that modifies the table and integrate it where it makes most sense...

 

Kean



Kean Walmsley

Platform Architect & Evangelist, Autodesk Research

Blog | Twitter
0 Likes