Solidworks 二次开发,eDrawings篇

C# WinForms中用Panel嵌入eDrawings,实现图档测量及预览功能

1): Solidworks的安装版本是2021.(听说2022版本后的Edrawings工具可以添加EModelView.dll至工具箱,然后直接当控件调用!)
2):Winforms 平台,Panel 控件.
3):直接上代码, 请参考.

第一步:在自己的项目下添加→组件→组件类(这里命名为:“edDWHost”)
在这里插入图片描述
第二步:在“edDWHost.cs”组件下添加代码
引用:
在这里插入图片描述
组件:
在这里插入图片描述

using EModelView;

namespace  xxxx       //“xxxx” 命名空间 请自行更改
{
    public partial class edDWHost
    {
        private EModelViewControl ocx;

        protected override void AttachInterfaces()
        {
            base.AttachInterfaces();
            ocx = (EModelViewControl)base.GetOcx();
            ocx.EnableFeatures = 16;
        }
      
    }
}

第三步:在“edDWHost.Designer.cs”组件下添加代码
在这里插入图片描述

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace xxxx   //“xxxx”命名空间 自行调整
{
    public partial class edDWHost : AxHost
    {
        // Constructor that takes a container as a parameter
        public edDWHost(IContainer container) : base("{22945A69-1191-4DCF-9E6F-409BDE94D101}")
        {
            // Required for Windows.Forms Class Composition Designer support
            if (container != null)
            {
                container.Add(this);
            }

            // Initialize the component
            InitializeComponent();
        }

        // Default constructor
        public edDWHost() : base("{22945A69-1191-4DCF-9E6F-409BDE94D101}")
        {
            // This call is required by the Component Designer.
            InitializeComponent();
        }

        // Component overrides dispose to clean up the component list.
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing && components != null)
                {
                    components.Dispose();
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        // Required by the Component Designer
        private IContainer components;

        // Initialize the components
        private void InitializeComponent()
        {
            components = new Container();
        }
    }
}

第四步:创建Button 事件

 private void button35_Click(object sender, EventArgs e)
        {
             Console.WriteLine(tabControl1.SelectedTab.Name);   //我这边因为是多页(tabcontrol控件),大家可以酌情删除)
            if ( tabControl1.SelectedTab.Name.ToLower() == "tabpage3")
            {
                hostContainer = new edDWHost();

                // 将容器添加到此窗体后,再尝试获取底层 OCX
                panel103.Controls.Add(hostContainer);     //panel103,大家可以酌情更改
                hostContainer.Dock = DockStyle.Fill;

                emvControl = (EModelViewControl)hostContainer.GetOcx();
                filepath = @"xxx";            //"xxx" 为 你要打开的文件,格式可以是Stp,XT,DWG等
               //open the document
                if (!string.IsNullOrEmpty(filepath))
                {
                    emvControl.OpenDoc(filepath, false, false, true, "");
                }
            }
            
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值