Author Topic: How to determine if the next vertex in polyline is a bulge  (Read 2767 times)

0 Members and 1 Guest are viewing this topic.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
How to determine if the next vertex in polyline is a bulge
« on: March 25, 2011, 03:31:06 AM »
If I have a loop through the vertex points of a polyline to extract a point3d, I get the endpoints of the vertices. But I also want the midpoint of bulges. What is the best way to get that point?

I thought: check to see if next vertex is a bulge, extract that vertex as an arc and get a point over a half distance.

But there might be simpeler solutions. Anyone?
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to determine if the next vertex in polyline is a bulge
« Reply #1 on: March 25, 2011, 04:23:23 AM »
Hi,

Look at the Curve.GetPointAtParameter() method.
For a polyline the parameter at each vertex is an integer (0.0, 1.0, 2.0, ...) so:
pline.GetPointAtParameter(0.5) returns the midpoint of the first segment of the pline (between vertex 0 and vertex 1), 
pline.GetPointAtParameter(1.5) the mipoint of the second segment, and so on.
Speaking English as a French Frog

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: How to determine if the next vertex in polyline is a bulge
« Reply #2 on: March 25, 2011, 05:16:57 AM »
Thanks Gile, that sounds clear :-)

But I'll get also points in the middle of straight vertices I assume. Is it possible to check if a vertex is a bulge? Or do I have to check if (midpoint between GetPointOnPArameter(0) and GetPointOnPArameter(1)) != GetPointOnPArameter(0.5)?
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to determine if the next vertex in polyline is a bulge
« Reply #3 on: March 25, 2011, 05:28:07 AM »
Have a look at

Polyline.HasBulges
Quote
Assesses if the polyline has any bulge factors set for any of the segments. Bulge factor values are required for arc segments within the polyline.

Polyline.GetBulgeAt
Quote
This function gets the bulge factor value at the index vertex. The bulge factor is used to indicate how much of an arc segment is present at this vertex. The bulge factor is the tangent of one fourth the included angle for an arc segment, made negative if the arc goes clockwise from the start point to the endpoint. A bulge of 0 indicates a straight segment, and a bulge of 1 is a semicircle.
While looping check if 0

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to determine if the next vertex in polyline is a bulge
« Reply #4 on: March 25, 2011, 05:29:21 AM »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: How to determine if the next vertex in polyline is a bulge
« Reply #5 on: March 25, 2011, 05:47:23 AM »
Wow! It is actually really simple :-) Thanks all!
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to determine if the next vertex in polyline is a bulge
« Reply #6 on: March 25, 2011, 06:33:40 AM »
Wow! It is actually really simple :-) Thanks all!


It takes a lot of effort to make it look that simple :) Thanks gile.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: How to determine if the next vertex in polyline is a bulge
« Reply #7 on: March 25, 2011, 07:34:09 AM »
..

It takes a lot of effort to make it look that simple :) Thanks gile.

Indeed. I already looked at the GetBulge option but didn't find out how it works. But the excellent code examples of Gile (and others) are very clear and instructive and now I am able to continue exploring, because now it looks simple :-)

I really do learn a lot from people like Gile and you Kerry, and many others. And probably we don't say "thank you" that much, but I am really grateful!
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

fixo

  • Guest
Re: How to determine if the next vertex in polyline is a bulge
« Reply #8 on: March 25, 2011, 09:00:52 AM »
Deep inside the code see SegmentType:

