to select a single polyline ,C#

to select a single polyline ,C#

essam-salah
Collaborator Collaborator
3,076 Views
3 Replies
Message 1 of 4

to select a single polyline ,C#

essam-salah
Collaborator
Collaborator

the next code is working, but i  want it to select just a singe object.

and if there any advice  to improve my code Smiley Happy

thanks in advance

 

[CommandMethod("CIE1")]
public static void SelectObjectOnScreen()
{
// get the current document and data base
Document acCurrDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurrDB = acCurrDoc.Database;

// Create a TypedValue array to define the filter criteria, To select 2Dpolylines only
TypedValue[] acTypedValueArray = new TypedValue[1];
acTypedValueArray.SetValue(new TypedValue(0 /*(int)DxfCode.Start*/, "LWPOLYLINE"), 0);
// Assign the filter criteria to a SelectionFilter object
SelectionFilter acSelectionFilter = new SelectionFilter(acTypedValueArray);

// to select a single object
PromptSelectionOptions acPoptions = new PromptSelectionOptions
{
SingleOnly = true,
SinglePickInSpace = true
};

// start a transaction
using (Transaction acCurrTrans = acCurrDB.TransactionManager.StartTransaction())
{
// request for object to be selected in the drawing area
PromptSelectionResult acPromptSelectionResult = acCurrDoc.Editor.GetSelection(acPoptions/*, acSelectionFilter*/);

// if the prompt status is OK, onjects were selected
if (acPromptSelectionResult.Status == PromptStatus.OK)
{
SelectionSet acSelecionSet = acPromptSelectionResult.Value;

// Check to make sure a valid SelectedObject object was returned.
if (acSelecionSet != null)
{
// Open the selected object for read
Entity acEntity = (Entity)acCurrTrans.GetObject(acPromptSelectionResult.Value[0].ObjectId,OpenMode.ForWrite);

if (acEntity != null)
{
// To Do ...
acEntity.ColorIndex = 3;
}
}

// Save the new object to the database
acCurrTrans.Commit();
}

// dispose the transaction
}

0 Likes
Accepted solutions (1)
3,077 Views
3 Replies
Replies (3)
Message 2 of 4

essam-salah
Collaborator
Collaborator

actually i want to iterate through each subentity of selected polyline


 

 

0 Likes
Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

For your first request, to select a single object, you can use Editor.GetEntity() instead of Editor.Getselection().

You do not need all this checks after having checked for the PromptStatus. PromptStatus.OK insures that the selection is not null (empty) and the selected entity is not null.

Here's an example:

        [CommandMethod("CIE1")]
        public void SelectObjectOnScreen()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect Polyline: ");
            options.SetRejectMessage("\nSelected object is no a Polyline.");
            options.AddAllowedClass(typeof(Polyline), true);

            var result = ed.GetEntity(options);
            if (result.Status == PromptStatus.OK)
            {
                // at this point we know an entity have been selected and it is a Polyline
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var pline = (Polyline)tr.GetObject(result.ObjectId, OpenMode.ForWrite);
                    pline.ColorIndex = 3;
                    tr.Commit();
                }
            }
        }

About your second question, the Polyline entity does not have subentities.

You can iterate through the polyline segments and vertices using the Polyline.NumberOfVertices property and the Polyline methods as GetPoint2dAt(), GetBulgeAt(), GetStartWidthAt(), ...

 

Otherwise, this is the Visual LISP discussion group. you should post .NET related questions in the .NET discussion group.

You should also use the 'Insert Code' button (</>)  when you want to insert some code.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

essam-salah
Collaborator
Collaborator

thank u so much sir

0 Likes