Working script to create metric bolts on the fly as block items (but some feedback needed)

yes I found that error indeed. Some refactoring to do for 0.6 :slight_smile:

(just upload a new version that checks the document units and adjusts the scale accordingly)

Hi Gijs,

Really great to share this project.
As I wanted to contribute as well: I added getting all options in one commandline

image

addBolt_vWD_options.py (17.9 KB)

for the DrawBreps I’d set the isocurve density to 0, much cleaner preview this way:

args.Display.DrawBrepWires(new, Color.Aquamarine,0)

Thanks en Groet
-Willem

1 Like

@Willem yes, much nicer/convenient! Do you know it it is possible to distinguish between Esc and space/enter? This way one could cancel the command with escape right away. edit: I see it already works this way the way you did it, that’s great.

I was just about to upload a new version, that had also split up the user options from the main function. I might as well do that anyways and then add your change after that for completeness of the progress. In the meantime I also split the spiral from the creation as suggested by @Dancergraham which should make it more generic to add other types of bolts later.

this is indeed cleaner thanks

Looks like update 4 broke this for me - maybe a Rhino 5 vs 6 thing ? or method overloads in Python?

Message: ToNurbsCurve() takes no arguments (2 given)
Traceback:
line 267, in createBolt, “C:\Users\KNAPP\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts\discourse\addBolt_v0.7.py”
line 435, in , “C:\Users\KNAPP\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts\discourse\addBolt_v0.7.py”

ah, that’s a pity, indeed it is not supported in V5 it seems. So I might have to replace it with converting to nurbs curve first and then rebuild.

Yes that seems to work here:

        hexagon = Rhino.Geometry.Circle(plane2, radius)
        hexagon = hexagon.ToNurbsCurve()
        hexagon = hexagon.Rebuild(6, 1, False)

Hello Gijs,

I have just been told that when using Python 2 it will s a good idea to inherit specifically from object, ie class Bolt(object) so you can use ´new style objets’. I haven’t experimented with this in Rhino because my Rhino programming is not object based and my OOP programming uses modern Python but I thought I would pass it on.

Best,

Graham

I don’t understand what this means.
Edit: I was trying to find what it means but can’t find any other info than this being ‘new style’. Are there some things I am missing then if I don’t inherit from object? Will some things go wrong if I don’t?

Good question!

I think that the main reasons to do this are:

  • To improve compatibility between Python 2 code (in Rhino) and modern Python code elsewhere,
  • To reduce the risks of breaking things whenever Rhino moves to Python 3, particularly where multiple inheritance is involved.
  • To allow certain optimisations such as the use of __slots__ to save memory.

Everything in Python is a reference to an object. Your flatBolt objects know about their pitch attribute because they inherit that functionality from your Bolt objects. They know how to store attributes and methods because they inherit this functionality from object. At some point this inheritance functionality was cleaned up and the object class was created to make this more explicit and allow you to modify fundamental things like where your class and instance data are stored. Using __slots__ with a very large number of bolt objects could theoretically save you lots of memory.

You can see a small example of the changed functionality by adding this code to AddBoltv7:

def test_bolt():
    print flatBolt.__mro__

if __name__ == "__main__":
    test_bolt()

Wtih class Bolt(): on line 42 you get an exception:

Message: ‘classobj’ object has no attribute ‘mro

Traceback:
line 438, in test_bolt, “C:\Users\KNAPP\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts\discourse\addBolt_v0.7.py”
line 441, in , “C:\Users\KNAPP\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts\discourse\addBolt_v0.7.py”

With class Bolt(object): you can see the method resolution order, i.e. that Python first looks for attributes and methods in the flatBolt class, then in Bolt, then in object.

(<class ‘main.flatBolt’>, <class ‘main.Bolt’>, <type ‘object’>)

1 Like

There is more on this on page 351 of Fluent Python

https://evanli.github.io/programming-book-3/Python/

1 Like

Violent Python sounds intriguing :open_mouth:

Hi guys, i try this script to create a M7 20mm DIN912… not possible. Only M6 or M8 :disappointed_relieved:

@zzzmutations you can easily add new sizes to the script. This was just a piece of code to learn working with classes in python.
Just add the M7 sizes of bolt diameter, pitch, head size etc to the dictionaries and it will work.
There is also BoltGen that can add bolts with thread to your doc. Or download bolts and nuts from McMaster

Could it be expanded to more Metric sizes above M8? For example, I regularly use M10, M12, M14 in my projects. :slight_smile:

Yes as mentioned this can be done very easily by adding those sizes to the dictionaries in the script. In the beginning of the script you see diameter and pitch sizes and each bolt class has head sizes and/ or hex socket sizes.
You can also send me those measurements then I’ll add those to the script.

Sure, here are the 3d models of most commonly used Metric screws that “BoltGen” creates. I opted for the versions without a thread to reduce the file size. I put a length of 50 mm to each, thought I use different lengths depending on the application.
M6, M8, M10, M12, M14 and M16.3dm (479.9 KB)

no, what I need is info on pitch size and head dimensions. Better if you can provide a table with all dimensions

See if this is helpful: