NumPy: Difference between revisions

From FreeCAD Documentation
(Removed link to forum topic: it not contain any additional info.)
Line 11: Line 11:


== Convert Vector list between FreeCAD Python and NumPy ==
== Convert Vector list between FreeCAD Python and NumPy ==

[https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 Forum topic].


=== From Python to NumPy ===
=== From Python to NumPy ===

Revision as of 11:49, 9 September 2022

Other languages:

Introduction

This page aims to document how to use NumPy with FreeCAD.

numpy comes installed as a dependency of FreeCAD, therefore you can import numpy as np without having to first install it like in normal Python project environments.

Convert Vector list between FreeCAD Python and NumPy

From Python to NumPy

import FreeCAD as App
import numpy as np

vector_list = [App.Vector(1, 0, 0), App.Vector(1, 2, 3), App.Vector(0, 3, 0)]
numpy_array = np.asarray(vector_list)
print(numpy_array)

Output is:

[[ 1.  0.  0.]
 [ 1.  2.  3.]
 [ 0.  3.  0.]]

From NumPy to Python

import FreeCAD as App
import numpy as np

cad_list = [App.Vector(itm) for itm in numpy_array]
print(cad_list)

Output is:

[Vector (1.0, 0.0, 0.0), Vector (1.0, 2.0, 3.0), Vector (0.0, 3.0, 0.0)]

FreeCAD Projects Using NumPy

FreeCAD Forum Discussion