루비 입문자 코드 오류 도와주세요

조회수 764회

구글스케치업에서 루비를 만드려는 루비 입문자입니다

루비는 워낙 입문자에겐 자료가 적어 힘드네요

코드 올립니다(매우 짧으니 귀찮더라도 봐주세요 ㅠㅠ)

class MakeboxTool
  def self.makebox

#======================↓정상작동하는 부분↓==============================
    model = Sketchup.active_model
    entities = model.active_entities
    selection = model.selection
    faces = selection.grep(Sketchup::Face)
    faces.each{|face|
      group = entities.add_group(face)
      group2 = group.copy
      group.explode
      entities2 = group2.entities
      before = entities2.grep(Sketchup::Face)
      before[0].material = [50, 255, 255]
      before[0].pushpull( 10.mm )
      new_faces = entities2.grep(Sketchup::Face) - before
      upper_face = new_faces.find {|f| f.normal == before[0].normal.reverse }
      unless upper_face.nil?
        start_point = upper_face.bounds.center
        offset_vector = upper_face.normal
        offset_vector.length = 90.mm
        end_point = start_point.offset(offset_vector)
      end
      edge = group2.entities.add_line(start_point, end_point)
      layer = Sketchup.active_model.layers.add("win")
      group2.layer = layer
      entities2.each{|e| e.layer = layer }

      if !model.active_path.nil?
        instance = model.active_path.last
        definition = instance.definition
        group2_copy = group2.parent.parent.parent.entities.add_instance(group2.definition,group2.transformation)
        group2.erase!
      end

      }


  end # of makebox class
end
#==================↑ 정상작동하는 부분↑ =========================


if( not file_loaded?("makebox.rb") )
    UI.menu("Plugins").add_item("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
    dir = Sketchup.find_support_file("Plugins")
    cmd = UI::Command.new("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
    cmd.large_icon = cmd.small_icon = dir+"/makebox.png"
    cmd.status_bar_text = cmd.tooltip = "Tool for making blue boxes"
    tb = UI::Toolbar.new("makebox")
    tb.add_item cmd
    tb.show if tb.get_last_state == -1
end

file_loaded("makebox.rb")

이미지

이렇게 벽(면) 위에 창문을 만드는 간단한 makebox툴입니다

중간에 점선으로 표시해놓은 부분은 단독으로 작동은 정상적으로 되지만

아이콘이 있는 실행버튼을 만드려고

윗부분에 있는 class def 메소드와

class MakeboxTool
  def self.makebox

아래부분에 있는 이 메소드를 추가시키면 실행해도 아무 반응이 없습니다

if( not file_loaded?("makebox.rb") )
    UI.menu("Plugins").add_item("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
    dir = Sketchup.find_support_file("Plugins")
    cmd = UI::Command.new("Start makebox Tool") { Sketchup.active_model.select_tool MakeboxTool.new }
    cmd.large_icon = cmd.small_icon = dir+"/makebox.png"
    cmd.status_bar_text = cmd.tooltip = "Tool for making blue boxes"
    tb = UI::Toolbar.new("makebox")
    tb.add_item cmd
    tb.show if tb.get_last_state == -1
end

file_loaded("makebox.rb")

다시 한 번 말씀드리면

정상작동하는 코드에 아이콘이 있는 실행버튼을 만드려고

위아래에 코드를 추가하였더니 실행이 먹통입니다.(오류메세지는 없음)

받았던 해결답변인데 이해는 안갑니다....

There are two major things wrong with your code.

First, your MakeboxTool class has only loose class-scope code, no methods (def’s) at all, not even initialize. So, the code is run as the file loads, but thereafter is dead. Creating an instance via MakeboxTool.new does not cause the class-scope code to run again, so your menu item and toolbar button just create dead objects.

You could convert the class into a one-shot by wrapping the loose code in def initialize…end. initialize is the method run to initialize an object created by new.

But, that takes us into the second major issue: your code does not implement any of the methods of the Tool protocol 2. It does not really qualify as a thing that should be activated using select_tool. If you want a one-shot, you could create a class method (def self.methodname) and invoke it in the command’s code block instead of selecting a Tool.

도와주세요!!!!!!!!!

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 대충 번역하면:

    지금 작성하신 MakeboxTool 클래스에 메소드가 하나도 없어요. initialize도 없고 그냥 느슨하게 정의돼서 실행되는 코드들이거든요. 그래서 파일이 로딩되면 실행은 쭉 되겠지만, 그러고 나면 더 이상 아무 일도 안 하게 됩니다. 그 코드들을 def initialize ... end 안에 넣어서, 이 클래스 최초 초기화 때 바로 실행되게 하면 좋겠지요.

    근데 또 그렇게 하면 추가로 문제가 되는 게, 지금 작성하신 코드가 Tool 프로토콜 2 메소드들을 전혀 구현하지 않고 있거든요. select_tool을 써서 활성화시키면 안 되는 뭔가가 된단 말입니다. Tool을 선택하지 마시고, 클래스 메소드를 하나 정의하셔서 명령 코드 블록에서 그걸 불러야 할 거 같네요.

    보통 이럴 때는 다른 사람들이 구현해놓은 '실행버튼'이 존재하는 뭔가를 받아서 참고하는데... 그런 게 없는 건가요?

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)