NX Open and VS Code

Table of Contents

Background

NX Open applications written in C# are usually created with Microsoft’s Visual Studio. NX has wizards to easily set up the corresponding projects. However, if you can’t or don’t want to use that development environment, the open-source program VS Code (Visual Studio Code) is a good alternative. Apart from the name and the manufacturer, those two tools have only a few things in common. VS Code is primarily a source code editor1.
For small and medium-sized NX Open projects, all necessary features are there. In particular, IntelliSense, which is indispensable for working productively with the numerous classes and methods of the NX Open API, corresponds to that of Visual Studio.

Requirements

In addition to the actual program, other components are required to use VS Code as a development environment for NX Open applications. The required version of these components depends on the NX target version. In the example presented here, the target version is NX 1899. All components necessary in addition to NX are available online for free.

  • NX 1899 including the Programming Tools Feature
  • .NET Framework 4.6.2 Developer Pack2
  • Visual Studio Code3
  • C# for Visual Studio Code (VS Code Extension)

If not only uncompiled journals are to be created, the compiler must be installed separately:

  • .NET Desktop-Buildtools (part of the der Buildtools for Visual Studio 20194). From the optional components at least the .NET Framework 4.6.2 development tools should be installed.

Details on the use of VS Code can be found here5. The C# extension for vs code can be installed from within the program.

Install C# extension.

VS Code with the C # extension is primarily aimed at .NET Core and not .NET Framework, which is required for NX Open development. Accordingly, a message may appear indicating that the .NET Core SDK is missing. This message can be ignored or suppressed.

The message on the .NET Core SDK can be ignored.

Furthermore, the environment variable UGII_BASE_DIR should be set when using VS Code for NX Open development. This can be done by using a small start script. The path to the build tools can also be added to the PATH variable here if not only uncompiled journals are to be created.

rem Set NX base path
set "UGII_BASE_DIR=c:\nx\NX 1899"
rem Add compiler path
set "PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin;%PATH%"
rem Run VS Code
code 

Set up NX Open Projects

First, a new directory must be created for the project and must be opened in VS Code. This can be done in one step by File/Open Folder… and the corresponding context menu.

Create and open project directory.

The project file required for a working IntelliSense and compilation must be created by hand. The template file of the wizard from the NX installation can serve as a template (<UGII_BASE_DIR>\UGOPEN\vs_files\VC#\VC#Wizards\ NXOpen_VCS\Templates\1033\default_dll.csproj). The extension of the new file must be csproj.

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x64</Platform>
    <ProductVersion>8.0.50727</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <!-- You may want to make this GUID unique -->
    <ProjectGuid>{ECAB86B8-89C2-4806-B397-C2B07638CFXX}</ProjectGuid>
    <!-- Use 'library' for internal NX Open program and 'exe' for external -->
    <OutputType>library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace></RootNamespace>
    <!-- Enter name of the compiled dll/exe -->
    <AssemblyName>nx_project</AssemblyName>
    <!-- Enter target framework -->
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <Prefer32Bit>false</Prefer32Bit>
	<PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <Prefer32Bit>false</Prefer32Bit>
	<PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="NXOpen, Culture=neutral, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(UGII_BASE_DIR)\nxbin\managed\NXOpen.dll</HintPath>
	  <Private>False</Private>
    </Reference>
    <Reference Include="NXOpen.UF, Culture=neutral, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(UGII_BASE_DIR)\nxbin\managed\NXOpen.UF.dll</HintPath>
	  <Private>False</Private>
    </Reference>
    <Reference Include="NXOpen.Utilities, Culture=neutral, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(UGII_BASE_DIR)\nxbin\managed\NXOpen.Utilities.dll</HintPath>
	  <Private>False</Private>
    </Reference>
    <Reference Include="NXOpenUI, Culture=neutral, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(UGII_BASE_DIR)\nxbin\managed\NXOpenUI.dll</HintPath>
	  <Private>False</Private>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <!-- Add sourcefiles to include -->
    <!-- 
    <Compile Include="<source>.cs" />
    -->
  </ItemGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets"/>
</Project>
Project directory with project file.

Now one or more source code files can be added to the project. All files must be entered into the project file.

...
  <ItemGroup>
    <!-- Add sourcefiles to include -->
    <Compile Include="program.cs" />
  </ItemGroup>
...

Now all features for source code highlighting and automatic completion are available.

Source code highlighting and autocomplete.

Compiling

Before compiling, the build tools must be installed and the PATH environment variable must be adjusted accordingly. Press Ctrl + Shift + b to compile. If the necessary build task is not yet available, the generation is offered.

Create build task.
Create JSON configuration file.
Select task template.

The tasks.json file can now be found in the .vscode subfolder of the project directory. It can be modified according to the following example.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "msbuild debug",
            "type": "shell",
            "command": "msbuild",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                "/t:build",
                "/p:Configuration=Debug",
                // Do not generate summary otherwise it leads to duplicate errors in Problems panel
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        },
        {
            "label": "msbuild release",
            "type": "shell",
            "command": "msbuild",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                "/p:Configuration=Release",
                "/t:build",
                // Do not generate summary otherwise it leads to duplicate errors in Problems panel
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }

    ]
}

After pressing Ctrl + Shift + b again you can choose between a debug and a release build.

Conclusion

VSCode can be set up with relatively little effort and used for the development of NX Open applications in C# as an alternative to Visual Studio, especially for uncompiled journals and smaller projects.


  1. VS Code on Wikipedia ↩︎

  2. .NET Framework 4.6.2 Developer Pack: https://dotnet.microsoft.com/download/dotnet-framework/net462 ↩︎

  3. Visual Studio Code: https://code.visualstudio.com ↩︎

  4. Buildtools for Visual Studio: https://visualstudio.microsoft.com/downloads ↩︎

  5. Visual Studio Code Documentation: https://code.visualstudio.com/docs ↩︎

Related