C# SendKeyin 调用VBA,结果不完全

参考了这个教程“Create Drawing”对话框设置 - 技术资料库 - Bentley 中国优先社区 - Bentley Communities

想不打开 Create Drawing 快速生成剖面图,录制的宏和VBA代码如下:

C#中用Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin调用keyin命令

public static MyModalDialogEvents Instance = new MyModalDialogEvents();
        void BIM.IModalDialogEvents.OnDialogOpened(string DialogBoxName, ref BIM.MsdDialogBoxResult DialogResult)
        {
            if (DialogBoxName == "Create Drawing")
            {
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_COINCIDENTSHEET_OFF");
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_ASSIGNSAVEDVIEWNAME 圆形井剖面" );

                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_SET_DRAWINGBOUNDARY 1");
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_COINCIDENTSHEET_OFF");

                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_SET_DISCIPLINE 建筑");
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_SET_PURPOSE 剖面");
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND DRAWCOMP_VISIBLEEDGES 1");
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND DRAWCOMP_VISIBLEEDGES 1");

                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_ADDSHEETTOSHEETINDEX_OFF");
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_COINCIDENTSHEET_OFF");

                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_OPENMODEL_ON");



                //DialogResult = BIM.MsdDialogBoxResult.OK;
            }
        }

其中MDL COMMAND CREATEDRAWING_SET_DRAWINGBOUNDARY 1,是选择drawing boundary的,默认情况是 new,想选择为第二个选项,如下图

执行代码后,发现对话框上的Name成功更改了,但是drawing boundary没有按照代码的设置,想问以下有什么办法能够将Drawing Boundary 或者其他的选项也通过代码设置成自己想要的内容呢?

  • 确实如您所说,我研究一下,有解决办法再回复您。

  • 您在C#中去修改这些选项时,换一种方式发送keyin命令,通过PInvoke调用C函数mdlInput_sendKeyin去发送,在某个类中如下声明这个函数:

    [DllImport("ustation.dll", CharSet = CharSet.Unicode)]
            public static extern void mdlInput_sendKeyin(string stringP, bool literal, int position, string taskIdP);

    然后在模态对话框那个监听函数里边这样发送:

     mdlInput_sendKeyin("MDL COMMAND CREATEDRAWING_SET_DRAWINGBOUNDARY 1", false, 0, "");

    Answer Verified By: Spartan117 梁 

  • 郭老师,我的代码结构如下:在按钮方法下写入监听函数,通过keyin设置剖面的基本参数,最后打开了Create Drawing 对话框

    我在监听函数的类里面,引入了你说的mdlInput_sendKeyin,并做了图名、drawing boundary、add sheet to index 三个设置的测试

    结果是图名一定成功,drawing boundary一直失败,add sheet to index 第一次失败,后面成功了

    不知您那儿的效果是怎么样的?是不是论坛提供的方法仅对input有效果,对combox,check box 要采取其他方法呢?

  • 我只是添加了一个模态对话框的监听器,然后手工去创建Callout,是没有问题的,请把您的代码用如下所示的菜单项上传一下,我在我这边试一下。

  • private void button3_Click(object sender, EventArgs e)
            {
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.AddModalDialogEventsHandler(MyModalDialogEvents.Instance);
                // 模拟输入Key-in
                Session.Instance.Keyin("rotate activeview top 1");
                Session.Instance.Keyin("place sectioncallout");
                Session.Instance.Keyin("detailingsymbol set drawingseed 水工-剖面");
                Session.Instance.Keyin("detailingsymbol savedview on");
                Session.Instance.Keyin("point absolute 0,3.55,0");
                Session.Instance.Keyin("point absolute 0,-3.55,0");
                Session.Instance.Keyin("point absolute 3.55,3.55,0");
    
            }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using BIM = Bentley.Interop.MicroStationDGN;
    
    namespace Tool
    {
        class MyModalDialogEvents : Bentley.Interop.MicroStationDGN.IModalDialogEvents
        {
            [DllImport("ustation.dll", CharSet = CharSet.Unicode)]
            public static extern void mdlInput_sendKeyin(string stringP, bool literal, int position, string taskIdP);
    
            public static MyModalDialogEvents Instance = new MyModalDialogEvents();
            void BIM.IModalDialogEvents.OnDialogOpened(string DialogBoxName, ref BIM.MsdDialogBoxResult DialogResult)
            {
                if (DialogBoxName == "Create Drawing")
                {
                    //Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CadInputQueue.SendKeyin("MDL COMMAND CREATEDRAWING_ASSIGNSAVEDVIEWNAME 圆形井剖面");
                    mdlInput_sendKeyin("MDL COMMAND CREATEDRAWING_ASSIGNSAVEDVIEWNAME 圆形井剖面", false, 0, "");
                    mdlInput_sendKeyin("MDL COMMAND CREATEDRAWING_SET_DRAWINGBOUNDARY 1", false, 0, "");
                    mdlInput_sendKeyin("MDL COMMAND CREATEDRAWING_ADDSHEETTOSHEETINDEX_ON", false, 0, "");
    
                   
                    //DialogResult = BIM.MsdDialogBoxResult.OK;
                }
            }
    
            void BIM.IModalDialogEvents.OnDialogClosed(string DialogBoxName, BIM.MsdDialogBoxResult DialogResult)
            {
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.RemoveModalDialogEventsHandler(MyModalDialogEvents.Instance);
            }
        }
    }