SU+Ruby ... is it a max number of entities in a group?


#1

[edit 6]
the class file … called from a (kind of) loop

class Tige
attr_accessor :x0, :hauteur, :profondeur, :localGroup

def initialize(x0, hauteur, profondeur)
@x0 = x0
@hauteur = hauteur
@profondeur = profondeur
@localGroup = $leGroupe
draw

end

def draw
centerpoint = Geom::Point3d.new((@x0).mm,0,(@hauteur+1000).mm) # positionnement du centre du cercle
vector = Geom::Vector3d.new 0,0,1 # l’axe sera Z
#myCircle = @localGroup.entities.add_circle centerpoint, vector, 10.mm # création du cercle, diam 10mm
myCircle = Sketchup.active_model.entities.add_circle centerpoint, vector, 10.mm # création du cercle, diam 10mm
#myFace = @localGroup.entities.add_face(myCircle)
if myCircle.class != Array
puts "No Circle added 2:"
puts "centerpoint: #{centerpoint.inspect}"
puts "vector: #{vector.inspect}"
fail
end
if myCircle.nil?
puts "No Circle added:"
puts "centerpoint = #{centerpoint.inspect}"
puts "vector: #{vector.inspect}"
fail
end # creation de la face
if myCircle.class != Array
puts "No Circle added 2:"
puts "centerpoint: #{centerpoint.inspect}"
puts "vector: #{vector.inspect}"
fail
end
myFace = Sketchup.active_model.entities.add_face(myCircle)

if myFace.class != Sketchup::Face 
		puts "No Face added 2:"
		puts "myCricle: #{myCircle.inspect}"
		fail
end
if myFace.nil?
		puts "No Face added:"
		puts "centerpoint = #{centerpoint.inspect}"
		puts "vector: #{vector.inspect}"
		fail
end							# creation de la face
	#puts "myFace :"+(myFace).to_s
	myFace.material = 0x00F7FF								# pas utile, déjà dans le bon sens
	myFace.material.alpha = 0.5
	myFace.pushpull 1500.mm 
	# pour le coté intérieur
	myCircle = nil
	centerpoint = Geom::Point3d.new((@x0).mm,(@profondeur).mm,(@hauteur+1000).mm)		# positionnement du centre du cercle
	vector = Geom::Vector3d.new 0,0,1							# l'axe sera sur Z
	#myCircle = @localGroup.entities.add_circle centerpoint, vector, 10.mm 					# création du cercle, diam 10mm
	#myFace = @localGroup.entities.add_face(myCircle) 							# création de la face
	myCircle = Sketchup.active_model.entities.add_circle centerpoint, vector, 10.mm 					# création du cercle, diam 10mm
	myFace = Sketchup.active_model.entities.add_face(myCircle) 							# création de la face
	myFace.material = 0x00F7FF									#
	myFace.material.alpha = 0.5
	myFace.pushpull 1500.mm 								# hauteur : 1 mètre
	# def add(designation,ref,qte,prix)
	#$listeCourse.add("Spirale","BE96 SSS",2,2)
	#puts "Ajout dans listeCourse ............................."
	#$listeCourse.show

end

end


#2

You should check the return value from both add_circle and add_face. If the value is nil then the circle or face was not added to the Group. At this point check the input data. For example:

myCircle = @localGroup.entities.add_circle centerpoint, vector, 10.mm
if myCircle.nil?
    puts "No Circle added:"
    puts "centerpoint = #{centerpoint.inspect}"
    puts "vector: #{vector.inspect}"
    fail
end
myFace = @localGroup.entities.add_face(myCircle)
myFace.material = 0xFF0000

Edit

Technically since the documentation does not say what gets returned on failure…

myCircle = @localGroup.entities.add_circle centerpoint, vector, 10.mm
if myCircle.class != Array && !myCircle.empty?
    puts "No Circle added:"
    puts "centerpoint: #{centerpoint.inspect}"
    puts "vector: #{vector.inspect}"
    fail
end
myFace = @localGroup.entities.add_face(myCircle)
if myFace.class != Sketchup::Face 
    puts "No Face added:"
    puts "myCricle: #{myCircle.inspect}"
    fail
end
myFace.material = 0xFF0000

#3

So far as I know, the max number of entities in a group is so large as to be unlimited for all sensible purposes. However, you should always test the return from any of the Entities#add_xxx methods to make sure the operation succeeded. Most of the Ruby API methods do not raise exceptions when they fail for other than basic Ruby reasons (such as the no method exception you saw). Instead they return nil, and empty Array, or similar void results. So you always have to check to make sure and Entities#add_xxx returned a legitimate object. It is possible that #add_circle failed due to some issue with centerpoint or vector. The resulting nil would propagate through #add_face (also returning nil) until it finally hit the no method exception for #material.

Edit: as you see, @jim_foltz wrote much the same while I was typing.


#4

What is the value of myCircle on failure?


#5

If you used the second version of jim’s code, it is possible the return was an empty Array, which would pass the test despite being a void return indicating an error.


#6

fixed

And then also add_face would generate a different error.

#<ArgumentError: Not enough edges - at least 3 required>


#7

Sorry, I don’t know what that means…


#8

I’ll try that and let you know


#9

We would need a more complete, preferably run-able example to help more. The numbers you provided work for me.


#10

Running my code twice returns nil for add_face. Maybe you are adding a duplicate face?