TheSwamp

Code Red => .NET => Topic started by: Andrey Bushman on July 22, 2010, 10:59:49 AM

Title: Cell styles in TableStyle...
Post by: Andrey Bushman on July 22, 2010, 10:59:49 AM
AutoCAD's table style has collection of cell styles (default it's are 'Title', 'Header' and 'Data').

How can I create/modify/delete custom cell style?

Class TableStyle has collection 'CellStyles', which content names of table cell styles. It's collection is ArrayList and contain method 'Add', and property 'IsReadOnly'. Property 'CellStyles' contain string objects; property 'IsReadOnly' = false, but if I add new string in 'CellStyles' - it's not happen.
Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 22, 2010, 05:21:53 PM
Perhaps you need to create the style then
Autodesk.AutoCAD.DatabaseServices.TableStyle.SetTextStyle(Autodesk.AutoCAD.DatabaseServices.ObjectId, string)
Title: Re: Cell styles in TableStyle...
Post by: fixo on July 22, 2010, 06:26:14 PM
Hi Bryco
Please, share with us your code snippet
I really have not found in docs how to create cell style
It's very urgent task fo me too
Oleg

~'J'~
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 23, 2010, 04:26:56 AM
Perhaps you need to create the style then
Autodesk.AutoCAD.DatabaseServices.TableStyle.SetTextStyle(Autodesk.AutoCAD.DatabaseServices.ObjectId, string)
At what here TextStyle? I need other.
Code: [Select]
  1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  [color=green]//Acad[/color]
   6:  using acad = Autodesk.AutoCAD.ApplicationServices.Application;
   7:  using Autodesk.AutoCAD.ApplicationServices;
   8:  using Autodesk.AutoCAD.DatabaseServices;
   9:  using Autodesk.AutoCAD.EditorInput;
  10:  using Autodesk.AutoCAD.Runtime;
  11:  using Autodesk.AutoCAD.Interop;
  12:  
  13:  namespace AcadBlockEditor
  14:  {
  15:      public class Class1
  16:      {
  17:          Document dwg;
  18:          Editor ed;
  19:          Database db;
  20:  
  21:          [CommandMethod("q1", CommandFlags.Modal)]
  22:          public void TestMethod()
  23:          {
  24:              dwg = acad.DocumentManager.MdiActiveDocument;
  25:              ed = dwg.Editor;
  26:              db = dwg.Database;
  27:              try
  28:              {
  29:                  using (Transaction tr = dwg.TransactionManager.StartTransaction())
  30:                  {
  31:                      DBDictionary tsd = (DBDictionary) tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead);
  32:                      foreach (DBDictionaryEntry item in tsd)
  33:                      {
  34:                          [color=green]//ed.WriteMessage(string.Format("{0}{1}{0}", "\n", item.Key));[/color]
  35:                          TableStyle ts = (TableStyle) tr.GetObject(item.Value, OpenMode.ForRead);
  36:                          ed.WriteMessage(string.Format("{0}Table style name: '{1}'", "\n", ts.Name));
  37:                          ed.WriteMessage(string.Format("{0}(TableStyle.CellStyles.IsReadOnly ='{1}')", "\n", ts.CellStyles.IsReadOnly));
  38:                          [color=green]//Print all cell style names of table style[/color]
  39:                          PrintCellStylesInfo(ts);
  40:                          string stName = "MyStyle";[color=green]// name for my new cell style[/color]
  41:                          lock (dwg.LockDocument())
  42:                          {
  43:                              ed.WriteMessage(string.Format("\nBefore modify: Cell Styles Count: {0}", ts.CellStyles.Count));
  44:                              ts.UpgradeOpen();                            
  45:                              ts.CellStyles.Add(stName);
  46:                              ed.WriteMessage(string.Format("\nAfter modify: Cell Styles Count: {0}\n", ts.CellStyles.Count));
  47:                              ts.SetCellClass(CellClass.Label, stName);
  48:                              tr.Commit();
  49:                          }                        
  50:                          ed.WriteMessage(string.Format("{0}{1}{0}", "\nAfter modify:", new string('*', 30)));
  51:                          [color=green]//Print all cell style names of table style[/color]
  52:                          PrintCellStylesInfo(ts);
  53:                      }                  
  54:                  }
  55:              }
  56:              catch (System.Exception e)
  57:              {
  58:                  ed.WriteMessage(e.Message);
  59:              }            
  60:          }
  61:  
  62:          private void PrintCellStylesInfo(TableStyle ts)
  63:          {
  64:              foreach (var cs in ts.CellStyles)
  65:              {
  66:                  ed.WriteMessage(string.Format("{0}CellStyle Type: '{1}'; CellStyle.ToString: {2}", "\n", cs.GetType(), cs.ToString()));
  67:              }            
  68:          }
  69:      }
  70:  }

result:

(http://habreffect.ru/files/cc6/b96d9ee49/23.07.png)
Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 24, 2010, 12:08:08 PM
if you change "Standard" + "YourTextstyleName" to what you need
this changes the styles

Code: [Select]
[CommandMethod("tts")]
        public static void TableStyleTest()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            string tableStyleName = "Standard";
            string textStyleName = "YourTextstyleName";
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable tstyles = tr.GetObject
                    (db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                if (!tstyles.Has(textStyleName)) return;
                ObjectId newId = tstyles[textStyleName];
                if (newId == ObjectId.Null) return;
 

                DBDictionary dic = tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead) as DBDictionary;
                foreach (DictionaryEntry eDict in dic)
                {
                    TableStyle tableStyle = tr.GetObject
                        ((ObjectId)eDict.Value, OpenMode.ForRead) as TableStyle;
                    if (tableStyle.Name == tableStyleName)
                    {
                        tableStyle.UpgradeOpen();
                        foreach (RowType rt in Enum.GetValues(typeof(RowType)))
                        {
                            if (rt == RowType.UnknownRow) continue;
                            //if (rt = RowType.DataRow) yadayada;
                            ed.WriteMessage("\n" + rt.ToString() + " textstyle is "
                                +(tr.GetObject(tableStyle.TextStyle(rt),OpenMode.ForRead)
                                as TextStyleTableRecord).Name);
                            ObjectId idv = tableStyle.TextStyle(rt);
                           tableStyle.SetTextStyle(newId, (int)rt);
                        }
                }

                }
                tr.Commit();
            }
           

        } // end TableStyleTest
Title: Re: Cell styles in TableStyle...
Post by: fixo on July 24, 2010, 12:47:08 PM
Dear Bryco,
Thanks for the code but it's not I wanted be
IOW I want create a TableStyle (I know that already) and
then PROGRAMMATICALLY add in there my custom cellstyle -
say, similar to how you could be add the cell style in Excel
Unfortunatelly, I can't rich at CellStyle object at all
Hope this make a sense

~'J'~
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 24, 2010, 12:57:29 PM
2 Bryco
Look my second message in this post...

I repeat:

 My question is not about TexStyle. I need to add/modify/delete CellStyle of TableStyle's object in my program code.

Probably, we did not understand each other correct.

(http://habreffect.ru/files/03e/38307853d/24.07.png)

p.s. I write code, which can serializable/deserializable TableStyle objects to/from xml format.

For example look this (http://sites.google.com/site/bushmansnetlaboratory/home/managed-autocad/stati/korzina-dla-musora) xml...


Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 24, 2010, 05:25:59 PM
I don't know much about tables but
using interop = Autodesk.AutoCAD.Interop;


interop.AcadApplication Ap = (interop.AcadApplication)acadApp.AcadApplication;
object ob = Ap.ActiveDocument.ObjectIdToObject( tableStyle.ObjectId.OldId)
 interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;
   ts.CreateCellStyle("U12");
Title: Re: Cell styles in TableStyle...
Post by: Jeff H on July 25, 2010, 12:43:32 AM
Have you tried  table.Cells[0,1].CellStyleOverrides
Title: Re: Cell styles in TableStyle...
Post by: fixo on July 25, 2010, 04:13:08 AM
Have you tried  table.Cells[0,1].CellStyleOverrides


Mate, the question is about TABLESTYLE not about TABLE... :pissed:

~'J'~
Title: Re: Cell styles in TableStyle...
Post by: fixo on July 25, 2010, 04:14:51 AM
I don't know much about tables but
using interop = Autodesk.AutoCAD.Interop;


interop.AcadApplication Ap = (interop.AcadApplication)acadApp.AcadApplication;
object ob = Ap.ActiveDocument.ObjectIdToObject( tableStyle.ObjectId.OldId)
 interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;
   ts.CreateCellStyle("U12");
Thanks Bryco,
But it is not a solution though...

~'J'~
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 25, 2010, 06:05:31 AM
I don't know much about tables but
using interop = Autodesk.AutoCAD.Interop;


interop.AcadApplication Ap = (interop.AcadApplication)acadApp.AcadApplication;
object ob = Ap.ActiveDocument.ObjectIdToObject( tableStyle.ObjectId.OldId)
 interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;
   ts.CreateCellStyle("U12");

Autodesk.AutoCAD.Interop.Common.IAcadTableStyle interface has not CreateCellStyle method. Please, check your assumption before the next answer.
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 25, 2010, 06:24:20 AM
Have you tried  table.Cells[0,1].CellStyleOverrides

My question about TableStyle.
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 25, 2010, 07:09:55 AM

Andrey,

I read your question to be about CellStyles too.

Perhaps it would help if you confirmed your exact question.

Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 25, 2010, 07:35:17 AM

Andrey,

I read your question to be about CellStyles too.

Perhaps it would help if you confirmed your exact question.

How to add/modify/delete CellStyle objects of TableStyle entity in program code?

____________________________________________

p.s. Not confuse CellStyle with TextStyle, and TableStyle with Table, please.
Look my screen - Reply #6 on: July 24, 2010, 11:57:29 am

fro2001 write me about Table object, but my question is about TextStyle with CellStyles.
Title: Re: Cell styles in TableStyle...
Post by: LE3 on July 25, 2010, 12:01:43 PM
just tried (have been a long time, since I did something for a table object)... using a Kean existing sample in his site, I can modify the cells with an existing cell type.

http://through-the-interface.typepad.com/through_the_interface/2008/11/creating-a-cust.html
Code: [Select]
tb.Cells[i, j].Style = "Header";
Have not tried to add a new one, but guess that has to be based on an existing one and change it from there...
Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 25, 2010, 01:15:56 PM
With this one can add a cellstyle, delete a cellstyle, set the textstyle for a cellstyle.
It requires you to add some textstyles and a tablestyle
1) Apparantly it is impossible (whatever you do don't actually try it)
2) It probably is no use to the OP , as it is in no way what he wanted.
3) Who wants to use interop anyway.

