Author Topic: Copy Civil 3d Label Style  (Read 2785 times)

0 Members and 1 Guest are viewing this topic.

internet explorer

  • Guest
Copy Civil 3d Label Style
« on: May 14, 2016, 02:11:40 AM »
hello...will you help me?  I want to copy a civil 3d label style and name it something else. i thought this would work but doesn't.

Code - C#: [Select]
  1.  using (Transaction trans = database.TransactionManager.StartTransaction())
  2.             {
  3.  
  4.                 ObjectId linelabelstyle = activeDocument.Styles.LabelStyles.GeneralLineLabelStyles["_Line Label"];                
  5.                 LabelStyle pSt = linelabelstyle.GetObject(OpenMode.ForRead) as LabelStyle;
  6.  
  7.                 for (int i = 0; i < pSt.ChildrenCount; i++)
  8.                 {
  9.                     LabelStyle chSt = trans.GetObject(pSt[i], OpenMode.ForRead) as LabelStyle;
  10.                     chSt.Name = myexpname;
  11.                     chSt.ExportTo(database, Autodesk.Civil.StyleConflictResolverType.Override);
  12.                    
  13.                 }                
  14.                 trans.Commit();              
  15.             }

Jeff_M

  • King Gator
  • Posts: 4100
  • C3D user & customizer
Re: Copy Civil 3d Label Style
« Reply #1 on: May 14, 2016, 09:12:28 AM »
Welcome to the Swamp, Internet Explorer! As you've found, you cannot ExportTo the same database that the style resides in. From the help for ExportTo(): System.InvalidOperationException   Thrown when the current style is in the same database as the destination database.

The Style objects have the CopyAsSibling() method, but I don't believe that will work for copying a labelstyle and it's children...you may also want to include the children's children....  What I would probably do is create a side database, use ExportTo(theSideDb), rename the styles there, then ExportTo(currentDb) from that side database back to the current dwg. Then discard the side db.

Actually, I have not tried this, but perhaps the CopyAsSibling() method will copy the style AND it's children.
« Last Edit: May 14, 2016, 09:41:28 AM by Jeff_M »

internet explorer

  • Guest
Re: Copy Civil 3d Label Style
« Reply #2 on: May 14, 2016, 03:08:37 PM »
thank you very much! I'll try that

internet explorer

  • Guest
Re: Copy Civil 3d Label Style
« Reply #3 on: May 14, 2016, 03:33:11 PM »
Actually, I have not tried this, but perhaps the CopyAsSibling() method will copy the style AND it's children.

This works. Thank you again.

mylabelstyle.CopyAsSibling(newname);

internet explorer

  • Guest
Re: Copy Civil 3d Label Style
« Reply #4 on: May 14, 2016, 05:48:49 PM »
now that I got the style copied, I'm trying to change one of the property fields.  Not sure why my try is not working:

Code - C#: [Select]
  1. foreach (ObjectId id in newlabel.GetComponents(LabelStyleComponentType.Text))
  2.                     {
  3.                         try
  4.                         {
  5.                             LabelStyleTextComponent component = id.GetObject(OpenMode.ForWrite) as LabelStyleTextComponent;
  6.                             if (component.Name.ToUpper().Contains(myexpname.ToUpper()))
  7.                             {                                
  8.                                 component.Text.Contents.Value = "<[" + myexpname + "(P2|RN|AP|GC|UN|Sn|OF)]>";
  9.                             }
  10.                         }
  11.                         catch
  12.                         {
  13.                         }
  14.  

internet explorer

  • Guest
Re: Copy Civil 3d Label Style
« Reply #5 on: May 14, 2016, 06:07:57 PM »
yay! Nevermind. I didn't know the text component and reference text components are separate.  I got it to work.  Thanks again Jeff!


now that I got the style copied, I'm trying to change one of the property fields.  Not sure why my try is not working:

Code - C#: [Select]
  1. foreach (ObjectId id in newlabel.GetComponents(LabelStyleComponentType.Text))
  2.                     {
  3.                         try
  4.                         {
  5.                             LabelStyleTextComponent component = id.GetObject(OpenMode.ForWrite) as LabelStyleTextComponent;
  6.                             if (component.Name.ToUpper().Contains(myexpname.ToUpper()))
  7.                             {                                
  8.                                 component.Text.Contents.Value = "<[" + myexpname + "(P2|RN|AP|GC|UN|Sn|OF)]>";
  9.                             }
  10.                         }
  11.                         catch
  12.                         {
  13.                         }
  14.  

internet explorer

  • Guest
Re: Copy Civil 3d Label Style
« Reply #6 on: May 14, 2016, 06:50:00 PM »
The below code doesn't seem to work. Any ideas? Working with 2014 Civil 3d. It does affect the value but doesn't match what I want.  If I put 0, the value will show zero. When I put 0.1, the value ends up being 1.2. I appears to be converting to feet. When I use 5 it changes the value to 60. Crazy :/

component.Text.XOffset.Value = 0.1;
« Last Edit: May 14, 2016, 06:58:42 PM by internet explorer »

Jeff_M

  • King Gator
  • Posts: 4100
  • C3D user & customizer
Re: Copy Civil 3d Label Style
« Reply #7 on: May 14, 2016, 08:11:42 PM »
I haven't tried to edt those in code before, but based on what you are saying, it sounds like the same thing which happens when defining a text height using Expressions. Those also need to be divided by 12 in order for them to display correctly.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Copy Civil 3d Label Style
« Reply #8 on: May 14, 2016, 09:28:01 PM »
I haven't tried to edt those in code before, but based on what you are saying, it sounds like the same thing which happens when defining a text height using Expressions. Those also need to be divided by 12 in order for them to display correctly.

It is strange, but if you want your value 0.1, you need to set the value to 1/12 as Jeff mentions above.