@swaywood wrote:
Hi:
I want to imitate the trim command and trim all the curlve outside a closed polyline or a circle.
I found the exactly lisp function but i want to finish it by c#.net, the following is the lisp code and gif pictrue.
https://autocadtips1.com/2012/03/14/trim-and-delete-outside-of-closed-polyline/
best wishes
swaywood
Give this a try:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
/// This code requires a reference to AcExportLayoutEx.dll:
using Autodesk.AutoCAD.ExportLayout;
namespace ClipExample
{
public static class ClipExampleCommands
{
static ObjectId GetClipBoundary(Editor ed)
{
var peo = new PromptEntityOptions("\nSelect boundary curve: ");
peo.SetRejectMessage("\nRequires a closed, planar curve");
peo.AddAllowedClass(typeof(Curve), false);
peo.AllowObjectOnLockedLayer = true;
peo.AllowNone = true;
var per = ed.GetEntity(peo);
if(per.Status != PromptStatus.OK)
return ObjectId.Null;
using(var tr = new OpenCloseTransaction())
{
try
{
Curve curve = (Curve) tr.GetObject(per.ObjectId, OpenMode.ForRead);
if(!(curve.Closed && curve.IsPlanar))
{
ed.WriteMessage("\nInvalid selection, requires a closed, planar curve.");
return ObjectId.Null;
}
return per.ObjectId;
}
finally
{
tr.Commit();
}
}
}
static SelectionSet GetObjectsToClip(Editor ed)
{
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.RejectObjectsFromNonCurrentSpace = true;
pso.RejectObjectsOnLockedLayers = true;
var filter = new SelectionFilter(
new[] { new TypedValue(0, "LINE,ARC,CIRCLE,SPLINE,LWPOLYLINE,ELLIPSE") });
var psr = ed.GetSelection(pso, filter);
return psr.Status == PromptStatus.OK ? psr.Value : null;
}
[CommandMethod("CLIPOBJECTS")]
public static void ClipObjectsCommand2()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
ObjectId boundaryId = GetClipBoundary(ed);
if(boundaryId.IsNull)
return;
SelectionSet selection = GetObjectsToClip(ed);
if(selection == null)
return;
try
{
using(Transaction tr = doc.TransactionManager.StartTransaction())
{
Curve boundary = (Curve) tr.GetObject(boundaryId, OpenMode.ForRead);
var btr = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach(ObjectId id in selection.GetObjectIds())
{
if(id != boundaryId)
{
using(Trimmer trimmer = new Autodesk.AutoCAD.ExportLayout.Trimmer())
{
Entity entityToTrim = (Entity) tr.GetObject(id, OpenMode.ForWrite);
trimmer.Trim(entityToTrim, boundary);
if(trimmer.HasAccurateResults)
{
foreach(Entity ent in trimmer.TrimResultObjects)
{
ent.SetPropertiesFrom(entityToTrim);
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
if(trimmer.EntityCompletelyOutside || trimmer.EntityOnBoundary)
entityToTrim.Erase();
}
}
}
}
tr.Commit();
}
}
catch(System.Exception ex)
{
ed.WriteMessage("\nOperation failed ({0})", ex.Message);
}
}
}
}