gumyr / cq_warehouse

A cadquery parametric part collection
Apache License 2.0
107 stars 23 forks source link

Custom bearing sizes #80

Open cyberhuman opened 1 year ago

cyberhuman commented 1 year ago

Hello! First of all, thank you for this great piece of work!

I wanted to use it but the size of the bearing I want to use in my design (M5-14-5) is not in the database. How do I extend the database so my CQ project remains portable? The documentation says:

See Extending the fastener sub-package (these instructions apply to bearings as well) for guidance on how to easily add new sizes or entirely new types of bearings.

and then

As mentioned previously, the data used to guide the creation of fastener objects is derived from .csv files found in the same place as the source code. One can add to the set of standard sized fasteners by inserting appropriate data into the tables.

So am I supposed to modify the source code of cq_warehouse? I've checked the code and it seems the only place the file(s) are read from is the package's directory: https://github.com/gumyr/cq_warehouse/blob/56fa36e2480fc510ac5dd3e60873bdb9797e9abe/src/cq_warehouse/fastener.py#L63

Do I miss anything? Any suggestions how to solve this issue? Thank you!

gumyr commented 1 year ago

There are an almost unlimited number of possible bearings out there so not all of them are modelled. As you quote above, all of the bearings are created from data files which you can customize to create the size you need. First you need to know what type of bearing and map that to a .csv file - e.g. single_row_deep_groove_ball_bearing_parameters.csv. If you open this file you'll see this: image which is the data that is used to create the bearing. Just add a new row for M5-14-5 with the appropriate dimensions and save the file back to where pip installed it and that new bearing will be available to you.

cyberhuman commented 1 year ago

@gumyr thanks, that's what I understood. But that makes my project not portable :-( What happens if I want to open it on my other workstation, or to share it with someone? And then my changes will be lost if I reinstall or update the package.

gumyr commented 1 year ago

I'm sorry that cq_warehouse isn't suitable for your needs.

cyberhuman commented 1 year ago

Maybe it's possible to make it possible to construct instances of bearings, screws etc by providing their parameters directly?

cyberhuman commented 1 year ago

I solved my issue using the following code, borrowed and modified from the cq_warehouse's source code:

def read_fastener_parameters_from_csv(filename: str) -> dict:
    """Parse a csv parameter file into a dictionary of strings"""
    parameters = {}
    with open(filename, mode='r', newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        fieldnames = reader.fieldnames
        for row in reader:
            key = row[fieldnames[0]]
            row.pop(fieldnames[0])
            parameters[key] = row
    return parameters

class CustomSingleRowDeepGrooveBallBearing(SingleRowDeepGrooveBallBearing):
    """Custom Single Row Deep Groove Ball Bearing"""
    bearing_data = read_fastener_parameters_from_csv("bearing.csv")