Boprs/Commands.cs

115 lines
2.9 KiB
C#
Raw Normal View History

2024-09-01 13:10:19 +02:00
#if ACAD24
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
#elif BCAD
using Teigha.Runtime;
using Bricscad.EditorInput;
using Teigha.Geometry;
using Teigha.DatabaseServices;
using Teigha.Colors;
#endif
using System;
using System.Collections.Generic;
using Boprs;
#if DEBUG
[assembly: CommandClass(typeof(Commands))]
namespace Boprs
{
internal class Commands
{
private Region UserCreateRegion(string msg)
{
AcAp.Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument;
Database acDb = acDoc.Database;
acDoc.Editor.WriteMessage("\n" + msg);
PromptSelectionResult res = acDoc.Editor.GetSelection();
if (res.Status != PromptStatus.OK || res.Value.Count == 0)
{
return null;
}
List<LineSegment2d> segs = new List<LineSegment2d>();
using (Transaction acTrans = acDb.TransactionManager.StartTransaction())
{
foreach (ObjectId asObjId in res.Value.GetObjectIds())
{
Polyline asPoly = (Polyline)acTrans.GetObject(asObjId, OpenMode.ForWrite);
using (DBObjectCollection exploded = new DBObjectCollection())
{
asPoly.Explode(exploded);
foreach(DBObject dBObject in exploded)
{
Line asLine = (Line)dBObject;
segs.Add(new LineSegment2d(asLine.StartPoint.To2d(), asLine.EndPoint.To2d()));
}
}
}
acTrans.Commit();
}
return Region.FromSegments(segs);
}
private BoprType UserChooseBoprType()
{
AcAp.Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument;
PromptKeywordOptions keyPrompt = new PromptKeywordOptions("\nBoolean operation type: ");
keyPrompt.Keywords.Add("Unite");
keyPrompt.Keywords.Add("Intersect");
keyPrompt.Keywords.Add("Subtract");
keyPrompt.Keywords.Add("eXclusive");
keyPrompt.AllowNone = false;
PromptResult keywRes = acDoc.Editor.GetKeywords(keyPrompt);
switch (keywRes.StringResult.ToLower())
{
case "unite":
return BoprType.UNITE;
case "intersect":
return BoprType.INTERSECT;
case "subtract":
return BoprType.SUBTRACT;
case "exclusive":
return BoprType.EXCLUSIVE;
default:
return BoprType.INTERSECT;
}
}
[CommandMethod("boprtest")]
public void Subdivide()
{
AcAp.Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument;
Region subject = UserCreateRegion("Enter polylines for first polygon:");
Region clip = UserCreateRegion("Enter polylines for second polygon:");
if(subject == null || clip == null)
{
acDoc.Editor.WriteMessage("\nOne of the polyline selections was empty.");
return;
}
BoprType type = UserChooseBoprType();
BooleanOperation bopr = new BooleanOperation(subject, clip, type);
bopr.Result.Draw();
}
}
}
#endif