.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Determine Top of polyline follow direction x-axits and original

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
hoathuongphuoc
854 Views, 8 Replies

Determine Top of polyline follow direction x-axits and original

Hi All,

As above Subject. How can i determine "StartPoint" top of polyline. i have attach image:

Hinh 1.png

When i change UCS:

Hinh2.png

 

Thanks everybody.

 

8 REPLIES 8
Message 2 of 9
SENL1362
in reply to: hoathuongphuoc

1. For Hinh1 (the UCS oriented like the WCS(ZAngle1=ZAngle2)):
Sort WCS coordinates based on X, then on Y
pts.OrderBy(p => p.X).ThenBy(p => p.Y).First();

2. For the other one:
Sort WCS coordinates large to small based on Y, then on X
pts.OrderByDescending(p => p.Y).ThenByDecending(p => p.X).First();



Message 3 of 9
hoathuongphuoc
in reply to: SENL1362

Sorry SENL1362, But i dont understant whats your meaning. Can you give me a example.
Message 4 of 9

When i changed ucs so point tops of this polyline dont change.

Message 5 of 9
_gile
in reply to: hoathuongphuoc

Hi,

 

may be you can get some inspiration from this:

 

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

            PromptEntityOptions opts = new PromptEntityOptions("\nSelect a polyline: ");
            opts.SetRejectMessage("Not a polyline.");
            opts.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult per = ed.GetEntity(opts);
            if (per.Status != PromptStatus.OK) return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
                pline.SetLowerLeftAboutUcsAsStartPoint();
                tr.Commit();
            }
        }

        private void SetLowerLeftAboutUcsAsStartPoint(Polyline pline)
        {
            for (int i = 0; i < GetUcsLowerLeftIndex(pline); i++)
            {
                PermuteVertices(pline);
            }
        }

        private int GetUcsLowerLeftIndex(Polyline pline)
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            Matrix3d ucs = ed.CurrentUserCoordinateSystem;
            Extents3d bbox = pline.GeometricExtents;
            bbox.TransformBy(ucs);
            Point3d minPt = bbox.MinPoint;
            int index = 0;
            double distance = minPt.DistanceTo(pline.GetPoint3dAt(0).TransformBy(ucs));
            if (distance == 0.0)
                return 0;
            for (int i = 1; i < pline.NumberOfVertices; i++)
            {
                double d = minPt.DistanceTo(pline.GetPoint3dAt(i).TransformBy(ucs));
                if (distance == 0.0)
                    return i;
                if (d < distance)
                {
                    distance = d;
                    index = i;
                }
            }
            return index;
        }

        private void PermuteVertices(Polyline pline)
        {
            int lastIndex = pline.NumberOfVertices - 1;
            Point2d pt = pline.GetPoint2dAt(0);
            double bulge = pline.GetBulgeAt(0);
            double startWidth = pline.GetStartWidthAt(0);
            double endWidth = pline.GetEndWidthAt(0);
            for (int i = 0; i < lastIndex; i++)
            {
                pline.SetPointAt(i, pline.GetPoint2dAt(i + 1));
                pline.SetBulgeAt(i, pline.GetBulgeAt(i + 1));
                pline.SetStartWidthAt(i, pline.GetStartWidthAt(i + 1));
                pline.SetEndWidthAt(i, pline.GetEndWidthAt(i + 1));
            }
            pline.SetPointAt(lastIndex, pt);
            pline.SetBulgeAt(lastIndex, bulge);
            pline.SetStartWidthAt(lastIndex, startWidth);
            pline.SetEndWidthAt(lastIndex, endWidth);
        }
    }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 9
hoathuongphuoc
in reply to: _gile

Hi _gile

 pline.SetLowerLeftAboutUcsAsStartPoint();

Its meaning 

SetLowerLeftAboutUcsAsStartPoint(pline);

 

Message 7 of 9
_gile
in reply to: hoathuongphuoc

Yes, as SetLowerLeftAboutUcsAsStartPoint() is not defined as an extension method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 9
hoathuongphuoc
in reply to: _gile

Its great. Thanks _gile. Thanks so much.

Message 9 of 9

Hi _gile,

I just have met 1 mistake when i used your code.This is.

- The first when i didn't change UCS. My StartPoint is ok.

h2.png

- The second when i changed UCS. My StartPoint is not correct.

h1.png

Thank for your help.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost