Ruby error with SketchUp 2024

def self.多面偏移()
   # 检查是否有选择项
   if Sketchup.active_model.selection.empty?
     UI.messagebox("请先选择面,再执行命令。 ")
     return
   end       

   # 定义一个包含默认值的数组
   defaults = ["等距偏移", 100, 1]

   # 定义一个包含下拉选项的数组
   list = ["等距偏移|递增偏移", "", ""]

   # 调用 inputbox 方法,传入 defaults 和 list 参数
   results = UI.inputbox(["偏移方式:", "偏移量:", "偏移次数:"], defaults, list, '参数设定')

   offset_type = results[0]
   width = results[1].to_l / 25.4
   count = results[2].to_i

   Sketchup.active_model.start_operation("multi_face_offset", true)

   Sketchup.active_model.selection.each do |e|
     if e.is_a? Sketchup::Face
       if offset_type == "等距偏移"
         e.offset_equidistant(width, count)
       elsif offset_type == "递增偏移"
         e.offset_incremental(width, count)
       end
     end
   end

   Sketchup.active_model.commit_operation

endError: #<NoMethodError: private method offset' called for #<Sketchup::Face:0x0000025e1b4dc6e8>> C:/Users/wzf/AppData/Roaming/SketchUp/SketchUp 2024/SketchUp/Plugins/Curve_gradient_selection.rb:3567:in block in offset_equidistant’
C:/Users/wzf/AppData/Roaming/SketchUp/SketchUp 2024/SketchUp/Plugins/Curve_gradient_selection.rb:3566:in times' C:/Users/wzf/AppData/Roaming/SketchUp/SketchUp 2024/SketchUp/Plugins/Curve_gradient_selection.rb:3566:in offset_equidistant’
C:/Users/wzf/AppData/Roaming/SketchUp/SketchUp 2024/SketchUp/Plugins/Curve_gradient_selection.rb:3552:in block in 多面偏移' C:/Users/wzf/AppData/Roaming/SketchUp/SketchUp 2024/SketchUp/Plugins/Curve_gradient_selection.rb:3549:in each’
C:/Users/wzf/AppData/Roaming/SketchUp/SketchUp 2024/SketchUp/Plugins/Curve_gradient_selection.rb:3549:in 多面偏移' C:/Users/wzf/AppData/Roaming/SketchUp/SketchUp 2024/SketchUp/Plugins/Curve_gradient_selection.rb:5452:in block in <top (required)>'How to modify it to work properly in 2024

Please post code correctly in the forum:

OK, thanks,I have modified it.

I think it would take a real miracle to deduce from 33 lines of code - which is an unfinished method - what the error might be in the remaining 5500+ lines of code.

It doesn’t help that the comments are in Chinese, not to mention the name of the method, which is also Chinese.
(Taking into account that the language of the forum and according to best practice the language of Ruby methods / comments is also English.)

Perhaps the code is not yours, so your best bet is to talk to the original developer or hire a professional coder.

def self.multi_face_offset()
  # Check if there is a selection
  if Sketchup.active_model.selection.empty?
    UI.messagebox("Please select a face first before executing the command.")
    return
  end

  # Define an array with default values
  defaults = ["Equidistant Offset", 100, 1]

  # Define an array with dropdown options
  list = ["Equidistant Offset|Incremental Offset", "", ""]
  
  # Call the inputbox method, passing in defaults and list parameters
  results = UI.inputbox(["Offset Type:", "Offset Amount:", "Offset Times:"], defaults, list, 'Parameter Setting')

  offset_type = results[0]
  width = results[1].to_l / 25.4
  count = results[2].to_i

  Sketchup.active_model.start_operation("multi_face_offset", true)

  Sketchup.active_model.selection.each do |e|
    if e.is_a? Sketchup::Face
      if offset_type == "Equidistant Offset"
        e.offset_equidistant(width, count)
      elsif offset_type == "Incremental Offset"
        e.offset_incremental(width, count)
      end
    end
  end

  Sketchup.active_model.commit_operation
end

class Sketchup::Face
  def offset_equidistant(dist, count)
    Sketchup.active_model.start_operation("Equidistant Offset")
    last_face = self
    count.times do |i|
      last_face = last_face.offset(-dist)
      correct_face_orientation(last_face)
    end
    Sketchup.active_model.commit_operation
  end

  def offset_incremental(dist, count)
    Sketchup.active_model.start_operation("Incremental Offset")
    accumulated_dist = dist
    last_face = self
    count.times do |i|
      last_face = last_face.offset(-accumulated_dist)
      correct_face_orientation(last_face)
      accumulated_dist *= 2
    end
    Sketchup.active_model.commit_operation
  end

  def correct_face_orientation(face)
    if face.normal.dot(Z_AXIS) < 0
      face.reverse!
    end
  end
end

multi_face_offset

Thank you for your reply,sorry,I forgot to paste it yesterday, so I added the location that caused the error.This is the code I wrote together with AI. I will help you translate it into English. It can run very well on 2023. Please select one or more faces and paste it into the ruby console to run, but in 2024, It seems that some API changes have caused running problems. I don’t know what’s going on, it seems like the offset method has changed to push and pull.I didn’t find a solution so I came to the forum for help.HAHA :grin:I did write a lot of code, and there may be some conflicts. This is a stand-alone version. Can be tested in 2023.

2024 error message:
multi_face_offset
Error: #<NoMethodError: undefined method offset' for #<Sketchup::Face:0x00000292723f9f60>> (eval):41:in block in offset_equidistant’
(eval):40:in times' (eval):40:in offset_equidistant’
(eval):26:in block in multi_face_offset' (eval):23:in each’
(eval):23:in multi_face_offset' (eval):66:in
SketchUp:in `eval’
=> nil

I refuse to run this code.

  1. There was never an offset method for Class: Sketchup::Face

  2. SketchUp extensions all run in a shared environment. To avoid clashes between similarly named methods and classes, all your extension’s Ruby code must be wrapped in a uniquely named module.

  3. SketchUp extensions run in a shared environment, changing the modules and classes of the Ruby API from one extension can clash with another extension. Don’t change these modules and classes.

  4. Forget about AI in programming unless you are a professional programmer.

You probably redefined and ruined up base classes and modules back and forth in that environment …

3 Likes

thank you for your reply. I won’t change it anymore, I’m not a professional programmer, I’m an interior designer.

Any use of an AI should use this rule:

  • Use refinements rather than modifying core Ruby classes.
1 Like

OK, Thanks~I will discard this program. Try new ways to solve this problem. :grinning: