Author Topic: Adding a row to a table  (Read 11553 times)

0 Members and 1 Guest are viewing this topic.

Joel Roderick

  • Guest
Adding a row to a table
« on: March 14, 2006, 06:35:15 PM »
Haven't posted here in a while, and I am new to .net...
I am trying to add a row to an existing table.  I am trying to not use the com interop (my crutch).

I am not sure if I am using transactions properly because I keep crashing AutoCAD when I try to open the object for write.  If I open the object for read, it doesn't crash.  Any help is much appreciated...

Here is what I have so far:

Code: [Select]
Dim editor As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim pOptions As PromptEntityOptions = New PromptEntityOptions("Select a table: ")
Dim pResults As PromptEntityResult = editor.GetEntity(pOptions)
Dim tbl As Table
Dim trans As Transaction
trans = HostApplicationServices.WorkingDatabase().TransactionManager.StartTransaction()
tbl = trans.GetObject(pResults.ObjectId, OpenMode.ForWrite)    '<---- ****crashes here****
tbl.InsertRows(1, 1.0, 1)
trans.Commit()
trans.Dispose()

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Adding a row to a table
« Reply #1 on: March 14, 2006, 07:26:04 PM »
It may be a casting thing, do you cast in vb?(maybe pResults.ObjectId as Table or similar??)
IOW, it's trying to turn an Object into a Table without seeing if it fits.
This is only a guess and maybe something to work with 'till someone has a better idea.
hth.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding a row to a table
« Reply #2 on: March 14, 2006, 09:10:51 PM »
or possibly something like

Table tbl = (Table)trans.GetObject(pResults.ObjectId, OpenMode.ForWrite);


aside;
Great to see you here Joel. We are still in our infancy as a group, and possibly won't be able to solve all issues.
Hope you Enjoy the site.


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Draftek

  • Guest
Re: Adding a row to a table
« Reply #3 on: March 15, 2006, 08:01:29 AM »
In addition, you may need to lock the document depending on your command type flags or the mode your command runs in (like modelss or inside events).

The document must always be locked before modification of entities.

Joel Roderick

  • Guest
Re: Adding a row to a table
« Reply #4 on: March 15, 2006, 09:11:09 AM »
Thanks Draftek!

Looks like locking the document worked.  Now that I am passed that hurdle, let's see what other damage I can do!

Here is the updated code:

Code: [Select]
Dim editor As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim pOptions As PromptEntityOptions = New PromptEntityOptions("Select a table: ")
Dim pResults As PromptEntityResult = editor.GetEntity(pOptions)
Dim tbl As Table
Dim trans As Transaction
trans = HostApplicationServices.WorkingDatabase().TransactionManager.StartTransaction()
editor.Document.LockDocument()
tbl = trans.GetObject(pResults.ObjectId, OpenMode.ForWrite)
tbl.InsertRows(1, 1.0, 1)
trans.Commit()
trans.Dispose()

LE

  • Guest
Re: Adding a row to a table
« Reply #5 on: March 15, 2006, 09:28:08 AM »
Nothing related, but maybe useful and not mine [no net yet]

Code: [Select]
<CommandMethod("MyTable")> _
  Public Shared Sub MyTableCmd()
        Dim mTable As Table
        Dim rowt As RowType
        Dim bt As BlockTable
        Dim btr As BlockTableRecord
        Try
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim objId As ObjectId = db.BlockTableId
            bt = objId.Open(OpenMode.ForRead)
            objId = bt.Item(btr.ModelSpace)
            btr = objId.Open(OpenMode.ForWrite)
            mTable = New Table()
            btr.AppendEntity(mTable)
            btr.Close()
            bt.Close()
            mTable.InsertColumns(0, 12, 1)
            mTable.InsertColumns(1, 40, 1)
            mTable.InsertColumns(2, 40, 1)
            mTable.InsertColumns(3, 40, 1)
            mTable.InsertColumns(4, 16, 1)
            mTable.InsertColumns(5, 30, 1)

            mTable.InsertRows(0, 8, 10)

            mTable.SetTextHeight(3, rowt.DataRow)
            mTable.SetTextString(0, 0, "TEST")
            mTable.SetTextString(0, 1, "TEST")
            mTable.SetTextString(0, 2, "TEST")
            mTable.SetTextString(0, 3, "TEST")
            mTable.SetTextString(0, 4, "TEST")
            mTable.SetTextString(0, 5, "TEST")

            mTable.SetTextString(1, 0, "TEST")
            mTable.SetTextString(1, 1, "TEST")
            mTable.SetTextString(1, 2, "TEST")
            mTable.SetTextString(1, 3, "TEST")
            mTable.SetTextString(1, 4, "TEST")

            mTable.Position = New Point3d(180, 80, 0)
            mTable.Close()
        Catch
        Finally
        End Try
    End Sub

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding a row to a table
« Reply #6 on: March 16, 2006, 04:03:52 AM »
Joel, This works for me ..
Code: [Select]
    public class Class1
    {
        public void
       TableTest()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            // restrict selection to Table only

            PromptEntityOptions entOpt = new PromptEntityOptions("\nSelect Table: ");
            entOpt.SetRejectMessage("\nOnly Tables may be selected.");
            entOpt.AddAllowedClass(typeof(Table), true);

            //Start the selection ...

            PromptEntityResult entRes = ed.GetEntity(entOpt);

            //If a Table was successfully selected, proceed

            if (entRes.Status != PromptStatus.OK) {
                return;
            }           
            try {
                using (Transaction trans = db.TransactionManager.StartTransaction()) {
                    Table tbl = (Table)trans.GetObject(entRes.ObjectId, OpenMode.ForWrite, true);
                    tbl.InsertRows(1, 1.0, 1);
                    trans.Commit();

                    ed.WriteMessage("\nPosition : " + tbl.Position.ToString());
                    ed.WriteMessage("\nNumber of Columns : " + tbl.NumColumns.ToString());
                    ed.WriteMessage("\nNumber of Rows    : " + tbl.NumRows.ToString());
                    ed.WriteMessage("\nWidth  : " + tbl.Width.ToString());
                    ed.WriteMessage("\nHeight : " + tbl.Height.ToString());                   
                }
            } catch (Autodesk.AutoCAD.Runtime.Exception acEx) {
                ed.WriteMessage("\nError: " + acEx.Message);
                return;
            }
        }
    }

