@@ -95,6 +95,20 @@ def get_object(self, name: str) -> Optional["PythonJcadObject"]:
95
95
data = self ._get_yobject_by_name (name ).to_py ()
96
96
return OBJECT_FACTORY .create_object (data , self )
97
97
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
+
98
112
def remove (self , name : str ) -> CadDocument :
99
113
index = self ._get_yobject_index_by_name (name )
100
114
if self ._objects_array and index != - 1 :
@@ -252,6 +266,7 @@ def add_box(
252
266
length : float = 1 ,
253
267
width : float = 1 ,
254
268
height : float = 1 ,
269
+ color : str = "#808080" ,
255
270
position : List [float ] = [0 , 0 , 0 ],
256
271
rotation_axis : List [float ] = [0 , 0 , 1 ],
257
272
rotation_angle : float = 0 ,
@@ -263,6 +278,7 @@ def add_box(
263
278
:param length: The length of the box.
264
279
:param width: The width of the box.
265
280
: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.
266
282
:param position: The shape 3D position.
267
283
:param rotation_axis: The 3D axis used for the rotation.
268
284
:param rotation_angle: The shape rotation angle, in degrees.
@@ -275,6 +291,7 @@ def add_box(
275
291
"Length" : length ,
276
292
"Width" : width ,
277
293
"Height" : height ,
294
+ "Color" : color ,
278
295
"Placement" : {
279
296
"Position" : position ,
280
297
"Axis" : rotation_axis ,
@@ -291,6 +308,7 @@ def add_cone(
291
308
radius2 : float = 0.5 ,
292
309
height : float = 1 ,
293
310
angle : float = 360 ,
311
+ color : str = "#808080" ,
294
312
position : List [float ] = [0 , 0 , 0 ],
295
313
rotation_axis : List [float ] = [0 , 0 , 1 ],
296
314
rotation_angle : float = 0 ,
@@ -303,6 +321,7 @@ def add_cone(
303
321
:param radius2: The top radius of the cone.
304
322
:param height: The height of the cone.
305
323
: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.
306
325
:param position: The shape 3D position.
307
326
:param rotation_axis: The 3D axis used for the rotation.
308
327
:param rotation_angle: The shape rotation angle, in degrees.
@@ -316,6 +335,7 @@ def add_cone(
316
335
"Radius2" : radius2 ,
317
336
"Height" : height ,
318
337
"Angle" : angle ,
338
+ "Color" : color ,
319
339
"Placement" : {
320
340
"Position" : position ,
321
341
"Axis" : rotation_axis ,
@@ -331,6 +351,7 @@ def add_cylinder(
331
351
radius : float = 1 ,
332
352
height : float = 1 ,
333
353
angle : float = 360 ,
354
+ color : str = "#808080" ,
334
355
position : List [float ] = [0 , 0 , 0 ],
335
356
rotation_axis : List [float ] = [0 , 0 , 1 ],
336
357
rotation_angle : float = 0 ,
@@ -342,6 +363,7 @@ def add_cylinder(
342
363
:param radius: The radius of the cylinder.
343
364
:param height: The height of the cylinder.
344
365
: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.
345
367
:param position: The shape 3D position.
346
368
:param rotation_axis: The 3D axis used for the rotation.
347
369
:param rotation_angle: The shape rotation angle, in degrees.
@@ -354,6 +376,7 @@ def add_cylinder(
354
376
"Radius" : radius ,
355
377
"Height" : height ,
356
378
"Angle" : angle ,
379
+ "Color" : color ,
357
380
"Placement" : {
358
381
"Position" : position ,
359
382
"Axis" : rotation_axis ,
@@ -370,6 +393,7 @@ def add_sphere(
370
393
angle1 : float = - 90 ,
371
394
angle2 : float = 90 ,
372
395
angle3 : float = 360 ,
396
+ color : str = "#808080" ,
373
397
position : List [float ] = [0 , 0 , 0 ],
374
398
rotation_axis : List [float ] = [0 , 0 , 1 ],
375
399
rotation_angle : float = 0 ,
@@ -382,6 +406,7 @@ def add_sphere(
382
406
:param angle1: The revolution angle of the sphere on the X axis (0: no sphere, 180: half sphere, 360: full sphere).
383
407
:param angle2: The revolution angle of the sphere on the Y axis (0: no sphere, 180: half sphere, 360: full sphere).
384
408
: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.
385
410
:param position: The shape 3D position.
386
411
:param rotation_axis: The 3D axis used for the rotation.
387
412
:param rotation_angle: The shape rotation angle, in degrees.
@@ -395,6 +420,7 @@ def add_sphere(
395
420
"Angle1" : angle1 ,
396
421
"Angle2" : angle2 ,
397
422
"Angle3" : angle3 ,
423
+ "Color" : color ,
398
424
"Placement" : {
399
425
"Position" : position ,
400
426
"Axis" : rotation_axis ,
@@ -412,6 +438,7 @@ def add_torus(
412
438
angle1 : float = - 180 ,
413
439
angle2 : float = 180 ,
414
440
angle3 : float = 360 ,
441
+ color : str = "#808080" ,
415
442
position : List [float ] = [0 , 0 , 0 ],
416
443
rotation_axis : List [float ] = [0 , 0 , 1 ],
417
444
rotation_angle : float = 0 ,
@@ -425,6 +452,7 @@ def add_torus(
425
452
:param angle1: The revolution angle of the torus on the X axis (0: no torus, 180: half torus, 360: full torus).
426
453
:param angle2: The revolution angle of the torus on the Y axis (0: no torus, 180: half torus, 360: full torus).
427
454
: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.
428
456
:param position: The shape 3D position.
429
457
:param rotation_axis: The 3D axis used for the rotation.
430
458
:param rotation_angle: The shape rotation angle, in degrees.
@@ -439,6 +467,7 @@ def add_torus(
439
467
"Angle1" : angle1 ,
440
468
"Angle2" : angle2 ,
441
469
"Angle3" : angle3 ,
470
+ "Color" : color ,
442
471
"Placement" : {
443
472
"Position" : position ,
444
473
"Axis" : rotation_axis ,
@@ -454,6 +483,7 @@ def cut(
454
483
base : str | int = None ,
455
484
tool : str | int = None ,
456
485
refine : bool = False ,
486
+ color : Optional [str ] = None ,
457
487
position : List [float ] = [0 , 0 , 0 ],
458
488
rotation_axis : List [float ] = [0 , 0 , 1 ],
459
489
rotation_angle : float = 0 ,
@@ -465,20 +495,26 @@ def cut(
465
495
: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.
466
496
: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.
467
497
: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.
468
499
:param position: The shape 3D position.
469
500
:param rotation_axis: The 3D axis used for the rotation.
470
501
:param rotation_angle: The shape rotation angle, in degrees.
471
502
:return: The document itself.
472
503
""" # noqa E501
473
504
base , tool = self ._get_boolean_operands (base , tool )
474
505
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
+
475
510
data = {
476
511
"shape" : Parts .Part__Cut .value ,
477
512
"name" : name if name else self ._new_name ("Cut" ),
478
513
"parameters" : {
479
514
"Base" : base ,
480
515
"Tool" : tool ,
481
516
"Refine" : refine ,
517
+ "Color" : color ,
482
518
"Placement" : {"Position" : [0 , 0 , 0 ], "Axis" : [0 , 0 , 1 ], "Angle" : 0 },
483
519
},
484
520
}
@@ -492,6 +528,7 @@ def fuse(
492
528
shape1 : str | int = None ,
493
529
shape2 : str | int = None ,
494
530
refine : bool = False ,
531
+ color : Optional [str ] = None ,
495
532
position : List [float ] = [0 , 0 , 0 ],
496
533
rotation_axis : List [float ] = [0 , 0 , 1 ],
497
534
rotation_angle : float = 0 ,
@@ -503,13 +540,18 @@ def fuse(
503
540
:param shape1: The first object used for the union. Can be the name of the object or its index in the objects list.
504
541
:param shape2: The first object used for the union. Can be the name of the object or its index in the objects list.
505
542
: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.
506
544
:param position: The shape 3D position.
507
545
:param rotation_axis: The 3D axis used for the rotation.
508
546
:param rotation_angle: The shape rotation angle, in degrees.
509
547
:return: The document itself.
510
548
""" # noqa E501
511
549
shape1 , shape2 = self ._get_boolean_operands (shape1 , shape2 )
512
550
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
+
513
555
data = {
514
556
"shape" : Parts .Part__MultiFuse .value ,
515
557
"name" : name if name else self ._new_name ("Fuse" ),
@@ -529,6 +571,7 @@ def intersect(
529
571
shape1 : str | int = None ,
530
572
shape2 : str | int = None ,
531
573
refine : bool = False ,
574
+ color : Optional [str ] = None ,
532
575
position : List [float ] = [0 , 0 , 0 ],
533
576
rotation_axis : List [float ] = [0 , 0 , 1 ],
534
577
rotation_angle : float = 0 ,
@@ -541,13 +584,18 @@ def intersect(
541
584
:param shape1: The first object used for the intersection. Can be the name of the object or its index in the objects list.
542
585
:param shape2: The first object used for the intersection. Can be the name of the object or its index in the objects list.
543
586
: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.
544
588
:param position: The shape 3D position.
545
589
:param rotation_axis: The 3D axis used for the rotation.
546
590
:param rotation_angle: The shape rotation angle, in degrees.
547
591
:return: The document itself.
548
592
""" # noqa E501
549
593
shape1 , shape2 = self ._get_boolean_operands (shape1 , shape2 )
550
594
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
+
551
599
data = {
552
600
"shape" : Parts .Part__MultiCommon .value ,
553
601
"name" : name if name else self ._new_name ("Intersection" ),
@@ -567,6 +615,7 @@ def chamfer(
567
615
shape : str | int = None ,
568
616
edge : int = 0 ,
569
617
dist : float = 0.1 ,
618
+ color : Optional [str ] = None ,
570
619
position : List [float ] = [0 , 0 , 0 ],
571
620
rotation_axis : List [float ] = [0 , 0 , 1 ],
572
621
rotation_angle : float = 0 ,
@@ -579,13 +628,18 @@ def chamfer(
579
628
:param shape: The input object used for the chamfer. Can be the name of the object or its index in the objects list.
580
629
:param edge: The edge index where to apply chamfer.
581
630
: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.
582
632
:param position: The shape 3D position.
583
633
:param rotation_axis: The 3D axis used for the rotation.
584
634
:param rotation_angle: The shape rotation angle, in degrees.
585
635
:return: The document itself.
586
636
""" # noqa E501
587
637
shape = self ._get_operand (shape )
588
638
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
+
589
643
data = {
590
644
"shape" : Parts .Part__Chamfer .value ,
591
645
"name" : name if name else self ._new_name ("Chamfer" ),
@@ -605,6 +659,7 @@ def fillet(
605
659
shape : str | int = None ,
606
660
edge : int = 0 ,
607
661
radius : float = 0.1 ,
662
+ color : Optional [str ] = None ,
608
663
position : List [float ] = [0 , 0 , 0 ],
609
664
rotation_axis : List [float ] = [0 , 0 , 1 ],
610
665
rotation_angle : float = 0 ,
@@ -617,13 +672,18 @@ def fillet(
617
672
:param shape: The input object used for the fillet. Can be the name of the object or its index in the objects list.
618
673
:param edge: The edge index where to apply fillet.
619
674
: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.
620
676
:param position: The shape 3D position.
621
677
:param rotation_axis: The 3D axis used for the rotation.
622
678
:param rotation_angle: The shape rotation angle, in degrees.
623
679
:return: The document itself.
624
680
""" # noqa E501
625
681
shape = self ._get_operand (shape )
626
682
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
+
627
687
data = {
628
688
"shape" : Parts .Part__Fillet .value ,
629
689
"name" : name if name else self ._new_name ("Fillet" ),
@@ -665,7 +725,16 @@ def set_visible(self, name: str, value):
665
725
if obj is None :
666
726
raise RuntimeError (f"No object named { name } " )
667
727
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
669
738
670
739
def check_exist (self , name : str ) -> bool :
671
740
if self .objects :
0 commit comments