为 Solidworks 构建 C# 插件 [英] Building a C# addin for Solidworks

查看:54
本文介绍了为 Solidworks 构建 C# 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多个站点上似乎相当复杂.使用 Visual Studio 2017 在 C# 中构建 Solidworks 插件的正确步骤是什么?

It seems quite complicated on multiple sites. What are the proper steps to build a Solidworks Addin in C# with Visual Studio 2017?

推荐答案

我想分享我使用 Visual Studio Professional 2017 和 Windows 10 为 Solidworks 2016 构建 C# 插件的经验.我希望我有这样的帖子节省我的时间,所以我认为未来的用户可以找到这个问题的帮助.

I'd like to share my experience on building a C# addin for Solidworks 2016 using Visual Studio Professional 2017 and Windows 10. I wish I had such a post to save me time so I think future users could find help into this one.

1.下载 Visual Studio 的 C# 模板并安装

我基本上遵循了 Solidworks 提供的步骤(需要 Solidworks 客户门户帐户),您可以在 这里 :

I basically followed the steps provided by Solidworks (requires a Solidworks Customer Portal account) that you can find here :

遇到的问题:exe 将被提取到 MSI 中,然后 MSI 将运行.安装完成后,我无法在 Visual Studio 中找到模板项目,也无法在其文件夹 (C:\Users[UserName]\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#) 中找到

Problem Encoutered : The exe would be extracted into a MSI and then the MSI would run. Once the installation done, I couldn't find the template project in Visual Studio, neither in its folder (C:\Users[UserName]\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#)

解决方案:我发现这个 post 建议使用 7zip 解压缩 MSI.果然,该解决方案对我有用,我能够将 zip 文件 (swcsharpaddin.zip) 复制到其各自的文件夹中.

Solution : I found this post that suggest unziping the MSI using 7zip. Sure enough, that solution worked for me and I was able to copy the zipfile (swcsharpaddin.zip) in its respectives folder.

2.使用 Visual Studio 创建 C# Soliworks 项目.

您可以在此处找到哪些信息一个>

You can found what information Solidworks provides here

一旦 .zip 文件位于 Visual Studio 的模板文件夹中,您应该有这个新的 选项 创建 C# 项目时.

Once the .zip file is in the templates folder of Visual Studio, you should have this new option when creating a C# project.

首先,如果您收到此错误,您需要在project is created 是项目的 .Net Framework 版本.您可以通过进入项目属性并将框架更改为等于或高于 DLL 之一的版本来实现.(此处为 4.0.0.0)

First, if you get this error, you'll need to change once the project is created is the .Net Framework version of your project. You can do so by going in the Project Properties and changing the framework to a version equals or higher than the one of the DLLs. (4.0.0.0 here)

其次,如果您收到此错误,则需要包含 C#参考.只需右键单击引用,添加引用,程序集,检查 Microsot.CSharp.

Second, if you get this error, you'll need to include the C# reference. Just right-click reference, Add Reference, Assemblies, check Microsot.CSharp.

第三,我个人不得不将项目 Platform Target 更改为 x64,以便我的 Addin to RegisterCOM.

Third, I personally had to change the project Platform Target to x64 in order for my Addin to RegisterCOM.

完成这些步骤后,您就可以构建您的项目了.由于此模板默认为COM Visible"和Register for COM interop",swAddin.cs 中名为 RegisterFunction 的函数应在 Buid 上调用(在 Clean 上调用 UnregisterFunction).请注意,如果在此函数中放置断点,则不会命中.

Once these steps are done, you may build your project. Since this template is by default "COM Visible" and "Register for COM interop", the function named RegisterFunction in swAddin.cs should be called on Buid (and UnregisterFunction on Clean). Note that if you place a breakpoint in this function, it won't be hit.

然后,您应该会在 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\SolidWorks\AddIns[GUID] 中看到您的插件信息,以及您的 SwAddin.cs 顶部提供的信息.

You should then see your addin info in Computer\HKEY_LOCAL_MACHINE\SOFTWARE\SolidWorks\AddIns[GUID] with the informations provided in the top of your SwAddin.cs.

    /// <summary>
/// Summary description for SwCSharpAddin1.
/// </summary>
[Guid("f0a50f9b-7c7f-4d90-8fc2-15d5d34a3a9f"), ComVisible(true)]
[SwAddin(
    Description = "SwCSharpAddin1 description",
    Title = "SwCSharpAddin1",
    LoadAtStartup = true
    )]

Fluff :您可以在项目属性/应用程序/程序集信息中填写您的程序集信息.对于 GUID 部分,您可以使用此网站.不要使用与代码部分相同的 GUID,它们必须不同.如果我可以与您分享我提供版本号的方式:YY.MM.DD.VV,如(Year.Month.Day.Incremental version for the day)

Fluff : You may fill your assembly information in the Project Properties/Application/AssemblyInformation. For the GUID part, you may use this site. Don't take the same GUID as in the code section, they must be different. If I may share with you my way of giving version numbers : YY.MM.DD.VV as in (Year.Month.Day.Incremental version for the day)

3.调试您的插件

在项目属性/调试部分,您应该已经使用 Solidworks 路径选中了启动外部程序".如果是这样,您现在可以调试您的应用程序.您可以在此处找到示例.您可以在 CreateCube 函数内放置一个断点,只要您在 Solidworks 中单击该选项,您就会立即点击它.

In the project Properties/Debug section, you should already have "Start External Program" checked with your Solidworks path. If so, you are now able to debug your application. You can find an example here . You can place a breakpoint inside the CreateCube function and you'll hit it as soon are you click the option in Solidworks.

注意:我不在这里包含任何代码.我宁愿让你从模板开始.更容易理解.

NOTE : I don't include any code here. I'd rather have you start from the template. Much easier to understand.

这篇关于为 Solidworks 构建 C# 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