Change one object's color within a selection set

Change one object's color within a selection set

Anonymous
Not applicable
790 Views
1 Reply
Message 1 of 2

Change one object's color within a selection set

Anonymous
Not applicable

Hi, I am learning .net and am stumbling on a relatively simple task. I'd like to create a selection set of points and then take one point, say, left most point(lowest X coordinate) and change its color. So the first part of the code is fine but how would you get to use that one point? Thanks!

 

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput

Public Class AdskClass
    <CommandMethod("SelectPTS")>
    Public Sub SelectPTS()
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim base As Database = doc.Database
        Dim ss(0) As TypedValue
        ss.SetValue(New TypedValue(DxfCode.Start, "point"), 0)
        Dim ssfilter As SelectionFilter = New SelectionFilter(ss)
        Dim sel As PromptSelectionResult
        sel = ed.GetSelection(ssfilter)

 

 

0 Likes
Accepted solutions (1)
791 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Here's a C# example (you should learn C# insted of VB, you'll find more examples and help with C#)

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

            // create a selection filter
            TypedValue[] filter = { new TypedValue(0, "POINT") };

            // prompt the user to select points
            var sel = ed.GetSelection(new SelectionFilter(filter));
            if (sel.Status != PromptStatus.OK)
                return;

            // start a transaction
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // initialize the minimum X ant a DBPoint variable
                double minX = double.MaxValue;
                DBPoint point = null;

                // iterate through the selected objetc to get the 'leftest' one
                foreach (SelectedObject so in sel.Value)
                {
                    var pt = (DBPoint)tr.GetObject(so.ObjectId, OpenMode.ForRead);
                    if (pt.Position.X < minX)
                    {
                        point = pt;
                        minX = pt.Position.X;
                    }
                }

                // open the 'leftest' point for write
                tr.GetObject(point.ObjectId, OpenMode.ForWrite);

                // force its color to red
                point.ColorIndex = 1;

                // save the changes
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes