How to use path array?

How to use path array?

gary.wuEQ8E4
Contributor Contributor
810 Views
1 Reply
Message 1 of 2

How to use path array?

gary.wuEQ8E4
Contributor
Contributor

I look through the Dev guide, there are only "Polar Arrays" and "Rectangular Array". Is there any tutorial or API ref that shows you how to use path array? Or is it possible?

0 Likes
811 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant

Hi,

 

This seems to work (2D only)

 

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

    var selection = ed.GetSelection();
    if (selection.Status != PromptStatus.OK)
        return;

    var entityOptions = new PromptEntityOptions("\nSelect path: ");
    entityOptions.SetRejectMessage("\nSelected object is not a valid path.");
    entityOptions.AddAllowedClass(typeof(Curve), false);
    PromptEntityResult entityResult;
    while (true)
    {
        entityResult = ed.GetEntity(entityOptions);
        if (entityResult.Status != PromptStatus.OK) return;
        if (entityResult.ObjectId.ObjectClass.DxfName != "XLINE" &&
            entityResult.ObjectId.ObjectClass.DxfName != "RAY")
            break;
    }

    var kwordOptions = new PromptKeywordOptions("\nChoose a method [Divide/Measure]: ", "Divide Measure");
    kwordOptions.AllowNone = true;
    kwordOptions.Keywords.Default = "Divide";
    var kwordResult = ed.GetKeywords(kwordOptions);
    if (kwordResult.Status != PromptStatus.OK)
        return;

    using (var tr = db.TransactionManager.StartTransaction())
    {
        var path = (Curve)tr.GetObject(entityResult.ObjectId, OpenMode.ForRead);
        double length = path.GetDistanceAtParameter(path.EndParam);

        int nbElements;
        double distance;

        if (kwordResult.StringResult == "Divide")
        {
            var intOptions = new PromptIntegerOptions("\nNumber of elements: ");
            intOptions.LowerLimit = 2;
            var intResult = ed.GetInteger(intOptions);
            if (intResult.Status != PromptStatus.OK)
                return;
            nbElements = intResult.Value;
            distance = length / (nbElements - 1);
        }
        else
        {
            var distOptions = new PromptDistanceOptions("\nSDistance between elements: ");
            distOptions.AllowNegative = false;
            distOptions.AllowZero = false;
            var distResult = ed.GetDistance(distOptions);
            if (distResult.Status != PromptStatus.OK)
                return;
            distance = distResult.Value;
            nbElements = (int)(length / distance) + 1;
        }

        var pickedPoint = entityResult.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem);
        var pointOnCurve = path.GetClosestPointTo(pickedPoint, false);
        bool fromEnd = length / 2.0 < path.GetDistAtPoint(pointOnCurve);
        var origin = fromEnd ? path.EndPoint : path.StartPoint;
        double rotation = Vector3d.XAxis.GetAngleTo(path.GetFirstDerivative(origin), Vector3d.ZAxis);
        var ids = new ObjectIdCollection(selection.Value.GetObjectIds());
        for (int i = 1; i < nbElements; i++)
        {
            var point = fromEnd ?
                path.GetPointAtDist(length - distance * i) :
                path.GetPointAtDist(distance * i);
            double angle = Vector3d.XAxis.GetAngleTo(path.GetFirstDerivative(point), Vector3d.ZAxis) - rotation;
            var xform =
                Matrix3d.Displacement(origin.GetVectorTo(point)) *
                Matrix3d.Rotation(angle, Vector3d.ZAxis, origin);
            var mapping = new IdMapping();
            db.DeepCloneObjects(ids, db.CurrentSpaceId, mapping, false);
            foreach (IdPair pair in mapping)
            {
                if (pair.IsCloned && pair.IsPrimary)
                {
                    var entity = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                    entity.TransformBy(xform);
                }
            }
        }
        tr.Commit();
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes