.NET

Reply
Valued Contributor
hoathuongphuoc
Posts: 59
Registered: ‎11-05-2013
Message 1 of 9 (489 Views)
Accepted Solution

Determine Top of polyline follow direction x-axits and original

489 Views, 8 Replies
12-26-2013 10:59 PM

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.

 

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);
}
}

 

Mentor
SENL1362
Posts: 181
Registered: ‎07-20-2011
Message 2 of 9 (464 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-27-2013 12:00 AM 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();



Valued Contributor
hoathuongphuoc
Posts: 59
Registered: ‎11-05-2013
Message 3 of 9 (445 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-27-2013 01:06 AM in reply to: SENL1362
Sorry SENL1362, But i dont understant whats your meaning. Can you give me a example.
Valued Contributor
hoathuongphuoc
Posts: 59
Registered: ‎11-05-2013
Message 4 of 9 (426 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-27-2013 02:09 AM in reply to: hoathuongphuoc

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

*Expert Elite*
_gile
Posts: 2,106
Registered: ‎04-29-2006
Message 5 of 9 (414 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-27-2013 02:48 AM 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
Valued Contributor
hoathuongphuoc
Posts: 59
Registered: ‎11-05-2013
Message 6 of 9 (358 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-29-2013 06:50 PM in reply to: _gile

Hi _gile

 pline.SetLowerLeftAboutUcsAsStartPoint();

Its meaning 

SetLowerLeftAboutUcsAsStartPoint(pline);

 

*Expert Elite*
_gile
Posts: 2,106
Registered: ‎04-29-2006
Message 7 of 9 (347 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-29-2013 11:21 PM in reply to: hoathuongphuoc

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

Gilles Chanteau
Valued Contributor
hoathuongphuoc
Posts: 59
Registered: ‎11-05-2013
Message 8 of 9 (333 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-30-2013 03:20 AM in reply to: _gile

Its great. Thanks _gile. Thanks so much.

Valued Contributor
hoathuongphuoc
Posts: 59
Registered: ‎11-05-2013
Message 9 of 9 (266 Views)

Re: Determine Top of polyline follow direction x-axits and original

12-30-2013 11:23 PM in reply to: hoathuongphuoc

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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.