Whats these code mean?
-
#requires face or faces within a selected group or number of groups require 'sketchup.rb' class Crack def Crack;;groups # get the active model model = Sketchup.active_model #get currently selected objects selection_set = model.selection cracks = 0 stepbystep = UI.messagebox "Exclude random faces from cracking?", MB_YESNOCANCEL, "Proceed" #use to validate each face before its drawn if stepbystep == 2 return nil end Sketchup.active_model.start_operation "Crack groups" # loop through all the selected faces, and triangulate them selection_set.each {|group| #selection_set.remove group if group.typename == "Group" then maxcracks = group.entities.count * 2 group.entities.each {|entity| if cracks > maxcracks then return nil end entities = group.entities if entity.typename == "Face" then #to crack a triangle # get all the vertices for the current face vertices = entity.vertices # find the centroid - equals the average of all vertices, so lets add all coordinates together to divide it centerx = 0 centery = 0 centerz = 0 0.upto(vertices.length-1) do |vert| centerx = centerx + (vertices[vert].position[0]/vertices.length) centery = centery + (vertices[vert].position[1]/vertices.length) centerz = centerz + (vertices[vert].position[2]/vertices.length) end geomcen = Geom;;Point3d.new(centerx,centery,centerz) #prepare the randomly excluded face crack random_exclusion = rand(vertices.length-1) # insert the edges 0.upto(vertices.length-1) do |vindex| if vindex != random_exclusion or stepbystep == 7 then edge = entities.add_edges geomcen,vertices[vindex] cracks = cracks + 1 end end end } end } Sketchup.active_model.commit_operation end #def end #class if( not $cracking_menu_loaded ) UI.menu("Plugins").add_item("Polygon Cracking") { Crack.groups } end
-
Where did you get the code?
It is unattributed...
If the whole of the code were in a file called sayCrackGroup.rb
inside the Plugins folder then it would load.
The code makes a class 'Crack' def 'group' that 'cracks' faces inside selected groups.
It adds a new Plugins menu item [NB: it needs to declare the menu is loaded as a last line...] called 'Polygon Cracking'.
This takes all faces [or all-random_few] in a selected group and 'cracks' them - i.e. if finds the face's center and draws extra edges out to vertices as if the face is 'cracked' [glass etc] - the number of 'cracks' is randomized to a limited extent but that is set to be the same on every face in the group [it could be made random for all of the faces ?].
It runs its 'cracks' over any nested faces and any 'holes' or reentrant areas [e.g. with L's] in faces.
It screws up its face divisions because it is written so it affects the very 'entities' it's iterating through and trips itself up - unless that's intentional it is readily fixed by usinggroup.entities.to_a.each{...
instead ofgroup.entities.each{...
so the added entities aren't considered in the set that's getting processed...
What is it for ?? -
-
The code finds the geometric center of a Face, then adds some Edges from the center to the outside vertexes. "Crack" means break, shatter, fracture, subdivide.
Advertisement