using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
#pragma warning disable 0618
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(Rivilis.HatchFix))]
namespace Rivilis
{
public class HatchFix
{
[CommandMethod("HatchFix")]
public void HatchFixCommand() // This method can have any name
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
SelectionFilter filter = new SelectionFilter(
new TypedValue[] { new TypedValue((int)DxfCode.Start, "HATCH") });
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "Выберите штриховки для исправления: ";
pso.MessageForRemoval = "Удалите штриховки из набора: ";
PromptSelectionResult rso = ed.GetSelection(pso, filter);
if (rso.Status == PromptStatus.OK && rso.Value.Count > 0)
{
foreach (ObjectId id in rso.Value.GetObjectIds())
HatchFix1(id);
}
}
public void HatchFix1(ObjectId id)
{
Point3d ptGrip = Point3d.Origin;
using (Hatch h = id.Open(OpenMode.ForWrite) as Hatch)
{
using (GripDataCollection grips = new GripDataCollection())
{
h.GetGripPoints(grips, 1, 3, Vector3d.ZAxis, (GetGripPointsFlags)0);
using (GripDataCollection gripsMove = new GripDataCollection())
{
// Смещаем ручку в одну сторону на 0.00001
ptGrip = grips[1].GripPoint + Vector3d.XAxis * 1e-5;
gripsMove.Add(grips[1]);
h.MoveGripPointsAt(gripsMove, Vector3d.XAxis * 1e-5, 0);
}
}
}
using (Hatch h = id.Open(OpenMode.ForWrite) as Hatch)
{
using (GripDataCollection grips = new GripDataCollection())
{
h.GetGripPoints(grips, 1, 3, Vector3d.ZAxis, (GetGripPointsFlags)0);
using (GripDataCollection gripsMove = new GripDataCollection())
{
foreach (GripData grip in grips)
{
if (ptGrip.DistanceTo(grip.GripPoint) < 1e-6)
{
// Смещаем эту же ручку в обратную сторону на 0.00001
gripsMove.Add(grip);
h.MoveGripPointsAt(gripsMove, -Vector3d.XAxis * 1e-5, 0);
}
}
}
}
}
}
}
}