nicescad API Documentation
axes_helper
AxesHelper
A Python class for creating a 3D helper for visualizing the axes in a scene.
This class is designed to be used with the nicegui
library (https://pypi.org/project/nicegui/)
for creating 3D scenes. It creates line objects for the x, y, and z axes,
each with a distinct color, and provides methods for changing these colors and toggling their visibility.
The original JavaScript code, which this Python code is based on, can be found at: https://raw.githubusercontent.com/mrdoob/three.js/master/src/helpers/AxesHelper.js The JavaScript code was refactored into this Python version by OpenAI's ChatGPT.
For the refactoring, following prompts were given:
1. Refactor a JavaScript class for handling ThreeJS scenes into a Python class using the nicegui
library.
2. The class should allow to hide/show axes.
3. Usage of nicegui
library's API for the colors.
4. Include Google docstrings and type hints to the code.
5. The scene should be a constructor parameter to be remembered.
6. The Axes should be named x, y and z. The name attribute should be assigned after the line object creation.
7. The set_colors
method should be called in the constructor.
8. Use with self.scene as scene
when drawing the lines.
9. The class should provide a method to toggle the visibility of axes.
10. The default size of the axes to be drawn should be 10.0.
11. Color information should be persisted when toggling the axes.
12. Use a dictionary to store colors by axes names.
According to nicegui's documentation (https://nicegui.io/documentation/scene), the 'scene.line()' function is used to create lines, here is an example: scene.line([-4, 0, 0], [-4, 2, 0]).material('#ff0000') To remove elements from the scene, one uses 'scene.remove()' function: scene.remove(element) where element is either the element instance or its ID.
Date: 2023-07-24 @author: Refactored into Python by OpenAI's ChatGPT
Attributes:
Name | Type | Description |
---|---|---|
size |
float
|
The size of the axes to be drawn. |
scene |
scene
|
The scene where the axes will be drawn. |
Usage
scene = ui.scene().classes('w-full h-64') axes_helper = AxesHelper(scene, size=10.0) axes_helper.set_colors(x_axis_color='#FF0000', y_axis_color='#00FF00', z_axis_color='#0000FF') # set colors for x, y, and z axes axes_helper.toggle_axes() # toggle visibility of the axes
Source code in nicescad/axes_helper.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
__init__(scene, size=10.0)
The constructor for AxesHelper class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
scene
|
The scene where the axes will be drawn. |
required |
size |
float
|
The size of the axes to be drawn. |
10.0
|
Source code in nicescad/axes_helper.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
set_colors(x_axis_color='#FF0000', y_axis_color='#00FF00', z_axis_color='#0000FF')
A method to set colors of the axes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_axis_color |
str
|
Color of the x-axis. |
'#FF0000'
|
y_axis_color |
str
|
Color of the y-axis. |
'#00FF00'
|
z_axis_color |
str
|
Color of the z-axis. |
'#0000FF'
|
Source code in nicescad/axes_helper.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
toggle_axes()
A method to toggle the visibility of the axes.
Source code in nicescad/axes_helper.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
blockscad_converter
A class to convert BlockSCAD XML files to SCAD files using the OpenAI language model API.
BlockSCAD Editor: https://www.blockscad3d.com/editor/ OpenSCAD: https://openscad.org/ NiceSCAD: http://nicescad.bitplan.com/ Relevant Issue: http://nicescad.bitplan.com/issue/23
Author: OpenAI ChatGPT Date: July 25, 2023
The code was created by the OpenAI ChatGPT model in response to the following prompts:
- "Create a class named 'BlockscadConverter' that converts BlockSCAD XML files to SCAD files."
- "The xml_path is given in the constructor."
- "The class should have a method named convert_to_scad
that outputs a path to the SCAD file."
- "Use the write method of a file object to output the SCAD file."
- "Include Google docstrings and type hints to the code."
- "Include comments that explain which prompts created the code. This helps other LLMs understand how to reproduce the same results."
- "Add links to the BlockSCAD editor (https://www.blockscad3d.com/editor/), OpenSCAD (https://openscad.org/), and the target platform (NiceSCAD, http://nicescad.bitplan.com/)."
- "Include the link to the relevant issue: 'support reading and converting blockscad files #23' on http://nicescad.bitplan.com/issue/23"
- "Add yourself (OpenAI ChatGPT) as the author and include the date"
- "keep the prompt list in the comments to be able to reproduce the results."
- "If the OpenAI API key is not available in the environment variables, look for it in a JSON file at ~/.openai/openai_api_key.json
."
- "If the OpenAI API key is not found, throw an exception."
BlockscadConverter
Attributes
xml_path : str path to the input BlockSCAD XML file
Methods
convert_to_scad(scad_path: str) -> Union[str, None] Converts the BlockSCAD XML file to a SCAD file and returns the SCAD file path.
Source code in nicescad/blockscad_converter.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
__init__(xml_path)
Parameters
xml_path : str path to the input BlockSCAD XML file
Source code in nicescad/blockscad_converter.py
48 49 50 51 52 53 54 55 |
|
convert_to_scad(scad_path)
Converts the BlockSCAD XML file to a SCAD file using the OpenAI language model API.
Parameters
scad_path : str path to the output SCAD file
Returns
Union[str, None] path to the output SCAD file if conversion is successful, None otherwise
Source code in nicescad/blockscad_converter.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
nicescad_cmd
Created on 2023-07-19
@author: wf
NiceScadCmd
Bases: WebserverCmd
command line handling for nicescad
Source code in nicescad/nicescad_cmd.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
__init__()
constructor
Source code in nicescad/nicescad_cmd.py
20 21 22 23 24 25 26 |
|
getArgParser(description, version_msg)
override the default argparser call
Source code in nicescad/nicescad_cmd.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
main(argv=None)
main call
Source code in nicescad/nicescad_cmd.py
48 49 50 51 52 53 54 |
|
openscad
Created on 2023-07-19
@author: wf
This module contains the class OpenScad, a wrapper for OpenScad.
OpenSCADLexer
Bases: RegexLexer
Lexer for OpenSCAD, a language for creating solid 3D CAD models.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the lexer. |
aliases |
list of str
|
A list of strings that can be used as aliases for the lexer. |
filenames |
list of str
|
A list of strings that define filename patterns that match this lexer. |
Source code in nicescad/openscad.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
OpenScad
A wrapper for OpenScad (https://openscad.org/).
Source code in nicescad/openscad.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
__init__(scad_prepend='', **kw)
Initializes the OpenScad object.
Source code in nicescad/openscad.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
cleanup_tmp_file(result, scad_tmp_file)
Cleanup temporary files after subprocess execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result |
Subprocess
|
The result of the subprocess execution. |
required |
scad_tmp_file |
str
|
The path to the temporary file. |
required |
Source code in nicescad/openscad.py
191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
highlight_code(code)
Highlights the provided OpenSCAD code and returns the highlighted code in HTML format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code |
str
|
The OpenSCAD code to highlight. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The input OpenSCAD code, highlighted and formatted as an HTML string. |
Source code in nicescad/openscad.py
93 94 95 96 97 98 99 100 101 102 103 104 |
|
openscad_str_to_file(openscad_str, stl_path)
async
Renders the OpenSCAD code to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
openscad_str |
str
|
The OpenSCAD code. |
required |
stl_path(str) |
the path to the stl file |
required |
Returns:
Name | Type | Description |
---|---|---|
Subprocess |
Subprocess
|
The result of the subprocess run, encapsulated in a Subprocess object. |
Source code in nicescad/openscad.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
render_to_file_async(openscad_str, stl_path)
async
Asynchronously renders an OpenSCAD string to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
openscad_str |
str
|
The OpenSCAD code. |
required |
stl_path(str) |
The path to the output file. |
required |
Returns:
Name | Type | Description |
---|---|---|
Subprocess |
Awaitable[Subprocess]
|
the openscad execution result |
Source code in nicescad/openscad.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
write_to_tmp_file(openscad_str, do_prepend=True)
Writes an OpenSCAD string to a temporary file.
The scad_prepend
string is prepended to the OpenSCAD code before writing,
unless the OpenSCAD code contains the string '//!OpenSCAD', or do_prepend
is set to False
. In these cases, only the OpenSCAD code is written to the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
openscad_str |
str
|
The OpenSCAD code. |
required |
do_prepend |
bool
|
If |
True
|
Returns:
Name | Type | Description |
---|---|---|
str |
The path to the temporary file where the OpenSCAD code (and
possibly the |
Source code in nicescad/openscad.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
process
Subprocess
dataclass
A class representing a subprocess execution result.
Source code in nicescad/process.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
run(cmd)
staticmethod
Runs a command as a subprocess and returns the result as an instance of this class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmd |
List[str]
|
The command to run. |
required |
Returns:
Name | Type | Description |
---|---|---|
Subprocess |
Subprocess
|
An instance of this class representing the result of the subprocess execution. |
Source code in nicescad/process.py
47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
run_async(cmd)
async
staticmethod
Asynchronously runs a command as a subprocess and returns the result as an instance of this class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmd |
List[str]
|
The command to run. |
required |
Returns:
Name | Type | Description |
---|---|---|
Subprocess |
Awaitable[Subprocess]
|
An instance of this class representing the result of the subprocess execution. |
Source code in nicescad/process.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
solidservice
Created on 2023-07-30
@author: wf
FastAPIServer
Class for FastAPI server.
Source code in nicescad/solidservice.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
convert(item)
async
Endpoint to convert Python code to OpenSCAD code.
Arguments: item: Item -- input Python code
Returns: dict -- the OpenSCAD code
Source code in nicescad/solidservice.py
84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
home()
async
Endpoint to return the homepage with links.
Returns: str -- HTML content
Source code in nicescad/solidservice.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
version()
async
Endpoint to return the version of the nicescad package.
Returns: dict -- the version
Source code in nicescad/solidservice.py
75 76 77 78 79 80 81 82 |
|
SolidConverter
Class for conversion of Python code to OpenSCAD code.
Source code in nicescad/solidservice.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
convert_to_openscad()
Function to convert the input Python code into OpenSCAD code using SolidPython
Returns: str -- OpenSCAD code
Source code in nicescad/solidservice.py
26 27 28 29 30 31 32 33 34 35 |
|
version
Created on 2023-06-19
@author: wf
Version
dataclass
Bases: object
Version handling for nicescad
Source code in nicescad/version.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
webserver
Created on 2023-06-19
@author: wf
NiceScadSolution
Bases: InputWebSolution
the NiceScad solution
Source code in nicescad/webserver.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
__init__(webserver, client)
Initialize the solution
Calls the constructor of the base solution Args: webserver (NiceScadWebServer): The webserver instance associated with this context. client (Client): The client instance this context is associated with.
Source code in nicescad/webserver.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
code_changed(cargs)
react on changed code
Source code in nicescad/webserver.py
187 188 189 190 191 192 |
|
configure_settings()
Generates the settings page with a link to the project's GitHub page.
Source code in nicescad/webserver.py
360 361 362 363 364 365 |
|
highlight_code(_cargs)
async
highlight the code and show the html
Source code in nicescad/webserver.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
home()
async
Generates the home page with a 3D viewer and a code editor.
Source code in nicescad/webserver.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
open_file()
async
Opens a Local filer picker dialog and reads the selected input file.
Source code in nicescad/webserver.py
167 168 169 170 171 172 173 |
|
pick_color(e)
async
Asynchronously picks a color based on provided event arguments.
This function changes the color of the 'color_picker_button' and the 'stl_object' according to the color specified in the event arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
e |
ColorPickEventArguments
|
An object containing event-specific arguments. The 'color' attribute of this object specifies the color to be applied. |
required |
Note
If 'stl_object' is None, the function will only change the color of 'color_picker_button'. Otherwise, it changes the color of both 'color_picker_button' and 'stl_object'.
Source code in nicescad/webserver.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
prepare_ui()
handle the command line arguments
Source code in nicescad/webserver.py
265 266 267 268 269 270 |
|
read_input(input_str)
Reads the given input and handles any exceptions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str |
str
|
The input string representing a URL or local file. |
required |
Source code in nicescad/webserver.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
render(_click_args=None)
async
Renders the OpenScad string and updates the 3D scene with the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
click_args |
object
|
The click event arguments. |
required |
Source code in nicescad/webserver.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
save_file()
Saves the current code to the last input file, if it was a local path.
Source code in nicescad/webserver.py
158 159 160 161 162 163 164 165 |
|
setup_pygments()
prepare pygments syntax highlighting by loading style
Source code in nicescad/webserver.py
177 178 179 180 181 182 183 184 185 |
|
toggle_axes()
async
toggle the axes of my scene
Source code in nicescad/webserver.py
232 233 234 235 236 237 238 239 240 241 |
|
toggle_grid(_ea)
async
toogle the grid of my scene
Source code in nicescad/webserver.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
NiceScadWebServer
Bases: InputWebserver
WebServer class that manages the server and handles OpenScad operations.
Attributes:
Name | Type | Description |
---|---|---|
oscad |
OpenScad
|
An OpenScad object that aids in performing OpenScad operations. |
Source code in nicescad/webserver.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
__init__()
Constructs all the necessary attributes for the WebServer object.
Source code in nicescad/webserver.py
42 43 44 45 46 47 48 49 50 51 |
|