Using SOLIDWORKS® API from Visual Studio Code and .NET Core
Visual Studio Code is one of the most popular free, open-source, and lightweight code editors. However, VS Code went well beyond the text editor and become an Integrated Development Environment for developing sites, web applications, technical documentation, and desktop applications.
.NET Core (or its future successor .NET 5) is a cross-platform framework developed alongside .NET Framework, which is newer, robust and will most likely eventually replace .NET Framework.
Although .NET Framework will be supported for long years to keep all legacy applications compatible, it will be no new features added to the framework so it is a good idea to start moving towards the new .NET Core/.NET 5 framework for your applications.
In this blog article, I will demonstrate how to create an application to automate SOLIDWORKS via SOLIDWORKS API using .NET Core and Visual Studio Code editor. I will be using the free and open-source xCAD.NET framework to streamline the development process. This framework allows use of built-in functions or direct SOLIDWORKS API.
We will create a simple application that allows to configure the SOLIDWORKS template model based on user input parameters and save the result into the specified location.
Application will perform the following steps:
- Start SOLIDWORKS
- Open template model
- Read user input parameters
- Apply parameters to corresponding dimensions
- Save model to the specified output location
- Close SOLIDWORKS
Watch the video demonstration below for creating, debugging, and running of .NET Core application to automate SOLIDWORKS from the Visual Studio Code.
Step-By-Step Instructions
- Install Microsoft Visual Studio Code
- Install the C# Extension to enable support of .NET Core. Find more information in this article
- Create new Console project in VS Code by running the following command in VS Code Terminal
> dotnet new console
Project will be created in the current working folder. To change the folder either use the cd command or open the folder in VS Code from the File->Open Menu or ctrl+K shortcut.
- Add the reference to Xarial.XCad.SolidWorks nuget package.
> dotnet add package xarial.xcad.solidworks
- Add the following code into the Program.cs file
using System; using System.Threading.Tasks; using Xarial.XCad.SolidWorks; using Xarial.XCad.SolidWorks.Enums; namespace model_generator { class Program { static async Task Main(string[] args) { using(var app = await SwApplication.Start(SwVersion_e.Sw2020, "/b")) { var doc = app.Documents.Open(new Xarial.XCad.Documents.Structures.DocumentOpenArgs() { Path = @"D:\Demo\model-generator\template\model1.SLDPRT", ReadOnly = true }); Console.WriteLine("Enter width in meters"); var widthStr = Console.ReadLine(); if(!string.IsNullOrEmpty(widthStr)) { doc.Dimensions["Width@Base"].SetValue(double.Parse(widthStr)); } Console.WriteLine("Enter height in meters"); var heightStr = Console.ReadLine(); if(!string.IsNullOrEmpty(heightStr)) { doc.Dimensions["Height@Boss"].SetValue(double.Parse(heightStr)); } Console.WriteLine("Enter length in meters"); var lengthStr = Console.ReadLine(); if(!string.IsNullOrEmpty(lengthStr)) { doc.Dimensions["Length@Base"].SetValue(double.Parse(lengthStr)); } Console.WriteLine("Enter output file path"); var outFilePath = Console.ReadLine(); int errs = -1; int warns = -1; if(!doc.Model.Extension.SaveAs(outFilePath, (int)SolidWorks.Interop.swconst.swSaveAsVersion_e.swSaveAsCurrentVersion, (int)SolidWorks.Interop.swconst.swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errs, ref warns)) { throw new Exception("Failed to save document"); } app.Close(); } } } }
- Use the following command to run the application
> dotnet run
- Alternatively build the application to run the executable as an independent program (outside of VS Code)
> dotnet build
The application will prompt for user inputs and configure the resulting model.