找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1541|回复: 0

[分享] Displaying a progress meter during long operations in AutoCAD using .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-11 17:00:28 来自手机 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 csharp 于 2014-5-12 08:16 编辑

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

Displaying a progress meter during long operations in AutoCAD using .NET
It's often desirable to show a progress meter during lengthy operations. Although there's currently no public API to make use of AutoCAD's progress meter from .NET, there are nevertheless a couple of approaches to doing so.
In this post I'll show how to do this using P/Invoke (using some code borrowed from Fenton Webb, from DevTech Americas) and in my next post I'll show how to use the "internal" AutoCAD managed assembly.
Here's the C# code that uses P/Invoke, which should work for AutoCAD 2007 and 2008:
using Autodesk.AutoCAD.Runtime;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ProgressMeterTest
{
  public class Cmds
  {
    [DllImport(
      "acad.exe",
      CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.Cdecl,
      EntryPoint = "?acedSetStatusBarProgressMeter@@YAHPB_WHH@Z"
      //This should work for AutoCAD 2006...
      //EntryPoint = "?acedSetStatusBarProgressMeter@@YAHPBDHH@Z"
  )]
    private static extern int
      acedSetStatusBarProgressMeter(
        string label,
        int minPos,
        int maxPos
      );
    [DllImport(
      "acad.exe",
      CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.Cdecl,
      EntryPoint = "?acedSetStatusBarProgressMeterPos@@YAHH@Z"
    )]
    private static extern int
      acedSetStatusBarProgressMeterPos(int pos);
    [DllImport(
      "acad.exe",
      CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.Cdecl,
      EntryPoint = "?acedRestoreStatusBar@@YAXXZ"
    )]
    private static extern int acedRestoreStatusBar();

    [CommandMethod("PB")]
    public void ProgressBar()
    {
      acedSetStatusBarProgressMeter("Testing Progress Bar", 0, 100);
      for (int i = 0; i <= 100; i++)
      {
        for (int j = 0; j <= 10; j++)
        {
          System.Threading.Thread.Sleep(1);
          acedSetStatusBarProgressMeterPos(i);
          // This allows AutoCAD to repaint
          Application.DoEvents();
        }
      }
      acedRestoreStatusBar();
    }
  }
}


And here's what you see when it runs:
0.jpg
Update:
Thanks to Chris Bray for pointing out the above technique (and the one I was about to show in Part 2) is unnecessary from AutoCAD 2007 onwards. A new class was introduced in AutoCAD 2007 called Autodesk.AutoCAD.Runtime.ProgressMeter.
Here's some C# code that demonstrates the use of this class:
using Autodesk.AutoCAD.Runtime;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ProgressMeterTest
{
  public class Cmds
  {
    [CommandMethod("PB")]
    public void ProgressBarManaged()
    {
      ProgressMeter pm = new ProgressMeter();
      pm.Start("Testing Progress Bar");
      pm.SetLimit(100);
      // Now our lengthy operation
      for (int i = 0; i <= 100; i++)
      {
        System.Threading.Thread.Sleep(5);
        // Increment Progress Meter...
        pm.MeterProgress();
        // This allows AutoCAD to repaint
        Application.DoEvents();
      }
      pm.Stop();
    }
  }
}

The original code is still the technique to use for AutoCAD 2005 & 2006, although you will need to uncomment the line in the DllImport attribute for acedSetStatusBarProgressMeter(), to make sure it uses the EntryPoint with the non-Unicode string argument ("?acedSetStatusBarProgressMeter@@YAHPBDHH@Z"). You'll clearly also need to comment out the current EntryPoint assignment, of course.
I'll forego the Part 2 post (and rename this one from Part 1), as there's really no need to look any other technique for this, at this stage.


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-1-6 01:25 , Processed in 0.396927 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表