Jump to content

Trimming Lines within a rectangle


Sundar

Recommended Posts

Hi All,

I just registered in this forum and this is my first thread.Hope i will get what i want from you all masters..

I just want to trim the poly lines within the boundary selected by user.Lines to be trimmed exactly at the intersection of line and the rectangle....

Link to comment
Share on other sites

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • Sundar

    9

  • SEANT

    6

  • fixo

    3

  • Dadgad

    3

Top Posters In This Topic

Posted Images

Use the TRIM command, as follows.

 

TRIM > ENTER > ENTER > make your selection with a crossing box (starting from right and moving to the left).

 

I am not sure if you are wondering how to do this, or are looking for coding help.

trim all applied to rectangles.JPG

Link to comment
Share on other sites

I need to do this through VBA coding.all the lines outside of the selected boundary should be deleted.

 

Never mind then, as it was your first post I thought you might have put it in the wrong forum. Sorry I can't help you with VBA.

Link to comment
Share on other sites

I think i have put in correct forum as its under VBA.if not could u pls direct me to the correct forum?

 

No, you have not made a mistake, I did. Your post is right where it should be.

 

Where have I heard that before?

Link to comment
Share on other sites

Trimming lines/polylines is a fairly complex task from a programming perspective.

 

You would first have to determine which entities intersect with the rectangle (IntersectWith method).

 

Determine the number of intersections. If the polyline has Arcs/Bulges, there may even be more than two intersections.

 

Recreate all the entitles that have a start- or endpoint not within the rectangle, using that exterior point and the correct intersection point on the rectangle. The Start Endpoint direction may be critical if there are any LineType issues.

 

Transfer all properties of the original entity to these new recreations.

 

Delete original entities.

 

Alternatively, you may be able to use TRIM command, perhaps with FENCE option via ThisDrawing.SendCommand.

 

Yet abnother alternative may be to use a WipeOut.

Link to comment
Share on other sites

Thanks SEANT..while i was googling for this i came across the EXTRIM command and i did "thisdrawing.sendcommand("extrim")" thing.it worked fine but what if i want the result to be in a separate layer. i cant process the trimmed objects (from my knowledge)...

Link to comment
Share on other sites

If that is the case then it may be beneficial to iterate - and process - each entity separately. That iteration, though, probably eliminates the SendCommand option due to its asynchronous nature.

 

 

 

In my opinion, VBA is a poor programming tool for this task. It does not have a native API for dealing with all of AutoCAD’s available Curve functionality. VBA is also a deprecated programming language. So even if all the necessary functionality were custom built with VBA, it would not be useful for future releases of AutoCAD.

 

 

 

The project is much more feasible with managed/unmanaged ARX(VB.NET, for instance), or AutoLisp.

Edited by SEANT
Link to comment
Share on other sites

You can try to start from this scratch,

compare both comands in action

 
       [CommandMethod("ipLine")]
       public void IntersectTestIn()
       {
           Database db = HostApplicationServices.WorkingDatabase;
           Document doc = acadApp.DocumentManager.MdiActiveDocument;
           Editor ed = doc.Editor;
           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               PromptEntityOptions peo = new PromptEntityOptions("\nSelect a Line >>");
               peo.SetRejectMessage("\nYou have to select the Line only >>");
               peo.AddAllowedClass(typeof(Line), false);
               PromptEntityResult res;
               res = ed.GetEntity(peo);
               if (res.Status != PromptStatus.OK)
                   return;
               Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
               if (ent == null)
                   return;
               Line line = (Line)ent as Line;
               if (line == null) return;
               peo = new PromptEntityOptions("\nSelect a Rectangle: ");
               peo.SetRejectMessage("\nYou have to select the Polyline only >>");
               peo.AddAllowedClass(typeof(Polyline), false);
               res = ed.GetEntity("\nSelect a Rectangle: ");
               if (res.Status != PromptStatus.OK)
                   return;
               ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
               if (ent == null)
                   return;
               Polyline pline = (Polyline)ent as Polyline;
               if (line == null) return;
               Point3dCollection pts = new Point3dCollection();
               pline.IntersectWith(line, Intersect.ExtendArgument, pts, 0, 0);
               if (pts.Count == 0) return;
               List<Point3d> points = new List<Point3d>();
               points.Add(line.StartPoint);
               points.Add(line.EndPoint);
               foreach (Point3d p in pts)
                   points.Add(p);
               DBObjectCollection objs = line.GetSplitCurves(pts);
               List<DBObject> lstobj = new List<DBObject>();
               foreach (DBObject dbo in objs)
                   lstobj.Add(dbo);
               points.Sort(delegate(Point3d p1, Point3d p2)
               {
                   return Convert.ToDouble(
                       Convert.ToDouble(line.GetParameterAtPoint(p1))).CompareTo(
                   Convert.ToDouble(line.GetParameterAtPoint(p2)));
               }
      );
               BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
               Line ln0 = lstobj[1] as Line;// middle
               Line ln1 = new Line(points[0], points[1]);
               Line ln2 = new Line(points[2], points[3]);
               if (!ln0.IsWriteEnabled)
                   ln0.UpgradeOpen();
               ln0.Dispose();

               if (!ln1.IsWriteEnabled)
                   ln1.UpgradeOpen();
               btr.AppendEntity(ln1);
               tr.AddNewlyCreatedDBObject(ln1, true);
               if (!ln2.IsWriteEnabled)
                   ln2.UpgradeOpen();
    
               btr.AppendEntity(ln2);
               tr.AddNewlyCreatedDBObject(ln2, true);
               if (!line.IsWriteEnabled)
                   line.UpgradeOpen();
               line.Erase();
               line.Dispose();
               ed.Regen();
               tr.Commit();
           }
       }
       [CommandMethod("opLine")]
       public void IntersectTest()
       {
           Database db = HostApplicationServices.WorkingDatabase;
           Document doc = acadApp.DocumentManager.MdiActiveDocument;
           Editor ed = doc.Editor;
           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               PromptEntityOptions peo = new PromptEntityOptions("\nSelect a Line >>");
               peo.SetRejectMessage("\nYou have to select the Line only >>");
               peo.AddAllowedClass(typeof(Line), false);
               PromptEntityResult res;
               res = ed.GetEntity(peo);
               if (res.Status != PromptStatus.OK)
                   return;
               Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
               if (ent == null)
                   return;
               Line line = (Line)ent as Line;
               if (line == null) return;
               peo = new PromptEntityOptions("\nSelect a Rectangle: ");
               peo.SetRejectMessage("\nYou have to select the Polyline only >>");
               peo.AddAllowedClass(typeof(Polyline), false);
               res = ed.GetEntity("\nSelect a Rectangle: ");
               if (res.Status != PromptStatus.OK)
                   return;
               ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
               if (ent == null)
                   return;
               Polyline pline = (Polyline)ent as Polyline;
               if (line == null) return;
               Point3dCollection pts = new Point3dCollection();
               pline.IntersectWith(line, Intersect.ExtendArgument, pts, 0, 0);
               if (pts.Count == 0) return;
               DBObjectCollection lstobj = line.GetSplitCurves(pts);

               BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
               Line ln0 = lstobj[1] as Line;// middle
               Line ln1 = lstobj[0] as Line;
  
              Line ln2 = lstobj[2] as Line;
               if (!ln0.IsWriteEnabled)
                   ln0.UpgradeOpen();
               btr.AppendEntity(ln0);
               tr.AddNewlyCreatedDBObject(ln0, true);
               if (!ln1.IsWriteEnabled)
                   ln1.UpgradeOpen();
               ln1.Dispose();
             
               if (!ln2.IsWriteEnabled)
                   ln2.UpgradeOpen();
               ln2.Dispose();
             
               if (!line.IsWriteEnabled)
                   line.UpgradeOpen();
               line.Erase();
               line.Dispose();
               ed.Regen();
               tr.Commit();
           }
       }

 

~'J'~

Edited by fixo
new code added
Link to comment
Share on other sites

thanks for your code.solved almost all reference missing issues.But couldn't solve for Documentmanager.

Got error on this line :

 

Document doc = acadApp.DocumentManager.MdiActiveDocument;

 

Error is :

Error 1 'Autodesk.AutoCAD.Interop.AcadApplication' does not contain a definition for 'DocumentManager' and no extension method 'DocumentManager' accepting a first argument of type 'Autodesk.AutoCAD.Interop.AcadApplication' could be found (are you missing a using directive or an assembly reference?) D:\Users\Sundar\Visual Studio 2008\NewTrim\NewTrim\Class1.cs 23 27 NewTrim

 

Please suggest which reference im missing..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...