如何在Visual Studio 2019中创建SwCSharpAddin项目?创建时遇报错怎么办?
Hey there! Let’s tackle your questions about building a SwCSharpAddin project in Visual Studio 2019, plus fix those frustrating errors that pop up along the way.
1. Step-by-Step Guide to Create a SwCSharpAddin Project in Visual Studio 2019
Let’s walk through this step by step to make sure you don’t miss anything:
- First, double-check you have Visual Studio 2019 installed with the .NET Desktop Development workload. If not, fire up the Visual Studio Installer, hit "Modify", and check that workload to install the necessary C# tools and frameworks.
- Launch Visual Studio 2019, then click Create a new project from the start screen.
- In the project search bar, type
SwCSharpAddin. If you don’t see this template, you need the SolidWorks API Templates extension:- Go to Extensions > Manage Extensions in Visual Studio.
- Search the Online marketplace for "SolidWorks API Templates", download and install it, then restart Visual Studio to load the extension.
- Once you spot the
SwCSharpAddintemplate, select it and click Next. - Name your project, pick a save location, and click Create to generate the project structure.
- You’ll see the default add-in setup: a class implementing
ISwAddin, the requiredConnect/Disconnectmethods, and attributes likeSwAddinAttributeto register the add-in with SolidWorks. - Before building, tweak a few project settings:
- Right-click the project > Properties > Build. Under "Conditional compilation symbols", add the symbol matching your SolidWorks version (e.g.,
SW2019for SolidWorks 2019 – check the SolidWorks API docs for your exact version’s symbol). - Head to References and confirm the SolidWorks interop assemblies (
SolidWorks.Interop.sldworks,SolidWorks.Interop.swconst,SolidWorks.Interop.swpublished) are present. The template should add these automatically, but it never hurts to double-check.
- Right-click the project > Properties > Build. Under "Conditional compilation symbols", add the symbol matching your SolidWorks version (e.g.,
- Build the project, and you’ll get a
.dllfile ready to load into SolidWorks.
2. Common Errors & Fixes When Creating SwCSharpAddin Projects
Here are the most frequent issues users run into, plus how to fix them:
Error: "The template 'SwCSharpAddin' could not be found"
Exact Error Message: We couldn't find a template matching your search criteria.
- How to Fix:
- Install the SolidWorks API Templates extension via Visual Studio’s Manage Extensions (as outlined in the creation steps above).
- If you already installed it, restart Visual Studio – extensions often don’t activate until you reboot the IDE.
- Make sure your Visual Studio 2019 is updated to the latest version (go to Help > Check for Updates) – older builds might not support the template pack.
Error: "The type or namespace name 'SolidWorks' could not be found"
Exact Error Message: The type or namespace name 'SolidWorks' could not be found (are you missing a using directive or an assembly reference?)
- How to Fix:
- Right-click your project > Add > Reference > Browse. Navigate to your SolidWorks installation folder (typically
C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS). - Select
SolidWorks.Interop.sldworks.dll,SolidWorks.Interop.swconst.dll, andSolidWorks.Interop.swpublished.dll, then click OK. - Alternatively, if you have the SolidWorks API SDK installed, these references will show up under Assemblies > Extensions in the Reference Manager.
- Right-click your project > Add > Reference > Browse. Navigate to your SolidWorks installation folder (typically
Error: "Undefined conditional compilation symbol 'SWXXX'"
Exact Error Message: The name 'swDocumentTypes_e' does not exist in the current context (or similar API type/constant errors)
- How to Fix:
- Go to project properties > Build > "Conditional compilation symbols". Add the correct symbol for your SolidWorks version (e.g.,
SW2020for SolidWorks 2020). You can find the exact symbol for your version in the SolidWorks API documentation. - Also, make sure you have the
using SolidWorks.Interop.swconst;directive at the top of your code file to access the API constants.
- Go to project properties > Build > "Conditional compilation symbols". Add the correct symbol for your SolidWorks version (e.g.,
内容的提问来源于stack exchange,提问作者风何之
相关产品推荐
相关产品推荐

