Array of faces attribute all turn to nil ?
-
Hi,
I have a script which takes a face and fits a grid to it. I tried to store information about the grid so that other scripts can analyse it. In particular, I have a 2D (so that the objects can be accessed with x and y coordinates) array called nodes which contains a set of Point3d objects, which I stored with:
grid.set_attribute("grid_properties", "nodes", nodes)
This worked fine, but when I similarly try to store a 2D array called cells which contains a lot of faces, i.e. it looks like:
[ [Face, Face, ...] , [Face, Face, ...] , ... ]
what gets stored is the array with all faces replaced by nil, i.e.
[ [nil, nil, ...] , [nil, nil, ...] , ... ]
The cells array itself is not affected. To demonstrate, I have the following code in my script:
puts cells grid.set_attribute("grid_properties", "cells", cells) puts grid.attribute_dictionaries["grid_properties"]["cells"]
The output from the console is then something like:
#Sketchup::Face:0x1ac7e9fc
#Sketchup::Face:0x1ac7e984
#Sketchup::Face:0x1ac7e90c
#Sketchup::Face:0x1ac7e894
#Sketchup::Face:0x1ac7e81c
#Sketchup::Face:0x1ac7e7a4
#Sketchup::Face:0x1ac7e72c
nil
nil
nil
nil
nil
nil
nilHelp?
-
'Attributes' can consist of several data types:
string ['text']
integer [1]
float [1.2]
boolean [true/false]
array [0,1,2] [of any of the above, and also nested arrays]
You'll see that Sketchup class-types are not a readily store-able data type.
However, Point3d objects and some others are, because they are a type of 'array'...
An object's 'id' is also transient across sessions so it cannot be used in that way - however, you can add a unique 'tid' attribute referring to a 'date stamp' -(1000*Time.now.to_f).to_i
to give an enduring attribute so you can link it to other data, objects etc later on...You can't access 'faces' in later sessions without some 'trickery' by adding attributes to them...
If your grid is a group you can of course get an array of its faces at any time thus:
faces=grid.entities.to_a.find_all{|e|e.is_a?(Sketchup::Face)}
Incidentally, if it's a component usedefn=instance.definition
then...defn.entities...
instead...
So you can remember the numerical settings as attributes with the container and inspect the container's contents to get its faces etc... -
What I've mainly needed is to be able to access the faces given x and y coordinates, hence I needed the cells array to provide the order (so "faces=grid.entities.to_a.find_all{|e|e.is_a?(Sketchup::Face)}" wouldn't have been quite good enough). Since I couldn't pass 'cells' on, my only solution was to recreate the faces in another script and make another cells array there. But this seemed wasteful and inelegant, so I was hoping for a better solution here. You've helped me to think of a better way, thanks.
Edit: I also wanted an explanation as to why it was acting weird, so thanks for that too.
-
If the transactions between the two scripts are in one session without any need to remember things for the next session then why not make the two scripts within the same module and pass @arrays between them ?
module Mojaki def self.script1() ... @faces<<face...etc ... end end#module
AND in another file
module Mojaki def self.script2() ... @faces.each{|f|...etc ... end end#module
You run the scripts asMojaki.script1()
etc as needed...
The @faces array is remembered/accessible between the two scripts because it's in the same Module...
So as script1 makes/changes it, script2 can read it...
Advertisement