Code: [Select]
        [CommandMethod("lotes")]
        public static void GenerateCoordTable()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;


            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {

                try
                {
                    PromptEntityOptions peo = new PromptEntityOptions("\nSelect a boundary polygon >>");

                    peo.SetRejectMessage("\nSelect Polyline only >>");

                    peo.AddAllowedClass(typeof(Polyline), false);

                    PromptEntityResult res;

                    res = ed.GetEntity(peo);

                    if (res.Status != PromptStatus.OK)

                        return;

                    Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);

                    if (ent == null)

                        return;

                    Polyline poly = (Polyline)ent as Polyline;

                    if (poly == null)

                        return;

                    string lotno = UserInteraction.UserInput.GetStringDefault(ed, "\nEnter a lot number", "101");

                    int prec;

                    if (!(UserInteraction.UserInput.GetIntDefault(ed, "\nEnter number of decimals for table values: ", 1, out prec)))
                    {
                        ed.WriteMessage("\nNo value entered. Programm exiting");
                        return;
                    }

                    double hgt = UserInteraction.UserInput.GetRealDefault(ed, "\nEnter text height for data cells: ", 0.2);

                    List<string[]> arr = PointsToTable(poly, prec);

                    if (arr == null)

                        return;

                    Point3d position = new Point3d();

                    if (!(UserInteraction.UserInput.GetFirstPoint(ed, "Specify insertion point: ", out position)))
                    {
                        ed.WriteMessage("\nNo points picked. Programm exiting");

                        return;
                    }

                    //draw table

                    int nmRows = arr.Count + 2;

                    int nmColumns = arr[0].Length;

                    double rowHeight = hgt * 1.5;

                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                    Table oTable = new Table();

                    oTable.IsHeaderSuppressed = false;

                    oTable.IsTitleSuppressed = false;

                    oTable.VerticalCellMargin = hgt * 0.1;

                    oTable.HorizontalCellMargin = hgt * 0.1;

                    oTable.NumColumns = nmColumns;

                    oTable.NumRows = nmRows;

                    oTable.Height = nmRows * hgt * 1.25;

                    oTable.Width = hgt * 76;

                    oTable.Position = position;

                    oTable.SuppressRegenerateTable(true);

                    oTable.GenerateLayout();

                    oTable.SetTextString(0, 0, "Lotes " + lotno.ToString());

                    SetCell(oTable, 0, 0, "Lotes " + lotno.ToString(), hgt * 2.0, CellAlignment.MiddleCenter);

                    oTable.SetColumnWidth(0, hgt * 16);

                    oTable.SetColumnWidth(1, hgt * 15);

                    oTable.SetColumnWidth(2, hgt * 15);

                    oTable.SetColumnWidth(3, hgt * 15);

                    oTable.SetColumnWidth(4, hgt * 15);

                    oTable.SetTextHeight(hgt * 2, (int)RowType.TitleRow);

                    oTable.SetTextHeight(hgt * 1.5, (int)RowType.HeaderRow);

                    oTable.SetTextHeight(hgt, (int)RowType.DataRow);

                    string[] hdl = new string[5];

                    hdl[0] = "Point No";

                    hdl[1] = "X";

                    hdl[2] = "Y";

                    hdl[3] = "Z";

                    hdl[4] = "Radius";

                    for (int i = 0; i < hdl.Length; i++)
                    {

                        string s = hdl[i];
                        SetCell(oTable, 1, i, s, hgt * 1.5, CellAlignment.MiddleCenter);

                    }

                    for (int i = 0; i < arr.Count; i++)
                    {
                        string[] line = arr[i];

                        for (int j = 0; j < hdl.Length; j++)
                        {
                            string s = line[j];
                            ed.WriteMessage("\n\t{0}\t{1}", i + 2, j);
                            if (s != "")
                                SetCell(oTable, i + 2, j, s, hgt,
                                    j == 0 ? CellAlignment.MiddleLeft : CellAlignment.MiddleCenter);

                        }
                    }


                    //e.g. merge cells in the the very last row
                    //CellRange trg = new CellRange(nmRows - 1, 0, nmRows - 1, 3);

                    //oTable.MergeCells(trg);

                    //oTable.SetTextString(nmRows - 1, 0, "Merged cells are goes here");

                    oTable.SuppressRegenerateTable(false);

                    btr.AppendEntity(oTable);

                    tr.AddNewlyCreatedDBObject(oTable, true);

                    ed.Regen();

                    oTable.Dispose();

                    bt.Dispose();

                    btr.Dispose();

                    tr.Commit();
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                }


            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="poly"></param>
        /// <param name="prec"></param>
        /// <returns></returns>
        public static List<string[]> PointsToTable(Polyline poly, int prec)
        {
            List<string[]> points = new List<string[]>();

            for (int i = 0; i < poly.NumberOfVertices; i++)
            {

                SegmentType stype = poly.GetSegmentType(i);

                if (stype == SegmentType.Line)
                {
                    LineSegment3d seg = poly.GetLineSegmentAt(i);
                    if (i == 0)
                    {

                        Point3d p = seg.StartPoint;
                        string[] ptitem = new string[5];

                        ptitem[0] = (i + 1).ToString();
                        ptitem[1] = string.Format("{0:f" + prec + "}", p.X);
                        ptitem[2] = string.Format("{0:f" + prec + "}", p.Y);
                        ptitem[3] = string.Format("{0:f" + prec + "}", p.Z);
                        ptitem[4] = "";
                        points.Add(ptitem);

                        p = seg.EndPoint;
                        ptitem = new string[5];
                        ptitem[0] = (i + 2).ToString();
                        ptitem[1] = string.Format("{0:f" + prec + "}", p.X);
                        ptitem[2] = string.Format("{0:f" + prec + "}", p.Y);
                        ptitem[3] = string.Format("{0:f" + prec + "}", p.Z);
                        ptitem[4] = "";
                        points.Add(ptitem);
                    }
                    else
                    {
                        string[] ptitem = new string[5];
                        Point3d p = seg.EndPoint;
                        ptitem[0] = (i + 2).ToString();
                        ptitem[1] = string.Format("{0:f" + prec + "}", p.X);
                        ptitem[2] = string.Format("{0:f" + prec + "}", p.Y);
                        ptitem[3] = string.Format("{0:f" + prec + "}", p.Z);
                        ptitem[4] = "";
                        points.Add(ptitem);
                    }
                }
                else
                {
                    if (stype == SegmentType.Arc)
                    {
                        CircularArc3d circ = poly.GetArcSegmentAt(i);
                        if (i == 0)
                        {


                            Point3d p = circ.StartPoint;
                            string[] ptitem = new string[5];


                            ptitem[0] = (i + 1).ToString();
                            ptitem[1] = string.Format("{0:f" + prec + "}", p.X);
                            ptitem[2] = string.Format("{0:f" + prec + "}", p.Y);
                            ptitem[3] = string.Format("{0:f" + prec + "}", p.Z);
                            ptitem[4] = "";
                            points.Add(ptitem);

                            double rad = circ.Radius;
                            Point3d cp = circ.Center;
                            ptitem = new string[5];
                            ptitem[0] = "Center";
                            ptitem[1] = string.Format("{0:f" + prec + "}", cp.X);
                            ptitem[2] = string.Format("{0:f" + prec + "}", cp.Y);
                            ptitem[3] = string.Format("{0:f" + prec + "}", cp.Z);
                            ptitem[4] = string.Format("{0:f" + prec + "}", rad);
                            points.Add(ptitem);

                            p = circ.EndPoint;
                            ptitem = new string[5];
                            ptitem[0] = (i + 2).ToString();
                            ptitem[1] = string.Format("{0:f" + prec + "}", p.X);
                            ptitem[2] = string.Format("{0:f" + prec + "}", p.Y);
                            ptitem[3] = string.Format("{0:f" + prec + "}", p.Z);
                            ptitem[4] = "";
                            points.Add(ptitem);

                        }
                        else
                        {
                            double rad = circ.Radius;
                            Point3d cp = circ.Center;
                            string[] ptitem = new string[5];
                            ptitem[0] = "Center";
                            ptitem[1] = string.Format("{0:f" + prec + "}", cp.X);
                            ptitem[2] = string.Format("{0:f" + prec + "}", cp.Y);
                            ptitem[3] = string.Format("{0:f" + prec + "}", cp.Z);
                            ptitem[4] = string.Format("{0:f" + prec + "}", rad);
                            points.Add(ptitem);

                            Point3d p = circ.EndPoint;
                            ptitem = new string[5];
                            ptitem[0] = (i + 2).ToString();
                            ptitem[1] = string.Format("{0:f" + prec + "}", p.X);
                            ptitem[2] = string.Format("{0:f" + prec + "}", p.Y);
                            ptitem[3] = string.Format("{0:f" + prec + "}", p.Z);
                            ptitem[4] = "";
                            points.Add(ptitem);
                        }

                    }
                }
            }
            return points;

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="oTable"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="value"></param>
        /// <param name="height"></param>
        /// <param name="cellalign"></param>
        public static void SetCell(Table oTable, int row, int column, string value,
           double height, CellAlignment cellalign)
        {
            oTable.SetTextString(row, column, value);

            oTable.SetTextHeight(row, column, height);

            oTable.SetAlignment(row, column, cellalign);

        }