1
1
# Define the show_object function which CadQuery execution environments need to provide
2
2
def show_object (cq_object , options = None ):
3
+ import cadquery as cq
3
4
import Part , FreeCAD , FreeCADGui
4
5
from PySide import QtGui
5
6
6
7
# Create the object that the BRep data will be read into
7
8
from io import BytesIO
8
9
brep_stream = BytesIO ()
9
10
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
+
10
42
# Get the title of the current document so that we can create/find the FreeCAD part window
11
43
doc_name = "untitled"
12
44
mw = FreeCADGui .getMainWindow ()
@@ -25,17 +57,15 @@ def show_object(cq_object, options=None):
25
57
ad = FreeCAD .activeDocument ()
26
58
27
59
# Convert the CadQuery object to a BRep string and then into a FreeCAD part shape
28
- rv = cq_object .val ().exportBrep (brep_stream )
29
60
brep_string = brep_stream .getvalue ().decode ('utf-8' )
30
61
part_shape = Part .Shape ()
31
62
part_shape .importBrepFromString (brep_string )
32
63
33
64
# options={"alpha":0.5, "color": (64, 164, 223)}
34
65
35
66
# 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
39
69
40
70
# Find the feature in the document, if it exists
41
71
cur_feature = None
0 commit comments