NumPy: Difference between revisions
(Wiki code.) |
|||
Line 10: | Line 10: | ||
<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. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</translate> |
|||
⚫ | |||
{{Code|code= |
|||
: <source lang="Python"> |
|||
import FreeCAD as App |
|||
import numpy as np |
import numpy as np |
||
vector_list = [App.Vector(1,0,0), App.Vector(1,2,3), App.Vector(0,3,0)] |
vector_list = [App.Vector(1, 0, 0), App.Vector(1, 2, 3), App.Vector(0, 3, 0)] |
||
numpy_array = np.asarray(vector_list) |
numpy_array = np.asarray(vector_list) |
||
print(numpy_array) |
print(numpy_array) |
||
}} |
|||
</source> |
|||
<translate> |
|||
⚫ | |||
: <source> |
|||
⚫ | |||
</translate> |
|||
{{Code|lang=text|code= |
|||
[[ 1. 0. 0.] |
[[ 1. 0. 0.] |
||
[ 1. 2. 3.] |
[ 1. 2. 3.] |
||
[ 0. 3. 0.]] |
[ 0. 3. 0.]] |
||
}} |
|||
</source> |
|||
<translate> |
|||
=== From NumPy to Python === |
|||
: <source lang="Python"> |
|||
</translate> |
|||
{{Code|code= |
|||
import FreeCAD as App |
|||
import numpy as np |
import numpy as np |
||
⚫ | |||
CAD_list = [] |
|||
⚫ | |||
⚫ | |||
}} |
|||
CAD_list.append(App.Vector(i)) |
|||
<translate> |
|||
⚫ | |||
</source> |
|||
⚫ | |||
: <source> |
|||
⚫ | |||
</source> |
|||
⚫ | |||
[[#top|top]] |
|||
</translate> |
|||
{{Code|lang=text|code= |
|||
⚫ | |||
}} |
|||
<translate> |
|||
==FreeCAD Projects Using NumPy== <!--T:4--> |
==FreeCAD Projects Using NumPy== <!--T:4--> |
||
</translate> |
</translate> |
||
* https://github.com/looooo/freecad.gears/blob/master/setup.py#L13 |
* https://github.com/looooo/freecad.gears/blob/master/setup.py#L13 |
||
* https://github.com/booya-at/OpenGlider/blob/develop/setup.py#L77 |
* https://github.com/booya-at/OpenGlider/blob/develop/setup.py#L77 |
||
<translate> |
<translate> |
||
==FreeCAD Forum Discussion== <!--T:5--> |
==FreeCAD Forum Discussion== <!--T:5--> |
||
<!--T:6--> |
<!--T:6--> |
||
* [https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 Any numpy experts?] |
* [https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 Any numpy experts?] |
||
</translate> |
</translate> |
Revision as of 11:44, 9 September 2022
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(i) for i 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
- https://github.com/looooo/freecad.gears/blob/master/setup.py#L13
- https://github.com/booya-at/OpenGlider/blob/develop/setup.py#L77