Skip to content

Commit 5c11f3f

Browse files
committed
Handle Build123d objects in show_object
1 parent a4c4307 commit 5c11f3f

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

CQGui/display.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
# Define the show_object function which CadQuery execution environments need to provide
22
def show_object(cq_object, options=None):
3+
import cadquery as cq
34
import Part, FreeCAD, FreeCADGui
45
from PySide import QtGui
56

67
# Create the object that the BRep data will be read into
78
from io import BytesIO
89
brep_stream = BytesIO()
910

11+
# Keep track of the feature name
12+
feature_name = None
13+
# Check to see what type of object we are dealing with
14+
if isinstance(cq_object, cq.Workplane):
15+
# Handle the label if the user set it
16+
if cq_object.val().label:
17+
feature_name = cq_object.val().label
18+
19+
# If we have a workplane, we need to convert it to a solid
20+
cq_object.val().exportBrep(brep_stream)
21+
elif isinstance(cq_object, cq.Shape):
22+
# If we have a solid, we can export it directly
23+
cq_object.exportBrep(brep_stream)
24+
elif hasattr(cq_object, "wrapped"):
25+
# Handle the label if the user has it set
26+
if hasattr(cq_object, "label"):
27+
feature_name = cq_object.label
28+
29+
from build123d import export_brep
30+
export_brep(cq_object, brep_stream)
31+
elif hasattr(cq_object, "_obj"):
32+
# Handle the label if the user has it set
33+
if hasattr(cq_object, "label"):
34+
feature_name = cq_object.label
35+
36+
from build123d import export_brep
37+
export_brep(cq_object._obj, brep_stream)
38+
else:
39+
print("Object type not suuport for display: ", type(cq_object).__name__)
40+
return
41+
1042
# Get the title of the current document so that we can create/find the FreeCAD part window
1143
doc_name = "untitled"
1244
mw = FreeCADGui.getMainWindow()
@@ -25,17 +57,15 @@ def show_object(cq_object, options=None):
2557
ad = FreeCAD.activeDocument()
2658

2759
# Convert the CadQuery object to a BRep string and then into a FreeCAD part shape
28-
rv = cq_object.val().exportBrep(brep_stream)
2960
brep_string = brep_stream.getvalue().decode('utf-8')
3061
part_shape = Part.Shape()
3162
part_shape.importBrepFromString(brep_string)
3263

3364
# options={"alpha":0.5, "color": (64, 164, 223)}
3465

3566
# If the user wanted to use a specific name in the tree, use it
36-
feature_name = doc_name
37-
if cq_object.val().label:
38-
feature_name = cq_object.val().label
67+
if not feature_name:
68+
feature_name = doc_name
3969

4070
# Find the feature in the document, if it exists
4171
cur_feature = None

0 commit comments

Comments
 (0)