Help needed fixing written code

Hi guys,

Me, with help from a friend, wrote this code. it’s a simple one. just to copy an entity in specific coordination (X, Y, Z). it was working well. now I’m trying to use it. but unfortunately, it gives no result. its working without giving any error. but there is no output. any help would be appreciated fixing it. thank you in advance. this is the code. ( list.txt containes the coordinations in this formate

class Array
def multiply_by(x)
collect { |n| n * x }

def self.copy_entities(tr, entarray)

Copies and transforms an array of entities

mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model

tempgroup = ent.add_group entarray
temptrans = tempgroup.transformation
tempdef = tempgroup.definition

newgroup = ent.add_instance tempdef, temptrans

ent.transform_entities(tr, newgroup)
end #def

#test using selection entities
myfile =“C:\Program Files\SketchUp\SketchUp 2018\Tools\list.txt”)
myfile.each {|line|
if line =~ /[(.),(.),(.*)]/
mod = Sketchup.active_model # Open model
ent = mod.entities[-1] # All entities in model
sel = mod.selection
result1 = self.copy_entities(tr1, sel)

Please format your code using the </> tags otherwise it’s hard to follow !

there is no need to modify the Array class to convert from meters…

the :m method returns a modified array in inches for internal use and is pretty close to your multiplier…

1.m => 39.370079

given an array…


=> [19527071.832322836, 166063705.16338584, 16.516102362204727]

also your list.txt should NOT be in SU program files, but somewhere in your ‘User’ space…

if line.is_a?(Array) may be more performant and easier to read…

your example doesn’t need :to_f as it’s already a float and even if it wasn’t for multiplying by a float, so it will be converted…

mod.entities[-1] # All entities in model

is only the LAST entity in the collection…

you should avoid globals i.e. :$ as they are disallowed in extensions you wish to share in Extension Warehouse, but are redundant if you use :m


1 Like

script2.rb (934 Bytes)

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.