How to set Dimensions to Annotative using C#.NET

How to set Dimensions to Annotative using C#.NET

jlobo2
Advocate Advocate
3,162 Views
9 Replies
Message 1 of 10

How to set Dimensions to Annotative using C#.NET

jlobo2
Advocate
Advocate

We have a template setup with a "_STANDARD" dimension that is Annotative.

When we plot through the C# code below, we access the dimension, and set that.

The properties are being set, except for the Annotation. Can you please suggest what am I missing?CodeCodeResultResult

0 Likes
Accepted solutions (1)
3,163 Views
9 Replies
Replies (9)
Message 2 of 10

qnologi
Advisor
Advisor

Was this intended for the .NET Forum?

 

https://forums.autodesk.com/t5/net/bd-p/152

qnologi
Message 3 of 10

jlobo2
Advocate
Advocate

Thanks

0 Likes
Message 4 of 10

Alexander.Rivilis
Mentor
Mentor

Try this sample code:

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

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

namespace DimChangeAnno
{

  public class MyCommands
  {
    [CommandMethod("ChgDimAnno")]
    public void ChgDimAnnoHandler()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      if (doc == null) return;
      Editor ed = doc.Editor;
      PromptEntityOptions prOpt = new PromptEntityOptions("\nSelect Dimension: ");
      prOpt.SetRejectMessage("\nIt is not Dimension!");
      prOpt.AddAllowedClass(typeof(Dimension), false);
      PromptEntityResult prRes = ed.GetEntity(prOpt);
      if (prRes.Status == PromptStatus.OK)
      {
        using (Transaction tr = doc.TransactionManager.StartTransaction())
        {
          Dimension dim = tr.GetObject(prRes.ObjectId, OpenMode.ForWrite) as Dimension;
          switch(dim.Annotative)
          {
            case AnnotativeStates.False:
              dim.Annotative = AnnotativeStates.True;
              ed.WriteMessage($"\nAnnotativeStates.False -> {dim.Annotative}");
              break;
            case AnnotativeStates.True:
              dim.Annotative = AnnotativeStates.False;
              ed.WriteMessage($"\nAnnotativeStates.True -> {dim.Annotative}");
              break;
            default:
              ed.WriteMessage("\nAnnotativeStates.NotApplicable");
              break;
          }
          tr.Commit();
        }
      }
    }
  }
}

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 5 of 10

jlobo2
Advocate
Advocate

@Alexander.Rivilis  I am using a similar code to change the annotative property. But whenever I use it, the dimensions do not show up in the drawing. I am plotting other block attributes as well.

The code, I am using is below:

            using (Transaction acTrans = theDb.TransactionManager.StartTransaction())
            {
                pBlockTbl = acTrans.GetObject(theDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
                pModelSpaceTblRcd = acTrans.GetObject(pBlockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                //pBlockTbl.Dispose();           

                // Prepare dimension
                string tempDimensionTextToPlot =  string.Format("{0:0.00}", (rightDist - leftDist));
                
                pDimension = new AlignedDimension(
                    new Point3d(leftDist, originElev, 0),
                    new Point3d(rightDist, originElev, 0),
                    new Point3d(leftDist + ((rightDist - leftDist) / 2), originElev + elevDeltaToDimLine, 0),
                    tempDimensionTextToPlot,
                    idDimensionStyleStandard);        

                //change
                pDimension.DimensionStyle = idDimensionStyleStandard;
                pDimension.LayerId = idLayerDimension;
                pDimension.Annotative =  AnnotativeStates.True;

                // Add dimension to database
                pModelSpaceTblRcd.AppendEntity(pDimension);
                acTrans.AddNewlyCreatedDBObject(pDimension, true);

                // Finished with this dimension
                //pDimension.Dispose();

                acTrans.Commit();
            }
0 Likes
Message 6 of 10

Alexander.Rivilis
Mentor
Mentor
Accepted solution
[CommandMethod("CreateDimAnno")]
public void CreateDimAnno()
{
  Document doc = Application.DocumentManager.MdiActiveDocument;
  if (doc == null) return;
  Database db = doc.Database;
  Editor ed = doc.Editor;
  using (Transaction tr = doc.TransactionManager.StartTransaction())
  {
    BlockTableRecord curSpace = 
      tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
    Dimension dim = new AlignedDimension(
                new Point3d(0, 0, 0),
                new Point3d(100, 0, 0),
                new Point3d(50, 20, 0),
                "<>",
                db.Dimstyle);
    dim.SetDatabaseDefaults(db);
    curSpace.AppendEntity(dim);
    dim.Annotative = AnnotativeStates.True;
    try
    {
      dim.AddContext(db.Cannoscale);
    }
    catch { }
    tr.AddNewlyCreatedDBObject(dim, true);
    tr.Commit();
  }
}

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 7 of 10

jlobo2
Advocate
Advocate

@Alexander.Rivilis 

 

That is simply awesome. That worked great. 

How do I know, that I have to append it first and then change the property?

Is there a website or a course I can learn all these things from?

0 Likes
Message 8 of 10

Alexander.Rivilis
Mentor
Mentor

@jlobo2 wrote:

@Alexander.Rivilis 

 

That is simply awesome. That worked great. 

How do I know, that I have to append it first and then change the property?

Is there a website or a course I can learn all these things from?


Annotation Scales reffer to Database. So you have first add it to Database, and after make it annotative.

Only samples on this forum, other forums and blogs can help you.

In English:

http://adndevblog.typepad.com/autocad/

http://through-the-interface.typepad.com/through_the_interface/

http://www.theswamp.org/index.php?action=forum

In Russian:

http://adn-cis.org/forum/

 

 

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 9 of 10

jlobo2
Advocate
Advocate

@Alexander.Rivilis 

Can you share some light for the following post please.

Rotate a set of objects along an axis

I would appreciate your help

0 Likes
Message 10 of 10

qnologi
Advisor
Advisor

I'm sorry, but code is above my pay grade : (

qnologi
0 Likes