Code: [Select]
       [CommandMethod("tts")]
        public static void TableStyleTest()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            string tableStyleName = "YourTableStyle";

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TableStyle tableStyle;
                DBDictionary dic = tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead) as DBDictionary;
                foreach (DictionaryEntry eDict in dic)
                {
                    tableStyle = tr.GetObject
                        ((ObjectId)eDict.Value, OpenMode.ForRead) as TableStyle;
                    if (tableStyle.Name == tableStyleName)
                    {
                        tableStyle.UpgradeOpen();

                        ArrayList ca = tableStyle.CellStyles;
                        interop.AcadApplication Ap = (interop.AcadApplication)acadApp.AcadApplication;
                        object ob = Ap.ActiveDocument.ObjectIdToObject( tableStyle.ObjectId.OldId);
                        interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;

                        TextStyleTable textstyletable=tr.GetObject
                            (db.TextStyleTableId,OpenMode.ForRead) as TextStyleTable;
                        object ob2 = Ap.ActiveDocument.ObjectIdToObject(textstyletable.ObjectId.OldId);
                        interop.Common.IAcadTextStyles tss = ob2 as interop.Common.IAcadTextStyles;
                      
                      
                        if(ca.Contains("U1")) ts.DeleteCellStyle("U1");
                        ts.CreateCellStyle("U1");
                        ts.SetCellClass("U1", 1);
                        if (ca.Contains("U2")) ts.DeleteCellStyle("U2");
                        ts.CreateCellStyle("U2");
                        ts.SetCellClass("U2", 2);
                        if (ca.Contains("U3")) ts.DeleteCellStyle("U3");
                        ts.CreateCellStyle("U3");
                        ts.SetCellClass("U3", 3);

                        ed.WriteMessage(tss.Item("User1").ObjectID.ToString());
                        if(textstyletable.Has("User1"))
                            ts.SetTextStyleId("U1", tss.Item("User1").ObjectID);

                        if (textstyletable.Has("User2"))
                            ts.SetTextStyleId("U2", tss.Item("User2").ObjectID);

                        if (textstyletable.Has("User3"))
                            ts.SetTextStyleId("U3", tss.Item("User3").ObjectID);
                    }

                }
                tr.Commit();
            }
            
        } // end TableStyleTest
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 25, 2010, 01:57:30 PM
With this one can add a cellstyle, delete a cellstyle, set the textstyle for a cellstyle.
It requires you to add some textstyles and a tablestyle
1) Apparantly it is impossible (whatever you do don't actually try it)
2) It probably is no use to the OP , as it is in no way what he wanted.
3) Who wants to use interop anyway.

Code: [Select]
       [CommandMethod("tts")]
        public static void TableStyleTest()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            string tableStyleName = "YourTableStyle";

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TableStyle tableStyle;
                DBDictionary dic = tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead) as DBDictionary;
                foreach (DictionaryEntry eDict in dic)
                {
                    tableStyle = tr.GetObject
                        ((ObjectId)eDict.Value, OpenMode.ForRead) as TableStyle;
                    if (tableStyle.Name == tableStyleName)
                    {
                        tableStyle.UpgradeOpen();

                        ArrayList ca = tableStyle.CellStyles;
                        interop.AcadApplication Ap = (interop.AcadApplication)acadApp.AcadApplication;
                        object ob = Ap.ActiveDocument.ObjectIdToObject( tableStyle.ObjectId.OldId);
                        interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;

                        TextStyleTable textstyletable=tr.GetObject
                            (db.TextStyleTableId,OpenMode.ForRead) as TextStyleTable;
                        object ob2 = Ap.ActiveDocument.ObjectIdToObject(textstyletable.ObjectId.OldId);
                        interop.Common.IAcadTextStyles tss = ob2 as interop.Common.IAcadTextStyles;
                      
                      
                        if(ca.Contains("U1")) ts.DeleteCellStyle("U1");
                        ts.CreateCellStyle("U1");
                        ts.SetCellClass("U1", 1);
                        if (ca.Contains("U2")) ts.DeleteCellStyle("U2");
                        ts.CreateCellStyle("U2");
                        ts.SetCellClass("U2", 2);
                        if (ca.Contains("U3")) ts.DeleteCellStyle("U3");
                        ts.CreateCellStyle("U3");
                        ts.SetCellClass("U3", 3);

                        ed.WriteMessage(tss.Item("User1").ObjectID.ToString());
                        if(textstyletable.Has("User1"))
                            ts.SetTextStyleId("U1", tss.Item("User1").ObjectID);

                        if (textstyletable.Has("User2"))
                            ts.SetTextStyleId("U2", tss.Item("User2").ObjectID);

                        if (textstyletable.Has("User3"))
                            ts.SetTextStyleId("U3", tss.Item("User3").ObjectID);
                    }

                }
                tr.Commit();
            }
            
        } // end TableStyleTest

