Skip to content

FeiSiPub/solvespace-csharp

Repository files navigation

SolveSpaceSharp

icon

C# wrapper for the SolveSpace geometric constraint solver (slvs), targeting .NET 8+ and .NET Standard 2.0 on Windows, Linux, and macOS.

SolveSpace is a parametric 2D/3D geometric constraint solver. This package wraps its C API (slvs.h) for use in .NET applications.

Quick Start

using SolveSpace;

var sketch = new Sketch(1);
var wp = sketch.AddBase2D();

// Create two 2D points
var p1 = sketch.AddPoint2D(0, 0, wp);
var p2 = sketch.AddPoint2D(10, 10, wp);

// Constrain distance between them
sketch.Distance(p1, p2, 20, wp);

// Solve
var result = sketch.Solve();
Console.WriteLine($"Solved: {result.Succeeded}");
Console.WriteLine($"p1: ({p1.U:F4}, {p1.V:F4})");
Console.WriteLine($"p2: ({p2.U:F4}, {p2.V:F4})");
Console.WriteLine($"Distance: {Math.Sqrt(Math.Pow(p2.U - p1.U, 2) + Math.Pow(p2.V - p1.V, 2)):F4}");

Installation

Nuget

dotnet add package SolveSpace

The NuGet package bundles native libraries for win-x64, linux-x64, osx-x64, and osx-arm64.

Building from Source

Prerequisites

  • .NET 8 SDK
  • CMake 3.16+
  • C++ compiler (Visual Studio 2022, Xcode, GCC/Clang)

Build native library

# macOS (arm64)
bash native/build-native.sh osx-arm64

# macOS (x64)
bash native/build-native.sh osx-x64

# Linux
bash native/build-native.sh linux-x64

# Windows (PowerShell)
.\native\build-native.ps1 -RID win-x64

Build and test

dotnet build
dotnet test

API Overview

Sketch

The Sketch class wraps a solver group. Create entities and constraints through it:

Method Description
AddPoint2D(u, v, workplane) 2D point in a workplane
AddPoint3D(x, y, z) 3D point in free space
AddLine2D(ptA, ptB, workplane) 2D line segment
AddLine3D(ptA, ptB) 3D line segment
AddArc(normal, center, start, end, workplane) Arc of a circle
AddCircle(normal, center, radius, workplane) Complete circle
AddCubic(ptA, ptB, ptC, ptD, workplane) Cubic Bezier spline
AddWorkplane(origin, normal) Custom workplane
AddBase2D() Default XY workplane
AddNormal3D(qw, qx, qy, qz) 3D normal (quaternion)
AddDistance(value, workplane) Distance parameter
Solve() Solve the sketch

Constraint methods: Distance, Coincident, Horizontal, Vertical, Angle, Parallel, Perpendicular, Equal, Midpoint, Tangential, Diameter, Dragged, and more.

For constraint types without a dedicated method, use AddConstraint() with the raw constraint type constant.

License

GNU General Public License v3.0 or later. See the COPYING.txt file in the solvespace submodule.

About

c# wrapper of solvespace

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors