ProgressBar C#

ProgressBar C#

k005
Advisor Advisor
4,566 Views
19 Replies
Message 1 of 20

ProgressBar C#

k005
Advisor
Advisor

 

 

Hello

 

I'm running a code that saves the dwg files in the 2010 version.
I want to add a progressbar showing this job

How should I do the coding?

 

Thanks.

0 Likes
Accepted solutions (1)
4,567 Views
19 Replies
Replies (19)
Message 2 of 20

hippe013
Advisor
Advisor

Use the built in progress meter. Autodesk.AutoCAD.Runtime.ProgressMeter

 

 

https://through-the-interface.typepad.com/through_the_interface/2007/05/displaying_a_pr.html

 

 <CommandMethod("TestProgressMeter")>
    Public Sub CmdTestProgMeter()
        Dim pm As New ProgressMeter
        pm.Start("Processing...")
        pm.SetLimit(100)

        For i As Integer = 0 To 99
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.01))
            pm.MeterProgress()
            System.Windows.Forms.Application.DoEvents()
        Next
        pm.Stop()
    End Sub

 

Message 3 of 20

k005
Advisor
Advisor

Hi @hippe013 

 

I want to make a Progressbar1 indicator on the form, not via AutoCAD cmd.

In other words, a conversion will be done and I want to see this process in progress.

How should I revise the codes?

0 Likes
Message 4 of 20

essam-salah
Collaborator
Collaborator

Hi @k005 

If you use winForms try this examples example1 , example2   

Message 5 of 20

hippe013
Advisor
Advisor

The progress bar that I showed you is the built-in progress bar that will show up in the AutoCAD UI. If you are looking to add a progress bar to a form then you would be using a Windows.Forms.Progressbar. Is this what you are need help with?

0 Likes
Message 6 of 20

k005
Advisor
Advisor

Hi @essam-salah 

 

I ran the codes in example1. But the progressbar is 8 seconds up. The process took 20 seconds ...

0 Likes
Message 7 of 20

k005
Advisor
Advisor

Hi @hippe013 

 

Yes, definitely.

 

There is a progressbar1 built on winform. I want to use this

Thanks.

0 Likes
Message 8 of 20

hippe013
Advisor
Advisor

It would much easier to help you if you posted your code rather than having people guess at the million things that could be going wrong. 

Message 9 of 20

k005
Advisor
Advisor

 

@hippe013 

 

 [CommandMethod("OPSV")] 
        public  void KaydetToplu ()
       {
        try
            {
                
                Stopwatch watch = new Stopwatch();
                
                
                watch.Start();
               
                var path = @"C:\TestBack\";
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] Files = d.GetFiles("*.dwg");
               

                foreach (FileInfo file in Files)
                {
                    var fileName = Path.GetFileName(file.FullName);
                    string dwgFlpath = path + fileName;
                  using (Database db = new Database(false, true))
                  {
                        db.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);
                        using (Transaction tr = db.TransactionManager.StartTransaction())
                        {
                            BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                            BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                            using (Circle crcl = new Circle())
                            {
                                crcl.Center = new Point3d(1, 1, 0);
                                crcl.Radius = 2;
                                btr.AppendEntity(crcl);
                                tr.AddNewlyCreatedDBObject(crcl, true);
                            }
                            tr.Commit();
                        }

                        db.SaveAs(dwgFlpath, DwgVersion.AC1024);
                        
                  }
                }

                watch.Stop();
                
                Application.ShowAlertDialog("Tüm dosyalar 2010 Formatında Kaydedildi. Süre --»  " + watch.Elapsed.Seconds.ToString() + " sn");

        }
            catch (System.Exception ex)
        {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
        }
       }
0 Likes
Message 10 of 20

k005
Advisor
Advisor
private void btn2010save_Click_2(object sender, EventArgs e)
        {
         
            //CaluculateAll(progressBar1);
            Myc.KaydetToplu();


        }
0 Likes
Message 11 of 20

hippe013
Advisor
Advisor
Accepted solution

It appears that you haven't created your form yet. Is this the case? 

 

Your command method should instantiate the form and then call ShowDialog and that is it really. Nothing else needed in your CommandMethod.

 

 

 [CommandMethod("TestProgressMeter")]
        public void CmdTestProgressMeter()
        {
            MyProgressForm form = new MyProgressForm();
            form.ShowDialog();
        }

 

 

Inside the form you might have a button that will start the process.  Here I call it button1. Also on the form is a ProgressBar and it is called progressBar1.

 

private void button1_Click(object sender, EventArgs e)
        {

            var path = @"C:\MyPathToMyDWGFiles\";
            DirectoryInfo d = new DirectoryInfo(path);
            FileInfo[] Files = d.GetFiles("*.dwg");

            progressBar1.Maximum = Files.Length - 1;
            progressBar1.Minimum = 0;

            foreach (FileInfo file in Files)
            {
                var fileName = Path.GetFileName(file.FullName);
                string dwgFlpath = path + fileName;
                using (Database db = new Database(false, true))
                {
                    db.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                        using (Circle crcl = new Circle())
                        {
                            crcl.Center = new Point3d(1, 1, 0);
                            crcl.Radius = 2;
                            btr.AppendEntity(crcl);
                            tr.AddNewlyCreatedDBObject(crcl, true);
                        }
                        tr.Commit();
                    }

                    db.SaveAs(dwgFlpath, DwgVersion.AC1024);

                }

                progressBar1.Increment(1);
            }
}

 

 

Note:

1) C# is not my first language, but you should be able to get the idea from the above example.

2) I have not tested your code. Only showing how to setup progresssBar1 and then increment using your foreach loop.

3) I am assuming that you've created your form and added a ProgressBar and a Button.

 

 

I hope that this helps.

Message 12 of 20

k005
Advisor
Advisor

@hippe013 

 

 

Thank you so much. All right. Progressbar works the way I want ...

 

🤗

0 Likes
Message 13 of 20

hippe013
Advisor
Advisor

Great to hear! I'm glad that I was able to  help.

Message 14 of 20

k005
Advisor
Advisor

@hippe013 

 

progressBar1.Maximum = Files.Length - 1;
            progressBar1.Minimum = 0;
            label1.Text= ?

 

How can I give the value of Label1%?

When all files are finished: 100%
Start: 0%

0 Likes
Message 15 of 20

hippe013
Advisor
Advisor

Just keep track of a counter and compute the percent complete.

 

int counter = 0;
int maxCount = Files.Length - 1;

foreach (FileInfo file in Files)
{
// Your Process

progressBar1.Increment(1);
counter += 1;
label1.Text = ((counter / maxCount) * 100).ToString() + "% Complete";
}

 

 

0 Likes
Message 16 of 20

k005
Advisor
Advisor

@hippe013 

 

I added the codes. But it doesn't show instantly.

When finished, label1 = 100% Complete appears. Am I doing it wrong somewhere?

0 Likes
Message 17 of 20

hippe013
Advisor
Advisor

You may need to refresh. Though like I said, I haven't tested your code. 

 

Try adding the DoEvents method.

 

label1.Text = (counter / maxCount).ToString() + "% Complete";
System.Windows.Forms.Application.DoEvents();

 

The DoEvents method should allow the window to update in the middle of the foreach. 

0 Likes
Message 18 of 20

k005
Advisor
Advisor

@hippe013 

 

It only writes the beginning and the end

when i press the button label1: 0% complete

When finished in a 100% complete.

no intermediate values.

 

 

0 Likes
Message 19 of 20

k005
Advisor
Advisor

 

 

 progressBar1.Increment(1);
                
                counter += 1;
                islem = ((counter * 100) / maxCount);
                label1.Text = islem.ToString() + "% Tamamlandı";
                System.Windows.Forms.Application.DoEvents();

 

I managed to get intermediate values by changing the code in this way. But when it's done, it passes 100% ... 😞 

0 Likes
Message 20 of 20

hippe013
Advisor
Advisor
islem = ((counter * 100) / maxCount);

 

It should be. 

 

islem = ((counter / maxCount) * 100);