using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Runtime.InteropServices;
namespace AcadTest
{
public class UCSTest
{
[CommandMethod(nameof(TestUcs), CommandFlags.Modal)]
public void TestUcs()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
using (var t = doc.TransactionManager.StartTransaction())
{
var ucs = doc.Editor.CurrentUserCoordinateSystem;
var ms = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite);
var ptUcs = doc.Editor.GetPoint("Точка").Value;
var ptWcs = ptUcs.TransformCS(UCSType.UCS, UCSType.WCS);
var ptDcs = ptWcs.TransformCS(UCSType.WCS, UCSType.DCS);
var pt2Dcs = ptDcs + new Vector3d(3, 6, 0);
var pt2Wcs = pt2Dcs.TransformCS(UCSType.DCS, UCSType.WCS);
var line = new Line(ptWcs, pt2Wcs);
ms.AppendEntity(line);
t.AddNewlyCreatedDBObject(line, true);
t.Commit();
}
}
}
static class UCSSupport
{
/// <summary>
/// Перевод координат точки из одной системы в другую
/// </summary>
/// <param name="point">Точка</param>
/// <param name="from">Система координат "из"</param>
/// <param name="to">Система координат "в"</param>
/// <returns>Точка с координатами в нужной СК</returns>
public static Point3d TransformCS(this Point3d point, UCSType from, UCSType to)
{
using (ResultBuffer resBufFrom = new ResultBuffer(new TypedValue(5003, from)))
using (ResultBuffer resBufTo = new ResultBuffer(new TypedValue(5003, to)))
{
double[]
coords = point.ToArray(),
retVal = new double[3];
AcedTrans
(coords, resBufFrom.UnmanagedObject,
resBufTo.UnmanagedObject, 0, retVal);
return new Point3d(retVal);
}
}
/// <summary>
/// http://adndevblog.typepad.com/autocad/2013/page/41/
/// </summary>
/// <param name="point">Точки на листе</param>
/// <param name="fromRb">Система координат "ИЗ"</param>
/// <param name="toRb">Система координат "В"</param>
/// <param name="disp"></param>
/// <param name="result"></param>
/// <returns></returns>
[DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "acedTrans")]
public static extern int AcedTrans(double[] point, IntPtr fromRb, IntPtr toRb, int disp, double[] result);
}
/// <summary>
/// Тип координатной системы
/// </summary>
public enum UCSType
{
/// <summary>
/// Мировая
/// </summary>
WCS = 0,
/// <summary>
/// Пользовательская
/// </summary>
UCS = 1,
/// <summary>
/// Экрана
/// </summary>
DCS = 2,
/// <summary>
/// Экрана пространства листа
/// </summary>
PSDCS = 3,
}
}