.NET

.NET

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

Draw triangle in segment of polyline in segment direction

783 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:

 

  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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 6 (759 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

what is your goal

 

~'J'~

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

Re: Draw triangle in segment of polyline in segment direction

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

Example

 

Thanks,

Pavel.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 6 (752 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()
        {
            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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 5 of 6 (734 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);

 

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.

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

Re: Draw triangle in segment of polyline in segment direction

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

Glad you solved it

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.