Validating a Medeek Wall

I’m currently working on my own API for the Medeek Wall and I would like for the user to be able to either pass the actual SketchUp group that is the wall assembly or the instance name of the group.

My validation routine is below but I’m putting this out there since I want to make sure I’m not missing anything or leaving loop holes open. The end result is a method that passes a true/false and the resulting SketchUp group:

def validate_medeekwall (group)
	
  if group == ''
    puts "No Wall Assembly Selected, action aborted."
    return false
  else
    if group.is_a? String
      if group =~ /_WALL_ASSEMBLY_/
        group_list = Sketchup.active_model.active_entities.grep(Sketchup::Group)
        wallgroup = group_list.find { |g| g.name == group }

        if wallgroup
          # Proceed, wall assembly found

          return true, wallgroup
        else
          puts "Wall Assembly: #{group} could not be found, action aborted."

          return false, group
        end
     else	
       puts "Not a valid name for a Medeek Wall Assembly, action aborted."

       return false, group
     end
   else
     if group.is_a?(Sketchup::Group)
       if group.valid?
         if group.name =~ /_WALL_ASSEMBLY_/
           # Proceed, valid wall assembly

           return true, group
         else
           puts "Not an existing Medeek Wall Assembly, action aborted."

           return false, group
         end
      else
        puts "Group does not exist in model, action aborted."
	
        return false, group
      end
    else
      puts "Not a valid SketchUp Group, action aborted."
	
      return false, group
    end
   end
  end
end

Sorry my indentation is all messed up again.

Mainly because you don’t have the editor set to replace TAB characters with 2 SPACE characters. The forum does not do so well with TABbed indents.

1 Like

Tried to fix it but I don’t like such small indentations, it is hard to read the code, hence I like to use tabs.

Your original code, colorized
def validate_medeekwall (group)
	
  if group == ''
    puts "No Wall Assembly Selected, action aborted."
    return false
  else
    if group.is_a? String
      if group =~ /_WALL_ASSEMBLY_/
        group_list = Sketchup.active_model.active_entities.grep(Sketchup::Group)
        wallgroup = group_list.find { |g| g.name == group }

        if wallgroup
          # Proceed, wall assembly found

          return true, wallgroup
        else
          puts "Wall Assembly: #{group} could not be found, action aborted."

          return false, group
        end
     else	
       puts "Not a valid name for a Medeek Wall Assembly, action aborted."

       return false, group
     end
   else
     if group.is_a?(Sketchup::Group)
       if group.valid?
         if group.name =~ /_WALL_ASSEMBLY_/
           # Proceed, valid wall assembly

           return true, group
         else
           puts "Not an existing Medeek Wall Assembly, action aborted."

           return false, group
         end
      else
        puts "Group does not exist in model, action aborted."
	
        return false, group
      end
    else
      puts "Not a valid SketchUp Group, action aborted."
	
      return false, group
    end
   end
  end
end

Some idea from me:

def m_wall_assemblies
  Sketchup.active_model.active_entities.grep(Sketchup::Group).select{|group|
    group.name.include?("_WALL_ASSEMBLY_")
    # group.name =~ /_WALL_ASSEMBLY_/
  }
end

def m_group_valid?(group)
  valid = group.valid? && group.name.include?("_WALL_ASSEMBLY_")
  # valid = group.valid? && !(group.name =~ /_WALL_ASSEMBLY_/ ).nil?
  puts "Not a valid group, or not an existing Medeek Wall Assembly" if !valid
  return valid, group
end

def m_string_valid?(str)
  group = m_wall_assemblies.find{|group| group.name == str }
  puts "Not a valid name, or could not be found or no Medeek Wall Assembly in a model editing context" if !group
  valid = group.nil? ? false : true
  return valid, group || str
end


def validate_medeekwall(group)
  case group
  when Sketchup::Group
    return m_group_valid?(group)
  when String
    return m_string_valid?(group)
  else
    puts "No Wall Assembly Selected."
    return false, group
  end
end
2 Likes

I need to study this some, there are some nice chunks of code here that are more concise than my own. This is why I like to post here once and a while, helps me improve my own coding habits and I always learn a few new tricks of the trade.

I’m not a programmer by trade (I’m sure that it probably pretty obvious at this point) but I do manage to get by and I’m still learning even at 52 years young.