using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(RotateAndScale.MyCommands))]
#pragma warning disable 0618
namespace RotateAndScale
{
public class MyCommands
{
[CommandMethod("TRS")]
public void Trs()
{
// Put your command code here
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
Point3d p1, p2, p3, p2_new, p3_new;
PromptPointOptions pr = new PromptPointOptions("");
pr.Message = "\nУкажите точку p1: ";
PromptPointResult rs = ed.GetPoint(pr);
if (rs.Status != PromptStatus.OK) return;
p1 = rs.Value;
pr.Message = "\nУкажите точку p2: ";
pr.UseBasePoint = true; pr.BasePoint = p1;
rs = ed.GetPoint(pr);
if (rs.Status != PromptStatus.OK) return;
p2 = rs.Value;
pr.Message = "\nУкажите точку p3: ";
pr.UseBasePoint = true; pr.BasePoint = p2;
rs = ed.GetPoint(pr);
if (rs.Status != PromptStatus.OK) return;
p3 = rs.Value;
ObjectId idPoly = ObjectId.Null;
using (BlockTableRecord curSpace = doc.Database.CurrentSpaceId.Open(OpenMode.ForWrite) as BlockTableRecord)
{
using (Polyline pline = new Polyline(3))
{
pline.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0,0,0);
pline.AddVertexAt(1, new Point2d(p2.X, p2.Y), 0, 0, 0);
pline.AddVertexAt(2, new Point2d(p3.X, p3.Y), 0, 0, 0);
idPoly = curSpace.AppendEntity(pline);
}
}
pr.Message = "\nУкажите точку p2_new: ";
pr.UseBasePoint = true; pr.BasePoint = p1;
rs = ed.GetPoint(pr);
if (rs.Status != PromptStatus.OK) return;
p2_new = rs.Value;
p3_new = Align3Point(p1, p2, p3, p2_new);
using (Polyline pline = idPoly.Open(OpenMode.ForRead) as Polyline)
using (Polyline pline_new = pline.Clone() as Polyline)
using (BlockTableRecord curSpace = doc.Database.CurrentSpaceId.Open(OpenMode.ForWrite) as BlockTableRecord)
{
pline_new.SetPointAt(1, new Point2d(p2_new.X, p2_new.Y));
pline_new.SetPointAt(2, new Point2d(p3_new.X, p3_new.Y));
curSpace.AppendEntity(pline_new);
}
}
Point3d Align3Point(Point3d p1, Point3d p2, Point3d p3, Point3d p2_new)
{
Point3d p;
Vector3d v = p2 - p1;
Vector3d v_new = p2_new - p1;
double ang = v.GetAngleTo(v_new,Vector3d.ZAxis); // Угол поворота
double scale = v_new.Length / v.Length; // Масштаб
Matrix3d matRot = Matrix3d.Rotation(ang, Vector3d.ZAxis, p1); // Матрица поворота
Matrix3d matScale = Matrix3d.Scaling(scale, p1); // Матрица масштабирования
p = p3;
p = p.TransformBy(matRot);
p = p.TransformBy(matScale);
return p;
}
}
}