How to use SimpleElastix as a reference of a new C# project? - c#

I git clone SimpleElastix [1] project from https://github.com/kaspermarstal/SimpleElastix, and built and compiled it successfully on my PC. The WRAP_CSHARP option was set in CMAKE. I would like to use this project as a reference/framework to implement a new medical image registration GUI with C#. However, I didn't find any .dll file in the compiled SimpleElastix solution. I have also tried to create a new C# project from the existing SimpleElastix project via Visual Studio, the new solution cannot be built successfully. Does anybody knows where to find th .dll file of SimpleElatix? Or how to use SimpleElastix as reference/framework in a new C# Project?
[1] SimpleElastix is a project based on ITK and SimpleITK for medical image registration, which is compatible with python, C#, Java etc. Here is the documentation http://simpleelastix.readthedocs.io/
best,
Siming

SimpleElastix is derived from the SimpleITK project[1]. From SimpleITK's wiki[2], you can find more information about how to use SimpleITK in various configurations and languages. There is even a "Visual Guide to SimpleITK with CSharp"[3].
The instructions in short say to add the manage library to the project "SimpleITKCSharpManaged.dll" in the ReferenceManager. Then add the native library, "SimpleITKCSharpNative.dll", as an exist item with the "Add As Link" option with a build option of "Copy always". If you do this with the SimpleElastix libraries it should work.
Here is a related question as to where to find more information about SimpleITK Where can I find the SimpleITK documentation and reference information?
[1] http://www.simpleitk.org//
[2] https://itk.org/Wiki/SimpleITK
[3] https://itk.org/Wiki/SimpleITK/GettingStarted/A_visual_guide_to_SimpleITK_with_CSharp

