How to trim the culve outside the polyline?

How to trim the culve outside the polyline?

swaywood
Collaborator Collaborator
8,207 Views
16 Replies
Message 1 of 17

How to trim the culve outside the polyline?

swaywood
Collaborator
Collaborator

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

0 Likes
Accepted solutions (1)
8,208 Views
16 Replies
Replies (16)
Message 2 of 17

ActivistInvestor
Mentor
Mentor
Accepted solution

@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);
         }
      }
   }
}
Message 3 of 17

_gile
Consultant
Consultant

@ActivistInvestor Very nice !

Thanks to make me discover this API.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 17

ActivistInvestor
Mentor
Mentor

@_gile wrote:

@ActivistInvestor Very nice !

Thanks to make me discover this API.

 


Thanks @_gile.

 

Don't forget that as you discovered not long ago, closed, 'heavy', spline-fit polylines will blow up GetSplitCurves() which the Trimmer class uses, so it shouldn't be used on them.

 

 

0 Likes
Message 5 of 17

_gile
Consultant
Consultant

Thank you for reminding this too.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 17

swaywood
Collaborator
Collaborator

@ActivistInvestor@_gile

 

thanks for your help.

It would be better if the trimer supports hatch entity.

Work-229.png.

0 Likes
Message 7 of 17

ActivistInvestor
Mentor
Mentor

@swaywood wrote:

@ActivistInvestor@_gile

 

thanks for your help.

It would be better if the trimer supports hatch entity.

.


The trimmer doesn't support blocks or hatching. That's supported by EXPORTLAYOUT but it happens at a higher-level in the code, which is more complicated to use because not all of the classes involved are public.

 

You can write code that explodes the hatching (and block insertions if you need that), and the trimmer will clip the exploded objects. The objects that are to be clipped do not have to be in a database.

Message 8 of 17

Anonymous
Not applicable

I have the same problem ,thanks

0 Likes
Message 9 of 17

Anonymous
Not applicable

you are a nice man ,thanks

0 Likes
Message 10 of 17

Anonymous
Not applicable

😀hi, Great!  I want to ask more, I have read this post: https://autocadtips1.com/2012/03/08/autolisp-trim-objects-on-one-side/
and I tried using the following code but why it didn't work? if typeof TrimEnt is Line then it'll work but it isn't working with Pline,Spline

Dim pick As ResultBuffer = New ResultBuffer()
                        pick.Add(New TypedValue(CShort(LispDataType.ListBegin)))
                        pick.Add(New TypedValue(CShort(LispDataType.ObjectId), TrimEntID))
                        pick.Add(New TypedValue(CShort(LispDataType.Point3d), Pickpt))
                        pick.Add(New TypedValue(CShort(LispDataType.ListEnd)))
   ed.Command("trim", boundset, "", pick, "")

Thanks a lot.

0 Likes
Message 11 of 17

Anonymous
Not applicable

what code must to edit if trim only in inside rectangle
i was try change this if (!trimmer.EntityOnBoundary) but not sucsess

0 Likes
Message 12 of 17

Anonymous
Not applicable

Hi! I'm trying to use the trimmer function in AutoCAD 2021, however I can't find any reference to "Autodesk.AutoCAD.ExportLayout" or AcExportLayoutEx.dll in the documentation. Do you know where I can find info on using these functionalities? Thanks 🙂
Tyler

0 Likes
Message 13 of 17

_gile
Consultant
Consultant

Hi,

You should find it in the AutoCAD installation folder (default: C:\Program Files\Autodesk\AutoCAD 20XX).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 14 of 17

kiruba
Contributor
Contributor

I had the same requirement, but with a hatch as output. but this helps

Digital Automation in Design Transformation
0 Likes
Message 15 of 17

kiruba
Contributor
Contributor

This is amazing. i was searching this for implementing custom shape hatch. Although the result is not a hatch, i can live with it. thanks again

Digital Automation in Design Transformation
0 Likes
Message 16 of 17

kiruba
Contributor
Contributor

This works perfectly. however, i need to clip with nested boundaries. For this i need to clip the entities inside trim boundary. i am unable to achieve this. is there a solution?

Digital Automation in Design Transformation
0 Likes
Message 17 of 17

Activist_Investor
Contributor
Contributor

Well, you could certainly modify the code I posted to perform the inverse operation (remove objects or portions thereof that are within the trim boundary rather than outside of it) and use that modified code with the nested boundary. The original code was never designed to trim against a region with inner loops.

 

 

0 Likes