Select objects inside block with a polygon with using .net

Select objects inside block with a polygon with using .net

anhtrung
Advocate Advocate
2,265 Views
9 Replies
Message 1 of 10

Select objects inside block with a polygon with using .net

anhtrung
Advocate
Advocate

Hi anybody,

 

I have a question and who is help me to solve this problem. That is a have a block and it's contain objects, and it's also have a polyline into block. So, i want to get all object inside block with inside polyline. how to create new function or new any idea help to me.

 

I'm using C.#

 

Thanks.

 

Accepted solutions (1)
2,266 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable

You could use the Editor. GetNestedEntity() method to get the line.

The objectId is the one the line within the block definition.

 

            using (var tr = db.TransactionManager.StartTransaction())

            {

                PromptNestedEntityResult result;

                while (true)

                {

                    result = ed.GetNestedEntity("\nSelect a line within a block reference: ");

 

                    // user cancelled

                    if (result.Status != PromptStatus.OK)

                    {

                        return;

                    }

 

                    // check if the selected object is a line

                    if (result.ObjectId.ObjectClass.DxfName == "LINE")

                    {

                        // check if the line is nested

                        if (result.GetContainers().Length == 0)

                        {

                            ed.WriteMessage("\nThe selected line is not nested in a block reference.");

                        }

                        else

                        {

                            break;

                        }

                    }

                    else

                    {

                        ed.WriteMessage("\nThe selected entity is not a line.");

                    }

                }

 

                // open the line of the block defintion

                var line = (Line)tr.GetObject(result.ObjectId, OpenMode.ForRead);

 

                // compute the transformation matrix from the containers

                var xform = result.GetContainers()

                    .Select(id => (BlockReference)tr.GetObject(id, OpenMode.ForRead))

                    .Select(br => br.BlockTransform)

                    .Aggregate((m1, m2) => m2 * m1);

 

                // transform the line start and ensd points

                var startPoint = line.StartPoint.TransformBy(xform);

                var endPoint = line.EndPoint.TransformBy(xform);

                ed.WriteMessage($"\nStart point: {startPoint}\nEnd point: {endPoint}");

                tr.Commit();

            }

Feel free to contact us is need additional assistance. @anhtrung 

0 Likes
Message 3 of 10

anhtrung
Advocate
Advocate

Thanks for your answer to me. 

This is a function only for one object. I mean want to get objects inside polyline and this objects into a block.

0 Likes
Message 4 of 10

_gile
Consultant
Consultant

Hi,

You should describe what you want to achieve with more details because block references (BlockReference) do not contain entities, they're only inserted references to a block definition (BlockTableRecord).

Only the block definition contains the entites.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 10

anhtrung
Advocate
Advocate

Thanks for your information.

 

Like you can see image below. I have a block reference and i want to get objects inside the yellow polyline.

 File attached.

2020-08-05_19-26-11.png

 

 

0 Likes
Message 6 of 10

_gile
Consultant
Consultant

As I said, these entities do not belong to the block reference which is only a geometric representation of the block definition. So, you have to elaborate what you mean by: "get objects" and about the context inputs.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 10

anhtrung
Advocate
Advocate

I mean, i can define get points boundary yellow plolyline, but i want to get objects inside this polyline, and all this objects into block reference. 

 

 

0 Likes
Message 8 of 10

_gile
Consultant
Consultant

Sorry, I'm not able to help you further

It looks like we do not understand each other



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 10

GiaBach
Contributor
Contributor
Accepted solution

Hi Trung,

Not sure I fully understand your request, just make a snippet. 

        public static void GetPlinesInside(BlockReference bref)
        {
            List<Polyline> plLst = new List<Polyline>();
            List<Polyline> boundaryLst = new List<Polyline>();

            DBObjectCollection objs = new DBObjectCollection();
            bref.Explode(objs);
            foreach (DBObject obj in objs)
            {
                if (obj is Polyline pl)
                {
                    if (pl.ColorIndex == 2)
                        boundaryLst.Add(pl);
                    else
                        plLst.Add(pl);
                }
            }

            if (boundaryLst.Count == 1)
            {
                Polyline boundary = boundaryLst[0];

                foreach (Polyline pl in plLst)
                {
                    // checking if the pl is inside boundary, refer the solution of Gile:
                    // https://forums.autodesk.com/t5/net/how-to-check-if-a-point2d-is-inside-a-polyline-or-not/m-p/8234741

                    if (isInside(boundary, pl))
                    {
                        // do something here
                    }
                }
            }
        }
0 Likes
Message 10 of 10

anhtrung
Advocate
Advocate

Thanks for your help. That all i need to do that.

 

0 Likes