A bug of IntersectWith() in Autocad 2014!

A bug of IntersectWith() in Autocad 2014!

Anonymous
Not applicable
1,697 Views
9 Replies
Message 1 of 10

A bug of IntersectWith() in Autocad 2014!

Anonymous
Not applicable

In a dwg ,I test a line is really intersectwith a blockreference .The dwg is the follow.But i found it always occour a error :enotapplicable!Oh my god !

The way to test a line is really intersectwith a blockreference is iterating the entity in BlocktableRecord of blockreference and testing whether the line is intersectwith the entity.Does anyone can give me some help?Thanks a lot!

Here is my code:

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\n请选择一条直线");
            peo.SetRejectMessage("\n选择的类型不正确");
            peo.AddAllowedClass(typeof(Line), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                PromptEntityOptions peo1 = new PromptEntityOptions("\n请选择一个图块");
                peo1.SetRejectMessage("\n选择的类型不正确");
                peo1.AddAllowedClass(typeof(BlockReference), true);
                PromptEntityResult per1 = ed.GetEntity(peo1);
                if (per1.Status == PromptStatus.OK)
                {
                    using (Transaction tran = db.TransactionManager.StartTransaction())
                    {
                        BlockReference br = per1.ObjectId.GetObject(OpenMode.ForRead) as BlockReference;
                        AcadLine l = per.ObjectId.GetObject(OpenMode.ForRead).AcadObject as AcadLine;
                        BlockTableRecord btr = br.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
                        
                        foreach (ObjectId entid in btr)
                        {
                            
                            AcadEntity ent = (entid.GetObject(OpenMode.ForRead).AcadObject as AcadEntity).Copy() as AcadEntity;
                            dynamic count = l.IntersectWith(ent, AcExtendOption.acExtendNone);
                            
                        }
                        tran.Commit();
                    }
                }
            }
0 Likes
Accepted solutions (1)
1,698 Views
9 Replies
Replies (9)
Message 2 of 10

_gile
Consultant
Consultant

Hi,

 

This is not a bug.

 

You're trying to use the IntersectWith method with entities that do not belong to the same owner:

  • the line owner is the the current space BlockTableRecord ;
  • the other entities owner is the selected block reference BlockTableRecord.

If you want to get the intersection points between the line and the block reference components, you may explode the block reference and check for intersection with the resulting entities.

 

PS: you do not need to use the COM IntersectWith() method, the .NET API provides one.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 10

_gile
Consultant
Consultant

You can try something like this:

 

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\n请选择一条直线");
            peo.SetRejectMessage("\n选择的类型不正确");
            peo.AddAllowedClass(typeof(Line), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                PromptEntityOptions peo1 = new PromptEntityOptions("\n请选择一个图块");
                peo1.SetRejectMessage("\n选择的类型不正确");
                peo1.AddAllowedClass(typeof(BlockReference), true);
                PromptEntityResult per1 = ed.GetEntity(peo1);
                if (per1.Status == PromptStatus.OK)
                {
                    using (Transaction tran = db.TransactionManager.StartTransaction())
                    {
                        var br = (BlockReference)tran.GetObject(per1.ObjectId, OpenMode.ForWrite);
                        var l = (Line)tran.GetObject(per.ObjectId, OpenMode.ForRead);
                        var pts = new Point3dCollection();
                        using (var dbObjs = new DBObjectCollection())
                        {
                            br.Explode(dbObjs);
                            foreach (DBObject dbObj in dbObjs)
                            {
                                try
                                {
                                    var ent = (Entity)dbObj;
                                    ent.IntersectWith(l, Intersect.OnBothOperands, pts, IntPtr.Zero, IntPtr.Zero);
                                }
                                catch (System.Exception ex)
                                {
                                    ed.WriteMessage("\n" + ex.Message);
                                }
                                finally
                                {
                                    dbObj.Dispose();
                                }
                            }
                        }
                        var spaceBtr = (BlockTableRecord)tran.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        db.Pdmode = 32;
                        foreach (Point3d pt in pts)
                        {
                            var dbPt = new DBPoint(pt);
                            dbPt.ColorIndex = 30;
                            spaceBtr.AppendEntity(dbPt);
                            tran.AddNewlyCreatedDBObject(dbPt, true);
                        }
                        tran.Commit();
                        ed.Regen();
                    }
                }
            }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 10

Anonymous
Not applicable

I try your code and i just found that error still alive...Did you use the drawing i given to test your code?

0 Likes
Message 5 of 10

_gile
Consultant
Consultant

Yes I did and got this error with the text containd in the block. I thaught it was a font issue (I had a message about unknown fonts when openig this drawing)

Anyway, this code worked fine with other blocks which also contain texts.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 10

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

I try your code and i just found that error still alive...Did you use the drawing i given to test your code?


You need to be more specific. If you are getting an exception of eNotApplicable, you have to find out what API call is throwing it. Use try/catch or run the code in the debugger to find out.

0 Likes
Message 7 of 10

_gile
Consultant
Consultant

My mistake, testing more accurately, the exception seems to be thrown by the hatch.

 

Try replacing:

catch (System.Exception ex)
{
    ed.WriteMessage("\n" + ex.Message);
}

with:

catch (System.Exception ex)
{
    ed.WriteMessage("\n{0}: {1}", dbObj.GetType().Name, ex.Message);
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 10

Anonymous
Not applicable

Yes,you are right.when i try to test the line is intersectwith the hatch ,it will occur a error:enotapplicable.I don't kown why will be that.

0 Likes
Message 9 of 10

_gile
Consultant
Consultant
Accepted solution

The error seems to be due to the SOLID hatch (try removing it from the block definition and all works fine).

 

The fact IntersectWith is not applicable with a SOLID hatch make sense for me.

 

Note this is not specific to AutoCAD 2014. I got the same exception with other versions of AutoCAD (2013 to 2018).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 10

Anonymous
Not applicable
Yes,you are right.Thanks a lot.
0 Likes