Bryco,
Look my previous message: "Reply #11 on: Today at 05:05:31 am".
I ask again:
Out whence in your code this methods: SetCellClass, CreateCellStyle, SetTextStyleId and DeleteCellStyle?
I even show screen (may be it was more understandable):

(http://habreffect.ru/files/0a4/c48681f43/25.07.png)

Is it your extension methods (http://msdn.microsoft.com/en-us/library/bb383977.aspx)?
Why you use OldId property in your code?
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 25, 2010, 02:12:15 PM
just tried (have been a long time, since I did something for a table object)... using a Kean existing sample in his site, I can modify the cells with an existing cell type.

http://through-the-interface.typepad.com/through_the_interface/2008/11/creating-a-cust.html
Code: [Select]
tb.Cells[i, j].Style = "Header";
Have not tried to add a new one, but guess that has to be based on an existing one and change it from there...

I know about this topic, and I read it before my topic was created. You can read my message in Kean Walmsley comments (July 22, 2010 at 04:40 PM).
Title: Re: Cell styles in TableStyle...
Post by: Jeff_M on July 25, 2010, 02:47:31 PM
Hwd,
I tested Bryco's code here on Acad2010 and it works fine. So, what version are you trying to use this with?

Also, I'm not sure when this was added to the .NET API, but this code (which you had asked about)
Code: [Select]
                        object ob = Ap.ActiveDocument.ObjectIdToObject( tableStyle.ObjectId.OldId);
                        interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;

can now be condensed to:
Code: [Select]
                        interop.Common.IAcadTableStyle ts = tableStyle.AcadObject as interop.Common.IAcadTableStyle;
The reason he had used the OldId to get the COM object, is because .NET & COM use 2 different ObjectId's.
Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 25, 2010, 02:49:28 PM
Add the references you need. I have both Autodac and AxdbLib.
Your on your own now, I think Fox News is hiring though.
Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 25, 2010, 02:52:47 PM
Oh, that's good to know Jeff
Title: Re: Cell styles in TableStyle...
Post by: LE3 on July 25, 2010, 03:13:28 PM
Add the references you need. I have both Autodac and AxdbLib.
Your on your own now, I think Fox News is hiring though. ???? woot?
nice solution Bryco, thanks!.... tried here via normal .net and appears to no way.
Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 25, 2010, 03:34:32 PM
Thanks
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 25, 2010, 04:41:47 PM
So, what version are you trying to use this with?
I use AutoCAD 2009 x64 SP3 Enu.
Quote
The reason he had used the OldId to get the COM object, is because .NET & COM use 2 different ObjectId's.

Thank you. When I must use OldId?
Quote
Add the references you need. I have both Autodac and AxdbLib.
Thank you. My project has some references, and I can't add new reference on AxdbLib.

AxdbLib is AcAxDb17enures.dll?
Autodac (may be Autocad?) is acax17enu.tbl?
I show screen my problem:

(http://habreffect.ru/files/a6d/8f37c68db/25.07.png)

Visual Studio 2010 not cry when I use other Autodesk.AutoCAD.Interop.Common.IAcadTextStyles interface methods.
But Visual Studio 2010 cry when I use methods, whish Bryco show me. Therefore I was think, what problem not in reference. I am mistaken?

Quote
Your on your own now, I think Fox News is hiring though.
I translate this suggestion, but I can't understand it.
I did not wish to offend anybody. If Bryco has taken offence - I ask excuse me.

Title: Re: Cell styles in TableStyle...
Post by: Jeff_M on July 25, 2010, 11:30:34 PM
You do not need to reference the Autodesk.AutoCAD.Interop.Common.

You need to reference the axdb17enu.tlb (the last one shown in the Browse dialog). You should be able to see this in the COM listings as "AutoCAD/ObjectDBX Common 17.0 Type Library". With this, and the AutoCAD reference you already have, these are the only 2 required to access most, in not all, AutoCAD Interop objects, methods & properties.

The OldId is used when combining the use of .NET and COM objects, just as Bryco showed for getting a .NET object's COM counterpart. That's about the extent of my ability to explain this. Perhaps some one else can do a better or more thorough job.
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 12:06:21 AM
You'll probably need to add there using statements also.

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using interop = Autodesk.AutoCAD.Interop;


I think a lot of the problems associated with attempting to resolve .NET problems is not having sufficient information.
Perhaps if we posted a zipped 'Solution' of all files for others to test some of the problems could be resolved earlier.

Sometimes it takes longer to find out what is missing than it does to resolve an actual problem .... and not all of us can make time to spare to become involved.
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 01:17:25 AM
You do not need to reference the Autodesk.AutoCAD.Interop.Common.

You need to reference the axdb17enu.tlb (the last one shown in the Browse dialog). You should be able to see this in the COM listings as "AutoCAD/ObjectDBX Common 17.0 Type Library". With this, and the AutoCAD reference you already have, these are the only 2 required to access most, in not all, AutoCAD Interop objects, methods & properties.

The OldId is used when combining the use of .NET and COM objects, just as Bryco showed for getting a .NET object's COM counterpart. That's about the extent of my ability to explain this. Perhaps some one else can do a better or more thorough job.

Thank you!
When I use Windoxs XP x86 SP3 Professional, and AutoCAD 2009 x86 SP3 Enu - then I see "AutoCAD/ObjectDBX Common 17.0 Type Library" on COM tab.
But when I use Windoxs 7 x64 Ultimate, and AutoCAD 2009 x64 SP3 Enu - then I not see "AutoCAD/ObjectDBX Common 17.0 Type Library" on COM tab.
Screen:

(http://habreffect.ru/files/555/b19725cd3/26.07.png)

"AutoCAD/ObjectDBX Common 17.0 Type Library" is absent for my x64.

Quote
Perhaps if we posted a zipped 'Solution' of all files for others to test some of the problems could be resolved earlier.

Sometimes it takes longer to find out what is missing than it does to resolve an actual problem .... and not all of us can make time to spare to become involved.

Thank you. Zipped 'Solution' (http://cfrqqa.blu.livefilestore.com/y1pXTy0VLq6rIoaC1ZH15M1Nkrv3dLneOjLpaJkspl7VGdc2ur8p8-n_xcu5PvIhlPbtHGtMqY9McWcXnGp51GlpNcaEyf_uF27/TableStyle.zip?download&psid=1).
Title: Re: Cell styles in TableStyle...
Post by: Jeff_M on July 26, 2010, 01:45:59 AM
OK, my apologies. It looks like the "AutoCAD/ObjectDBX Common 17.0 Type Library" actually does point to "Autodesk.AutoCAD.Interop.Common.dll", so that should have been all you needed.

I don't have plain AutoCAD 2009, nor to I have VS2010 so I can't help with the posted file. I hope someone can get this sorted out for you.

Good Luck!
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 02:40:19 AM

The only option I have for testing on this box is MSVS2010 with AC2011.

Opened the solution,
Re-linked the references,
Set the ACAD.EXE version in Project Properties,
Built the debug dll

loaded into AutoCAD and ran without error

A piccy:
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 03:04:03 AM
May be it not work for AutoCAD 2009?

In Windows XP x86 SP3 I see reference, and can add it in my Project:

(http://habreffect.ru/files/957/980e890af/26.07.png)

But...

(http://habreffect.ru/files/850/d99ec87a9/26.07-2.png)

The problem remained... (((
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 03:27:37 AM

What happens if you use these references ??

the ObjectARX 2009 Reference:
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 03:38:05 AM

What happens if you use these references ??

the ObjectARX 2009 Reference:


At first I did too. You can see result on my Reply #24 on: July 25, 2010, 03:41:47 pm for Windows 7 x64 (I showed the screen).

For Windows XP x86 other screen:

(http://habreffect.ru/files/e1d/d9460f9d2/26.07.png)
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 03:50:31 AM


Perhaps you could remove ALL the AutoCad based references and re-reference them :)

Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 03:55:01 AM
Quote
At first I did too. You can see result on my Reply #24 on: July 25, 2010, 03:41:47 pm for Windows 7 x64 (I showed the screen).

For Windows XP x86 other screen:

Have the rules changed again ??

Which version and Operating System are you actually trying to build on ???
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 04:07:20 AM


Perhaps you could remove ALL the AutoCad based references and re-reference them :)

If I delete AutoCAD reference, then I get result which I show on my Reply #17 on: July 25, 2010, 12:57:30 pm.
Quote
Have the rules changed again ??

Which version and Operating System are you actually trying to build on

I have Windows 7 x64 Ultimate and AutoCAD 2009 x64 SP3 Enu on my home.
I have Windows XP x86 SP3 Professional and AutoCAD 2009 x86 SP3 Enu on my office (work).

Some my users use Windows 7 x64, and some - Windows XP x86 SP3 Professional. Most my users (99%) use AutoCAD 2009.

Quote
Perhaps you could remove ALL the AutoCad based references and re-reference them
I tried to do it.

Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 04:10:20 AM


Perhaps you could remove ALL the AutoCad based references and re-reference them :)

If I delete AutoCAD reference, then I get result which I show on my Reply #17 on: July 25, 2010, 12:57:30 pm.

re-read what I said.
Remove the ALL AutoCAD based references.
Reference them again.
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 04:12:34 AM
Quote
Have the rules changed again ??

Which version and Operating System are you actually trying to build on

I have Windows 7 x64 Ultimate and AutoCAD 2009 x64 SP3 Enu on my home.
I have Windows XP x86 SP3 Professional and AutoCAD 2009 x86 SP3 Enu on my office (work).

Some my users use Windows 7 x64, and some - Windows XP x86 SP3 Professional. Most my users (99%) use AutoCAD 2009.


great. How about resolving ONE issue at a time.
Choose one build and get it correct.
Then resolve the other build.
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 04:14:51 AM

Quote
Perhaps you could remove ALL the AutoCad based references and re-reference them
I tried to do it.


Perhaps you should try again
... and tell us which build you are working on.
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 04:28:17 AM
Quote
great. How about resolving ONE issue at a time.
Choose one build and get it correct.
Then resolve the other build.
and
Quote
Perhaps you should try again
... and tell us which build you are working on.
Most important variant - Windows XP x86 SP3 and AutoCAD 2009 x86.

I write video (YouTobe):
http://www.youtube.com/watch?v=TMNdpoNopxg

fixo write me, what on him AutoCAD 2009 all it good work. I was tangled... (((
I have asked him to send me a full project code - then I  compare all references.
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 04:53:21 AM
The video was a bit unclear for me.

I just looked at  the contents of  Autodesk.AutoCAD.Interop.Common for 2009

There is a public interface  IAcadTableStyle : IAcadObject

and a public interface IAcadTableStyle2 : IAcadTableStyle

ONLY the IAcadTableStyle2 has
void DeleteCellStyle(string bstrCellStyle);
void CreateCellStyle(string bstrCellStyle);

I can't test this, but perhaps try

// interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;
interop.Common.IAcadTableStyle2 ts = ob as interop.Common.IAcadTableStyle2;
 
Title: The problem is solved
Post by: Andrey Bushman on July 26, 2010, 05:05:54 AM
The video was a bit unclear for me.

I just looked at  the contents of  Autodesk.AutoCAD.Interop.Common for 2009

There is a public interface  IAcadTableStyle : IAcadObject

and a public interface IAcadTableStyle2 : IAcadTableStyle

ONLY the IAcadTableStyle2 has
void DeleteCellStyle(string bstrCellStyle);
void CreateCellStyle(string bstrCellStyle);

I can't test this, but perhaps try

// interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;
interop.Common.IAcadTableStyle2 ts = ob as interop.Common.IAcadTableStyle2;
 

Yes!!!!! ))))))
I use code from Reply #16 on: July 25, 2010, 12:15:56 pm within IAcadTableStyle. Now I change on IAcadTableStyle2 and all ok!!!!

All Thanks for help!
Title: Re: Cell styles in TableStyle...
Post by: fixo on July 26, 2010, 05:42:13 AM
With this one can add a cellstyle, delete a cellstyle, set the textstyle for a cellstyle.
It requires you to add some textstyles and a tablestyle
1) Apparantly it is impossible (whatever you do don't actually try it)
2) It probably is no use to the OP , as it is in no way what he wanted.
3) Who wants to use interop anyway.

Code: [Select]
       [CommandMethod("tts")]
        public static void TableStyleTest()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            string tableStyleName = "YourTableStyle";

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TableStyle tableStyle;
                DBDictionary dic = tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead) as DBDictionary;
                foreach (DictionaryEntry eDict in dic)
                {
                    tableStyle = tr.GetObject
                        ((ObjectId)eDict.Value, OpenMode.ForRead) as TableStyle;
                    if (tableStyle.Name == tableStyleName)
                    {
                        tableStyle.UpgradeOpen();

                        ArrayList ca = tableStyle.CellStyles;
                        interop.AcadApplication Ap = (interop.AcadApplication)acadApp.AcadApplication;
                        object ob = Ap.ActiveDocument.ObjectIdToObject( tableStyle.ObjectId.OldId);
                        interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;

                        TextStyleTable textstyletable=tr.GetObject
                            (db.TextStyleTableId,OpenMode.ForRead) as TextStyleTable;
                        object ob2 = Ap.ActiveDocument.ObjectIdToObject(textstyletable.ObjectId.OldId);
                        interop.Common.IAcadTextStyles tss = ob2 as interop.Common.IAcadTextStyles;
                      
                      
                        if(ca.Contains("U1")) ts.DeleteCellStyle("U1");
                        ts.CreateCellStyle("U1");
                        ts.SetCellClass("U1", 1);
                        if (ca.Contains("U2")) ts.DeleteCellStyle("U2");
                        ts.CreateCellStyle("U2");
                        ts.SetCellClass("U2", 2);
                        if (ca.Contains("U3")) ts.DeleteCellStyle("U3");
                        ts.CreateCellStyle("U3");
                        ts.SetCellClass("U3", 3);

                        ed.WriteMessage(tss.Item("User1").ObjectID.ToString());
                        if(textstyletable.Has("User1"))
                            ts.SetTextStyleId("U1", tss.Item("User1").ObjectID);

                        if (textstyletable.Has("User2"))
                            ts.SetTextStyleId("U2", tss.Item("User2").ObjectID);

                        if (textstyletable.Has("User3"))
                            ts.SetTextStyleId("U3", tss.Item("User3").ObjectID);
                    }

                }
                tr.Commit();
            }
            
        } // end TableStyleTest

Thanks, Bryco, ya da man!
I've adopted your code to my A2009
It's working like a charm
Regards,

Oleg

~'J'~
Title: Re: Cell styles in TableStyle...
Post by: pkohut on July 26, 2010, 06:05:42 AM
Problem solved, now I'll I can ask my OT question.

What software was used to annotate Kerry's and Hwd's pictures.  Can never be enough cool tools.
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 06:08:42 AM
Problem solved, now I'll ask my OT question.

What software was used to annotate Kerry's and Hwd's pictures.  Can never be enough cool tools.
this (http://www.techsmith.com/screen-capture.asp)
Title: Re: Cell styles in TableStyle...
Post by: pkohut on July 26, 2010, 06:10:24 AM
Problem solved, now I'll ask my OT question.

What software was used to annotate Kerry's and Hwd's pictures.  Can never be enough cool tools.
this (http://www.techsmith.com/screen-capture.asp)

Cool, thanks.  That is on my sort list already of software to checkout. Been reading lots of good reviews about it but hadn't really seen it in action.
Title: Re: Cell styles in TableStyle...
Post by: Glenn R on July 26, 2010, 06:28:35 AM
The video was a bit unclear for me.

I just looked at  the contents of  Autodesk.AutoCAD.Interop.Common for 2009

There is a public interface  IAcadTableStyle : IAcadObject

and a public interface IAcadTableStyle2 : IAcadTableStyle

ONLY the IAcadTableStyle2 has
void DeleteCellStyle(string bstrCellStyle);
void CreateCellStyle(string bstrCellStyle);

I can't test this, but perhaps try

// interop.Common.IAcadTableStyle ts = ob as interop.Common.IAcadTableStyle;
interop.Common.IAcadTableStyle2 ts = ob as interop.Common.IAcadTableStyle2;
 


They're Versioned COM interfaces Kerry - we've been seeing that for some years now e.g. IAcadBlock1 2 and 3, possibly more. A COM interface can't really be changed once it's 'published', so devleopers tend to 'version' them...at least that's what I remember from my C++ COM days...
Title: Re: Cell styles in TableStyle...
Post by: Glenn R on July 26, 2010, 06:29:43 AM
...they version them to add the 'new' features of the underlying objects they want to expose...
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 06:56:46 AM
...they version them to add the 'new' features of the underlying objects they want to expose...

Yes, that's what I discovered ... needed to look at the library in the Browser or Reflector to sort it out ... should have done that earlier I s'pose :)
Title: Re: Cell styles in TableStyle...
Post by: Glenn R on July 26, 2010, 06:58:57 AM
You could probably apply the technique TT showed a couple of years ago here, about using PInvoke on an arx object. It was to do with the default indexer's on Symboltables returning erased objects...you might remember it...I'll have a dig on here...
Title: Re: Cell styles in TableStyle...
Post by: Glenn R on July 26, 2010, 07:03:51 AM
Ah...found it... (http://www.theswamp.org/index.php?topic=12123.msg151231#msg151231)
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 07:18:03 AM
Andrey,
Something you may want to check before you wrap this one up.

Is the library released with AC2009 (and any service pack) the same at the SDK ObjectARX2009.

ie: are
void DeleteCellStyle(string bstrCellStyle);
void CreateCellStyle(string bstrCellStyle);

in the interface IAcadTableStyle2 for the released version ??
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 07:20:11 AM


Please Donate to theSwamp.org (http://www.theswamp.org/donate.html)
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 07:54:35 AM
Please Donate to theSwamp.org (http://www.theswamp.org/donate.html)

I do not trust PayPal - they have deceived many people (I read about it). Has the forum a electronic purse on WebMoney (http://www.wmtransfer.com/eng)?
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 08:02:47 AM

I don't know of anyone at theSwamp who has been deceived.


Perhaps an Administrator would like to move this discussion to a more suitable location.
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 08:19:54 AM
I don't know of anyone at theSwamp who has been deceived.
I am afraid to enter of my confidential data (data of my card) on page www.paypal.com
But I can do remittance on WebMoney electronic purse.

Ok. I learn as do it safe today.
Title: Re: Cell styles in TableStyle...
Post by: Kerry on July 26, 2010, 08:29:04 AM

Andrey,
I'll forward this on to Mark who is the site owner ... he should be able to answer your question.

I wasn't suggesting that you had to make a donation, and that's not the reason I became involved in your problem.
I posted that comment because I know that a lot of people will be looking at that thread over the next few months and donations are always appreciated because they help keep this place operating.

Thanks and Regards,
Kerry
Title: Re: Cell styles in TableStyle...
Post by: Bryco on July 26, 2010, 09:03:29 AM
Thanks Oleg.
Title: Re: Cell styles in TableStyle...
Post by: Glenn R on July 26, 2010, 09:38:58 AM
A quicky for 2010:

Code: [Select]

using System;
using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;


using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(CellStyles.MyCommands))]

namespace CellStyles
{
    public class MyCommands
    {
        [CommandMethod("q1", CommandFlags.Modal)]
        public void TestMethod()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                DBDictionary tsd = (DBDictionary) tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry item in tsd)
                {
                    TableStyle ts = (TableStyle)tr.GetObject(item.Value, OpenMode.ForRead);
                    ed.WriteMessage(string.Format("{0}Table style name: '{1}'", Environment.NewLine, ts.Name));
                    ed.WriteMessage(string.Format("{0}(TableStyle.CellStyles.IsReadOnly ='{1}')",
                        Environment.NewLine, ts.CellStyles.IsReadOnly));

                    //Print all cell style names of table style
                    PrintCellStylesInfo(ts);
                    string stName = "MyStyle";// name for my new cell style

                    ed.WriteMessage(string.Format("{0}Before modify: Cell Styles Count: {1}",
                        Environment.NewLine, ts.CellStyles.Count));

                    ts.UpgradeOpen();

                    AcDbTable.createCellStyle(ts.UnmanagedObject, stName);

                    ed.WriteMessage(string.Format("{0}After modify: Cell Styles Count: {1}{0}",
                                    Environment.NewLine, ts.CellStyles.Count));

                    ts.SetCellClass(CellClass.Label, stName);

                    ed.WriteMessage(string.Format("{0}{1}{0}", "\nAfter modify:", new string('*', 30)));
                    //Print all cell style names of table style
                    PrintCellStylesInfo(ts);
                }

                tr.Commit();
            }         
        }
 
        private void PrintCellStylesInfo(TableStyle ts)
        {
            foreach (var cs in ts.CellStyles)
            {
                Document doc = acadApp.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                ed.WriteMessage(string.Format("{0}CellStyle Type: '{1}'; CellStyle.ToString: {2}",
                                Environment.NewLine, cs.GetType(), cs.ToString()));
            }           
        }
    }

    public static class AcDbTable
    {
        // Acad::ErrorStatus createCellStyle(const ACHAR* pszCellStyle);

        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acdb18.dll", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Unicode,
           EntryPoint = "?createCellStyle@AcDbTableStyle@@QEAA?AW4ErrorStatus@Acad@@PEB_W@Z")]
        public static extern ErrorStatus createCellStyle(IntPtr tableStyle, string name);
    }

}
Title: Re: Cell styles in TableStyle...
Post by: Glenn R on July 26, 2010, 09:39:30 AM
2010 64bit btw...
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 10:28:36 AM
2010 64bit btw...
Thank you.
But "acdb18.dll" reference to concrete AutoCAD version.

May be it better (extension method):
Code: [Select]
   public static class AcadTableStyle
    {
        /// <summary>
        /// Extended method for TableStyle class.
        /// </summary>
        /// <param name="tableStyle">TableStyle object (source)</param>
        /// <param name="cellStyleName">Cell Style name</param>        
        public static void AddCellStyle(this TableStyle tableStyle, string cellStyleName)
        {
            Document dwg = acad.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;            
            try
            {
                if (tableStyle.CellStyles.Contains(cellStyleName)) throw new System.Exception("TableStyle has CellStyle with it name already!");
                TableStyle ts = tableStyle;
                IAcadTableStyle2 ts2 = (IAcadTableStyle2)((AcadApplication)acad.AcadApplication).ActiveDocument.ObjectIdToObject((int)ts.ObjectId.OldIdPtr);
                using (Transaction t = dwg.TransactionManager.StartTransaction())
                {                    
                    lock (dwg.LockDocument())
                    {
                        ts.UpgradeOpen();
                        ts2.CreateCellStyle(cellStyleName);
                        t.Commit();
                    }                    
                }                
            }
            catch (System.Exception e)
            {
                ed.WriteMessage(string.Format("{0}{1}{0}", "\n", e.Message));
            }          
        }
    }
? I do not confirm, but I ask.

