Polyline 2D Create

Polyline 2D Create

k005
Advisor Advisor
1,615 Views
13 Replies
Message 1 of 14

Polyline 2D Create

k005
Advisor
Advisor

Hello everyone


I am creating a polyline from the points in the datagridview.


The problem is ;


ucs draws a polyline from where the icon is...


I want to do; it just draws the given coordinates... how should I change that. ?


so it should draw from the first coordinate...

 

Thanks in advance for the help friend.

 

 

 

 

 

 

 

  acPoly.AddVertexAt(acPoly.NumberOfVertices, ikiDpoint, 0, 0, 0);

 

0 Likes
Accepted solutions (1)
1,616 Views
13 Replies
Replies (13)
Message 2 of 14

hippe013
Advisor
Advisor

@k005 You need to provide more information. 

 

I am creating a polyline from the points in the datagridview.

 

Okay, I am assuming you are using some sort of form. Need to know more about your dataGridView. 

 

ucs draws a polyline from where the icon is..

 

Do you mean that it is drawing from 0,0? Or that one of the points is 0,0? Or what icon are you referring to? 

 

I want to do; it just draws the given coordinates... how should I change that. ?

 

Need to see more than just the one line of code that your provide. 

Message 3 of 14

GeeHaa
Collaborator
Collaborator

 

I'm not exactly sure what you're trying to do but I usually draw a polyline similar to below. Just extrapolate for you're own needs.

 

acPoly = new Polyline();

acPoly.SetDatabaseDefaults();

acPoly.AddVertexAt(0, new Point2d(ptStart2.X, ptStart2.Y), 0, wid, wid);

acPoly.AddVertexAt(1, new Point2d(ptStart2.X + border2X, ptStart2.Y), 0, wid, wid);

 

Message 4 of 14

k005
Advisor
Advisor

@hippe013 

@GeeHaa 

 

I am sorry. The question was a bit rushed.


I explain in the attached pictures.

 

1.png2.png

 

0 Likes
Message 5 of 14

k005
Advisor
Advisor

I will try this. I have to go now...

0 Likes
Message 6 of 14

kerry_w_brown
Advisor
Advisor

@k005 

 

To avoid this thread becoming a waste of effort and bandwidth you should post your code (relevant complete portion) along with a description of your expectations.
This way your peers will have an unambiguous starting point for helping you.

Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 7 of 14

kerry_w_brown
Advisor
Advisor

Perhaps something like this will give you some ideas :
Note this is proof of concept, without error checking etc.

[CommandMethod("DrawPline")]
public void TestDrawPline()
{
   Document doc = AcadApp.DocumentManager.MdiActiveDocument;
   Database db = doc.Database;
   Editor ed = doc.Editor;

   Point2dCollection point2Ds = new Point2dCollection() 
   {
      new Point2d(216, 230),
      new Point2d(478, 230),
      new Point2d(478, 85),
      new Point2d(216, 84)
   };


   using (var tr = db.TransactionManager.StartTransaction())
   {
      // Open Model space for write
      var dbBlockTable = (BlockTable)tr.GetObject(
         db.BlockTableId, OpenMode.ForRead);

      var msBlockTableRecord = (BlockTableRecord)tr.GetObject(
         dbBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

      var pline = DrawPolyline(point2Ds, 0);
      pline.Closed = true;
      msBlockTableRecord.AppendEntity(pline);
      tr.AddNewlyCreatedDBObject(pline, true);
      tr.Commit();
   }
}
public static Polyline DrawPolyline(Point2dCollection pts, double width)
{
   var pline = new Polyline();
   pline.SetDatabaseDefaults();
   for (int i = 0; i < pts.Count; i++)
   {
      pline.AddVertexAt(i, pts[i], 0, width, width);
   }
   return pline;
}

 

TestDrawPline 2023-04-28_10-16-36.png

 Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 8 of 14

k005
Advisor
Advisor

@kerry_w_brown  Here is the whole code.

 

 

 

 

 private void btnKordinatCiz_Click(object sender, EventArgs e)
        {
            this.Hide();
            Aapp.Document acDoc = aCap.DocumentManager.MdiActiveDocument;
           Database acCurDb = acDoc.Database;


            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            using (Aapp.DocumentLock docLock = Adoc.LockDocument())

            {
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, Adb.OpenMode.ForRead) as BlockTable;
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], Adb.OpenMode.ForWrite) as BlockTableRecord;

                Polyline acPoly = new Polyline();
                acPoly.SetDatabaseDefaults();
                var noktalar = new List<Point2d>();
               

                foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                {
                    var x = Convert.ToDouble(dgvr.Cells[1].Value);
                    var y = Convert.ToDouble(dgvr.Cells[2].Value);
                    var z = Convert.ToDouble(dgvr.Cells[3].Value);
                  
                    var point = new Point3d(x, y, z);
                    var ikiDpoint = new Point2d(x, y);
                    noktalar.Add(ikiDpoint);

                    using (var acPoint = new DBPoint(point))
                    {
                        acBlkTblRec.AppendEntity(acPoint);
                        acTrans.AddNewlyCreatedDBObject(acPoint, true);
                        //acPoly.ColorIndex = 1;
                        //acPoly.Closed = false;
                        //acPoly.AddVertexAt(acPoly.NumberOfVertices, ikiDpoint, 0, 0, 0);
                        //acPoly.AddVertexAt(0, new Point2d(point.X, point.Y), 0, 0, 0);

                    }
                   
                }

                acBlkTblRec.AppendEntity(acPoly);
                acTrans.AddNewlyCreatedDBObject(acPoly, true);
                acTrans.Commit();
                this.Close();
                aCap.SetSystemVariable("pdmode", 34);
                ZoomExtents();
            }
        }

 

 

0 Likes
Message 9 of 14

kerry_w_brown
Advisor
Advisor

I'm pretty sure, after you iterate through the dataGridView,  you could call a method similar to the DrawPolyline I posted that takes your list noktalar  instead of the  Point2dCollection  point2Ds that I used.

 

. . . or you could just add the code in-line to the body of your method  🙂

 

I like to separate functionality, mainly for testing, but there's no rule that says you have to.

 

Be sure to add a couple of debug breakpoints in your testing code and view the values in the critical variables in the 'Locals' pane at each breakpoint, ( or while stepping through the code )

 

 

Regards,


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 10 of 14

hippe013
Advisor
Advisor

It appears that the issue is that you are iterating through every row of the data grid view. The very bottom row is an empty row. That is where you are creating the point at 0,0 and adding it to your polyline. Does this appear to be the case? You may want to test for that bottom row so that it doesn't get added. 

Message 11 of 14

k005
Advisor
Advisor

So how can I remove the last empty datagridview1 row?

0 Likes
Message 12 of 14

hippe013
Advisor
Advisor

Instead of doing for each row...

 

 foreach (DataGridViewRow dgvr in dataGridView1.Rows)

 

Get the row count and minus one from it. (As long as you are certain that the grid will always contain an empty row.) Then use a simple for loop. 

 

 

Message 13 of 14

kerry_w_brown
Advisor
Advisor
Accepted solution

 

Perhaps try 
(untested, may require test for null or string.empty  on cells.)

 

 

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
   if (dgvr.IsNewRow) {continue;}
   //else read cells
   // . . .  
}

 

 

 

added:
Have you actually set a debug breakpoint to check the value of noktalar  or stepped through the foreach in debug to actually see what is happening ??

 

added:
I assume you are handling the DataGrid as part of your process.
If so, perhaps remove empty rows before saving/proceeding.
eg: https://codepedia.info/delete-empty-null-rows-datatable   (thanks mrs google )


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 14 of 14

k005
Advisor
Advisor

Thank you very much. Ok. 🤗

 

@kerry_w_brown 

@hippe013 

@GeeHaa 

 

0 Likes