Skip to content

Commit e94e6ad

Browse files
arjxn-pypre-commit-ci[bot]trungleducgithub-actions[bot]
authored
Re-add setColor api (#552)
* Add color param to set color * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update python/jupytercad_lab/jupytercad_lab/notebook/cad_document.py Co-authored-by: Duc Trung Le <[email protected]> * fix set_visible * set_color working * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update Playwright Snapshots * modify notebook for color param * for `set_color` too * Update python/jupytercad_lab/jupytercad_lab/notebook/cad_document.py Co-authored-by: Duc Trung Le <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Duc Trung Le <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent cf2e817 commit e94e6ad

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

examples/Notebook.ipynb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@
4343
"metadata": {},
4444
"outputs": [],
4545
"source": [
46-
"doc.add_cone().add_sphere(radius=0.8).cut()"
46+
"doc.add_cone().add_sphere(radius=0.8).cut(color='#ff0000')"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"id": "ab3d7be6",
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"doc.set_color('Cut 1', '#00ff00')"
4757
]
4858
},
4959
{

python/jupytercad_lab/jupytercad_lab/notebook/cad_document.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ def get_object(self, name: str) -> Optional["PythonJcadObject"]:
9595
data = self._get_yobject_by_name(name).to_py()
9696
return OBJECT_FACTORY.create_object(data, self)
9797

98+
def _get_color(self, shape_id: str | int) -> str:
99+
"""
100+
Retrieve the color of a shape by its name or index.
101+
102+
:param shape_id: The name or index of the shape.
103+
:return: The color of the shape in hex format.
104+
"""
105+
shape = self.get_object(shape_id)
106+
if hasattr(shape, "parameters") and hasattr(shape.parameters, "Color"):
107+
color = shape.parameters.Color
108+
return color
109+
else:
110+
return "#808080"
111+
98112
def remove(self, name: str) -> CadDocument:
99113
index = self._get_yobject_index_by_name(name)
100114
if self._objects_array and index != -1:
@@ -252,6 +266,7 @@ def add_box(
252266
length: float = 1,
253267
width: float = 1,
254268
height: float = 1,
269+
color: str = "#808080",
255270
position: List[float] = [0, 0, 0],
256271
rotation_axis: List[float] = [0, 0, 1],
257272
rotation_angle: float = 0,
@@ -263,6 +278,7 @@ def add_box(
263278
:param length: The length of the box.
264279
:param width: The width of the box.
265280
:param height: The height of the box.
281+
:param color: The color of the box in hex format (e.g., "#FF5733") or RGB float list.
266282
:param position: The shape 3D position.
267283
:param rotation_axis: The 3D axis used for the rotation.
268284
:param rotation_angle: The shape rotation angle, in degrees.
@@ -275,6 +291,7 @@ def add_box(
275291
"Length": length,
276292
"Width": width,
277293
"Height": height,
294+
"Color": color,
278295
"Placement": {
279296
"Position": position,
280297
"Axis": rotation_axis,
@@ -291,6 +308,7 @@ def add_cone(
291308
radius2: float = 0.5,
292309
height: float = 1,
293310
angle: float = 360,
311+
color: str = "#808080",
294312
position: List[float] = [0, 0, 0],
295313
rotation_axis: List[float] = [0, 0, 1],
296314
rotation_angle: float = 0,
@@ -303,6 +321,7 @@ def add_cone(
303321
:param radius2: The top radius of the cone.
304322
:param height: The height of the cone.
305323
:param angle: The revolution angle of the cone (0: no cone, 180: half cone, 360: full cone).
324+
:param color: The color of the cone in hex format (e.g., "#FF5733") or RGB float list.
306325
:param position: The shape 3D position.
307326
:param rotation_axis: The 3D axis used for the rotation.
308327
:param rotation_angle: The shape rotation angle, in degrees.
@@ -316,6 +335,7 @@ def add_cone(
316335
"Radius2": radius2,
317336
"Height": height,
318337
"Angle": angle,
338+
"Color": color,
319339
"Placement": {
320340
"Position": position,
321341
"Axis": rotation_axis,
@@ -331,6 +351,7 @@ def add_cylinder(
331351
radius: float = 1,
332352
height: float = 1,
333353
angle: float = 360,
354+
color: str = "#808080",
334355
position: List[float] = [0, 0, 0],
335356
rotation_axis: List[float] = [0, 0, 1],
336357
rotation_angle: float = 0,
@@ -342,6 +363,7 @@ def add_cylinder(
342363
:param radius: The radius of the cylinder.
343364
:param height: The height of the cylinder.
344365
:param angle: The revolution angle of the cylinder (0: no cylinder, 180: half cylinder, 360: full cylinder).
366+
:param color: The color of the cylinder in hex format (e.g., "#FF5733") or RGB float list.
345367
:param position: The shape 3D position.
346368
:param rotation_axis: The 3D axis used for the rotation.
347369
:param rotation_angle: The shape rotation angle, in degrees.
@@ -354,6 +376,7 @@ def add_cylinder(
354376
"Radius": radius,
355377
"Height": height,
356378
"Angle": angle,
379+
"Color": color,
357380
"Placement": {
358381
"Position": position,
359382
"Axis": rotation_axis,
@@ -370,6 +393,7 @@ def add_sphere(
370393
angle1: float = -90,
371394
angle2: float = 90,
372395
angle3: float = 360,
396+
color: str = "#808080",
373397
position: List[float] = [0, 0, 0],
374398
rotation_axis: List[float] = [0, 0, 1],
375399
rotation_angle: float = 0,
@@ -382,6 +406,7 @@ def add_sphere(
382406
:param angle1: The revolution angle of the sphere on the X axis (0: no sphere, 180: half sphere, 360: full sphere).
383407
:param angle2: The revolution angle of the sphere on the Y axis (0: no sphere, 180: half sphere, 360: full sphere).
384408
:param angle3: The revolution angle of the sphere on the Z axis (0: no sphere, 180: half sphere, 360: full sphere).
409+
:param color: The color of the sphere in hex format (e.g., "#FF5733") or RGB float list.
385410
:param position: The shape 3D position.
386411
:param rotation_axis: The 3D axis used for the rotation.
387412
:param rotation_angle: The shape rotation angle, in degrees.
@@ -395,6 +420,7 @@ def add_sphere(
395420
"Angle1": angle1,
396421
"Angle2": angle2,
397422
"Angle3": angle3,
423+
"Color": color,
398424
"Placement": {
399425
"Position": position,
400426
"Axis": rotation_axis,
@@ -412,6 +438,7 @@ def add_torus(
412438
angle1: float = -180,
413439
angle2: float = 180,
414440
angle3: float = 360,
441+
color: str = "#808080",
415442
position: List[float] = [0, 0, 0],
416443
rotation_axis: List[float] = [0, 0, 1],
417444
rotation_angle: float = 0,
@@ -425,6 +452,7 @@ def add_torus(
425452
:param angle1: The revolution angle of the torus on the X axis (0: no torus, 180: half torus, 360: full torus).
426453
:param angle2: The revolution angle of the torus on the Y axis (0: no torus, 180: half torus, 360: full torus).
427454
:param angle3: The revolution angle of the torus on the Z axis (0: no torus, 180: half torus, 360: full torus).
455+
:param color: The color of the torus in hex format (e.g., "#FF5733") or RGB float list.
428456
:param position: The shape 3D position.
429457
:param rotation_axis: The 3D axis used for the rotation.
430458
:param rotation_angle: The shape rotation angle, in degrees.
@@ -439,6 +467,7 @@ def add_torus(
439467
"Angle1": angle1,
440468
"Angle2": angle2,
441469
"Angle3": angle3,
470+
"Color": color,
442471
"Placement": {
443472
"Position": position,
444473
"Axis": rotation_axis,
@@ -454,6 +483,7 @@ def cut(
454483
base: str | int = None,
455484
tool: str | int = None,
456485
refine: bool = False,
486+
color: Optional[str] = None,
457487
position: List[float] = [0, 0, 0],
458488
rotation_axis: List[float] = [0, 0, 1],
459489
rotation_angle: float = 0,
@@ -465,20 +495,26 @@ def cut(
465495
:param base: The base object that will be used for the cut. Can be the name of the object or its index in the objects list.
466496
:param tool: The tool object that will be used for the cut. Can be the name of the object or its index in the objects list.
467497
:param refine: Whether or not to refine the mesh during the cut computation.
498+
:param color: The color in hex format (e.g., "#FF5733") or RGB float list. Defaults to the base object's color if None.
468499
:param position: The shape 3D position.
469500
:param rotation_axis: The 3D axis used for the rotation.
470501
:param rotation_angle: The shape rotation angle, in degrees.
471502
:return: The document itself.
472503
""" # noqa E501
473504
base, tool = self._get_boolean_operands(base, tool)
474505

506+
# Use specified color or fall back to the base object's color
507+
if color is None:
508+
color = self._get_color(base)
509+
475510
data = {
476511
"shape": Parts.Part__Cut.value,
477512
"name": name if name else self._new_name("Cut"),
478513
"parameters": {
479514
"Base": base,
480515
"Tool": tool,
481516
"Refine": refine,
517+
"Color": color,
482518
"Placement": {"Position": [0, 0, 0], "Axis": [0, 0, 1], "Angle": 0},
483519
},
484520
}
@@ -492,6 +528,7 @@ def fuse(
492528
shape1: str | int = None,
493529
shape2: str | int = None,
494530
refine: bool = False,
531+
color: Optional[str] = None,
495532
position: List[float] = [0, 0, 0],
496533
rotation_axis: List[float] = [0, 0, 1],
497534
rotation_angle: float = 0,
@@ -503,13 +540,18 @@ def fuse(
503540
:param shape1: The first object used for the union. Can be the name of the object or its index in the objects list.
504541
:param shape2: The first object used for the union. Can be the name of the object or its index in the objects list.
505542
:param refine: Whether or not to refine the mesh during the union computation.
543+
:param color: The color in hex format (e.g., "#FF5733") or RGB float list. Defaults to the base object's color if None.
506544
:param position: The shape 3D position.
507545
:param rotation_axis: The 3D axis used for the rotation.
508546
:param rotation_angle: The shape rotation angle, in degrees.
509547
:return: The document itself.
510548
""" # noqa E501
511549
shape1, shape2 = self._get_boolean_operands(shape1, shape2)
512550

551+
# Use specified color or fall back to the base object's color
552+
if color is None:
553+
color = self._get_color(shape1)
554+
513555
data = {
514556
"shape": Parts.Part__MultiFuse.value,
515557
"name": name if name else self._new_name("Fuse"),
@@ -529,6 +571,7 @@ def intersect(
529571
shape1: str | int = None,
530572
shape2: str | int = None,
531573
refine: bool = False,
574+
color: Optional[str] = None,
532575
position: List[float] = [0, 0, 0],
533576
rotation_axis: List[float] = [0, 0, 1],
534577
rotation_angle: float = 0,
@@ -541,13 +584,18 @@ def intersect(
541584
:param shape1: The first object used for the intersection. Can be the name of the object or its index in the objects list.
542585
:param shape2: The first object used for the intersection. Can be the name of the object or its index in the objects list.
543586
:param refine: Whether or not to refine the mesh during the intersection computation.
587+
:param color: The color in hex format (e.g., "#FF5733") or RGB float list. Defaults to the base object's color if None.
544588
:param position: The shape 3D position.
545589
:param rotation_axis: The 3D axis used for the rotation.
546590
:param rotation_angle: The shape rotation angle, in degrees.
547591
:return: The document itself.
548592
""" # noqa E501
549593
shape1, shape2 = self._get_boolean_operands(shape1, shape2)
550594

595+
# Use specified color or fall back to the base object's color
596+
if color is None:
597+
color = self._get_color(shape1)
598+
551599
data = {
552600
"shape": Parts.Part__MultiCommon.value,
553601
"name": name if name else self._new_name("Intersection"),
@@ -567,6 +615,7 @@ def chamfer(
567615
shape: str | int = None,
568616
edge: int = 0,
569617
dist: float = 0.1,
618+
color: Optional[str] = None,
570619
position: List[float] = [0, 0, 0],
571620
rotation_axis: List[float] = [0, 0, 1],
572621
rotation_angle: float = 0,
@@ -579,13 +628,18 @@ def chamfer(
579628
:param shape: The input object used for the chamfer. Can be the name of the object or its index in the objects list.
580629
:param edge: The edge index where to apply chamfer.
581630
:param dist: The distance of the chamfer.
631+
:param color: The color in hex format (e.g., "#FF5733") or RGB float list. Defaults to the base object's color if None.
582632
:param position: The shape 3D position.
583633
:param rotation_axis: The 3D axis used for the rotation.
584634
:param rotation_angle: The shape rotation angle, in degrees.
585635
:return: The document itself.
586636
""" # noqa E501
587637
shape = self._get_operand(shape)
588638

639+
# Use specified color or fall back to the base object's color
640+
if color is None:
641+
color = self._get_color(shape)
642+
589643
data = {
590644
"shape": Parts.Part__Chamfer.value,
591645
"name": name if name else self._new_name("Chamfer"),
@@ -605,6 +659,7 @@ def fillet(
605659
shape: str | int = None,
606660
edge: int = 0,
607661
radius: float = 0.1,
662+
color: Optional[str] = None,
608663
position: List[float] = [0, 0, 0],
609664
rotation_axis: List[float] = [0, 0, 1],
610665
rotation_angle: float = 0,
@@ -617,13 +672,18 @@ def fillet(
617672
:param shape: The input object used for the fillet. Can be the name of the object or its index in the objects list.
618673
:param edge: The edge index where to apply fillet.
619674
:param radius: The radius of the fillet.
675+
:param color: The color in hex format (e.g., "#FF5733") or RGB float list. Defaults to the base object's color if None.
620676
:param position: The shape 3D position.
621677
:param rotation_axis: The 3D axis used for the rotation.
622678
:param rotation_angle: The shape rotation angle, in degrees.
623679
:return: The document itself.
624680
""" # noqa E501
625681
shape = self._get_operand(shape)
626682

683+
# Use specified color or fall back to the base object's color
684+
if color is None:
685+
color = self._get_color(shape)
686+
627687
data = {
628688
"shape": Parts.Part__Fillet.value,
629689
"name": name if name else self._new_name("Fillet"),
@@ -665,7 +725,16 @@ def set_visible(self, name: str, value):
665725
if obj is None:
666726
raise RuntimeError(f"No object named {name}")
667727

668-
obj["visible"] = False
728+
obj["visible"] = value
729+
730+
def set_color(self, name: str, value: str):
731+
obj: Optional[Map] = self._get_yobject_by_name(name)
732+
733+
if obj is None:
734+
raise RuntimeError(f"No object named {name}")
735+
parameters = obj.get("parameters", {})
736+
parameters["Color"] = value
737+
obj["parameters"] = parameters
669738

670739
def check_exist(self, name: str) -> bool:
671740
if self.objects:
4.91 KB
Loading
902 Bytes
Loading
15 Bytes
Loading

0 commit comments

Comments
 (0)