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, "");
}
}
}
2145

被折叠的 条评论
为什么被折叠?



