# Help needed fixing written code

**URL:** <https://forums.sketchup.com/t/help-needed-fixing-written-code/97927>\
**Category:** Ruby API\
**Tags:** sketchup\
**Created:** [June 26, 2019, 8:15pm UTC](https://forums.sketchup.com/t/help-needed-fixing-written-code/97927 "2019-06-26T20:15:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MNAJAR](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@MNAJAR](https://forums.sketchup.com/u/MNAJAR)\
**Post date:** [June 26, 2019, 8:15pm UTC](https://forums.sketchup.com/t/help-needed-fixing-written-code/97927/1 "2019-06-26T20:15:52Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [June 26, 2019, 8:23pm UTC](https://forums.sketchup.com/t/help-needed-fixing-written-code/97927/2 "2019-06-26T20:23:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [June 26, 2019, 9:03pm UTC](https://forums.sketchup.com/t/help-needed-fixing-written-code/97927/3 "2019-06-26T21:03:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MNAJAR](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@MNAJAR](https://forums.sketchup.com/u/MNAJAR)\
**Post date:** [June 26, 2019, 10:42pm UTC](https://forums.sketchup.com/t/help-needed-fixing-written-code/97927/4 "2019-06-26T22:42:20Z")

</div>

[script2.rb](https://forums.sketchup.com/uploads/short-url/eLzJgkqvroxXLB7tpE7UT8K3mKb.rb) (934 Bytes)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [December 27, 2019, 6:42am UTC](https://forums.sketchup.com/t/help-needed-fixing-written-code/97927/5 "2019-12-27T06:42:25Z")

</div>

Closed on December 27, 2019, 6:42am UTC

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