How can I store binary data in an attribute
-
I would like to store a "binary" String (which use all values 0-255 for each character) in a SketchUp attribute.
SketchUp seems to truncate String attributes which contain a 0, and also seems to convert strings to multi-byte format when they are in extended ansi format.
We currently are converting our "binary" strings to hex strings - but this involves a 2 to 1 increase in size.
I have tried "unpacking" the string into an array of FixNums using L* and N* format, which should have created FixNum's - but "unpack" created some BigNum's which SketchUp won't process.
Has anyone found a way to store binary data in an attribute without having to hex encode it?
-
I had the same problem with binary strings, because Sketchup stops the decoding at first \0 character.
But if you just wish to store an array of fixnum, why don't you store it 'naturally'. Sketchup entity attributes does it well, back and forth. I used this for my bezierspline.rb macro, where I do store arrays of 3D-points in this way.
-
You can convert to hex, or use #pack('m') when storing and unpack('m') on the way back out.
You might could also Marshal the data.
Todd
ps Fred, if you are saving 3D points as attributes - what happens when a user moves the spline?
-
I have stored successfully binary data in attributes. Specifically a matrix.
I did find a bug in get/set_attribute() that would cause the character 0x0d ('\n') to be replaced with 0x0d 0x0a. I got around it by replacing '\n' with 'I_AM_0D' in the string before packing. And of course reversing it after unpacking.
Hope that helps.
Chris -
@unknownuser said:
ps Fred, if you are saving 3D points as attributes - what happens when a user moves the spline?
Very interesting question, that I did not ask myself (actually, I just inherited the method from the previous bezier.rbscript by @Last). But magically, it seems to work fine, whether you move, scale or rotate the curve.
There must be a trick!
But this macro is full of surprise, for instance with an undocumented method "curve.move_vertices", which allows moving the vertices of a curve without having to recreate one.
-
I am trying to store a raster image on a material to use as a bump map when rendering.
Since raster images (.jpg or .png files) can be large, I am trying to store it without making it too much bigger.
We are currently converting it to a hex string, which makes it twice as large.
You cannot store 4 binary bytes in a ruby fixnum because ruby only stores 31 bits in a Fixnum, before it switched to a bignum, which is longer than 1 word. And you cannot store a Bignum as a SketchUp attribute.
Try:
model = Sketchup.active_model i1 = 0xaabbccdd # 32 bit integer, which cannot be stored in a Fixnum puts i1.class # returns 'Bignum' model.set_attribute('test','bignum',i1) # returns Error; #<RangeError; bignum too big to convert into `int'>
-
Sounds like a case for a new requirement of the API to be able to handle a BLOB (binary large object). Perhaps a new flavor of an attribute. I'll forward your plea.
Todd
Advertisement