def setUp(self): self.dxf4 = DXFGeometry('./DXFTests/DXFTest4.dxf', testing=True) self.ampoule = DXFGeometry('./DXFTests/Ampoule2.dxf') self.directory = './DXFTests/' for f in ['flow.ctrl', 'flow.out', 'flow.mshc', 'mesh_plot.eps', 'memory.dump']: try: os.remove(self.directory+f) except OSError: pass
def test_dxf_intersection_3(self): '''IntersectTest3.dxf test''' dxf = DXFGeometry('./DXFTests/IntersectTest3.dxf') inters = lineintersect.find_intersections(dxf.verts.vertices) check = [i.type for i in inters] == [2, 1, 4] msg = '''Intersections are incorrect for test''' self.assertTrue(check, msg)
def test_dxf_intersection_2(self): '''IntersectTest2.dxf test''' dxf = DXFGeometry('./DXFTests/IntersectTest2.dxf') inters = lineintersect.find_intersections(dxf.verts.vertices, tol=.05) #print('IntersectTest2.dxf intersections:') types = [2, 3, 3, 3, 2, 3, 1, 1, 1, 1, 3, 2, 2, 3] coords = [(((-8.0, -16.0), (-8.0, 8.0)), ((8.0, -16.0), (-8.0, -16.0))), (((-8.0, -12.0), (8.0, -12.0)), ((8.0, -16.0), (8.0, 8.0))), (((-8.0, -12.0), (8.0, -12.0)), ((-8.0, 8.0), (-8.0, -16.0))), (((-8.0, 4.0), (8.0, 4.0)), ((-8.0, 8.0), (-8.0, -16.0))), (((-8.0, 8.0), (8.0, 8.0)), ((-8.0, -16.0), (-8.0, 8.0))), (((-7.98, -4.0), (7.98, -4.0)), ((-8.0, -16.0), (-8.0, 8.0))), (((-4.0, 9.0), (11.0, -16.98076211)), ((8.0, -16.0), (8.0, 8.0))), (((-4.0, 9.0), (11.0, -16.98076211)), ((-7.98, -4.0), (7.98, -4.0))), (((-4.0, 9.0), (11.0, -16.98076211)), ((-8.0, 4.0), (8.0, 4.0))), (((-4.0, 9.0), (11.0, -16.98076211)), ((-8.0, 8.0), (8.0, 8.0))), (((8.0, -16.0), (8.0, 8.0)), ((-8.0, 4.0), (8.0, 4.0))), (((8.0, -16.0), (8.0, 8.0)), ((-8.0, 8.0), (8.0, 8.0))), (((8.0, -16.0), (8.0, 8.0)), ((-8.0, -16.0), (8.0, -16.0))), (((8.0, -16.0), (8.0, 8.0)), ((-7.98, -4.0), (7.98, -4.0)))] for k, i in enumerate(inters): check = i.lines == coords[k] msg = 'intersections don\'t match. Standard: {}, Output: {}'.format(coords[k], i.lines) self.assertTrue(check, msg) check = i.type == types[k] msg = 'intersection types don\'t match. Standard: {}, Output: {}'.format(types[k], i.type) self.assertTrue(check, msg)
class DXFtoCrysMASTests(unittest.TestCase): '''Test cases for converting a DXF geometry into a CrysMAS .pcs file''' def setUp(self): self.dxf = DXFGeometry('./DXFTests/DXFTest_Clamshellv5.dxf') def tearDown(self): self.dxf = None def test_DXFGeomtoCrys(self): '''Convert DXF geometry to CrysMAS .pcs file''' standard = './DXFTests/DXFTest_Clamshellv5.pcs.const' self.dxf.output_to_crysmas(f_name='Testing') test_file = './DXFTests/Testing.pcs' check = filecmp.cmp(test_file, standard) msg = 'File information differs between {} and {}'.format(standard, test_file) self.assertTrue(check, msg)
def test_DXFs(self): '''DXF geometries are same as saved standards''' for i in range(self.num_tests): dxf = DXFGeometry('{0}DXFTest{1}.dxf'.format(self.test_path, i+1), testing=True) standard = open('{0}DXFTest{1}_segments.set'.format(self.test_path, i+1), 'rb') std_segs = pickle.load(standard) check = dxf.segments == std_segs msg = '''DXFTest{} Geometry does not match standard geometry'''.format(i) self.assertTrue(check, msg)
class DXFtoCats2DTests(unittest.TestCase): '''Test cases for converting a DXF geometry into a Cats2D mesh''' def setUp(self): self.dxf4 = DXFGeometry('./DXFTests/DXFTest4.dxf', testing=True) self.ampoule = DXFGeometry('./DXFTests/Ampoule2.dxf') self.directory = './DXFTests/' for f in ['flow.ctrl', 'flow.out', 'flow.mshc', 'mesh_plot.eps', 'memory.dump']: try: os.remove(self.directory+f) except OSError: pass def tearDown(self): self.dxf4 = None self.ampoule = None for f in ['flow.ctrl', 'flow.out', 'flow.mshc', 'mesh_plot.eps', 'memory.dump']: try: os.remove(self.directory+f) except OSError: pass def test_DXFGeomtoCats(self): '''Convert DXF geometry for creating Cats2D mesh''' standard = open('./DXFTests/DXFTest4_cats2d.pick', 'rb') pick_info = pickle.load(standard) check = self.dxf4.cats2d_convert(invert_coords=False) == pick_info msg = 'Cats2D information for DXFTest4 did not match the saved standard' self.assertTrue(check, msg) def test_DXFtoCatsMesh(self): standard = './DXFTests/flow.mshc.const' test_file = './DXFTests/flow.mshc' vertex_list,edge_list,bulge_list = self.ampoule.cats2d_convert(invert_coords=True, len_scale=6) mesh = c2d_premesh_v5.C2DMesh(vertex_list, edge_list) cats2d_path = 'cats2d.x' pexpect_c2dmesh_v2.make_c2d_mesh(mesh, cats2d_path, working_dir=self.ampoule.work_dir) check = filecmp.cmp(test_file, standard) msg = 'File information differs between {} and {}'.format(standard, test_file)
def test_dxf_intersection_1(self): '''IntersectTest1.dxf test''' dxf = DXFGeometry('./DXFTests/IntersectTest1.dxf') inters = lineintersect.find_intersections(dxf.verts.vertices, tol=0.6) #print('IntersectTest1.dxf intersections:') types = [1, 3, 1, 3, 4] coords = [(((-8.0, -2.0), (12.0, -2.0)), ((-10.0, -5.0), (21.17691454, 13.0))), (((-8.0, 8.0), (12.0, 8.0)), ((21.17691454, 13.0), (-10.0, -5.0))), (((1.5, 3.0), (4.96410162, 1.0)), ((-10.0, -5.0), (21.17691454, 13.0))), (((17.90646664, 11.11180602), (21.17691454, 5.50880909)), ((-10.0, -5.0), (21.17691454, 13.0))), (((21.17691454, 5.50880909), (22.49452864, 3.25144618)), ((17.90646664, 11.11180602), (21.17691454, 5.50880909)))] for k, i in enumerate(inters): check = i.lines == coords[k] msg = 'intersections don\'t match. Standard: {}, Output: {}'.format(coords[k], i.lines) self.assertTrue(check, msg) check = i.type == types[k] msg = 'intersection types don\'t match. Standard: {}, Output: {}'.format(types[k], i.type) self.assertTrue(check, msg)
def test_suite(verbose=False, dxf_test=True): '''Runs the test suite''' if verbose: verbosity = 2 else: verbosity = 1 suites = [] suites.append(unittest.TestLoader().loadTestsFromTestCase(DXFTestSuite.TestVertex)) suites.append(unittest.TestLoader().loadTestsFromTestCase(DXFTestSuite.TestVertexList)) suites.append(unittest.TestLoader().loadTestsFromTestCase(DXFTestSuite.LineIntersectTests)) if dxf_test: suites.append(unittest.TestLoader().loadTestsFromTestCase(DXFTestSuite.TestDXFGeometry)) suites.append(unittest.TestLoader().loadTestsFromTestCase(DXFTestSuite.DXFTestCases)) suites.append(unittest.TestLoader().loadTestsFromTestCase(DXFTestSuite.DXFtoCats2DTests)) suites.append(unittest.TestLoader().loadTestsFromTestCase(DXFTestSuite.DXFtoCrysMASTests)) dxf1 = DXFGeometry('./DXFTests/DXFTest1.dxf', testing=True) dxf1.display() dxf2 = DXFGeometry('./DXFTests/DXFTest2.dxf', testing=True) dxf2.display() dxf3 = DXFGeometry('./DXFTests/DXFTest3.dxf', testing=True) dxf3.display() dxf4 = DXFGeometry('./DXFTests/DXFTest4.dxf', testing=True) dxf4.display() print alltests = unittest.TestSuite(suites) unittest.TextTestRunner(verbosity=verbosity).run(alltests) # Show the plotted DXF file if available plt.show()
# Specify testing mode from the command line if args.dxf_file == 'test': # Create new standards if specified if args.newstandard: print('Creating new standards for tests') CreateStandards.save_standards(args, num_tests=4) else: test_suite(verbose=args.verbose, dxf_test=not(args.nodxf)) # Exit immediately after testing is complete sys.exit() # Create distribution from the command line elif args.dxf_file == 'dist': make_dist() # Otherwise create a DXF geometry object else: dxf = DXFGeometry(args.dxf_file, verbose=args.verbose) # Create a CrysMAS file if args.crysmas: if args.crysmas != True: crys_file = args.crysmas else: crys_file = None dxf.output_to_crysmas(dxf_units=args.units, f_name=crys_file) # Create a Cats2D mesh if args.cats2d: if args.c2dplot: plotting_args = {'plotting':True, 'plotting_pause':float(args.c2dplot)} else: plotting_args = {}
def test_suite(verbose=False, dxf_test=True): '''Runs the test suite''' if verbose: verbosity = 2 else: verbosity = 1 suites = [] suites.append(unittest.TestLoader().loadTestsFromTestCase( DXFTestSuite.TestVertex)) suites.append(unittest.TestLoader().loadTestsFromTestCase( DXFTestSuite.TestVertexList)) suites.append(unittest.TestLoader().loadTestsFromTestCase( DXFTestSuite.LineIntersectTests)) if dxf_test: suites.append(unittest.TestLoader().loadTestsFromTestCase( DXFTestSuite.TestDXFGeometry)) suites.append(unittest.TestLoader().loadTestsFromTestCase( DXFTestSuite.DXFTestCases)) suites.append(unittest.TestLoader().loadTestsFromTestCase( DXFTestSuite.DXFtoCats2DTests)) suites.append(unittest.TestLoader().loadTestsFromTestCase( DXFTestSuite.DXFtoCrysMASTests)) dxf1 = DXFGeometry('./DXFTests/DXFTest1.dxf', testing=True) dxf1.display() dxf2 = DXFGeometry('./DXFTests/DXFTest2.dxf', testing=True) dxf2.display() dxf3 = DXFGeometry('./DXFTests/DXFTest3.dxf', testing=True) dxf3.display() dxf4 = DXFGeometry('./DXFTests/DXFTest4.dxf', testing=True) dxf4.display() print alltests = unittest.TestSuite(suites) unittest.TextTestRunner(verbosity=verbosity).run(alltests) # Show the plotted DXF file if available plt.show()
# Specify testing mode from the command line if args.dxf_file == 'test': # Create new standards if specified if args.newstandard: print('Creating new standards for tests') CreateStandards.save_standards(args, num_tests=4) else: test_suite(verbose=args.verbose, dxf_test=not (args.nodxf)) # Exit immediately after testing is complete sys.exit() # Create distribution from the command line elif args.dxf_file == 'dist': make_dist() # Otherwise create a DXF geometry object else: dxf = DXFGeometry(args.dxf_file, verbose=args.verbose) # Create a CrysMAS file if args.crysmas: if args.crysmas != True: crys_file = args.crysmas else: crys_file = None dxf.output_to_crysmas(dxf_units=args.units, f_name=crys_file) # Create a Cats2D mesh if args.cats2d: if args.c2dplot: plotting_args = { 'plotting': True, 'plotting_pause': float(args.c2dplot)
def setUp(self): self.dxf = DXFGeometry('./DXFTests/DXFTest_Clamshellv5.dxf')
def setUp(self): # Open DXF file for all tests self.test_dxf = dxfgrabber.readfile('./DXFTests/DXFTest2.dxf') self.empty_dxfgeom = DXFGeometry('empty_test', testing=True)
class TestDXFGeometry(unittest.TestCase): '''\tTest case for the DXFGeometry class''' def setUp(self): # Open DXF file for all tests self.test_dxf = dxfgrabber.readfile('./DXFTests/DXFTest2.dxf') self.empty_dxfgeom = DXFGeometry('empty_test', testing=True) def tearDown(self): # Get rid of DXF file self.test_dxf = None self.empty_dxfgeom = None def check_vertices(self, tup, dxfgeom, entity): '''vertices are in vertex list and they are connected''' tol = dxfgeom.tol start = (approx(tup[0][0], tol), approx(tup[0][1],tol)) end = (approx(tup[1][0], tol), approx(tup[1][1],tol)) # Check to make sure start and end are vertices now check = (start in dxfgeom.verts.coordinates) and (end in dxfgeom.verts.coordinates) msg = '{} and {} were not added to vertices'.format(start, end) self.assertTrue(check, msg) # Make sure vertices are connected check = end in dxfgeom.verts.vertices[start].connections msg = '{} not properly connected by line {}'.format(end, entity) self.assertTrue(check, msg) def test_add_line(self): '''add a line to the geometry''' # Loop through entities and add only lines tol = self.empty_dxfgeom.tol for e in self.test_dxf.entities: if e.dxftype == 'LINE': self.empty_dxfgeom.add_line(e) start = (approx(e.start[0], tol=tol), approx(e.start[1], tol=tol)) end = (e.end[0], e.end[1]) self.check_vertices((start, end), self.empty_dxfgeom, e) def test_add_arc(self): '''add an arc to the geometry''' # Loop through entities and add only arcs for e in self.test_dxf.entities: if e.dxftype == 'ARC': self.empty_dxfgeom.add_arc(e) tol = self.empty_dxfgeom.tol start_angle = math.radians(e.startangle) end_angle = math.radians(e.endangle) center = (approx(e.center[0], tol=tol), approx(e.center[1], tol=tol)) radius = e.radius # Calculate bulge and start/stop information theta = ccw_angle_diff(start_angle, end_angle) bulge = math.tan(theta/4) start = (approx(radius*math.cos(start_angle) + center[0], tol=tol), approx(radius*math.sin(start_angle) + center[1], tol=tol)) end = (approx(radius*math.cos(end_angle) + center[0], tol=tol), approx(radius*math.sin(end_angle) + center[1], tol=tol)) self.check_vertices((start, end), self.empty_dxfgeom, e) def test_add_polyline(self): '''add a polyline to the geometry''' # Loop through entities and add only arcs tol = self.empty_dxfgeom.tol for e in self.test_dxf.entities: if e.dxftype == 'POLYLINE': self.empty_dxfgeom.add_polyline(e) for i, p in enumerate(e.points): try: p_next = e.points[i+1] except IndexError: if e.is_closed: p_next = e.points[0] else: break start = (approx(p[0], tol=tol), approx(p[1], tol=tol)) end = (approx(p_next[0], tol=tol), approx(p_next[1], tol=tol)) self.check_vertices((start, end), self.empty_dxfgeom, e) def test_add_entities(self): '''add entites from a DXF file''' # Add entities self.empty_dxfgeom.add_entities(self.test_dxf.entities) # There should be 18 lines, arcs, and polylines in the file num_ents = 18 num_added = len(self.empty_dxfgeom.segments) check = num_added == num_ents msg = '''{} entities were added but {} addable entities exist in test file '''.format(num_added, num_ents) self.assertTrue(check, msg) def test_move_vertex(self): '''move a vertex''' # Lines to be added lines = set([(( (0.,0.), (1.,0.) ), ()), (( (0.,0.), (1.,1.) ), ()), (( (-1.,-1.), (0.,0.) ), ())]) # Add lines to geometry self.empty_dxfgeom.segments = lines for l in lines: self.empty_dxfgeom.verts.add(l[0][0]) self.empty_dxfgeom.verts.add(l[0][1]) self.empty_dxfgeom.verts.connect(l[0][0], l[0][1]) # Move the (0,0) vertex self.empty_dxfgeom.move_vertex((0,0), (0.5, 0.5)) # Check whether it worked new_lines = set([(( (0.5,0.5), (1,0) ), ()), (( (0.5,0.5), (1,1) ), ()), (( (-1,-1), (0.5,0.5) ), ())]) check = new_lines == self.empty_dxfgeom.segments msg = '''Segment list was not updated correctly after vertex move''' self.assertTrue(check, msg) def test_rem_reversed(self): '''remove reversed lines and arcs''' # Lines to be added lines = set([(( (0,0), (1,0) ), ()), (( (1,0), (0,0) ), ()), (( (0,0), (1,1) ), ())]) # Arcs to be added arcs = set([]) bulges = [1, -1, 1] for l, b in zip(lines, bulges): start = l[0][0] end = l[0][1] segment = bulge_to_arc(start, end, b) arcs.add(segment) # Lines that are not duplicates: true_lines = set([(( (0,0), (1,0) ), ()), (( (0,0), (1,1) ), ())]) # Arcs that are not duplicates: true_arcs = set([bulge_to_arc((0,0), (1,0), -1), bulge_to_arc((0,0), (1,1), 1)]) # Add all lines and vertices self.empty_dxfgeom.segments |= lines | arcs for l in lines: self.empty_dxfgeom.verts.add(l[0][0]) self.empty_dxfgeom.verts.add(l[0][1]) self.empty_dxfgeom.verts.connect(l[0][0], l[0][1]) # Remove reversed line and arc self.empty_dxfgeom.rem_reversed() # Check whether it worked check = self.empty_dxfgeom.segments == true_lines | true_arcs msg = '''Extra line that should have been removed: {} '''.format(self.empty_dxfgeom.segments - true_lines | true_arcs) self.assertTrue(check, msg)