Macro to turn every object into a block

which is easier said than done, since there is no ‘awareness’ of these objects being the same.

Gijs, I have a layer and all objects inside that layer are same if the layer contains different objects script can’t identify them so it will replace all of them with the first one and thats ok.
actually I’m not a programmer but I tried to change pascals script and it worked to some extent.
first. pascals code makes all blocks in 0,0,0 which is not ideal.
because when it replaces the other blocks the position will change. so I changed that script so that first it identifes object centroid and then make a block of object in centroid with the layer name.
but it’s not complete. if I upload that here can you please refine it?

I can have a look, but as said, detecting objects that are the same is not easy. Of course you could do a couple of checks like edges length and volume, but then there is also transforms that need to be derived from the model.

No Gijs I don’t want script to detect same objects.
I as an user should put all same objects to same layer, and script just should check the name of layer and make a block of object with the name of layer. now if name of layer was the same so it does not make a new block.
in other words algorithm should do this:

  1. select objects
    2.make a block of object with the name of objects layer name
    3.if the name used before so its not possible to make a new block then software automatically replaces the object by a block

you can try this sequence manually:
make 2 spheres in the scene and make a block from first sphere on its centroid with the name “A”
then try to make a new block from second sphere with the name “A”
software asks you that you want to replace the second sphere with first block. and this is it. I just want this to be automated.

for example we have this objects and these layers:

as you can see all same objects are in same layers- in this image all objects are closed poly surface

as a result I need this:

in this image all objects are blocks

Just that simple.

Yes that simple example would work. But in a realistic scenario these objects are or can be oriented differently (rotated). Since Rhino does not store object transforms of anything but block instances, what do you want to do with that?

Even if your objects are just rotated around the z axis, your will loose those transforms.

oooooooo you’re right! in that situation i will end up with wrong orientation.
but I think i can manually fix the orientation of those blocks after script.
it is still better than manually doing all blocks.
in real world the most repeated objects are bolts and nuts or other off the shelf parts like bearings or … in an assembly. and with my experience many bolts are plannar. so I will for example make a M5 bolt and nut and washer group all of that and assign in the layer named M5.
then i will copy or array ( not mirror) that group to let’s say 50 locstion without changing the orientation.
now I need to assemble that group in a perpendicular plane, if i rotate that i will have a problem. but I do that.
I will do this for every objects and after it finished and i wanted to export that in SOLIDWORKS i run the script and then check wrong orientations and manually fix them. and finally export it as a step file.

I think this is better than doing all of that manually.

if all you want to do is that, then you’re better off using the script I made for adding oriented bolts and then afterwards replace the blocks with other fasteners where needed. Use the latest version 0.8 in that thread

fwiw I recall years ago hacking a “real scale” texture mapping in rhino, and using the old sdk’s RhinoObject.GetFrame() to accomplish keeping my box-projected uvs aligned with & sized to an object even after it was rotated/scaled, by building a transform from the returned OnPlane. Not a perfect solution, but in practice it was good enough to be generally useful

Gijs, 3 years ago when I didn’t know about rhino blocks forming assembly in SOLIDWORKS, I just exported whole rhino file to step and opened that in SOLIDWORKS as a multibody part.
and then used “save bodies as part” with " make an assembly" option.
and surprisingly solidworks could identify same objects and just reused same object in an assembly and increasing quantity.
I don’t know how! but it seams something stored in STEP file.

@jdhill I suppose this requires to add the texture mapping to the object prior to making transformed copies of it, correct?

This might be one of the later feature recognition tools that was added to SW. The latest version I upgraded to (2018) did not yet have this. But it is certainly not info that is stored in the step file.

it’s difficult to remember exactly, but I think this was used only when there was no explicit mapping defined, so that the plugin was free to create its own uvs, and that I then used regular rhino sdk methods for setting texture coords, using a box mapping and the transform derived from the frame

but I was only mentioning this in case it could be useful for coming up with a decently usable transform for arbitrary objects