Sketchupプログラム自動化(七)Rubyプロセス制御

3781 ワード

Sketchupの二次開発を行う際には、論理を判断することに関連し、これらの判断もプログラマーがプログラムに思考を与える過程であり、以下はコードを通じてRubyの強力なプロセス制御を振り返る.

model = Sketchup.active_model
ent = model.entities
# Ruby       

#     
x = "ASD"

a = 1
b = 2

# if  

if a > b
    puts a
else
    puts b
end

# case :  

puts case x
      when String; puts "String"
      when Numeric then puts "Numeric"
      when TrueClass,FalseClass ; puts "Boolen"
      else
          puts "Other"
      end
      
      
x = 1

# while :   
while x <10
    
    puts x
    x += 1
    
end

x = 1

# until :  ...   
until x > 10
    
    puts x
    x += 1
    
end


for i in 1..10
    puts i
end

#     
# return     ,   nil       
# break       
# next             
# redo 

puts "======= redo ======="

#        
i = 0
while i < 3
    puts i
    i += 1
    redo if i == 3
end

#################    

# times         
3.times{
    
    puts "666666666"
}

# each     for in
arr = [11,22,33]
arr.each{ |item|
    puts item
}

#        
arr.each do |item| 
    
    puts item
end

# map         ,          
[1,2,3].map{|item|
  item * item
}

puts "---------"
# upto   1 10          1 10   
1.upto(10) {|x|
    puts x
}

#    Numeric    

#   0     0.1    ,    PI
0.step(Math::PI,0.1){ |x|
    
    puts Math::sin(x)
}

#       
#    
ent.each{ |item|
    if item.typename == "Face" && item.normal == [1,0,0]
        puts "   X   "
    end
}

#             

face = ent.add_face [[0,0,0],[100,0,0],[100,100,0],[0,100,0]]
face.pushpull -100

vertex_arr = []
ent.each do |entity| 
    
    if entity.typename == "Edge"
          
          #      
          if entity.start.position.x > 0 && entity.start.position.y > 0
              entity.smooth = true
          end
          #        ,        
          vertex_arr = vertex_arr | entity.vertices
    end
end

vertex_arr.each{ |vertex|
    
    puts vertex.position
}