NumPy: Difference between revisions

From FreeCAD Documentation
Line 12: Line 12:


=== Convert App.Vector between FreeCAD Python and NumPy ===
=== Convert App.Vector between FreeCAD Python and NumPy ===
* ''[https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 source]''


* From Python to NumPy
* From Python to NumPy
: <source lang="Python">
: <source lang="Python">

Revision as of 11:18, 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 App.Vector between FreeCAD Python and NumPy


  • From Python to NumPy
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 numpy as np

CAD_list = []
for i in numpy_array:
    CAD_list.append(App.Vector(i))
 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