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

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

问题描述

在多个站点上看起来很复杂。使用Visual Studio 2017在C#中构建Solidworks插件的正确步骤是什么?

解决方案

我想分享一下我使用Visual Studio Professional 2017和Windows 10为SolidWorks 2016构建C#插件的经验。我希望我有这样的帖子可以节省我的时间,所以我认为将来的用户可以在这篇文章中找到帮助。



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



我基本上遵循了Solidworks提供的步骤(需要SolidWorks客户门户帐户),您可以找到< a href = http://help.solidworks.com/2017/english/api/sldworksapiprogguide/GettingStarted/SolidWorks_API_Getting_Started_Overview.htm rel = noreferrer>此处:



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



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



2。



您可以找到Solidworks提供的信息此处



.zip文件位于Visual Studio的模板文件夹中,在创建C#项目时,您应该具有这个新的选项。 / p>

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



第二,如果您收到此错误,则需要包含C#参考。只需右键单击参考,添加参考,装配体,然后检查Microsot.CSharp。



第三,我个人不得不将Project Platform Target更改为x64,以便将Addin添加到RegisterCOM。



完成这些步骤后,就可以构建项目了。由于默认情况下此模板为 COM可见和注册COM互操作,因此应在Buid上调用swAddin.cs中名为RegisterFunction的函数(在Clean上调用UnRegisterFunction)。请注意,如果您在此函数中放置一个断点,则不会出现该断点。



然后您应该在 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\SolidWorks\AddIns [GUID] 中查看包含您提供的信息的加载项信息在您的SwAddin.cs顶部。

  ///< summary> 
/// SwCSharpAddin1的摘要描述。
///< / summary>
[Guid( f0a50f9b-7c7f-4d90-8fc2-15d5d34a3a9f),ComVisible(true)]
[SwAddin(
Description = SwCSharpAddin1 description,
Title = SwCSharpAddin1,
LoadAtStartup = true
)]

绒毛:您可以填写项目属性/应用程序/ AssemblyInformation中的装配信息。对于GUID部分,您可以使用此网站。不要使用与代码部分相同的GUID,它们必须不同。如果我可以与您分享我提供版本号的方式:YY.MM.DD.VV,如(Year.Month.Day。当天的增量版本)



3。调试您的外接程序



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



注意:我在这里未包含任何代码。我希望您从模板开始。更容易理解。


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

解决方案

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. Downloading the C# template for visual Studio and installing it

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

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#)

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. Creating a C# Soliworks Project with Visual Studio.

You can found what information Solidworks provides here

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

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)

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

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

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.

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 : 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. Debugging your Addin

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天全站免登陆