|
| 1 | +#! python3 |
| 2 | +# venv: timber_connections |
| 3 | + |
| 4 | +import Rhino |
| 5 | +import Rhino.Input |
| 6 | +import Rhino.DocObjects |
| 7 | +from typing import * |
| 8 | +from wood_rui import wood_rui_globals, generalized_input_method, Element, add_sub_layer |
| 9 | +import System |
| 10 | + |
| 11 | +def my_callback(input_dict, dataset_name): |
| 12 | + |
| 13 | + if not input_dict["elements"]: |
| 14 | + return |
| 15 | + |
| 16 | + elements : list[Element] = input_dict["elements"] |
| 17 | + |
| 18 | + for idx, element in enumerate(elements): |
| 19 | + element.boolean_difference() |
| 20 | + # add_sub_layer(element.geometry_plane[0], "plane", plane_axes, [System.Drawing.Color.Red, System.Drawing.Color.Green, System.Drawing.Color.Blue], idx==0) |
| 21 | + |
| 22 | + |
| 23 | +if __name__ == "__main__": |
| 24 | + |
| 25 | + dataset_name = "beam" |
| 26 | + wood_rui_globals.init_data(dataset_name) |
| 27 | + |
| 28 | + input_dict = { |
| 29 | + "elements" : ([], List[Element]), |
| 30 | + } |
| 31 | + |
| 32 | + # Call the generalized input method with the dataset name and input dictionary |
| 33 | + generalized_input_method(dataset_name, input_dict, my_callback, False, True) |
| 34 | + |
| 35 | +# import Rhino |
| 36 | +# import System |
| 37 | + |
| 38 | +# from wood_rui import ( |
| 39 | +# select_and_find_valid_groups, polyline_obj_to_plane |
| 40 | +# ) |
| 41 | + |
| 42 | + |
| 43 | +# def prepare_for_boolean_difference(geometry_planes): |
| 44 | +# shapes = [] |
| 45 | +# cuts = [] |
| 46 | + |
| 47 | +# for geometry_plane in geometry_planes: |
| 48 | +# obj = geometry_plane[0] |
| 49 | +# geometry = obj.Geometry |
| 50 | +# string_dictionary = obj.Attributes.GetUserStrings() |
| 51 | + |
| 52 | +# T = Rhino.Geometry.Transform.PlaneToPlane(Rhino.Geometry.Plane.WorldXY, polyline_obj_to_plane(geometry_plane[1])) |
| 53 | +# shapes.append(geometry) |
| 54 | + |
| 55 | +# # Determine type-specific processing |
| 56 | +# obj_type = type(geometry) |
| 57 | +# valid_types = { |
| 58 | +# Rhino.Geometry.Brep: Rhino.DocObjects.ObjectType.Brep, |
| 59 | +# Rhino.Geometry.Mesh: Rhino.DocObjects.ObjectType.Mesh |
| 60 | +# } |
| 61 | + |
| 62 | +# if obj_type in valid_types: |
| 63 | +# cut_list = [] |
| 64 | +# expected_type = valid_types[obj_type] |
| 65 | + |
| 66 | +# for key in string_dictionary: |
| 67 | +# value = string_dictionary[key] |
| 68 | +# if "feature" in key: |
| 69 | +# feature_geometry = Rhino.Geometry.GeometryBase.FromJSON(value) |
| 70 | +# feature_geometry.Transform(T) |
| 71 | +# if feature_geometry.ObjectType == expected_type: |
| 72 | +# cut_list.append(feature_geometry) |
| 73 | + |
| 74 | +# cuts.append(cut_list) |
| 75 | + |
| 76 | +# # Redraw the document to reflect changes |
| 77 | +# Rhino.RhinoDoc.ActiveDoc.Views.Redraw() |
| 78 | + |
| 79 | +# return shapes, cuts |
| 80 | + |
| 81 | +# def clean_brep(brep : Rhino.Geometry.Brep): |
| 82 | +# copy = brep.DuplicateBrep() |
| 83 | +# copy.Faces.SplitKinkyFaces() |
| 84 | +# if Rhino.Geometry.BrepSolidOrientation.Inward == copy.SolidOrientation: |
| 85 | +# copy.Flip() |
| 86 | +# return copy |
| 87 | + |
| 88 | +# def clean_mesh(mesh : Rhino.Geometry.Mesh): |
| 89 | +# copy = mesh.DuplicateMesh() |
| 90 | +# copy.Compact(); |
| 91 | +# copy.Vertices.CombineIdentical(True, True); |
| 92 | +# copy.Vertices.CullUnused(); |
| 93 | +# copy.UnifyNormals(); |
| 94 | +# copy.Weld(3.14159265358979); |
| 95 | +# copy.FaceNormals.ComputeFaceNormals(); |
| 96 | +# copy.Normals.ComputeNormals(); |
| 97 | +# if (copy.SolidOrientation() == -1): |
| 98 | +# copy.Flip(True, True, True); |
| 99 | +# return copy |
| 100 | + |
| 101 | +# def brep_boolean_difference(shape : Rhino.Geometry.Brep, cutters : list[Rhino.Geometry.Brep]): |
| 102 | + |
| 103 | +# for i in range(len(shapes)): |
| 104 | + |
| 105 | +# shape_cleaned = clean_brep(shape) |
| 106 | + |
| 107 | +# for brep in cutters: |
| 108 | +# cutter_cleaned = clean_brep(brep) |
| 109 | +# out_breps = Rhino.Geometry.Brep.CreateBooleanDifference(shape_cleaned, cutter_cleaned, Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance) |
| 110 | + |
| 111 | +# volume = 0 |
| 112 | + |
| 113 | +# if out_breps: |
| 114 | +# for o in out_breps: |
| 115 | +# o_cleaned = clean_brep(o) |
| 116 | +# current_volume = Rhino.Geometry.VolumeMassProperties.Compute(o_cleaned, True, False, False, False).Volume; |
| 117 | +# if current_volume > volume: |
| 118 | +# volume = current_volume |
| 119 | +# shape_cleaned = o_cleaned |
| 120 | + |
| 121 | +# return shape_cleaned |
| 122 | + |
| 123 | +# def mesh_boolean_difference(shape : Rhino.Geometry.Mesh, cutters : list[Rhino.Geometry.Mesh]): |
| 124 | +# print(shape, cutters) |
| 125 | +# for i in range(len(shapes)): |
| 126 | + |
| 127 | +# shape_cleaned = clean_mesh(shape) |
| 128 | + |
| 129 | +# for mesh in cutters: |
| 130 | + |
| 131 | +# cutter_cleaned = clean_mesh(mesh) |
| 132 | +# out_meshes = Rhino.Geometry.Mesh.CreateBooleanDifference([shape_cleaned], [cutter_cleaned]) |
| 133 | + |
| 134 | +# volume = 0 |
| 135 | + |
| 136 | +# if out_meshes: |
| 137 | +# for o in out_meshes: |
| 138 | +# o_cleaned = clean_mesh(o) |
| 139 | +# current_volume = Rhino.Geometry.VolumeMassProperties.Compute(o_cleaned, True, False, False, False).Volume; |
| 140 | +# if current_volume > volume: |
| 141 | +# volume = current_volume |
| 142 | +# shape_cleaned = o_cleaned |
| 143 | + |
| 144 | +# return shape_cleaned |
| 145 | + |
| 146 | +# def boolean_difference(shapes : list[Rhino.Geometry.GeometryBase], cuts : list[list[Rhino.Geometry.GeometryBase]]): |
| 147 | + |
| 148 | +# for i in range(len(shapes)): |
| 149 | +# if isinstance(shapes[i], Rhino.Geometry.Brep): |
| 150 | +# shapes[i] = brep_boolean_difference(shapes[i], cuts[i]) |
| 151 | +# Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep(shapes[i]) |
| 152 | +# elif isinstance(shapes[i], Rhino.Geometry.Mesh): |
| 153 | +# shapes[i] = mesh_boolean_difference(shapes[i], cuts[i]) |
| 154 | +# Rhino.RhinoDoc.ActiveDoc.Objects.AddMesh(shapes[i]) |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +# if __name__ == "__main__": |
| 160 | +# geometry_planes = select_and_find_valid_groups("Elements") |
| 161 | +# shapes, cuts = prepare_for_boolean_difference(geometry_planes) |
| 162 | +# boolean_difference(shapes, cuts) |
| 163 | + |
| 164 | + |
0 commit comments