intersect method of two intersected lines not working

intersect method of two intersected lines not working

a.kouchakzadeh
Advocate Advocate
1,146 Views
12 Replies
Message 1 of 13

intersect method of two intersected lines not working

a.kouchakzadeh
Advocate
Advocate

Hi every one, Im facing a wierd condition that tow lines are interesected, but the intersect method does not work.

this is the code:

 

akouchakzadeh_0-1676366702811.png

its not just this, some lines that intersect with blocks have the same issue:

 

this is my code:

 

 

 

[CommandMethod("intersector")]
        public void inters()
        {
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var promptEntOpt1 = new PromptEntityOptions("Please select a first object");
                var res1 = ed.GetEntity(promptEntOpt1);
                var ent1 = (Entity)tr.GetObject(res1.ObjectId, OpenMode.ForRead);

                promptEntOpt1.Message = "\nPlease select a  second object";
                var res2 = ed.GetEntity(promptEntOpt1);
                var ent2 = (Entity)tr.GetObject(res1.ObjectId, OpenMode.ForRead);

                var intersect = new Point3dCollection();
                ent1.IntersectWith(ent2, Intersect.OnBothOperands, intersect, IntPtr.Zero, IntPtr.Zero);
                Application.ShowAlertDialog(intersect.Count.ToString());

            }
        }

 

0 Likes
Accepted solutions (1)
1,147 Views
12 Replies
Replies (12)
Message 2 of 13

_gile
Consultant
Consultant

Hi,

 

var ent2 = (Entity)tr.GetObject(res1.ObjectId, OpenMode.ForRead);

It should be: res2.ObjectId.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 13

a.kouchakzadeh
Advocate
Advocate

Hi Giles,

that was my mistake but actually it was just a test method to check the main problem.

how ever still I have this issue with my real drawing. 

 

akouchakzadeh_0-1676379868077.png

 

akouchakzadeh_1-1676379884097.png

Please take a look at this condition,

both line and arc are on the same plane and have no z value

 

but when I try the intersect method, it showing 0 even though end of the line is at the same point which is end of the Arc

akouchakzadeh_2-1676380056781.png

now the crazy part is when I manually change the end point of the arc and return it back to where it was, it counts the intersection.

the arcs where created using fillet (if this has any impact on the process).

why is the program acting like this and how can i get the intersection at the end?

 

 

0 Likes
Message 4 of 13

SENL1362
Advisor
Advisor

I've seen similar issues while Joining to Polylines, endpoint are equal, but still failed to add.

Can be solved by manually moving the Arc endpoint back and forth to the same endpoint.

The real issue might be a not exact alignments of the Tangential Lines at the endpoints.

Check/Set StartAngle and EndAngle.

 

0 Likes
Message 5 of 13

a.kouchakzadeh
Advocate
Advocate

its not just Line and Arc objects. I this problem with Line and Block references as well. some blocks and lines wont show the intersection even though the line is completely on that specific block and has intersection with all its elements.

I have also check if objects are on the same plane and they are. non of them has a Z value

0 Likes
Message 6 of 13

_gile
Consultant
Consultant

Are the entites far from the origin ? If so, there can be imprecisions due to the double precision floating point.

In this case, you can move the entities nearer to the Origin, get the intersections and move back the entities and the points.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 13

a.kouchakzadeh
Advocate
Advocate
I dont know what do you mean by origin.
can you tell me what is origin and how can I check it?
also can you tell me how to move entities nearer to the origin?

I checked the start and end point of both line and arcs. as shown in the picture, Line.EndPoint && Arc.EndPoint are at the same point.
how ever with only 4 precessions.
0 Likes
Message 8 of 13

_gile
Consultant
Consultant

Sorry for the late reply. By "Origin" I meant the origin of WCS (0.0, 0.0, 0.0). But looking at your screenshot, I don't think this is the problem. Your coordinates are in the tens of thousands and these inaccuracy problems occur in the tens of millions.

 


@a.kouchakzadeh  a écrit :
I checked the start and end point of both line and arcs. as shown in the picture, Line.EndPoint && Arc.EndPoint are at the same point.
how ever with only 4 precessions.

