def test_json_asset(self): writer = threemf.io.Writer() reader = threemf.io.Reader() reader.register_extension(TestExtension) with io.BytesIO() as f: writer.write(self.tmf, f) zip_bytes = f.getvalue() tmf2 = threemf.ThreeMF() with io.BytesIO(zip_bytes) as f: reader.read(tmf2, f) self.assertEqual(len(tmf2.extensions), 1) self.assertEqual(len(tmf2.extensions[0].assets), 1) asset = tmf2.extensions[0].assets[0] self.assertTrue(isinstance(asset.content, dict)) self.assertEqual(asset.content['name'], "my extension's file") self.assertEqual(asset.content['mass'], 258.1) self.assertTrue('num_tris' in asset.content.keys()) self.assertEqual(asset.content['num_tris'], 12)
def _read_3mf_bytes(self, tmf_bytes) -> threemf.ThreeMF: tmf = threemf.ThreeMF() rdr = threemf.io.Reader() rdr.register_extension(pywim.smartslice.ThreeMFExtension) rdr.read(tmf, tmf_bytes) return tmf
def read_threemf(tmf_path) -> threemf.ThreeMF: tmf = threemf.ThreeMF() tmf_reader = threemf.io.Reader() tmf_reader.register_extension(pywim.smartslice.ThreeMFExtension) with open(tmf_path, 'rb') as tmf_bytes: tmf_reader.read(tmf, tmf_bytes) return tmf
def setUp(self): self.tmf = threemf.ThreeMF() cura_ext = threemf.extension.Cura() cura_asset = threemf.extension.RawFile('printer.def.json') cura_asset.content = json.dumps({'test': 100}) cura_ext.assets.append(cura_asset) self.tmf.extensions.append(cura_ext)
def write_and_read(tmf: threemf.ThreeMF) -> threemf.ThreeMF: writer = threemf.io.Writer() reader = threemf.io.Reader() with io.BytesIO() as f: writer.write(tmf, f) zip_bytes = f.getvalue() tmf2 = threemf.ThreeMF() with io.BytesIO(zip_bytes) as f: reader.read(tmf2, f) return tmf2
def test_cube_in_cube(self): self.tmf = threemf.ThreeMF() c1 = threemf.geom.Cube(100., 100., 5.) c2 = threemf.geom.Cube(20., 20., 3.) self.cube1 = self.tmf.default_model.object_from_stl(c1.stl_mesh()) self.cube2 = self.tmf.default_model.object_from_stl(c2.stl_mesh()) cos45 = 0.7071 sin45 = 0.7071 self.componentT = np.array( [ [cos45, -sin45, 0.0, 40.0], [sin45, cos45, 0.0, 40.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 1.0] ] ) self.cube1.add_component(self.cube2, self.componentT) self.tmf.default_model.build.add_item(self.cube1) tmf2 = write_and_read(self.tmf) objs = tmf2.default_model.objects build = tmf2.default_model.build self.assertEqual(len(objs), 2) self.assertEqual(len(build.items), 1) main_obj_id = build.items[0].objectid self.assertEqual(main_obj_id, self.cube1.id) main_obj = next(o for o in objs if o.id == main_obj_id) self.assertEqual(len(main_obj.components), 1) component = main_obj.components[0] self.assertEqual(component.objectid, self.cube2.id) self.assertTrue(np.array_equal(component.transform, self.componentT))
def _create_3mf(self): tmf = threemf.ThreeMF() mdl = tmf.default_model mesh = stl_loader.load_from_file('cube.stl') obj = mdl.object_from_stl(mesh) mdl.build.add_item(obj) ss = pywim.smartslice.ThreeMFExtension() asset = ss.make_asset('job.json') job = asset.content ss.assets.append(asset) tmf.extensions.append(ss) return tmf
def setUp(self): self.tmf = threemf.ThreeMF() mdl = self.tmf.default_model c = threemf.geom.Cube(10., 10., 5.) self.cube = self.tmf.default_model.object_from_stl(c.stl_mesh()) self.tmf.default_model.build.add_item(self.cube) self.ext = TestExtension() jasset = threemf.extension.JsonFile('test.json') jasset.content = { 'name': 'my extension\'s file', 'mass': 258.1 } self.ext.assets.append(jasset) self.tmf.extensions.append(self.ext)
def setUp(self): self.tmf = threemf.ThreeMF() c = threemf.geom.Cube(100., 100., 5.) self.cube = self.tmf.default_model.object_from_stl(c.stl_mesh()) self.cube.add_meta_data_cura('infill_pattern', 'grid') self.cube.add_meta_data_cura('infill_sparse_density', 50) self.cubeT = np.array( [ [0.3, 0.7, 0.0, 10.], [0.4, 0.5, 0.1, 11.], [0.2, 0.2, 0.6, 12.], [0.0, 0.0, 0.0, 1.0] ] ) self.tmf.default_model.build.add_item(self.cube, self.cubeT)
def extractSmartSliceJobFrom3MF(self, file) -> pywim.smartslice.job.Job: tmf = threemf.ThreeMF() tmf_reader = threemf.io.Reader() tmf_reader.register_extension(pywim.smartslice.ThreeMFExtension) tmf_reader.read(tmf, file) if len(tmf.extensions) != 1: raise Exception('3MF extension count is not 1') ext = tmf.extensions[0] job_assets = list( filter(lambda a: isinstance(a, pywim.smartslice.JobThreeMFAsset), ext.assets) ) if len(job_assets) == 0: raise SmartSliceCloudJob.JobException('Could not find smart slice information in 3MF') return job_assets[0].content
def test_recover_assets(self): writer = threemf.io.Writer() reader = threemf.io.Reader() cura_ext = reader.register_extension(threemf.extension.Cura) with io.BytesIO() as f: writer.write(self.tmf, f) zip_bytes = f.getvalue() tmf2 = threemf.ThreeMF() with io.BytesIO(zip_bytes) as f: reader.read(tmf2, f) self.assertEqual(len(tmf2.extensions), 1) self.assertEqual(cura_ext.Name, 'Cura') self.assertEqual(len(cura_ext.assets), 1) content = json.loads(cura_ext.assets[0].content.decode('utf-8')) self.assertEqual(len(content.keys()), 1) self.assertEqual(content['test'], 100)