Hi everyone,
I’m creating a tool that when a component is selected, a hot spot are drawn in each corner of element. Like image:
But this is consuming a lot of CPU (about 10% ~ 14% more) each time that have to draw those hot spots.
I know my code is not performatic at all, I would like to get some advices to improve it and I think I’m working wrong with the Tool, maybe need to start_transaction and stuffs like that.
Here is what I’m doing:
module My_module
class My_class
def initialize
@state = 0
@group_entities = []
end
def deactivate(view)
reset
end
def reset
ents = Sketchup.active_model.entities
@group_entities.each {|e| ents.erase_entities e}
@group_entities = []
@state = 0
end
def onMouseMove(flags, x, y, view)
ph = view.pick_helper
ph.do_pick x,y
@element = ph.best_picked
if @element.is_a? Sketchup::ComponentInstance
ip = view.inputpoint x, y
selected_face = ip.face
trans = ip.transformation
#puts element.definition.get_attribute('dynamic_attributes', 'puxador_medida').to_f.to_mm
elsif @element.is_a? NilClass
if @state == 1
reset
@state = 0
end
end
view.refresh
end
def draw(view)
if @element.is_a? Sketchup::ComponentInstance
if @state == 0
@state = 1
draw_selectable_corners
end
end
end
def draw_selectable_corners
draw_left_front_bottom
draw_left_front_top
draw_right_front_bottom
draw_right_front_top
draw_left_back_bottom
draw_left_back_top
draw_right_back_bottom
draw_right_back_top
end
def draw_right_back_top
pt = @element.definition.bounds.corner BB_RIGHT_BACK_TOP
point = pt.transform(@element.transformation)
ents = Sketchup.active_model.entities
#X_VEC = [1,0,0] and X_VEC_R = [-1,0,0], RADIUS = 3
arc1 = ents.add_arc point, X_VEC_R, Z_VEC, RADIUS, 0, 90.degrees
arc2 = ents.add_arc point, X_VEC_R, Y_VEC, RADIUS, 0, -90.degrees
arc3 = ents.add_arc point, Y_VEC_R, X_VEC, RADIUS, 0, 90.degrees
line1 = ents.add_line point, [point[0] - RADIUS, point[1], point[2]]
line2 = ents.add_line point, [point[0], point[1] - RADIUS, point[2]]
line3 = ents.add_line point, [point[0],point[1], point[2] - RADIUS]
create_faces arc1, arc2, arc3
group = ents.add_group arc1, arc2, arc3, line1, line2, line3, line1.faces, line2.faces
@group_entities << group.entities.map {|e| e}
end
#and the others "draw_each_corner" like above.
def create_faces arc1, arc2, arc3
arc1.map {|a| a.find_faces}
arc2.map {|a| a.find_faces}
arc3.map {|a| a.find_faces}
end
I know that need to refactor a lot of thing, but the point is what could be improved to draw 8 corners and what could be improved in Tool too, like the states, transactions, observers, etc…
Thanks a lot.