import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
# Global variables to keep track of the Brep and point mappings
brep_id = None
brep = None
vertex_point_ids = []
vertex_point_mapping = {}
def extract_brep_vertices_with_update():
global brep_id, brep, vertex_point_ids, vertex_point_mapping
# Prompt the user to select a Brep (surface or polysurface)
brep_id = rs.GetObject("Select a Brep (surface or polysurface)", rs.filter.polysurface | rs.filter.surface)
if not brep_id:
print("No Brep selected.")
return
# Get the Brep geometry
brep_obj = sc.doc.Objects.FindId(brep_id)
if not brep_obj:
print("Unable to find the Brep in the document.")
return
brep = brep_obj.Geometry.Duplicate() # Make a copy of the Brep geometry
if not isinstance(brep, Rhino.Geometry.Brep):
print("The selected object is not a valid Brep.")
return
# Initialize mappings
vertex_point_ids = []
vertex_point_mapping = {}
# Iterate over all vertices in the Brep
for i, vertex in enumerate(brep.Vertices):
# Get the vertex location
vertex_point = vertex.Location
# Add the point to the document
pt_id = rs.AddPoint(vertex_point)
# Store the mapping from point ID to vertex index
vertex_point_mapping[pt_id] = i
# Keep track of the point IDs
vertex_point_ids.append(pt_id)
# Subscribe to the ReplaceRhinoObject event
handler = BrepUpdateHandler()
sc.sticky["brep_update_handler"] = handler # Prevent garbage collection
print("Brep vertices added as points. Move the points to adjust the Brep geometry.")
class BrepUpdateHandler:
def __init__(self):
# Enable the ReplaceRhinoObject event handler
Rhino.RhinoDoc.ReplaceRhinoObject += self.OnReplaceRhinoObject
def OnReplaceRhinoObject(self, sender, e):
# Called when an object is replaced in the document
if e.ObjectId in vertex_point_ids:
# If one of our points was replaced (moved), update the Brep
UpdateBrepGeometry()
def Dispose(self):
# Unsubscribe from events
Rhino.RhinoDoc.ReplaceRhinoObject -= self.OnReplaceRhinoObject
def UpdateBrepGeometry():
global brep_id, brep, vertex_point_ids, vertex_point_mapping
# Create a new Brep to modify
new_brep = brep.Duplicate()
if not new_brep:
return
# Update vertex positions
for pt_id in vertex_point_ids:
# Get the new location of the point
new_pt = rs.PointCoordinates(pt_id)
if new_pt is None:
continue
# Get the corresponding vertex index
vertex_index = vertex_point_mapping[pt_id]
# Update the vertex location
brep_vertex = new_brep.Vertices[vertex_index]
brep_vertex.Location = new_pt
# Rebuild edges and faces to reflect the new vertex positions
new_brep.RebuildEdges()
new_brep.Faces.ConvertSurfaceToNurbs() # Ensure surfaces are NURBS for consistency
# Replace the old Brep with the new one in the document
sc.doc.Objects.Replace(brep_id, new_brep)
sc.doc.Views.Redraw()
# Update the global Brep variables
brep = new_brep
brep_id = sc.doc.Objects.Find(new_brep).Id
# Run the function when the script is executed
if __name__ == "__main__":
extract_brep_vertices_with_update()
What I need :
-user selects brep
-script extract point
-subscribe to event
(works good til now)
-when the point is moved, the vertex of the solid should be moved too updating the 3d geometry
Why is the vertex moving not working? Should be all good