By default the Tolerance.Global.EqualPoint values is 1E-10 (10 digits).

You should check it by code using: Line.EndPoint.IsEqualTo (Arc.EndPoint).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 13

a.kouchakzadeh
Advocate
Advocate

I have uploaded the file just in case if you could take a look Giles.

I had this issue with some block to lines intersections as well.

0 Likes
Message 10 of 13

_gile
Consultant
Consultant

Finally, the issue seems to be related to great coordinates.

Moving the entities closer to the WCS Origin seems to work.

 

Here's an example to get all the intersections points between the supplied entities.

        static Point3dCollection GetIntersections(IEnumerable<Entity> source)
        {
            var entities = source.ToArray();

            // Get the vector from WCS Origin to the extents of the first entity
            var extents = entities[0].GeometricExtents;
            var displacement = extents.MinPoint.GetAsVector();

            // Move all entities nearer to origin
            var xform = Matrix3d.Displacement(displacement.Negate());
            foreach (var entity in entities)
            {
                if (!entity.IsWriteEnabled) entity.UpgradeOpen();
                entity.TransformBy(xform);
            }

            // Get all the intersections between entities
            var points = new Point3dCollection();
            for (var i = 0; i < entities.Length - 1; i++)
            {
                var ent1 = entities[i];
                for (int j = i + 1; j < entities.Length; j++)
                {
                    var ent2 = entities[j];
                    ent1.IntersectWith(ent2, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
                }
            }

            // Move back the entities
            xform = Matrix3d.Displacement(displacement);
            foreach (var entity in entities)
            {
                entity.TransformBy(xform);
            }

            // Return the transformed points
            return new Point3dCollection(
                points
                .Cast<Point3d>()
                .Select(p => p.TransformBy(xform))
                .ToArray());
        }

 

A testing command:

        [CommandMethod("TEST")]
        public void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            db.Pdmode = 32;

            var selection = ed.GetSelection();
            if (selection.Status != PromptStatus.OK)
                return;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var entities = selection.Value
                    .GetObjectIds()
                    .Select(id => (Entity)tr.GetObject(id, OpenMode.ForRead));
                var points = GetIntersections(entities);
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (Point3d pt in points)
                {
                    var point = new DBPoint(pt);
                    point.ColorIndex = 30;
                    curSpace.AppendEntity(point);
                    tr.AddNewlyCreatedDBObject(point, true);
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 13

a.kouchakzadeh
Advocate
Advocate

Giles, your code throws an error

the program still cant detect the intersection:

akouchakzadeh_0-1676586204954.png

 

Im trying to get the line and the AOM block intersection.

0 Likes
Message 12 of 13

_gile
Consultant
Consultant
Accepted solution

This error is due to the fact there's no intersection point (points.Count == 0) because of the Z coordinate of the AOM block.

Here's a correctd code which corrects that.

        static Point3dCollection GetIntersections(IEnumerable<Entity> source)
        {
            var entities = source.ToArray();

            // Get the vector from WCS Origin to the extents of the first entity
            var extents = entities[0].GeometricExtents;
            var displacement = extents.MinPoint.GetAsVector();

            // Move all entities nearer to origin
            var xform = Matrix3d.Displacement(displacement.Negate());
            foreach (var entity in entities)
            {
                if (!entity.IsWriteEnabled) entity.UpgradeOpen();
                entity.TransformBy(xform);
            }

            // Get all the intersections between entities
            var points = new Point3dCollection();
            for (var i = 0; i < entities.Length - 1; i++)
            {
                var ent1 = entities[i];
                for (int j = i + 1; j < entities.Length; j++)
                {
                    var ent2 = entities[j];
                    ent1.IntersectWith(ent2, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
                }
            }

            // Move back the entities
            xform = Matrix3d.Displacement(displacement);
            foreach (var entity in entities)
            {
                entity.TransformBy(xform);
            }

            // Return the transformed points
            if (0 < points.Count)
            {
                return new Point3dCollection(
                    points
                    .Cast<Point3d>()
                    .Select(p => p.TransformBy(xform))
                    .ToArray());
            }
            else
            {
                return points;
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 13 of 13

a.kouchakzadeh
Advocate
Advocate

Thank you sir

0 Likes