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
[495987.623129000010000,4218018.181710000100000,0.4100620]
[495987.624541000000000,4218018.111150000200000,0.4195090]
)

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

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)
newgroup.explode
tempgroup.explode
end #def

#test using selection entities
myfile = File.open(“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
sel.add(ent)
tr1 =Geom::Transformation.new([$1.to_f,$2.to_f,$3.to_f].multiply_by(39.37007874))
result1 = self.copy_entities(tr1, sel)
end
}

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…

[495987.624541000000000,4218018.111150000200000,0.4195090].map(&:m)

=> [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

john

1 Like

script2.rb (934 Bytes)

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