XARIAL BLOG
XARIAL BLOG
Blog about software development, automating SOLIDWORKS with macros and API, C# Visual Basic, WPF, WCF, web development, tips & tricks

Using SOLIDWORKS® API from Visual Studio Code and .NET Core

16 Jun, 2020

Visual Studio Code for SOLIDWORKS Automation
Visual Studio Code for SOLIDWORKS Automation

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

Template model with width, height, and length driving parameters
Template model with width, height, and length driving parameters

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

> dotnet new console

Visual Studio Code Terminal
Visual Studio Code Terminal

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.

> 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.

Running the application in the Windows Command Line
Running the application in the Windows Command Line

Subscribe to Newsletter

Powered by Docify