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:
Vector3d zaDirection = line.GetLineSegmentAt(centerSegmentIndex).Direction;
Next, I create list of points:
List<Point3d> points = new List<Point3d>();
points.Add(centerPosition);
points.Add(centerPosition.Add(new Vector3d(ZASize, +ZASize, 0)));
points.Add(centerPosition.Add(new Vector3d(ZASize, -ZASize, 0)));
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:
points.Add(centerPosition.Add(new Vector3d(ZASize, +ZASize, 0)).RotateBy(0, zaDirection, centerPosition));
But it's not the same as I want.
How I can draw triangle by serment direction?
Pavel.
Pavel,
Upload a screenshot or image to see
what is your goal
~'J'~
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() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; 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; Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead); Autodesk.AutoCAD.DatabaseServices.Polyline pline = ent as Autodesk.AutoCAD.DatabaseServices.Polyline; 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>(); points.Add(sp); points.Add(ap); points.Add(ep); Autodesk.AutoCAD.DatabaseServices.Polyline tri = new Autodesk.AutoCAD.DatabaseServices.Polyline(); 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())); btr.AppendEntity(tri); tr.AddNewlyCreatedDBObject(tri, true); } success = true; tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message + "\n" + ex.StackTrace); success = false; } finally { if (success==false) Application.ShowAlertDialog(new ErrorStatus().ToString()); } }
~'J'~
Thanks for idea with Plan class. I did it!
It's my code:
Plane plan = newPlane(Point3d.Origin, line.Normal);
double angle = zaDirection.AngleOnPlane(plan);
List<Point3d> points = newList<Point3d>();
points.Add(centerPosition.RotateBy(angle, line.Normal, centerPosition));
points.Add(centerPosition.Add(newVector3d(ZASize, +ZASize, 0)).RotateBy(angle, line.Normal, centerPosition));
points.Add(centerPosition.Add(newVector3d(ZASize, -ZASize, 0)).RotateBy(angle, line.Normal, centerPosition));
wd.Geometry.Polygon(new Point3dCollection(points.ToArray()));
Thanks,
Pavel.