Can't get intersection point using Entity.IntersectWith sometimes

Can't get intersection point using Entity.IntersectWith sometimes

tom.zhengGNS4G
Contributor Contributor
827 Views
6 Replies
Message 1 of 7

Can't get intersection point using Entity.IntersectWith sometimes

tom.zhengGNS4G
Contributor
Contributor

Hi! I'm new to AutoCAD .NET and trying to get the intersection point of 2 lines using Entity.IntersectWith method lately. Usually, it can find the intersection points. but sometimes it doesn't get it when there should be intersection point between 2 lines.  Can' t figured it out why. can someone help or give some suggestions? thx!

 

Below is my code for test. I iterate each 2 lines in all lines selected by user and draw a circle on every intersection point. I write in c#:

```

[CommandMethod("Test")]
public void Test()
{
    Active.UsingTransaction(tr =>
    {

        var lineResult = Selector.GetSelection("\nselect lines: ");  //get user selection
        if (lineResult.Status != PromptStatus.OK) return;

 

        var lines = new List<Line>();
        foreach (var objId in lineResult.Value.GetObjectIds())
            lines.Add(objId.GetObject(OpenMode.ForRead) as Line);

 

        for (var i = 0; i < lines.Count; i++)
        {
            if (i + 1 == lines.Count) break;

            for (var j = i + 1; j < lines.Count; j++)
            {
                Line line1 = lines[i], line2 = lines[j];
                var interPts = new Point3dCollection();

 

                line1.IntersectWith(line2, Intersect.OnBothOperands, interPts, IntPtr.Zero, IntPtr.Zero);

                if (interPts.Count > 0) Draw.DrawCircle(interPts[0], 1);  //draw a circle with radius 1 on intersection point
            }
        }
    });
}

```

 

tomzhengGNS4G_1-1684816311353.png

tomzhengGNS4G_2-1684816416224.png

 

 

 

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

_gile
Consultant
Consultant

Hi,

Try with LineSegment3d.IntersectWith instead.

 

        [CommandMethod("Test")]
        public void Test()
        {
            Active.UsingTransaction(tr =>
            {
                var lineResult = Selector.GetSelection("\nselect lines: ");  //get user selection
                if (lineResult.Status != PromptStatus.OK) return;

                var lineSegments = lineResult.Value
                    .GetObjectIds()
                    .Select(id => (Line)id.GetObject(OpenMode.ForRead))
                    .Select(line => new LineSegment3d(line.StartPoint, line.EndPoint))
                    .ToArray();

                for (var i = 0; i < lineSegments.Length - 1; i++)
                {
                    for (var j = i + 1; j < lineSegments.Length; j++)
                    {
                        var line1 = lineSegments[i];
                        var line2 = lineSegments[j];
                        var interPts = line1.IntersectWith(line2);
                        if (interPts != null) 
                            Draw.DrawCircle(interPts[0], 1);  //draw a circle with radius 1 on intersection point
                    }
                }
            });
        }

 

When you post some code:

  • Use the 'Insert/Edit code sample' button.
  • If the code calls method from external libraries, provide the libraries source or a link to them.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

_gile
Consultant
Consultant

I should have started there.

Are you sure there is an intersection between these lines (i.e. they are on the same plane)?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 7

tom.zhengGNS4G
Contributor
Contributor

I checked all the 'Z' values of line's StartPoint/EndPoint are '0'. Does this mean they are on the same plane?  sorry I'm not familiar with AutoCAD.

0 Likes
Message 5 of 7

_gile
Consultant
Consultant

@tom.zhengGNS4G  a écrit :

I checked all the 'Z' values of line's StartPoint/EndPoint are '0'. Does this mean they are on the same plane?  sorry I'm not familiar with AutoCAD.


Yes, that means all the lines lies on the XY plane.

 

Another thing, if the lines are far away from the origin (about x0,000,000 units for x and/or y) there may be an accuracy issue. A workaround is moving all lines closer to the origin, get the intersections and move back the lines (and intsection points).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 7

tom.zhengGNS4G
Contributor
Contributor

Thanks for your suggestion! I found that some lines are not on the same plane. The property panel in AutoCAD shows that all lines are on the Z=0 plane. But when I get the position by code it shows that some line's Z value is not 0.

 

tomzhengGNS4G_0-1684831199720.png

in AutoCAD it shows that all the Z-value is 0.

 

 

tomzhengGNS4G_1-1684831415464.png

get the position of same line by code.

 

so I use a method to transform line to Z=0 plane if it is not.

void transformZAxis(Line line)
{
    line.UpgradeOpen();

    if (line.StartPoint.Z != 0)
        line.StartPoint = line.StartPoint.TransformBy(Matrix3d.Displacement(new Vector3d(0, 0, -line.StartPoint.Z)));
    if (line.EndPoint.Z != 0)
        line.EndPoint = line.EndPoint.TransformBy(Matrix3d.Displacement(new Vector3d(0, 0, -line.EndPoint.Z)));

    line.DowngradeOpen();
}

 

After all the lines transform to the same plane. it successfully get all the intersection points. Thanks again for your advice.

0 Likes
Message 7 of 7

_gile
Consultant
Consultant
Accepted solution

You could simply do:

void transformZAxis(Line line)
{
    line.UpgradeOpen();

    if (line.StartPoint.Z != 0)
        line.StartPoint = new Point3d(line.StartPoint.X, line.StartPoint.Y, 0.0);
    if (line.EndPoint.Z != 0)
        line.EndPoint = new Point3d(line.EndPoint.X, line.EndPoint.Y, 0.0);

    line.DowngradeOpen();
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub