draw polyline realtime

draw polyline realtime

k005
Advisor Advisor
1,396 Views
12 Replies
Message 1 of 13

draw polyline realtime

k005
Advisor
Advisor

Hello friends,


I want to make an addition to this code. In that :


From the first point I clicked, it continues to draw the Polyline without leaving the point...


how can I do that?


Thanks in advance to the helpful friend.

 

 

 

 [CommandMethod("HesArea")]
        public void AreaCalc()
        {
            // Prompt the user for 5 points
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            PromptPointResult pPtRes;
            Point2dCollection colPt = new Point2dCollection();
            PromptPointOptions pPtOpts = new PromptPointOptions("");
            Application.SetSystemVariable("OSMODE", 32 | 1);
            BirimCeviri();
            // Prompt for the first point
            pPtOpts.Message = "\nİlk nokta > ";
            pPtRes = acDoc.Editor.GetPoint(pPtOpts);
            colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));
            // Exit if the user presses ESC or cancels the command
            if (pPtRes.Status == PromptStatus.Cancel) return;
            pPtOpts.Message = "\ndiğer nokta >";
            pPtOpts.UseBasePoint = true;
            pPtOpts.AllowNone = true;
            while (true)
            {
                // Use the previous point as the base point
                pPtOpts.BasePoint = pPtRes.Value;
                pPtRes = acDoc.Editor.GetPoint(pPtOpts);
                // Break the loop if the user does not specify a point
                if (pPtRes.Status != PromptStatus.OK)
                    break;
                colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));
            }

            using (Polyline acPoly = new Polyline())
            {
                int i = 0;
                foreach (Point2d point in colPt)
                {
                    acPoly.AddVertexAt(i, point, 0, 0, 0);
                }

                // Close the polyline
                acPoly.Closed = true;

                double al1 = acPoly.Area / Durum.BolenA;
                double cv1 = acPoly.Length / Durum.Bolen;

                string aLNSonuc = string.Format("\nAlan   : {1}          Uzunluk : {2}",
                Environment.NewLine, al1.ToString("F3") + " m²", cv1.ToString("F3") + " m");
                System.Windows.Forms.MessageBox.Show(aLNSonuc, "Sonuç");
                YaziOlustur("Alan : " + al1.ToString("F2") + " m²     Uzunluk : " + cv1.ToString("F2") + " m");
            }

        }

 

0 Likes
Accepted solutions (1)
1,397 Views
12 Replies
Replies (12)
Message 2 of 13

norman.yuan
Mentor
Mentor

You probably want to study the topic of "Jig". Searching this forum and/or the net would bring you plenty of sample code to get started with.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 13

k005
Advisor
Advisor

Thanks. I will search

0 Likes
Message 4 of 13

k005
Advisor
Advisor

I did it this way. However, the polyline does not appear on the screen.

 

[CommandMethod("HesArea")]
public void AreaCalc()
{
    // Prompt the user for 5 points
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;
    Editor acEd = acDoc.Editor;

    PromptPointResult pPtRes;
    Point2dCollection colPt = new Point2dCollection();
    PromptPointOptions pPtOpts = new PromptPointOptions("");
    Application.SetSystemVariable("OSMODE", 32 | 1);
    BirimCeviri();

    // Prompt for the first point
    pPtOpts.Message = "\nİlk nokta > ";
    pPtRes = acDoc.Editor.GetPoint(pPtOpts);
    colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));

    // Exit if the user presses ESC or cancels the command
    if (pPtRes.Status == PromptStatus.Cancel) return;

    pPtOpts.Message = "\ndiğer nokta >";
    pPtOpts.UseBasePoint = true;
    pPtOpts.AllowNone = true;

    while (true)
    {
        // Use the previous point as the base point
        pPtOpts.BasePoint = pPtRes.Value;
        pPtRes = acDoc.Editor.GetPoint(pPtOpts);

        // Break the loop if the user does not specify a point
        if (pPtRes.Status != PromptStatus.OK)
            break;

        colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));
    }

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the BlockTable for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                      OpenMode.ForRead) as BlockTable;

        // Open the BlockTableRecord for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;

        using (Polyline acPoly = new Polyline())
        {
            int i = 0;
            foreach (Point2d point in colPt)
            {
                acPoly.AddVertexAt(i, point, 0, 0, 0);
            }

            // Close the polyline
            acPoly.Closed = true;

            // Add the polyline to the drawing
            acBlkTblRec.AppendEntity(acPoly);
            acTrans.AddNewlyCreatedDBObject(acPoly, true);

            double al1 = acPoly.Area / Durum.BolenA;
            double cv1 = acPoly.Length / Durum.Bolen;

            string aLNSonuc = string.Format("\nAlan   : {1}          Uzunluk : {2}",
                Environment.NewLine, al1.ToString("F3") + " m²", cv1.ToString("F3") + " m");
            System.Windows.Forms.MessageBox.Show(aLNSonuc, "Sonuç");
            YaziOlustur("Alan : " + al1.ToString("F2") + " m²     Uzunluk : " + cv1.ToString("F2") + " m");
        }

        // Commit the changes and dispose of the transaction
        acTrans.Commit();
    }
}

 

0 Likes
Message 5 of 13

kerry_w_brown
Advisor
Advisor

Perhaps try incrementing the vertex node index  i  in the foreach statement

 

 

acPoly.AddVertexAt(i, point, 0, 0, 0);

 

 



Perhaps try something like :  ( untested )

 

 

acPoly.AddVertexAt(i++, point, 0, 0, 0);

 

 

 

 

 


// 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 6 of 13

k005
Advisor
Advisor

I tried it in both code blocks. It didn't work. I couldn't see any polylines on the screen. . He didn't draw. It just catches the point and continues... 😞

0 Likes
Message 7 of 13

kerry_w_brown
Advisor
Advisor

 

This works for me.
I've commented out unnecessary code . . . 
The code is essentially your last posted code with iteration of the vertex index in the foreach block in line 60

( as I previously posted )

and translation of the prompts to English.

 

      [CommandMethod("Answer_1209")]

      public void AreaCalc()
      {
         // Prompt the user for 5 points
         Document acDoc = Application.DocumentManager.MdiActiveDocument;
         Database acCurDb = acDoc.Database;
         Editor acEd = acDoc.Editor;

         PromptPointResult pPtRes;
         Point2dCollection colPt = new Point2dCollection();
         PromptPointOptions pPtOpts = new PromptPointOptions("");
         Application.SetSystemVariable("OSMODE", 32 | 1);

         ////BirimCeviri();

         // Prompt for the first point
         pPtOpts.Message = "\nDraw Polyline First point > ";
         pPtRes = acDoc.Editor.GetPoint(pPtOpts);
         colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));

         // Exit if the user presses ESC or cancels the command
         if (pPtRes.Status == PromptStatus.Cancel) return;

         pPtOpts.Message = "\nNextPoint >";
         pPtOpts.UseBasePoint = true;
         pPtOpts.AllowNone = true;

         while (true)
         {
            // Use the previous point as the base point
            pPtOpts.BasePoint = pPtRes.Value;
            pPtRes = acDoc.Editor.GetPoint(pPtOpts);

            // Break the loop if the user does not specify a point
            if (pPtRes.Status != PromptStatus.OK)
               break;

            colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));
         }

         // Start a transaction
         using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
         {
            // Open the BlockTable for read
            BlockTable acBlkTbl;
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                          OpenMode.ForRead) as BlockTable;

            // Open the BlockTableRecord for write
            BlockTableRecord acBlkTblRec;
            acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                            OpenMode.ForWrite) as BlockTableRecord;

            using (Polyline acPoly = new Polyline())
            {
               int i = 0;
               foreach (Point2d point in colPt)
               {
                  acPoly.AddVertexAt(i++, point, 0, 0, 0);
               }

               // Close the polyline
               acPoly.Closed = true;

               // Add the polyline to the drawing
               acBlkTblRec.AppendEntity(acPoly);
               acTrans.AddNewlyCreatedDBObject(acPoly, true);

               /*
               double al1 = acPoly.Area / Durum.BolenA;
               double cv1 = acPoly.Length / Durum.Bolen;

               string aLNSonuc = string.Format("\nAlan   : {1}          Uzunluk : {2}",
                   Environment.NewLine, al1.ToString("F3") + " m²", cv1.ToString("F3") + " m");
               System.Windows.Forms.MessageBox.Show(aLNSonuc, "Sonuç");
               YaziOlustur("Alan : " + al1.ToString("F2") + " m²     Uzunluk : " + cv1.ToString("F2") + " m");
               */
            }

            // Commit the changes and dispose of the transaction
            acTrans.Commit();
         }
      }

 

 

kdub_nz_0-1702102605303.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
0 Likes
Message 8 of 13

k005
Advisor
Advisor

I compiled the code you sent. But the problem is;


When I click on the first point and then click on the second point, I should see the polyline between the two points... Not when it ends...

0 Likes
Message 9 of 13

kerry_w_brown
Advisor
Advisor

@k005 ,

 

Then do as suggested by @norman.yuan , build a jig.

 

If you study the code you posted you will easily see that it is not designed as a jig, but as a simple procedural method.  ie collect all the points , then create a closed polyline using the points.

 

 

There are many jig examples available in various forums and blogs.

This forum has several ( search Polyline jig )

Kean Walmsley has several

TheSwamp has several

forums.augi.com has several

Github has several

Perhaps a Google search for something like "Polyline jig AutoCAD C#" would be a good start

or try ChatGPT

 

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 10 of 13

k005
Advisor
Advisor

 

Ok. Thanks.

0 Likes
Message 11 of 13

k005
Advisor
Advisor

My goal is for the first code I sent to work like in this video...

 

 

 

0 Likes
Message 12 of 13

_gile
Consultant
Consultant
Accepted solution

Hi,

You should be able to draw inspiration from this answer.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 13

k005
Advisor
Advisor

Yes, I compiled the code. This is the code I'm looking for. Thank you master.


* I'll try merging it with my previous code.

 

**************************************************************

 

I combined it. 🙂  🤗

0 Likes