Testing:

Code: [Select]
using System;
using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;


using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(CellStyles.MyCommands))]

namespace CellStyles
{
    public class MyCommands
    {
        [CommandMethod("q1", CommandFlags.Modal)]
        public void TestMethod()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                DBDictionary tsd = (DBDictionary)tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry item in tsd)
                {
                    TableStyle ts = (TableStyle)tr.GetObject(item.Value, OpenMode.ForRead);
                    ed.WriteMessage(string.Format("{0}Table style name: '{1}'", Environment.NewLine, ts.Name));
                    ed.WriteMessage(string.Format("{0}(TableStyle.CellStyles.IsReadOnly ='{1}')",
                        Environment.NewLine, ts.CellStyles.IsReadOnly));

                    //Print all cell style names of table style
                    PrintCellStylesInfo(ts);
                    string stName = "MyStyle";// name for my new cell style

                    ed.WriteMessage(string.Format("{0}Before modify: Cell Styles Count: {1}",
                        Environment.NewLine, ts.CellStyles.Count));

                    ts.UpgradeOpen();

                    [color=red]ts.AddCellStyle(stName);[/color][color=green]//Extension method[/color]

                    //AcDbTable.createCellStyle(ts.UnmanagedObject, stName);

                    ed.WriteMessage(string.Format("{0}After modify: Cell Styles Count: {1}{0}",
                                    Environment.NewLine, ts.CellStyles.Count));

                    ts.SetCellClass(CellClass.Label, stName);

                    ed.WriteMessage(string.Format("{0}{1}{0}", "\nAfter modify:", new string('*', 30)));
                    //Print all cell style names of table style
                    PrintCellStylesInfo(ts);
                }
                tr.Commit();
            }
        }

        private void PrintCellStylesInfo(TableStyle ts)
        {
            foreach (var cs in ts.CellStyles)
            {
                Document doc = acadApp.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                ed.WriteMessage(string.Format("{0}CellStyle Type: '{1}'; CellStyle.ToString: {2}",
                                Environment.NewLine, cs.GetType(), cs.ToString()));
            }
        }
    }
}
Title: Re: Cell styles in TableStyle...
Post by: Glenn R on July 26, 2010, 10:37:47 AM
Maybe, but you're still using COM and that's version dependent as well. Personally, I would use the DLLImport option over COM. Alexander Rivilis had a good implementation for multiple versions shown here (http://www.theswamp.org/index.php?topic=10606.msg134917#msg134917).
Title: Re: Cell styles in TableStyle...
Post by: Andrey Bushman on July 26, 2010, 10:40:24 AM
Maybe, but you're still using COM and that's version dependent as well. Personally, I would use the DLLImport option over COM. Alexander Rivilis had a good implementation for multiple versions shown here (http://www.theswamp.org/index.php?topic=10606.msg134917#msg134917).
Thank you!