TraceBoundary

TraceBoundary

hakocivil
Contributor Contributor
1,611 Views
10 Replies
Message 1 of 11

TraceBoundary

hakocivil
Contributor
Contributor
 
Hello, I want to access the objects around it by clicking inside a limit in AutoCAD, for example, when I click inside a limit, I want to know if there is a line or polyline around it, what is its color.
I use TraceBoundary
0 Likes
Accepted solutions (2)
1,612 Views
10 Replies
Replies (10)
Message 2 of 11

kerry_w_brown
Advisor
Advisor

@hakocivil 

Wouldn't you iterate the collection returned, and decide, according to your requirements ?

// Determine the objects making up the boundary 

      DBObjectCollection objs =  _ed.TraceBoundary(ppr.Value, true);
      if (objs.Count > 0)
      {
          // . . . do your magic

 

Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 11

hakocivil
Contributor
Contributor

Thank you very much for your reply
But what I mean is that when I use TraceBoundary I want to access the boundary drawings
For example, when I clicked inside this limit according to the image below, it returned the following value
A yellow line
A red line
and two white lines

 

 

img.jpg

0 Likes
Message 4 of 11

hakocivil
Contributor
Contributor
If ppr.Status <> PromptStatus.OK Then Return
Dim objs As DBObjectCollection = ed.TraceBoundary(ppr.Value, True)
If objs.Count > 0 Then
Dim tr As Transaction = doc.TransactionManager.StartTransaction()

Using tr
Dim bt As BlockTable = CType(tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Dim ids As ObjectIdCollection = New ObjectIdCollection()
For Each obj As DBObject In objs
Dim ent As Entity = TryCast(obj, Entity)
If ent IsNot Nothing Then
'Here it creates New Boundary that I don't need, I just want the information from which it creates this Boundary
ids.Add(btr.AppendEntity(ent))
tr.AddNewlyCreatedDBObject(ent, True)
End If
Next
0 Likes
Message 5 of 11

_gile
Consultant
Consultant

Hi,

If you do not want to do it, do not append these DBObjects to the model space.

Simply get the object type, its color (or layer) an do what you want with these data.

foreach (Entity obj in objs)
{
    ed.WriteMessage($"\nType = {obj.GetType().Name} Color Index = {obj.ColorIndex}");
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 11

hakocivil
Contributor
Contributor

Hi,

I have drawn a closed boundary with several lines, and now I want to be able to click inside that boundary and get information about the lines that make up the boundary. For example, tell me that this boundary is made up of 4 lines, one of which is yellow, one is red, and two are white.img.jpg

0 Likes
Message 7 of 11

hakocivil
Contributor
Contributor

And another question is that I want when I click inside this shape, don't consider the purple line and only convert the yellow lines to a polyline.img2.JPG

0 Likes
Message 8 of 11

norman.yuan
Mentor
Mentor

In the case as you showed, Editor.TraceBoundary() would return a DBObjectCollection only containing one Polyline. So, you would not be able to tell which of the 4 lines in he drawing form this Polyline. You need to compare the geometries of the lines with the Polyline, something like:
foreach (var entId in [ModelSpaceBlock]
{
if (entId.ObjectClass.DxfName!="LINE") continue;
If IsPartOfBoundary(entId, boundaryPoly)
{
... ...
}
}

In the IsPartOfBoundary() method, you can test if 2 vertices of each segment of the boundary polyline are the same as the Line's Start/EndPoint (or on the line, if the line is actually longer then the polyline's segment).

HTH.

Norman Yuan

Drive CAD With Code

EESignature

Message 9 of 11

norman.yuan
Mentor
Mentor
Accepted solution

As for your last question, if you do not want the purple line to interfere the boundary test, you need to make sure it is located on different layer and freeze the layer right before calling TraceBoundary()  (then thaw the layer, if necessary).

 

Norman Yuan

Drive CAD With Code

EESignature

Message 10 of 11

_gile
Consultant
Consultant
Accepted solution

Another way could be using the vertices of the returned polyline to make a polygon selection.

Here's a quick and dirty example (it needs to be improved).

        [CommandMethod("BOUNDARYTEST")]
        public static void BoundaryTest()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var ppr = ed.GetPoint("\nPick a point: ");
            var objs = ed.TraceBoundary(ppr.Value, true);
            if (objs.Count == 1 && objs[0] is Polyline)
            {
                var pline = (Polyline)objs[0];
                var polygon = new Point3dCollection();
                for (int i = 0; i < pline.NumberOfVertices; i++)
                {
                    polygon.Add(pline.GetPoint3dAt(i));
                }
                pline.Dispose();
                var selection = ed.SelectCrossingPolygon(polygon);
                if (selection.Status == PromptStatus.OK)
                {
                    using (var tr = db.TransactionManager.StartTransaction())
                    {
                        foreach (ObjectId id in selection.Value.GetObjectIds())
                        {
                            var entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
                            ed.WriteMessage($"\nType = {entity.GetType().Name} Color index = {entity.ColorIndex}");
                        }
                        tr.Commit();
                    }
                }
            }
            else
            {
                foreach (DBObject obj in objs)
                {
                    obj?.Dispose();
                }
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 11

hakocivil
Contributor
Contributor

Thank you to all my dear friends for solving the previous problem.🙏🙏

Please consider the following figure. I would like to obtain an output similar to the following when I select the entire shape in AutoCAD:
1- Area A2, from the north with a length of 1.53, to area A27
2- Area A2, from the east with a length of 1.51, to area A27
3- Area A2, from the south with a length of 1.55, to area A18
4- Area A2, from the west in two parts: the first part with a length of 0.86 in area A18 and the second part with a length of 0.59 in area A1

In general, I want to identify the neighbors of area A2.
Of course, I need to do this for all areas. Also note that all shapes are drawn using lines.

 

img3.JPG

0 Likes