Use Trim option by C# api

Use Trim option by C# api

IT-Fundilusa
Explorer Explorer
722 Views
6 Replies
Message 1 of 7

Use Trim option by C# api

IT-Fundilusa
Explorer
Explorer

Hello,

I need to use the Trim by code option, I create lines in a Sketch line1, line2, line3 and line4.
I need to trim line 4 from the end of the line to the intersection with line 2

Code:

PlanarSketch sketch = default(PlanarSketch);
sketch = partDef.Sketches.Add(partDef.WorkPlanes[3]);
TransientGeometry tg = default(TransientGeometry);
tg = ThisApplication.TransientGeometry;

Line1 = sketch.SketchLines.AddByTwoPoints(tg.CreatePoint2d(0, 10), tg.CreatePoint2d(-20, 10));
Line2 = sketch.SketchLines.AddByTwoPoints(tg.CreatePoint2d(-20, 10), tg.CreatePoint2d(-15, 0));
Line3 = sketch.SketchLines.AddByTwoPoints(tg.CreatePoint2d(0, 10), tg.CreatePoint2d(0, 5));
Line4 = sketch.SketchLines.AddByTwoPoints(tg.CreatePoint2d(0, 5), tg.CreatePoint2d(-20, 5));

0 Likes
Accepted solutions (2)
723 Views
6 Replies
Replies (6)
Message 2 of 7

florian_wenzel
Advocate
Advocate

Hi,

i think there is no API Methode for Trim in Sketch.

Correct me if im wrong.

But you can Create new line using the Intersection Points.

The Result is similary

0 Likes
Message 3 of 7

IT-Fundilusa
Explorer
Explorer

How do I get the point of intersection of two lines?

0 Likes
Message 4 of 7

florian_wenzel
Advocate
Advocate

 Line4.IntersectWithCurve(Line2,)

 

 

0 Likes
Message 5 of 7

IT-Fundilusa
Explorer
Explorer

The type of line4 is SketchLine and the does not have this function. 😞

0 Likes
Message 6 of 7

florian_wenzel
Advocate
Advocate
Accepted solution

Change the SketchLine into LineSegment2d.

Inventor 2022 Help | LineSegment2d Object | Autodesk

 

Here a sample in VB.NET

 

Dim oLineSegment01 As LineSegment2d
oLineSegment01 = oSketchLine01.Geometry

 

C# maybe 🙂

 

LineSegment2d oLineSegment01 =oSketchLine01.Geometry

0 Likes
Message 7 of 7

Michael.Navara
Advisor
Advisor
Accepted solution

Here is a sample code for something like Trim Function

var pick1 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchCurveLinearFilter, "Pick line 1");
var pick2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchCurveLinearFilter, "Pick line 2");
SketchLine line1 = pick1 as SketchLine;
SketchLine line2 = pick2 as SketchLine;

var intersectWithCurve = line1.Geometry.IntersectWithCurve(line2.Geometry);
foreach (var o in intersectWithCurve)
{
    if (o is Point2d pt2d)
    {
        var geometricConstraints = line1.Parent.GeometricConstraints;
        var line1StartSketchPoint = line1.StartSketchPoint;
        var line1EndSketchPoint = line1.EndSketchPoint;

        var distanceFromStart = line1StartSketchPoint.Geometry.DistanceTo(pt2d);
        var distanceFromEnd = line1EndSketchPoint.Geometry.DistanceTo(pt2d);

        if (distanceFromStart > distanceFromEnd)
        {
            line1EndSketchPoint.MoveTo(pt2d);
            geometricConstraints.AddCoincident(line1EndSketchPoint as SketchEntity, line2 as SketchEntity);
        }
        else
        {
            line1StartSketchPoint.MoveTo(pt2d);
            geometricConstraints.AddCoincident(line1StartSketchPoint as SketchEntity, line2 as SketchEntity);
        }
    }
}