find block inside a polyline

find block inside a polyline

Anonymous
Not applicable
4,673 Views
9 Replies
Message 1 of 10

find block inside a polyline

Anonymous
Not applicable

Hi all,

 

Does any one knows how to find a block inside a polyline??

I need to do this so I can read some data from the block and add it as XData to the polyline.

 

Thanks in advance,

 

0 Likes
Accepted solutions (1)
4,674 Views
9 Replies
Replies (9)
Message 2 of 10

SENL1362
Advisor
Advisor

There are numerous samples. 

Try Google on "c# AutoCAD SelectWindowPolygon"    or  "IsPointInside for Polyline" "counting blocks inside" "Region inside polyline"

Also have a look at the Polyline extensions of Gilles.

Also samples from FIXO and Tony Tanzillo will give you the necessary inspirations.

 

 

 

Message 3 of 10

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can use this Editor.SelectByPolyline() extension method.

 

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;

namespace Autodesk.AutoCAD.EditorInput
{
    public enum PolygonSelectionMode { Crossing, Window }

    public static class EditorExtension
    {
        public static PromptSelectionResult SelectByPolyline(this Editor ed, Polyline pline, PolygonSelectionMode mode, params TypedValue[] filter)
        {
            if (ed == null) throw new ArgumentNullException("ed");
            if (pline == null) throw new ArgumentNullException("pline");
            Matrix3d wcs = ed.CurrentUserCoordinateSystem.Inverse();
            Point3dCollection polygon = new Point3dCollection();
            for (int i = 0; i < pline.NumberOfVertices; i++)
            {
                polygon.Add(pline.GetPoint3dAt(i).TransformBy(wcs));
            }
            PromptSelectionResult result;
            using (ViewTableRecord curView = ed.GetCurrentView())
            {
                ed.Zoom(pline.GeometricExtents);
                if (mode == PolygonSelectionMode.Crossing)
                    result = ed.SelectCrossingPolygon(polygon, new SelectionFilter(filter));
                else
                    result = ed.SelectWindowPolygon(polygon, new SelectionFilter(filter));
                ed.SetCurrentView(curView);
            }
            return result;
        }

        public static void Zoom(this Editor ed, Extents3d extents)
        {
            if (ed == null) throw new ArgumentNullException("ed");
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                Matrix3d worldToEye =
                    (Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
                    Matrix3d.Displacement(view.Target - Point3d.Origin) *
                    Matrix3d.PlaneToWorld(view.ViewDirection))
                    .Inverse();
                extents.TransformBy(worldToEye);
                view.Width = extents.MaxPoint.X - extents.MinPoint.X;
                view.Height = extents.MaxPoint.Y - extents.MinPoint.Y;
                view.CenterPoint = new Point2d(
                    (extents.MaxPoint.X + extents.MinPoint.X) / 2.0,
                    (extents.MaxPoint.Y + extents.MinPoint.Y) / 2.0);
                ed.SetCurrentView(view);
            }
        }
    }
}

 

 

Testing example to select block references strctly inside the selected polyline:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace SelectInsidePolygonSample
{
    public class Commands
    {
        [CommandMethod("TEST")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect polyline: ");
            options.SetRejectMessage("\nMust be a polyline.");
            options.AddAllowedClass(typeof(Polyline), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK) return;

            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                Polyline pline = (Polyline)tr.GetObject(result.ObjectId, OpenMode.ForRead);
                PromptSelectionResult selection = 
                    ed.SelectByPolyline(pline, PolygonSelectionMode.Window, new TypedValue(0, "INSERT"));
                if (selection.Status == PromptStatus.OK)
                    ed.SetImpliedSelection(selection.Value);
                tr.Commit();
            }
        }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 10

Anonymous
Not applicable

That was awsome, thank you so much.

 

0 Likes
Message 5 of 10

Automohan
Advocate
Advocate
How to use this Program? Regards Automohan
"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
0 Likes
Message 6 of 10

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

Hi all,

 

Does any one knows how to find a block inside a polyline??

I need to do this so I can read some data from the block and add it as XData to the polyline.

 

Thanks in advance,

 


If your polyine has curved segments, you might want to consider pursing an analytical solution or 'spatial query' that doesn't depend on visual object selection, since it can only select objects within a polygon (e.g., no curved segments).

 

You can search this forum on the keyword "GetPointContainment" to find solutions like this one, that use a region to find out if a block reference's insertion point lies inside a closed, non self-intersecting polyline.

0 Likes
Message 7 of 10

Automohan
Advocate
Advocate

Dear Gile;

 

I need to know how to run the code with a screencast video

 

thanks

Automohan

"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
0 Likes
Message 8 of 10

_gile
Consultant
Consultant

This code is a C# code which cannot be run as simply as an AutoLISP code.

 

You have to first compile this code referencing some AutoCAD .NET libraries to get a DLL. This can be done using an IDE like Visual Studio. You can then NETLOAD the DLL in AutoCAD and launch the TEST testing command.

 

But your question make me think you do not have any .NET programming knowledge and do not really want to learn such stuff but you just want some ready made custom command.

 

If I'm right, you should ask for some routine in the AutoLISP forum (similar things have certainly be done many times) that you could much more easily load and run.

 

If I'm wrong and you want to learn AutoCAD .NET programming, you should first learn the basics of .NET programming outside of AutoCAD (you can find many tutorials about .NET, C# and Visual Studio), then learn the basics of AutoCAD customization with .NET (you can find learning material >>here<<).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 10

Automohan
Advocate
Advocate

Yes Sir, I know that "find block inside a polyline" can be done by a lisp Program, but someone reply me as below Image, for some functions you need .Net Programs, so at least I should know how to run the code if someone issue me a program!

 

cvd3dreminders.jpg

 

I didn't think of learning .Net Program, need a lots of hard work, need more time to spend or I may create programs that for small works

"A few people only success in advance programs that do large scale of work in the world"

"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
0 Likes
Message 10 of 10

ActivistInvestor
Mentor
Mentor

@Automohan wrote:

Yes Sir, I know that "find block inside a polyline" can be done by a lisp Program, but someone reply me as below Image, for some functions you need .Net Programs, so at least I should know how to run the code if someone issue me a program!

 

 

 

I didn't think of learning .Net Program, need a lots of hard work, need more time to spend or I may create programs that for small works

"A few people only success in advance programs that do large scale of work in the world"


Without knowing how to program in .NET or use Visual Studio to compile code and build a working program, you are not going to get anywhere with code you find here. Screencast is for recording what happens on your screen, that's all.

 

Also, you don't have to post screen shots of other discussion group posts. You can include a hyperlink to the post that anyone reading it can open and view.

 

 

0 Likes