# ---------- Component Builders ---------- # TODO: build your own objects in whatever way suits your application. def build_obj(cls, **kwargs): # Take any parameters relevent to the given class from kwargs # (all other parameters ignored) class_params = cls.class_params() obj_kwargs = { key: kwargs.pop(key, param.default) for (key, param) in class_params.items() } return cls(**obj_kwargs) # ---------- Create Catalogue ---------- catalogue = JSONCatalogue(args.output, clean=True) with open(args.input, "rb" ) as csv_fileio: csv_reader = csv.DictReader(csv_fileio) for line in csv_reader: obj = build_obj(ClothesPeg, **line) criteria = { key: line[key] for key in line.keys() if (not hasattr(obj, key)) and (key not in ('ID',)) } catalogue.add(id=line['ID'], obj=obj, criteria=criteria)
'obj_params': { 'neck_exposed': 6, 'length': 65, # exposing 15mm of thread 'neck_length': 50, }, 'criteria': { 'type': 'screw', 'thread_length': 15, 'compatible_anchor': 'anchor_15', }, }, ] for screw in screws: obj = WoodScrew(**screw['obj_params']) catalogue.add(id=screw['id'], criteria=screw['criteria'], obj=obj) # Add anchors to catalogue anchors = [ { 'id': 'anchor_10', 'obj_params': { # parameters to WoodScrew constructor 'diameter': 10, 'height': 7, }, 'criteria': {'type': 'anchor'}, }, { 'id': 'anchor_15', 'obj_params': { # parameters to WoodScrew constructor 'diameter': 15,
args = parser.parse_args() catalogue = JSONCatalogue(args.out, clean=True) # -------- Add catalogue items names = ['ball_screw', 'iso68', 'triangular'] lengths = [1, 2, 4, 10] diameters = [1, 3, 4, 5] for (i, (name, length, diam)) in enumerate(itertools.product(names, lengths, diameters)): thread = threads.find(name=name)( _simple=False, length=length, diameter=diam, ) print("adding: %r" % thread) catalogue.add( "%s_%gx%g_%03i" % (name, length, diam, i), thread, criteria={ 'type': name, 'length': length, 'diam': diam, }, ) # close catalogue after additions catalogue.close()
}), ('box_b', { 'length': 20, 'width': 20, 'height': 20 }), ('box_c', { 'length': 10, 'width': 20, 'height': 30 }), ] for (idstr, params) in box_data: obj = partslib.Box(**params) print("adding: %r" % obj) catalogue.add(id=idstr, obj=obj, criteria={}) cylinder_data = [ ('cyl_a', { 'length': 5, 'radius': 3 }), ('cyl_b', { 'length': 10, 'radius': 0.5 }), ('cyl_c', { 'length': 4, 'radius': 10 }), ]
}, 23: { 'shaft_length': 21.0, 'hole_spacing': 47.0, 'hole_size': 5.0, 'length': 56.0, 'width': 57.0, 'boss_size': 38.0, 'shaft_diam': 6.35, 'boss_length': 1.6, }, } # Generate Catalogue catalogue = JSONCatalogue( filename=os.path.join('..', CATALOGUE_NAME), clean=True, # starts fresh ) # Create Steppers for (size, params) in NEMA_SIZES.items(): catalogue.add( id='NEMA_Stepper_%i' % size, obj=Stepper(**params), criteria={ 'type': 'motor', 'class': 'stepper', 'size': size, }, )
"diam": 20.4, "shaft_length": 6, "cover_height": 0.0, "step_diam": 12.0, "thickness": 15.4, "bush_height": 0.4, "step_height": 0.0, "shaft_diam": 2.0, "bush_diam": 6.15, "height": 10 } } # Generate Catalogue catalogue = JSONCatalogue( filename=os.path.join('..', CATALOGUE_NAME), clean=True, # starts fresh ) # Create Motors for (name, params) in DC_HOBBY.items(): catalogue.add( id=name, obj=DCMotor(**params), criteria={ 'type': 'motor', 'class': 'dc', 'diam': params['diam'], }, )