Select object and divide equal part in c#

Select object and divide equal part in c#

Anonymous
Not applicable
1,499 Views
2 Replies
Message 1 of 3

Select object and divide equal part in c#

Anonymous
Not applicable

I want to select object line,circle or anything and divide equal part with my value in c#.

0 Likes
1,500 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

Have a look at the Autodesk.AutoCAD.DatabaseServices.Curve methods and properties

 

get the curve length:

double length = someCurve.GetdistanceAtParameter(someCurve.EndParam);

get a point on the curve at 1/5 of total length:

Point3d point = someCurve.GetPointAtDist(length / 5.0);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

_gile
Consultant
Consultant

You should get some inspiration from this snippet which mimics the DIVIDE command:

 

        public void DivideCurve()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var rejectMessage = "\nCannot divide that object.";
                var entityOptions = new PromptEntityOptions("\nSelect object to divide: ");
                entityOptions.SetRejectMessage(rejectMessage);
                entityOptions.AddAllowedClass(typeof(Curve), false);
                PromptEntityResult entityResult;
                Curve curve;
                while (true)
                {
                    entityResult = ed.GetEntity(entityOptions);
                    if (entityResult.Status != PromptStatus.OK)
                        return;
                    curve = (Curve)tr.GetObject(entityResult.ObjectId, OpenMode.ForRead);
                    if (curve is Xline || curve is Ray)
                        ed.WriteMessage(rejectMessage);
                    else
                        break;
                }

                var intOptions = new PromptIntegerOptions("\nEnter the number of segments: ");
                intOptions.LowerLimit = 2;
                intOptions.UpperLimit = 32767;
                var intResult = ed.GetInteger(intOptions);
                if (intResult.Status != PromptStatus.OK)
                    return;

                var length = curve.GetDistanceAtParameter(curve.EndParam);
                var numSegments = intResult.Value;
                var dist = length / numSegments;

                var btr = (BlockTableRecord)tr.GetObject(curve.OwnerId, OpenMode.ForWrite);
                for (int i = 1; i < numSegments; i++)
                {
                    var point = new DBPoint(curve.GetPointAtDist(i * dist));
                    btr.AppendEntity(point);
                    tr.AddNewlyCreatedDBObject(point, true);
                }
                tr.Commit();
            }

        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub