def test_clean(self): c = JSONCatalogue(self.filename) self.populate_catalogue(c) self.assertGreater(len(c.items.all()), 0) c.close() c = JSONCatalogue(self.filename, clean=True) # should clear catalogue self.assertEquals(len(c.items.all()), 0)
def get_components(self): # Find minimum neck length (total effect length, minus last effect) neck_length_min = abs(self.evaluator.eval[-1].start_point - self.evaluator.eval[0].start_point) thread_length_max = abs(self.evaluator.eval[-1].end_point - self.evaluator.eval[-1].start_point) # Get the catalogue of available items catalogue = JSONCatalogue(catalogue_filename) item = catalogue.get_query() # Find viably sized wood-screw screw_item = sorted( catalogue.search( # eval sets minimum evaluation length (item.obj.params.neck_length >= neck_length_min) & # thread shouldn't pierce through last part (item.criteria.thread_length < thread_length_max)), # sort by shortest first key=lambda x: x['obj']['params']['neck_length'])[ 0] # first result; shortest screw return { 'screw': catalogue.deserialize_item(screw_item), 'anchor': catalogue.get( item.id == screw_item['criteria']['compatible_anchor']), }
def add_catalogue(filename): catalogue = JSONCatalogue(filename) cls = testlabel('catalogue')(CatalogueTest.create_from(catalogue)) globals()[cls.__name__] = cls
# ---------- 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)
origin=(0, 0, 25), # 25mm above panel1 surface xDir=(0, -1, 0) # rotate so anchor faces inside ), ), ] # ------------------- Catalogue ------------------- from cqparts.catalogue import JSONCatalogue import tempfile # Temporary catalogue (just for this script) catalogue_filename = tempfile.mkstemp()[1] #catalogue_filename = '/tmp/db.json' catalogue = JSONCatalogue(catalogue_filename) # Add screws to catalogue # note: this is the kind of information you'd store in a csv # file, then import with a script similar to this one, to convert that # information to a Catalogue. screws = [ { 'id': 'screw_30', 'obj_params': { # parameters to WoodScrew constructor 'neck_exposed': 5, 'length': 40, # exposing 10mm of thread 'neck_length': 30, }, 'criteria': { 'type': 'screw',
# Threads from cqparts_fasteners.solidtypes import threads # Commandline arguments parser = argparse.ArgumentParser(description="Generate a test catalogue") parser.add_argument('out', type=str, nargs='?', default='thread_catalogue.json', help='filename to output') 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)
from cqparts.utils.test import CatalogueTest from cqparts.catalogue import JSONCatalogue catalogue = JSONCatalogue('test-files/catalogue.json') CatalogueTest.create_from(catalogue, add_to=globals())
def get_catalogue(self): return JSONCatalogue(self.filename)
sys.path.append('..') import partslib # Commandline arguments parser = argparse.ArgumentParser(description="Generate a test catalogue") parser.add_argument('out', type=str, nargs='?', default='catalogue.json', help='filename to output') args = parser.parse_args() catalogue = JSONCatalogue(args.out, clean=True) # -------- Add catalogue items box_data = [ ('box_a', { 'length': 10, 'width': 10, 'height': 10 }), ('box_b', { 'length': 20, 'width': 20, 'height': 20 }), ('box_c', {
import unittest from base import testlabel from cqparts.utils.test import CatalogueTest from cqparts.catalogue import JSONCatalogue catalogue = JSONCatalogue('test-files/thread_catalogue.json') cls = testlabel('complex_thread')(CatalogueTest.create_from(catalogue)) # FIXME: when #1 is fixed, remove this so tests are not permenantly skipped cls = unittest.skip('skipped until #1 is fixed')(cls) globals()[cls.__name__] = cls
}, 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'], }, )
from cqparts.display import display from cqparts.constraint import Fixed, Coincident from cqparts.constraint import Mate from cqparts.utils import CoordSystem from cqparts.catalogue import JSONCatalogue import cqparts_motors import os from .motor_mount import MountedStepper from .stepper import Stepper filename = os.path.join(os.path.dirname(cqparts_motors.__file__), "catalogue", "stepper-nema.json") catalogue = JSONCatalogue(filename) item = catalogue.get_query() steppers = catalogue.iter_items() stepper_list = [] for i in steppers: s = catalogue.deserialize_item(i) cl = type(str(i["id"]), (Stepper, ), {}) p = cl.class_params(hidden=True) for j, k in p.items(): pr = type(k) setattr(cl, j, pr(i["obj"]["params"][j])) stepper_list.append(cl) class StepperCat(cqparts.Assembly): def initialize_parameters(self):