Ошибка при попытке добавления xdata

Автор Тема: Ошибка при попытке добавления xdata  (Прочитано 2319 раз)

0 Пользователей и 1 Гость просматривают эту тему.

Оффлайн AtomohodАвтор темы

  • ADN OPEN
  • ****
  • Сообщений: 453
  • Карма: 1
Здравствуйте!
Пытаюсь добавить Xdata к создаваемому Mtext, хватаю ошибку. Может ли быть проблема в двойном определении приложения regApp или я накосячил где-то еще? Пытался убрать повторное определение - получаю ошибку - неверный порядок xdata.

Код - C# [Выбрать]
  1. public class Crossection
  2.     {
  3.         public static string sectionName;
  4.         public static Point3d pL;
  5.         public static Point3d pR;
  6.         public static Point3d pointer;
  7.  
  8.         public static void CreateSectionMark(Point3d pL, Point3d pR, string sectionName, Point3d pointer, int drawingScale = 200)
  9.         {
  10.             //--------------------------------------------------------------------Mandatory variables
  11.             Document doc = Application.DocumentManager.MdiActiveDocument;
  12.             Database db = doc.Database;
  13.             Editor ed = doc.Editor;
  14.             Transaction tr = db.TransactionManager.StartTransaction();
  15.             //--------------------------------------------------------------------Mandatory variables
  16.             ObjectIdCollection collection = new ObjectIdCollection();
  17.             using (tr)
  18.             {
  19.                 BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  20.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  21.  
  22.                 Polyline directrix = new Polyline();
  23.                 directrix.SetDatabaseDefaults();
  24.  
  25.                 directrix.AddVertexAt(0, new Point2d(pL.X, pL.Y), 0, 0, 0);
  26.                 directrix.AddVertexAt(1, new Point2d(pR.X, pR.Y), 0, 0, 0);
  27.  
  28.                 directrix.LineWeight = LineWeight.ByLineWeightDefault;
  29.                 directrix.Layer = "0";
  30.  
  31.                 Vector3d directrixDirection = directrix.GetFirstDerivative(directrix.EndPoint);
  32.  
  33.  
  34.  
  35.                 double angleToRotate = Vector3d.XAxis.GetAngleTo(directrixDirection, Vector3d.ZAxis);
  36.  
  37.                 double stripeLength = 7 * drawingScale;
  38.                 int arrowLength = 7;
  39.  
  40.                 Polyline stripe = new Polyline();
  41.                 stripe.SetDatabaseDefaults();
  42.                 double thickness = 2.5;
  43.  
  44.                 stripe.AddVertexAt(0, new Point2d(pL.X, pL.Y), 0, thickness, thickness);
  45.                 stripe.AddVertexAt(1, new Point2d(pL.X + stripeLength, pL.Y), 0, thickness, thickness);
  46.  
  47.                 stripe.LineWeight = LineWeight.ByLineWeightDefault;
  48.                 stripe.Layer = "0";
  49.                 stripe.ColorIndex = 2;
  50.                 btr.AppendEntity(stripe);
  51.                 tr.AddNewlyCreatedDBObject(stripe, true);
  52.  
  53.                 collection.Add(stripe.ObjectId);
  54.  
  55.                 stripe.TransformBy(Matrix3d.Rotation(angleToRotate, Vector3d.ZAxis, stripe.StartPoint));
  56.  
  57.                 Point3d startPoint = stripe.GetPointAtDist(0.33 * stripe.Length);
  58.                 Vector3d perpDirection = stripe.GetFirstDerivative(stripe.GetParameterAtPoint(startPoint));
  59.                 perpDirection = perpDirection.GetNormal();
  60.                 perpDirection = perpDirection.TransformBy(Matrix3d.Rotation(Math.PI / 2, stripe.Normal, Point3d.Origin));
  61.  
  62.                 Polyline arrow = new Polyline();
  63.                 arrow.SetDatabaseDefaults();
  64.                 Point2d p1 = new Point2d(startPoint.X, startPoint.Y);
  65.                 Point2d p2 = p1 + new Vector2d(perpDirection.X, perpDirection.Y) * arrowLength / 2.5 /* 2.0*/;
  66.                 Point2d p3 = p1 + new Vector2d(perpDirection.X, perpDirection.Y) * arrowLength;
  67.  
  68.                 arrow.AddVertexAt(0, p3, 0, thickness / 6, thickness / 6);
  69.                 arrow.AddVertexAt(1, p2, 0, thickness, 0);
  70.                 arrow.AddVertexAt(2, p1, 0, 0, 0);
  71.  
  72.                 arrow.LineWeight = LineWeight.ByLineWeightDefault;
  73.                 arrow.Layer = "0";
  74.                 arrow.ColorIndex = 1;
  75.                 btr.AppendEntity(arrow);
  76.                 tr.AddNewlyCreatedDBObject(arrow, true);
  77.  
  78.                 collection.Add(arrow.ObjectId);
  79.                 arrow.TransformBy(Matrix3d.Scaling(drawingScale, new Point3d(p1.X, p1.Y, 0)));
  80.                 DBObjectCollection mirrorLine = arrow.GetOffsetCurves(-directrix.Length / 2 + 0.33 * stripe.Length);
  81.                 Polyline ii = (Polyline)mirrorLine[0];
  82.                 Line3d l3d = new Line3d(ii.StartPoint, ii.EndPoint);
  83.                 Polyline arrow2 = arrow.Clone() as Polyline;
  84.                 Polyline stripe2 = stripe.Clone() as Polyline;
  85.  
  86.                 arrow2.TransformBy(Matrix3d.Mirroring(l3d));
  87.                 stripe2.TransformBy(Matrix3d.Mirroring(l3d));
  88.                 btr.AppendEntity(arrow2);
  89.                 tr.AddNewlyCreatedDBObject(arrow2, true);
  90.                 btr.AppendEntity(stripe2);
  91.                 tr.AddNewlyCreatedDBObject(stripe2, true);
  92.  
  93.                 Vector3d perpDirection2 = arrow.GetFirstDerivative(arrow.GetParameterAtPoint(arrow.StartPoint));
  94.                 perpDirection2 = perpDirection2.GetNormal();
  95.                 perpDirection2 = perpDirection2.TransformBy(Matrix3d.Rotation(Math.PI / 2, arrow.Normal, arrow.StartPoint));
  96.  
  97.                 Point3d textPoint = arrow.StartPoint - new Vector3d(perpDirection2.X, perpDirection2.Y, 0) * 5 * drawingScale;
  98.  
  99.  
  100.                 MText mtext = new MText();
  101.  
  102.                 mtext.Location = textPoint;
  103.                 mtext.TextHeight = ESKDValues.SectionTextHeight * drawingScale;
  104.                 mtext.Attachment = AttachmentPoint.MiddleCenter;
  105.                 mtext.Width = 10;
  106.                 mtext.Contents = sectionName;
  107.                 mtext.Layer = "0";
  108.                
  109.                
  110.                 AddRegAppTableRecord("ITAT");
  111.                 ResultBuffer rb = new ResultBuffer();
  112.                 rb.Add(new TypedValue((int) DxfCode.ExtendedDataRegAppName, "ITAT"));
  113.                 rb.Add(new TypedValue((int)DxfCode.Text, "sec"));
  114.  
  115.  
  116.  
  117.                 btr.AppendEntity(mtext);
  118.                 tr.AddNewlyCreatedDBObject(mtext, true);
  119.                 mtext.XData = rb;
  120.                 rb.Dispose();
  121.  
  122.                 MText mtext2 = mtext.Clone() as MText;
  123.                 mtext2.TransformBy(Matrix3d.Mirroring(l3d));
  124.                 mtext2.Normal = new Vector3d(0, 0, 1);
  125.                 btr.AppendEntity(mtext2);
  126.                 tr.AddNewlyCreatedDBObject(mtext2, true);
  127.  
  128.                 tr.Commit();
  129.             }
  130.         }
  131.  
  132.         [CommandMethod("0sec")]
  133.         public static void DoSection()
  134.         {
  135.             sectionName = GetStringParameter("Set section name");
  136.             pL =GetPointFromUser("Set left point");
  137.             pR = GetPointFromUser("Set right point");
  138.            
  139.             CreateSectionMark(pL, pR, sectionName, pointer);
  140.         }
  141.  public static Point3d GetPointFromUser(string message)
  142.         {
  143.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  144.             Document doc = Application.DocumentManager.MdiActiveDocument;
  145.             Database db = doc.Database;
  146.             PromptPointOptions pickPoint = new PromptPointOptions(message);
  147.             PromptPointResult insertPoint = ed.GetPoint(pickPoint);
  148.             return insertPoint.Value;
  149.         }
  150. public static string GetStringParameter(string message)
  151.         {
  152.             Document doc = Application.DocumentManager.MdiActiveDocument;
  153.             PromptStringOptions pso1 = new PromptStringOptions($"\n{message}");
  154.             PromptResult stringParameter = doc.Editor.GetString(pso1);
  155.             string parameter = stringParameter.StringResult;
  156.             return parameter;
  157.         }
  158.  public static void AddRegAppTableRecord(string regAppName)
  159.  
  160.         {
  161.             Document doc = Application.DocumentManager.MdiActiveDocument;
  162.  
  163.             Editor ed = doc.Editor;
  164.  
  165.             Database db = doc.Database;
  166.  
  167.             Transaction tr = doc.TransactionManager.StartTransaction();
  168.  
  169.             using (tr)
  170.  
  171.             {
  172.                 RegAppTable rat = (RegAppTable)tr.GetObject(db.RegAppTableId, OpenMode.ForRead, false);
  173.  
  174.                 if (!rat.Has(regAppName))
  175.  
  176.                 {
  177.                     rat.UpgradeOpen();
  178.  
  179.                     RegAppTableRecord ratr = new RegAppTableRecord();
  180.  
  181.                     ratr.Name = regAppName;
  182.  
  183.                     rat.Add(ratr);
  184.  
  185.                     tr.AddNewlyCreatedDBObject(ratr, true);
  186.                 }
  187.  
  188.                 tr.Commit();
  189.             }
  190.         }
  191.     }




Отмечено как Решение Atomohod 08-09-2019, 20:03:36

Оффлайн Александр Ривилис

  • Administrator
  • *****
  • Сообщений: 13829
  • Карма: 1784
  • Рыцарь ObjectARX
  • Skype: rivilis
Re: Ошибка при попытке добавления xdata
« Ответ #1 : 08-09-2019, 19:57:24 »
или я накосячил где-то еще?
Вместо:
Код - C# [Выбрать]
  1. rb.Add(new TypedValue((int)DxfCode.Text, "sec"));
должно быть:
Код - C# [Выбрать]
  1. rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, "sec"));
Остальное не смотрел.
Не забывайте про правильное Форматирование кода на форуме
Создание и добавление Autodesk Screencast видео в сообщение на форуме
Если Вы задали вопрос и на форуме появился правильный ответ, то не забудьте про кнопку Решение

Оффлайн AtomohodАвтор темы

  • ADN OPEN
  • ****
  • Сообщений: 453
  • Карма: 1
Re: Ошибка при попытке добавления xdata
« Ответ #2 : 08-09-2019, 20:05:35 »
Проблема действительно была в Ascii, спасибо! А в каких случаях тогда применять:
rb.Add(new TypedValue((int)DxfCode.Text,
?

Оффлайн Александр Ривилис

  • Administrator
  • *****
  • Сообщений: 13829
  • Карма: 1784
  • Рыцарь ObjectARX
  • Skype: rivilis
Re: Ошибка при попытке добавления xdata
« Ответ #3 : 08-09-2019, 20:16:37 »
Проблема действительно была в Ascii, спасибо! А в каких случаях тогда применять:
rb.Add(new TypedValue((int)DxfCode.Text,
?

Не в расширенных данных.
Не забывайте про правильное Форматирование кода на форуме
Создание и добавление Autodesk Screencast видео в сообщение на форуме
Если Вы задали вопрос и на форуме появился правильный ответ, то не забудьте про кнопку Решение