Quote
Command: Test0316
Select Table:
Nothing Selected.
Select Table:
Only Tables may be selected.
Select Table:
Position : (1107.35495576984,2.99726696006678,0)
Number of Columns : 5
Number of Rows    : 9
Width  : 317.5
Height : 83
« Last Edit: March 16, 2006, 04:07:12 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding a row to a table
« Reply #7 on: March 16, 2006, 04:19:42 AM »
Just a note :
If I try this ;
Code: [Select]
Table tbl = trans.GetObject(entRes.ObjectId, OpenMode.ForWrite, true);instead of this ;
Code: [Select]
Table tbl = (Table)trans.GetObject(entRes.ObjectId, OpenMode.ForWrite, true);I get this compile error, as I expected.
Quote
Error   1   
Cannot implicitly convert type 'Autodesk.AutoCAD.DatabaseServices.DBObject' to 'Autodesk.AutoCAD.DatabaseServices.Table'.
An explicit conversion exists (are you missing a cast?)   
:\VisualStudio2005\Projects\KDUB\theSwamp\TestTable\TestTable\Class1.cs   105   33   TestTable
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Joel Roderick

  • Guest
Re: Adding a row to a table
« Reply #8 on: March 16, 2006, 03:00:07 PM »
Luis, Kerry,
Thanks for the replies.  It actually helped me to understand more about how .net handles things vs VB/A.

I noticed that the code that Luis posted doesn't use a transaction.  I am still a little confused as to the advantages of a transaction when you just want to do something simple.  Maybe I should start another topic...


LE

  • Guest
Re: Adding a row to a table
« Reply #9 on: March 16, 2006, 03:06:37 PM »
Luis, Kerry,
Thanks for the replies.  It actually helped me to understand more about how .net handles things vs VB/A.

I noticed that the code that Luis posted doesn't use a transaction.  I am still a little confused as to the advantages of a transaction when you just want to do something simple.  Maybe I should start another topic...



Hi Joel;

The reason is that when you use transactions, you do not have to close the objects, is the same way as using smart pointers...

 :-)

But, I do not write code in C# yet... only ARX [right now I am trying to write this in ObjectARX]

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding a row to a table
« Reply #10 on: March 16, 2006, 03:17:24 PM »
Hi Joel,

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Joel Roderick

  • Guest
Re: Adding a row to a table
« Reply #11 on: March 16, 2006, 03:59:13 PM »
Thanks Kerry,
I must have read that document a dozen times and never saw that section...

LE

  • Guest
Re: Adding a row to a table
« Reply #12 on: March 16, 2006, 08:19:20 PM »
Hi Kerry;

In your TableTest() function, when you add the row(s), are they shown automatically on the selected Table?, or you have to grip the table and stretched and after that, the new row(s) are shown?

Thanks.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Adding a row to a table
« Reply #13 on: March 16, 2006, 08:50:49 PM »
Showing automagically.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: Adding a row to a table
« Reply #14 on: March 16, 2006, 08:54:03 PM »
Showing automagically.

Thanks, I did the test on arx, but have not found the method to do that magic.... part  :-(

It will add the row, but I have to select after that [grip] the Table to show the new added row.