Building a C# addin for Solidworks [closed]

房东的猫 提交于 2020-01-02 08:44:08

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/51823756/building-a-c-sharp-addin-for-solidworks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!