Help with errors in code

I have errors showing up in lines 39 and 67 saying this…

Error: #<SyntaxError: /Users/caydenwilson/Library/Application Support/SketchUp 2019/SketchUp/Plugins/extension.rb:39: trailing `_’ in number
model.active_layer=1_new
^
/Users/caydenwilson/Library/Application Support/SketchUp 2019/SketchUp/Plugins/extension.rb:39: syntax error, unexpected tIDENTIFIER, expecting keyword_end
model.active_layer=1_new
^~~
/Users/caydenwilson/Library/Application Support/SketchUp 2019/SketchUp/Plugins/extension.rb:67: syntax error, unexpected end-of-input, expecting keyword_end>

:in `load' :in `' SketchUp:1:in `eval'

My code:

model=Sketchup.active_model
selection=Sketchup.active_model.selection
entities=Sketchup.active_model.entities
materials=Sketchup.active_model.materials
layer_array=Sketchup.active_model.layers

require "sketchup.rb"
#Adding extension to menu
cmd=UI::Command.new("ProTrim") {
  selection=Sketchup.active_model.selection
  selection.each do |e|
  end
}
#Validation to check for Edge
cmd.set_validation_proc {
  selection=Sketchup.active_model.selection
  ok=selection.find{ |e| e.typename=="Edge"}
  ok ? MF_ENABLED : MF_GRAYED
}
#Context menu
UI.add_context_menu_handler do |menu|
  menu.add_separator
  menu.add_item cmd
end
#Input boxes in UI menu which gather measurements for trim
prompts=["Trim Height", "Trim Thickness:", "Quarter Round:", "R:", "G:", "B:"]
defaults=[5.0,0.5,"No",255.0,255.0,255.0]
list=["","","Yes|No","","",""]
input=UI.inputbox prompts, defaults, list, "Stair Info"
  height,thickness,quarter_round=input
if quarter_round=='Yes'
  quarter_round_width=0.75
  quarter_round_height=0.75
notification=UI::Notification.new(sketchup_extension, "Select the trim path.")
notification.show
def build_trim
  new_layer=model.layers.add "Trim"
  model.active_layer=1_new
  name=trim
  visable=True

  #creating points for the new face
  #pts[number]=[width,depth,height]
  pts=[]
  pts[0]=[0,0,0]
  pts[1]=[0,0,height]
  pts[2]=[width,0,0]
  pts[3]=[width,0,height]
  #Creating the trim material
  trim=Sketchup.active_model.materials.add"Interior Trim"
  trim.color=['R','G','B']
  trim_group.material=trim

  #Creating a face with the array
  face=entities.add_face(pts)
  connected=face.all_connected
  face.material=new_material
  #Checking if selection is an edge
  def check_false
    selection=Sketchup.active_model.selection
    ok=selection.find{|e| e.typename=="Edge"}
    ok ? MF_ENABLED : MF_GRAYED
  end
  path=selection
  face.followme path
end


Any and all help is appreciated.

Thanks!
# this defines a  'named' tag/layer
  new_layer = model.layers.add('Trim')
 # model.active_layer = 1_new # causing error as it starts with a numeric and is un-defined as well
model.active_layer = new_layer
 # name=trim # already named above

john

def build_trim
  new_layer=model.layers.add "Trim"
  model.active_layer=1_new

should be:

def build_trim
  new_layer=model.layers.add("Trim")
  model.active_layer=new_layer

read the lines where the error points you…

2 Likes

Thanks! The guide I was using had that…always wondered if it should’ve been new_layer

… will result in …
Error: #<NameError: uninitialized constant True>

The singleton instance of the TrueClass is true.

Ruby defines a global constant TRUE that is assigned to point at the instance true.

You may use either (but I normally see code just use true.)


BTW, “visible” does not have an “a” character in it.

1 Like

Speed is very important within UI validation procs.
The general rule is do as little as possible to quickly return the menu function values.

(1) Avoid the use of Sketchup::Entity#typename. String comparison is relatively slow in Ruby compared to class identity.
Instead use Enumerable#grep when checking collections for entity types. (It’s very fast.)

(2) Avoid unneeded reference assignments.

A better proc would be …

# Validation to check for Edge
cmd.set_validation_proc {
  Sketchup.active_model.selection.grep(
    Sketchup::Edge
  ).empty? ? MF_GRAYED | MF_DISABLED : MF_ENABLED
}

… or … (if your style guide suggests to avoid the use of the tertiary if statement, then) …

# Validation to check for Edge
cmd.set_validation_proc {
  if Sketchup.active_model.selection.grep(Sketchup::Edge).empty?
    MF_GRAYED | MF_DISABLED
  else
    MF_ENABLED
  end
}

However, it’s generally better to not add items to a context menu when the “context” is such that the command does not apply. (Ie, the whole reason that it’s called a “context menu”.)

So instead of this …

#Context menu
UI.add_context_menu_handler do |menu|
  menu.add_separator
  menu.add_item cmd
end

… do the following …

# Context menu
UI.add_context_menu_handler do |menu|
  selset = Sketchup.active_model.selection
  if !selset.empty? && !selset.grep(Sketchup::Edge).empty?
    menu.add_separator
    menu.add_item(cmd)
  end
end

:bulb:

My bad on the spelling :sweat_smile: