NumPy: Difference between revisions

From FreeCAD Documentation
(Marked this version for translation)
(adding example from thread)
Line 9: Line 9:
<!--T:3-->
<!--T:3-->
<code>numpy</code> comes installed as a dependency of FreeCAD, therefore you can <code>import numpy as np</code> without having to first install it like in normal Python project environments.
<code>numpy</code> comes installed as a dependency of FreeCAD, therefore you can <code>import numpy as np</code> without having to first install it like in normal Python project environments.


=== Convert AppVector between FreeCAD Python and NumPy ===
* From Python to NumPy
: <source lang="Python">
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)
</source>
:Output is
: <source>
[[ 1. 0. 0.]
[ 1. 2. 3.]
[ 0. 3. 0.]]
</source>

*From NumPy to Python
: <source lang="Python">
import numpy as np

CAD_list = []
for i in numpy_array:
CAD_list.append(App.Vector(i))
print(CAD_list)
</source>
:Output is
: <source>
[Vector (1.0, 0.0, 0.0), Vector (1.0, 2.0, 3.0), Vector (0.0, 3.0, 0.0)]
</source>


==FreeCAD Projects Using NumPy== <!--T:4-->
==FreeCAD Projects Using NumPy== <!--T:4-->

Revision as of 11:16, 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 AppVector 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