.NET

## .NET

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

# Determine Top of polyline follow direction x-axits and original

467 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.

Mentor
Posts: 158
Registered: ‎07-20-2011
Message 2 of 9 (442 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
Posts: 59
Registered: ‎11-05-2013
Message 3 of 9 (423 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
Posts: 59
Registered: ‎11-05-2013
Message 4 of 9 (404 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*
Posts: 2,077
Registered: ‎04-29-2006
Message 5 of 9 (392 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.");
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);
tr.Commit();
}
}

{
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
Posts: 59
Registered: ‎11-05-2013
Message 6 of 9 (336 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*
Posts: 2,077
Registered: ‎04-29-2006
Message 7 of 9 (325 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
Posts: 59
Registered: ‎11-05-2013
Message 8 of 9 (311 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
Posts: 59
Registered: ‎11-05-2013
Message 9 of 9 (244 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

### You are not logged in.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.