.Net C# z-координата для Aligned Dimension

Автор Тема: .Net C# z-координата для Aligned Dimension  (Прочитано 4877 раз)

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

Тема содержит сообщение с Решением. Нажмите здесь чтобы посмотреть его.

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

  • ADN OPEN
  • Сообщений: 14
  • Карма: 0
Как можно привязать Aligned Dimension у нужной точке на Polyline в трехмерной системе координат? Polyline с некой глубиной, и при установке Aligned Dimension к верхней точке, он устанавливается в любом случае в нуле. Прилагаю вложения, на первом то что получается, на втором - то что должно быть.
Я еще только начала разбираться с программированием под автокад. Попробовала сделать через Leader, но в нужное место крепится только блок с текстом.
Код - C# [Выбрать]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Autodesk.AutoCAD.Geometry;
  10. using Autodesk.AutoCAD.Runtime;
  11.  
  12. namespace DimFinal
  13. {
  14.     public class Class1
  15.     {
  16.  
  17.  
  18.         [CommandMethod("GetPointsFromUser")]
  19.         public static void GetPointsFromUser()
  20.         {
  21.             // Get the current database and start the Transaction Manager
  22.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  23.             Database acCurDb = acDoc.Database;
  24.             Editor ed = acDoc.Editor;
  25.  
  26.             PromptPointResult pPtRes;
  27.             PromptPointOptions pPtOpts = new PromptPointOptions("")
  28.             {
  29.  
  30.                 // Prompt for the start point
  31.                 Message = "\nEnter the start point of the line: "
  32.             };
  33.             pPtRes = ed.GetPoint(pPtOpts);
  34.  
  35.  
  36.             // Exit if the user presses ESC or cancels the command
  37.             if (pPtRes.Status != PromptStatus.OK)
  38.             {
  39.                 if (pPtRes.Status == PromptStatus.Cancel)
  40.                 {
  41.                     ed.WriteMessage("\nInterrupted by user"); return;
  42.                 }
  43.                 else
  44.                 {
  45.                     ed.WriteMessage("\nError on specifying a point"); return;
  46.                 }
  47.             }
  48.             Point3d ptStart = pPtRes.Value;
  49.  
  50.             // Prompt for the end point
  51.             pPtOpts.Message = "\nEnter the end point of the line: ";
  52.             pPtOpts.UseBasePoint = true;
  53.             pPtOpts.BasePoint = ptStart;
  54.             pPtRes = ed.GetPoint(pPtOpts);
  55.             if (pPtRes.Status != PromptStatus.OK)
  56.             {
  57.                 if (pPtRes.Status == PromptStatus.Cancel)
  58.                 {
  59.                     ed.WriteMessage("\nInterrupted by user"); return;
  60.                 }
  61.                 else
  62.                 {
  63.                     ed.WriteMessage("\nError on specifying a point"); return;
  64.                 }
  65.             }
  66.             Point3d ptEnd = pPtRes.Value;
  67.  
  68.             // Prompt for the 3d point
  69.             pPtOpts.Message = "\nEnter the 3d point of the line: ";
  70.             pPtRes = ed.GetPoint(pPtOpts);
  71.             Point3d pt3 = pPtRes.Value;
  72.             if (pPtRes.Status == PromptStatus.Cancel) return;
  73.  
  74.             // Start a transaction
  75.             using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  76.             {
  77.                 BlockTable acBlkTbl;
  78.                 BlockTableRecord acBlkTblRec, acBlkTblRec1;
  79.                 // Open Model space for write
  80.                 acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
  81.                 acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  82.                 acBlkTblRec1 = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
  83.                 AlignedDimension alDim = new AlignedDimension();
  84.                 using (alDim)
  85.                 {
  86.                     alDim.XLine1Point = ptStart;
  87.                     alDim.XLine2Point = ptEnd;
  88.                     alDim.DimLinePoint = pt3;
  89.                     alDim.DimensionStyle = acCurDb.Dimstyle;
  90.                    
  91.                     // Add the line to the block table
  92.                     acBlkTblRec1.AppendEntity(alDim);
  93.                     string text = alDim.DimensionText;
  94.                     acTrans.AddNewlyCreatedDBObject(alDim, false);
  95.  
  96.                     ObjectId mtextId = ObjectId.Null;
  97.                     MText mtx = new MText()
  98.                     {
  99.                         //mtx.SetDatabaseDefaults();
  100.                         Contents = text,
  101.                         Location = ptStart
  102.                     };
  103.                     mtextId = acBlkTblRec.AppendEntity(mtx);
  104.                     acTrans.AddNewlyCreatedDBObject(mtx, true);
  105.                    
  106.                      ObjectId leaderId = ObjectId.Null;
  107.                      Leader ld = new Leader();
  108.                      ld.AppendVertex(ptStart);
  109.                      ld.AppendVertex(pt3);
  110.                      ld.SetDatabaseDefaults();
  111.                      leaderId = acBlkTblRec.AppendEntity(ld);
  112.                      ld.Annotation = mtextId;
  113.                      ld.EvaluateLeader();
  114.                     acTrans.AddNewlyCreatedDBObject(ld, true);
  115.  
  116.                  }
  117.  
  118.                 // Commit the changes and dispose of the transaction
  119.                 acTrans.Commit();
  120.             }
  121.         }
  122.     }
  123. }
« Последнее редактирование: 06-05-2017, 19:47:35 от Александр Ривилис »

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

  • Administrator
  • *****
  • Сообщений: 13830
  • Карма: 1784
  • Рыцарь ObjectARX
  • Skype: rivilis
Re: .Net C# z-координата для Aligned Dimension
« Ответ #1 : 06-05-2017, 19:49:24 »
Приветствую на форуме!
1. Обратите внимание на то как следует форматировать код на форуме (у меня в подписи)
2. Я не понял за чем нужна выноска (Leader). Нужен просто размер, но на нужном уровне?
Не забывайте про правильное Форматирование кода на форуме
Создание и добавление Autodesk Screencast видео в сообщение на форуме
Если Вы задали вопрос и на форуме появился правильный ответ, то не забудьте про кнопку Решение

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

  • ADN OPEN
  • Сообщений: 14
  • Карма: 0
Re: .Net C# z-координата для Aligned Dimension
« Ответ #2 : 06-05-2017, 20:06:21 »
Да, нужен aligned dimension на нужной высоте. Как это можно реализовать?

Отмечено как Решение AYulia 06-05-2017, 23:45:17

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

  • Administrator
  • *****
  • Сообщений: 13830
  • Карма: 1784
  • Рыцарь ObjectARX
  • Skype: rivilis
Re: .Net C# z-координата для Aligned Dimension
« Ответ #3 : 06-05-2017, 20:57:20 »


Исправлял минимально, и не учитывал что ПСК может не соответствовать МСК:

Код - C# [Выбрать]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6.  
  7. namespace DimFinal
  8. {
  9.   public class Class1
  10.   {
  11.     [CommandMethod("GetPointsFromUser")]
  12.     public static void GetPointsFromUser()
  13.     {
  14.       // Get the current database and start the Transaction Manager
  15.       Document acDoc = Application.DocumentManager.MdiActiveDocument;
  16.       Database acCurDb = acDoc.Database;
  17.       Editor ed = acDoc.Editor;
  18.  
  19.       PromptPointResult pPtRes;
  20.       PromptPointOptions pPtOpts =
  21.         new PromptPointOptions("\nEnter the start point of the line: ");
  22.  
  23.       pPtRes = ed.GetPoint(pPtOpts);
  24.  
  25.       // Exit if the user presses ESC or cancels the command
  26.       if (pPtRes.Status != PromptStatus.OK)
  27.       {
  28.         if (pPtRes.Status == PromptStatus.Cancel)
  29.         {
  30.           ed.WriteMessage("\nInterrupted by user"); return;
  31.         }
  32.         else
  33.         {
  34.           ed.WriteMessage("\nError on specifying a point"); return;
  35.         }
  36.       }
  37.  
  38.       Point3d ptStart = pPtRes.Value;
  39.  
  40.       // Prompt for the end point
  41.       pPtOpts.Message = "\nEnter the end point of the line: ";
  42.       pPtOpts.UseBasePoint = true;
  43.       pPtOpts.BasePoint = ptStart;
  44.       pPtRes = ed.GetPoint(pPtOpts);
  45.       if (pPtRes.Status != PromptStatus.OK)
  46.       {
  47.         if (pPtRes.Status == PromptStatus.Cancel)
  48.         {
  49.           ed.WriteMessage("\nInterrupted by user"); return;
  50.         }
  51.         else
  52.         {
  53.           ed.WriteMessage("\nError on specifying a point"); return;
  54.         }
  55.       }
  56.  
  57.       Point3d ptEnd = pPtRes.Value;
  58.  
  59.       // Prompt for the 3d point
  60.       pPtOpts.Message = "\nEnter the 3d point of the line: ";
  61.       pPtRes = ed.GetPoint(pPtOpts);
  62.       Point3d pt3 = pPtRes.Value;
  63.       if (pPtRes.Status == PromptStatus.Cancel) return;
  64.       // Start a transaction
  65.       using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  66.       {
  67.         // Open Model space for write
  68.         BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
  69.         BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  70.         AlignedDimension alDim = new AlignedDimension();
  71.         alDim.XLine1Point = new Point3d(ptStart.X, ptStart.Y, 0);
  72.         alDim.XLine2Point = new Point3d(ptEnd.X, ptEnd.Y, 0);
  73.         alDim.DimLinePoint = new Point3d(pt3.X, pt3.Y, 0);
  74.         alDim.DimensionStyle = acCurDb.Dimstyle;
  75.         // Add the line to the block table
  76.         acBlkTblRec.AppendEntity(alDim);
  77.         Vector3d v = new Vector3d(0, 0, ptStart.Z);
  78.         Matrix3d mat = Matrix3d.Displacement(v);
  79.         alDim.TransformBy(mat);
  80.         acTrans.AddNewlyCreatedDBObject(alDim, true);
  81.         // Commit the changes and dispose of the transaction
  82.         acTrans.Commit();
  83.       }
  84.     }
  85.   }
  86. }
Не забывайте про правильное Форматирование кода на форуме
Создание и добавление Autodesk Screencast видео в сообщение на форуме
Если Вы задали вопрос и на форуме появился правильный ответ, то не забудьте про кнопку Решение

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

  • ADN OPEN
  • Сообщений: 14
  • Карма: 0
Re: .Net C# z-координата для Aligned Dimension
« Ответ #4 : 06-05-2017, 23:45:07 »
Спасибо большое!

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

  • Administrator
  • *****
  • Сообщений: 13830
  • Карма: 1784
  • Рыцарь ObjectARX
  • Skype: rivilis
Re: .Net C# z-координата для Aligned Dimension
« Ответ #5 : 07-05-2017, 17:52:31 »
AYulia,
Gilles Chanteau здесь подсказал, что можно было просто установить alDim.Elevation в значение ptStart.Z:
https://forums.autodesk.com/t5/net/set-aligned-dimension-z-coordinate/m-p/7067298#M53181
Не забывайте про правильное Форматирование кода на форуме
Создание и добавление Autodesk Screencast видео в сообщение на форуме
Если Вы задали вопрос и на форуме появился правильный ответ, то не забудьте про кнопку Решение

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

  • ADN OPEN
  • Сообщений: 14
  • Карма: 0
Re: .Net C# z-координата для Aligned Dimension
« Ответ #6 : 07-05-2017, 19:09:14 »
Спасибо, буду изучать  :)