You can find the 2 required DLLs in the folder SimpleITK-build\Wrapping\CSharp\CSharpBinaries, relative to the directory that you specified as the build output. The folder should contain
SimpleITKCSharpNative.dll (a huge DLL)
SimpleITKCSharpManaged.dll (a small one - that's the actual C# wrapper)
With those two DLLs, proceed as described in the answer by blowekamp

Related

ANTLR4 with Unity3D

I am currently trying to use my ANTLR4 parser/lexer in an Unity project.
The steps I have taken are:
Generate the parser and lexer from the grammar in a separate project and copying the Lexer, Parser and the visitors/listeners into the Unity project
Since obviously the Antlr4 runtime is missing I added the Antlr4.Runtime.v3.5.dll since Unity uses .NET 3.5
I fixed an error in the parser since IReadOnlyList could not be resolved. I just changed it to an IList
After all these steps, Unity presents me with the following error:
-----CompilerOutput:-stdout--exitcode:
-1073741819--compilationhadfailure: True
--outfile: Temp/Assembly-CSharp.dll
compute_class_bitmap: Invalid type 13 for field
Antlr4.Runtime.Recognizer`2[Symbol,ATNInterpreter]:_interp
can anyone help me with this matter? It is very important, that I get this parser up and running because otherwise I'd need to write the parser on my own which would be extremely annoying...
Make sure you are using the C# runtime for ANTLR, which can be found on github here:
https://github.com/tunnelvisionlabs/antlr4cs
The Readme file at that link has a lot of information about installing the runtime. I will refer to this Readme a couple of times as I give you a step-by-step list.
I am assuming you are using Windows. You cannot currently compile an ANTLR4 parser for C# using Mac.
1) Install Visual Studio Community (if you haven't already) and at install time make sure to install:
Core Visual Studio editor (default)
.NET desktop development (under Workloads)
NuGet Package manager (under Individual Components -> Code tools)
2) Create a new Project.
Select Windows Classic Desktop
Select a .NET Framework app
Once Project is created, right-click project name and select Properties and take note of your version of .NET Framework (mine is 4.6.1)
3) Use NuGet to install ANTLR4. Install a recent stable version of ANTLR4 (mine is 4.6.4)
Right click the top-level solution node in the Solution Explorer window and select Manage NuGet Packages for Solution...
Select Browse and in the search box type Antlr4
In the search results there will be several options. Click on the one whose name is exactly Antlr4
In the right-hand pane, choose the version to be the latest stable version (4.6.4) and then click Install
4) Add your grammar file (.g4) to your Project by clicking Project -> Add Existing Item... and navigating to your .g4 file.
5) From the github Readme at the link posted above:
If you have the ANTLR Language Support extension installed, this step is automatically performed. Otherwise, you will need to right click the grammar file in Solution Explorer and select Properties. In the properties window, configure the following items.
Build Action: Antlr4 (If Antlr4 doesn't appear as an option in the dropdown list, restart Visual Studio then try again)
Custom Tool: MSBuild:Compile
Custom Tool Namespace: The complete name of the namespace you want the generated classes to be located within
6) Restart Visual Studio and reload Project
7) Select Build -> Build Solution
8) Look in ~/repos/projectName/projectName/obj/Debug to find the generated files
copy *.tokens and *.cs to a new folder
also copy repos/projectName/projectName/Properties/AssemblyInfo.cs to that new folder
Open AssemblyInfo.cs and add the line "using System;" at the top and the line “[assembly: CLSCompliant(false)]” at the bottom
9) Move to Unity and import the folder you created in the previous step.
10) Open directory where you've installed Antlr4, and find subdirectory Antlr4.Runtime.4.6.4/lib (where you may need to replace 4.6.4 with your version of ANTLR4) and then choose the folder of the corresponding .NET version as used in step 2 (in my case the closest one is net4.5). Drag Antlr4.Runtime.dll from that subdirectory into your Unity project.
And you should be good to go for using ANTLR4 inside Unity.
Assuming that you've already created your parser in another project, and only want to use generated classes with Unity, it's as simple as putting Antlr4.Runtime.Standard.dll file into your Unity folder Assets/Plugins. This worked for us in Unity 2019.3.9f1.

Adding c++ OpenBLAS library to c# project

I'm trying to use OpenBLAS in a c# project (more specifically I'm attempting to use it with Math.NET via MathNet.Numerics.Control.LinearAlgebraProvider). I've successfully created the files following https://github.com/xianyi/OpenBLAS/wiki/How-to-use-OpenBLAS-in-Microsoft-Visual-Studio.
Now I have a .lib and .dll file in a new directory (as per subsection Use OpenBLAS .dll library in Visual Studio), but I'm not sure where to go from here!
Does anyone know what I'm supposed to do from here? I cannot directly add the generated dll to my project, and from further reading it seems like I need to generate a new dll using Visual Studio.
Use the NativeWrappers solution file provided by MathNet.
Similar instructions for MKL and ACML are here:
http://mathnetnumerics.codeplex.com/wikipage?title=Native%20Providers&referringTitle=Documentation

How to Create a DLL file in Visual Studio C# 2010 Express edition?

I have already come across the Stack Overflow question "Is there a way to generate a DLL file from Visual Studio Express without explicitly creating a DLL project?", but it does not directly answer my query, so I'm raising it here.
The problem I am facing while trying to make the DLL is that I can't find any option under Build named build class file.
I have changed the project property to class file (shown below)
This is how it is:
And here is how my build option is getting displayed:
Also when I am using the command-line option the dll file is getting generated but it is not getting the properties I'm setting in the application.
I am new with Visual Studio so a litte bit confused about this part.
The "Build Solution" option in your second screenshot is the thing you need to click to produce your dll, alternatively you can right click on your project in the Solution Explorer and click "Build":
(If you only have one project in your solution then these two will both do exactly the same thing)
The output dll will normally be placed in the bin\Debug or bin\Release directory depending on whether you are in Release or Debug configuration, check the "Build" tab of the project properties for the exact path.
The reason why you aren't seeing a "Build class file" option is because this is what the "Build project" menu item does - it will produce a class library if the project output type is "Class Library", a windows executable if the project output type is "Windows Application" etc...
You're not trying to build a class file - you're trying to build a class library.
And you just build the solution - that will build each of the projects in your solution, including your LicenseCheckLibrary project is just a class library project.
It looks like you're basically there - look in the bin\Debug or bin\Release folders under LicenseCheckLibrary, and you'll find the DLL.
Why would you want to avoid building a DLL file in the first place? Are you developing an EXE file in order to test the logic and then conver it to DLL once it is working fine? If yes, why not create two projects: Windows Console and Class Library. Inside Class Library implement the licensing logic and use Windows COnsole to test the logic. When you say you are new with Visual Studio, what exactly do you mean? You never used it before or you are new to .NET Framework programming? .NET Framework has certain classes for developing licenses. Also, there were quetions here on stackoverflow regarding the licensing. Find some of them instead of reinventing the wheel.
Have a look at this article http://www.developer.com/net/net/article.php/3074001
Create a new class library project
Create classes and code
compile Project
Dll Created
Create a new project
Click on Add Reference
Navigate to the class library folder
Go into the debug folder or whatever and include
Remember you will prob have to include the namespace. in the new
project.

Problem creating COM assembly in C# 2005

I am kind of new to C#. I built a class library in order to create a DLL which I need to reference from a VB.Net application.
I followed all the steps detailed in this this article in order to generate the COM assembly based on my C# class library.
All the process ran smoothly (create the key, run gacutil.exe to add the assembly to the cache), but I still can't see the DLL from my main project in order to add it as a reference.
What am I doing wrong? Any help will be appreciated
There's some pretty bad advice in that article, the GAC is a deployment detail and has no relevance to the task of writing and using an assembly on your dev machine. The entire process also has nothing to do with COM at all.
Get ahead by opening your VB.NET solution in Visual Studio. Right-click the solution in the Solution Explorer window, Add, New Project. Pick "Class Library" from the C# node. Now right-click your VB.NET project, Add Reference, Project tab and select your C# project. Any of the public C# classes you write are now available in your VB.NET code.
You might not see the solution if it contains only one VB.NET project. Fix with Tools + Options, Project and Solutions, General, tick "Always show solution".

Generate C# project using CMake

I'm trying to generate a C# project within an existing C++ CMake code base on Windows. After some research, I could find only two projects that built their own CSharp compilers for CMake:
gdcm and kde.
I tried both of them. Unfortunately, the first one failed to generate a C# project. Instead it created a VS C++ project with cs files in it, and because of C++ flags set for linker, the build always failed with errors. After experimenting with the sample project they provided, I wonder whether this could be due to a limitation of the "Visual Studio 8 2005" code generator?
The second project was primarily aimed at Mono, so I wasn't successful with it either.
Has anyone had a positive experience with building C# projects using one of those CMake modules or something else?
As of CMake 3.8.2, CSharp project generation is officially supported by CMake.
To build the default Visual Studio 2017 generated C#/WPF project using CMake, create a CMakeList.txt file as follows.
Project Declaration
project(Example VERSION 0.1.0 LANGUAGES CSharp)
Include CMake CSharpUtilities if you are planning on using WPF or other designer properties.
include(CSharpUtilities)
Add all cs, xaml, settings, properties
add_executable(Example
App.config
App.xaml
App.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs
Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings)
Link designer files, xaml files, and other properties files with their corresponding cs files
csharp_set_designer_cs_properties(
Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings)
csharp_set_xaml_cs_properties(
App.xaml
App.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs)
Set app App.xaml properties file as program entry point (if project is a WPF project)
set_property(SOURCE App.xaml PROPERTY VS_XAML_TYPE "ApplicationDefinition")
Set other csproj file flags
set_property(TARGET Example PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
set_property(TARGET Example PROPERTY WIN32_EXECUTABLE TRUE)
// ...
Add libraries
set_property(TARGET Example PROPERTY VS_DOTNET_REFERENCES
"Microsoft.CSharp"
"PresentationCore"
"PresentationFramework"
"System"
"System.Core"
"System.Data"
"System.Data.DataSetExtensions"
"System.Net.Http"
"System.Xaml"
"System.Xml"
"System.Xml.Linq"
"WindowsBase")
For a working WPF example, see https://github.com/bemehiser/cmake_csharp_example
For a WinForms example, see this answer.
CMake 2.8.9 and up add a TYPE parameter to include_external_msproject like so:
include_external_msproject(
MyProject MyProject.csproj
TYPE FAE04EC0-301F-11D3-BF4B-00C04F79EFBC)
This lets you specify that the project is C# (the magic GUID above), otherwise things struggle (see docs).
You will probably still want to use the configure_file template approach mentioned elsewhere with your .csproj file to get the right paths into it, unless you're building straight into your source tree.
The good news is that you can wildcard your C# files in the .csproj.template file like so:
<ItemGroup>
<Compile Include="${DOS_STYLE_SOURCE_DIR}\**\*.cs" />
</ItemGroup>
And you'll need something like this in your CMakeLists.txt to convert CMake's unix-style forwardslash path separators into Windows-style backslashes, otherwise it will compile the files but they won't show up as links in the project in Visual Studio:
FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" DOS_STYLE_SOURCE_DIR)
Then it's just:
CONFIGURE_FILE(MyProject.csproj.template MyProject.csproj)
In your CMakeLists.txt to configure the template file into a real project file with the right wildcard path.
HTH.
Just in case anyone is still looking for information about this, there is really no reason to generate C# projects with CMake, they are cross platform by design. On Linux, C# projects are generally managed with MonoDevelop which can read .csproj files from visual studio just fine. This should enable cross-platform development of C# projects.
The only potential issue would be if you had native c++ projects mixed with c# projects (like a backend written in c++ with a GUI in c#), in this case just have cmake copy over your .csproj files as though they were data and you should be good to go.
CMake is intended to set up your build environment to be cross platform. This comes in really handy with c++ where code is built in a very different way on linux than on windows (or other OSs if youre into that), but is kind of unnecessary for c# which can execute cross-platform, and, thanks to design decisions by the mono team, can build cross platform. CMake does provide some great tools to automate things, but much of this functionality can be recovered with a properly configured .csproj file.
Anyway I know this question is over a year old but it's one of the top search results I stumbled on when I was looking up how to do this. I've since made this realization.
To piggy-back on the answer provided by #the_storyteller, CMake v3.8 and greater indeed supports C# as a first-class language. Because a WPF example was already provided, here is a complete CMake example for a simple Windows Forms application. I've provided the optional commands for linking in other libraries built locally in the source tree, and linking 3rd party library dependencies.
Note that Windows Forms applications require the use of the csharp_set_windows_forms_properties CMake command, whereas WPF projects use csharp_set_designer_cs_properties and csharp_set_xaml_cs_properties.
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(MyWinFormApp LANGUAGES CSharp)
# Include CMake utilities for CSharp, for WinForm and WPF application support.
include(CSharpUtilities)
# Define the executable, including any .cs files.
# The .resx and other Properties files are optional here, but including them makes them visible in the VS solution for easy editing.
add_executable(MyWinFormApp
App.config
Form1.cs
Form1.Designer.cs
Form1.resx
Program.cs
Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings
)
# Set the .NET Framework version for the executable.
set_property(TARGET MyWinFormApp PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
# Set the executable to be 32-bit.
set_property(TARGET MyWinFormApp PROPERTY WIN32_EXECUTABLE TRUE)
# Set the C# language version (defaults to 3.0).
set(CMAKE_CSharp_FLAGS "/langversion:latest")
# Set the source file properties for Windows Forms use.
csharp_set_windows_forms_properties(
Form1.cs
Form1.Designer.cs
Form1.resx
Program.cs
Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings
)
# If necessary, link in other library dependencies that were built locally in this source tree.
target_link_libraries(MyWinFormApp MyLocalLib)
# If necessary, link in other library/DLL references, such as 3rd party libraries.
set_property(TARGET MyWinFormApp PROPERTY
VS_DOTNET_REFERENCE_MyThirdPartyLib /path/to/libs/MyThirdPartyLib.dll)
# Add in the .NET reference libraries.
set_property(TARGET MyWinFormApp PROPERTY VS_DOTNET_REFERENCES
"Microsoft.CSharp"
"System"
"System.Core"
"System.Data"
"System.Drawing"
"System.Windows.Forms"
)
I was finally able to generate a valid solution using the second c# module - kde.
Although, cmake created a number of .vcproj files while I expected to get .csproj, but I guess that is the only form "Visual Studio 8 2005" generator can offer.
Nevertheless I was able to successfully build this solution and produce executables and dll libraries.
You can create a project with Visual Studio, than take it apart and make CMake write it using configure_file command (you'll have to generate a bit of XML with the list of sources) and add it to the solution with include_external_msproject (for other generators you'll need to create the custom target to run msbuild on it manually; cmake does not seem to support doing that yet). The project is rather simple, so it should be possible to do that.
I wanted to point out that it is now possible to generate C# Projects using the SDK-Style project XML format using CMake 3.23.
Although it is still a release candidate version, it works pretty well with CMake 3.23 rc2. To create C# SDK-style projects use DOTNET_SDK target property.
For example one could use C# SDK-style project with this line
set_target_properties(myTarget PROPERTIES DOTNET_SDK "Microsoft.NET.Sdk")
Also remember that you don't need a project() command when creating C# Projects with CMake. The project() command always creates a new solution which could be useful but not necessary. One can use enable_language(CSharp) to declare a Target being a C# Project.
To test C# SDK-style projects I used Visual Studio 2019 with Visual Studio 16 Generator on Windows.

Categories