.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trim function

8 REPLIES 8
Reply
Message 1 of 9
alecu8
708 Views, 8 Replies

Trim function

I'm looking for a way to trim an ellipse. Would trim function work in here? If i open the drawing it is very easy to use trim function, but in .net i'm not to sure.

 

I am attaching a pic for that.

 

Thank you for your time.

 

Alex

 

 

acad_trim.jpg

 

 

8 REPLIES 8
Message 2 of 9
_gile
in reply to: alecu8

Hi,

 

You should give more information about the context you want to achieve this and/or provide the code you have write so far.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 9
alecu8
in reply to: _gile

Thanks for your reply.

 

The curves are to ellipses cut by a vertical line. i got the intersection points using IntersectWith function and i created 2 small vertical lines, one on top and one at the bottom. Now next step is to eliminate the left side of the ellipses at the vertical lines. To use trim in actual drawing is quite easy, and i'have been doing this for some time, but to program, i don't know how to do it. I have no code to trim the ellipses. Just code to draw them and to intersect with the vertical line.

 

Thanks,

Alex

 

 

Message 4 of 9
_gile
in reply to: alecu8

The following code assumes the intersection points (intersPts) have the same X coordinates (vertical line).

 

        private void TrimLeft(Ellipse ellipse, Point3dCollection intersPts, Transaction tr)
        {
            var parameters = new DoubleCollection(
                intersPts.Cast<Point3d>()
                .Select(p => ellipse.GetParameterAtPoint(p))
                .OrderBy(p => p)
                .ToArray());
            var x = intersPts[0].X;
            var btr = (BlockTableRecord)tr.GetObject(ellipse.OwnerId, OpenMode.ForWrite);
            foreach (Ellipse item in ellipse.GetSplitCurves(parameters))
            {
                if (item.GetPointAtParameter((item.StartParam + item.EndParam) / 2.0).X < x)
                {
                    item.Dispose();
                }
                else
                {
                    btr.AppendEntity(item);
                    tr.AddNewlyCreatedDBObject(item, true);
                }
            }
            if (!ellipse.IsWriteEnabled)
                ellipse.UpgradeOpen();
            ellipse.Erase();
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 9
alecu8
in reply to: _gile

Gilles,

 

 

Thank you so much for the code. I will implement in vb.net and check it out. I have a spline instead of ellipse. will the code work with spline or do i have to do some transformations first?

 

Thanks,

Alex

 

Message 6 of 9
_gile
in reply to: alecu8

Not tested but it should work with any type derived from Curve.

The line used to get intersection points must be vertical.

 

        private void TrimLeft(Curve curve, Point3dCollection intersPts, Transaction tr)
        {
            var parameters = new DoubleCollection(
                intersPts.Cast<Point3d>()
                .Select(p => curve.GetParameterAtPoint(p))
                .OrderBy(p => p)
                .ToArray());
            var x = intersPts[0].X;
            var btr = (BlockTableRecord)tr.GetObject(curve.OwnerId, OpenMode.ForWrite);
            foreach (Curve item in curve.GetSplitCurves(parameters))
            {
                if (item.GetPointAtParameter((item.StartParam + item.EndParam) / 2.0).X < x)
                {
                    item.Dispose();
                }
                else
                {
                    btr.AppendEntity(item);
                    tr.AddNewlyCreatedDBObject(item, true);
                }
            }
            if (!curve.IsWriteEnabled)
                curve.UpgradeOpen();
            curve.Erase();
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 9
SENL1362
in reply to: _gile

Nice LINQ interpretation for  sorted points based on the ellips parameters.

You're realy good in LINQ solutions.

Thanks for these samples.

Message 8 of 9
_gile
in reply to: SENL1362

Thanks @SENL1362

I like to use Linq extension methods because I think these (higher order) functions make things simpler and clearer.

This is perhaps due to some functional programming background with LISP and F# (Linq extension methods chaining looks like F# pipe-lining).

If it was not so confidential in AutoCAD .NET programming, I'd like to use more often F#.

 

let trimLeft (curve: Curve) (pts: Point3dCollection) (tr: Transaction) =
    let parameters = 
        DoubleCollection(pts
            |> Seq.cast<Point3d>
            |> Seq.map(fun p -> curve.GetParameterAtPoint(p))
            |> Seq.sort
            |> Seq.toArray)
    let x = pts.[0].X
    let btr = tr.GetObject(curve.OwnerId, OpenMode.ForWrite) :?> BlockTableRecord
    curve.GetSplitCurves(parameters)
    |> Seq.cast<Curve>
    |> Seq.iter(fun c ->
        if c.GetPointAtParameter((c.StartParam + c.EndParam) / 2.0).X < x then
            c.Dispose()
        else
            btr.AppendEntity(c) |> ignore
            tr.AddNewlyCreatedDBObject(c, true))
    if not curve.IsWriteEnabled then curve.UpgradeOpen()
    curve.Erase()


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 9
SENL1362
in reply to: _gile

I totally agree with you, clean and  to the point.

And than only for the Query form so it can be chained (.where().select()...

I am not familiar with F, heard from it and seen it occasionaly in the SWAMP 🙂

Seeing LET gives me flashbacks from the ancient VB times....

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost