.NET

## .NET

Active Contributor
Posts: 48
Registered: ‎07-22-2012
Message 1 of 6 (425 Views)

# Draw triangle in segment of polyline in segment direction

425 Views, 5 Replies
09-09-2012 12:39 AM

I want to draw triangle in segment of polyline in segment direction.

I have target segment and center point of it:

Polyline line = ...
LineSegment3d centerSegment = line.GetLineSegmentAt(centerSegmentIndex);
Point3d centerPosition = centerSegment.MidPoint;

I can find direction of this segment:

Next, I create list of points:

List<Point3d> points = new List<Point3d>();

And draw it:

wd.Geometry.Polygon(new Point3dCollection(points.ToArray()));

Ok, I have triangle but... it's not in segment direction, it's X-direction.

I try to rotate it:

But it's not the same as I want.

How I can draw triangle by serment direction?

Pavel.

*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 6 (401 Views)

# Re: Draw triangle in segment of polyline in segment direction

09-09-2012 11:15 PM in reply to: pva75

Pavel,

Upload a screenshot or image to see

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
Posts: 48
Registered: ‎07-22-2012
Message 3 of 6 (397 Views)

# Re: Draw triangle in segment of polyline in segment direction

09-10-2012 12:40 AM in reply to: Hallex

Thanks,

Pavel.

*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 6 (394 Views)

# Re: Draw triangle in segment of polyline in segment direction

09-10-2012 01:05 AM in reply to: pva75

Well, I just finished to write an example, but it's not you exactly need

Try to rewrite you code using this example:

```       public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
{
// credits to Tony Tanzillo
return new Point3d(
basepoint.X + (distance * Math.Cos(angle)),
basepoint.Y + (distance * Math.Sin(angle)),
basepoint.Z);
}
// by gile (Gilles Chanteau)
// Clockwise method, returns 1 if p1, p2, p3 are clockwise,
// 0 if they're aligned, -1 if they're counterclockwise

public static int Clockwise(Point3d p1, Point3d p2, Point3d
p3, Vector3d normal)
{
const double pi = 3.141592653589793;
Vector3d v1 = p1.GetVectorTo(p2);
Vector3d v2 = p1.GetVectorTo(p3);
double angle = v1.GetAngleTo(v2, normal);
if (angle == 0.0 || angle == pi)
return 0;
else
{
if (v1.GetAngleTo(v2, normal) < pi)
return -1;
else
return 1;
}
}
[CommandMethod("trio")]
public static void DrawTriangles()
{

Database db = doc.Database;

Editor ed = doc.Editor;

bool success = false;
double ZASize = 10.0;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId id = ed.GetEntity("\nSelect Polyline: ").ObjectId;
Plane plan = new Plane(Point3d.Origin, pline.Normal);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

Point3d p1 = new Point3d(); Point3d p2 = new Point3d(); Point3d p3 = new Point3d(); int dir = 0;
if (pline.NumberOfVertices>2)
{
p1 = pline.GetPoint3dAt(0);
p2 = pline.GetPoint3dAt(1);
p3 = pline.GetPoint3dAt(2);
}

dir= Clockwise(p1,p2,p3,pline.Normal);

for (int i = 0; i < pline.NumberOfVertices; i++)
{
Point3d ep=new Point3d();
if ((!pline.Closed) && ((i + 1) < pline.NumberOfVertices))
{
ep = pline.GetPoint3dAt(i + 1);

}
else if (pline.Closed)
{
try
{
ep = pline.GetPoint3dAt(i + 1);
}
catch
{
ep = pline.GetPoint3dAt(0);
}
}
else
{
break;
}
Point3d sp = pline.GetPoint3dAt(i);
Point3d centerPosition = new Point3d((sp.X + ep.X) / 2, (sp.Y + ep.Y) / 2, (sp.Z + ep.Z) / 2);
//I can find direction of this segment:
double perp;
if (dir > 0)
{
perp = (ep - sp).GetPerpendicularVector().AngleOnPlane(plan);
}
else
{
perp = (sp - ep).GetPerpendicularVector().AngleOnPlane(plan);
}
Point3d ap = PolarPoint(centerPosition, perp, ZASize);

//Next, I create list of points:

List<Point3d> points = new List<Point3d>();

for (int n = 0; n < points.Count; n++)
{
tri.AddVertexAt(n, new Point2d(points[n].X, points[n].Y), 0, 0, 0);
}
// commented overruling code block, I did't test this part of code
// wd.Geometry.Polygon(new Point3dCollection(points.ToArray()));
}
success = true;
tr.Commit();
}

}

{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message + "\n" + ex.StackTrace);
success = false;
}
finally
{
if (success==false)
}
}```

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
Posts: 48
Registered: ‎07-22-2012
Message 5 of 6 (376 Views)

# Re: Draw triangle in segment of polyline in segment direction

09-10-2012 10:09 AM in reply to: pva75

Thanks for idea with Plan class. I did it!

It's my code:

Plane plan = newPlane(Point3d.Origin, line.Normal);

List<Point3d> points = newList<Point3d>();

wd.Geometry.Polygon(new Point3dCollection(points.ToArray()));

Thanks,

Pavel.

*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 6 (360 Views)

# Re: Draw triangle in segment of polyline in segment direction

09-10-2012 08:54 PM in reply to: pva75

Cheers

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

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