def stl_to_nastran_filename(stl_filename, bdf_filename, nnodes_offset=0, nelements_offset=0, pid=100, mid=200, size=8, is_double=False, log=None): model = STLReader(log=log) model.read_stl(stl_filename) nid = nnodes_offset + 1 cid = None load_id = 10 nodal_normals = model.get_normals_at_nodes(model.elements) bdf = open(bdf_filename, 'wb') bdf.write('CEND\n') #bdf.write('LOAD = %s\n' % load_id) bdf.write('BEGIN BULK\n') nid2 = 1 magnitude = 100. if size == 8: print_card = print_card_8 elif size == 16: if is_double: print_card = print_card_16 else: print_card = print_card_double for x, y, z in model.nodes: card = ['GRID', nid, cid, x, y, z] bdf.write(print_card_16(card)) #nx, ny, nz = nodal_normals[nid2 - 1] #card = ['FORCE', load_id, nid, cid, magnitude, nx, ny, nz] #bdf.write(print_card_8(card)) nid += 1 nid2 += 1 eid = nelements_offset + 1 for (n1, n2, n3) in (model.elements + (nnodes_offset + 1)): card = ['CTRIA3', eid, pid, n1, n2, n3] bdf.write(print_card_8(card)) eid += 1 t = 0.1 card = ['PSHELL', pid, mid, t] bdf.write(print_card_8(card)) E = 1e7 G = None nu = 0.3 card = ['MAT1', mid, E, G, nu] bdf.write(print_card_8(card)) bdf.write('ENDDATA\n') bdf.close()
def stl_to_plot3d_filename(stl_filename, p3d_filename, log=None, ascii=True): model = STLReader(log=log) model.read_stl(stl_filename) #nodal_normals = model.get_normals_at_nodes(model.elements) p3d = open(p3d_filename, 'wb') nblocks = len(model.elements) #nblocks = 10 p3d.write('%i\n' % nblocks) for iblock in xrange(nblocks): p3d.write('2 2 1\n') nodes = model.nodes elements = model.elements if 0: for i in [0, 1, 2]: for iblock in xrange(nblocks): (n1, n2, n3) = elements[iblock] p1 = nodes[n1, :] p2 = nodes[n2, :] p3 = nodes[n3, :] p4 = p3 xi = [[p1[i], p2[i], p3[i], p4[i]]] savetxt(p3d, xi, fmt='%f') else: for iblock in xrange(nblocks): for i in [0, 1, 2]: (n1, n2, n3) = elements[iblock] p1 = nodes[n1, :] p2 = nodes[n2, :] p3 = nodes[n3, :] p4 = p3 xi = [[p1[i], p2[i], p3[i], p4[i]]] savetxt(p3d, xi, fmt='%f') #p3d.write("----\n") #x = [[p1[0], p2[0], p3[0], p4[0]]] #y = [[p1[1], p2[1], p3[1], p4[1]]] #z = [[p1[2], p2[2], p3[2], p4[2]]] #savetxt(p3d, x, fmt='%f') #savetxt(p3d, y, fmt='%f') #savetxt(p3d, z, fmt='%f') #p3d.write('\n') #p3d.write(' '.join(x) + '\n') #p3d.write(' '.join(y) + '\n') #p3d.write(' '.join(z) + '\n') #xxxz #yyyz #zzzz p3d.close()
def stl_to_plot3d_filename(stl_filename, p3d_filename, log=None, ascii=True): model = STLReader(log=log) model.read_stl(stl_filename) #nodal_normals = model.get_normals_at_nodes(model.elements) p3d = open(p3d_filename, 'wb') nblocks = len(model.elements) #nblocks = 10 p3d.write('%i\n' % nblocks) for iblock in range(nblocks): p3d.write('2 2 1\n') nodes = model.nodes elements = model.elements if 0: for i in [0, 1, 2]: for iblock in range(nblocks): (n1, n2, n3) = elements[iblock] p1 = nodes[n1, :] p2 = nodes[n2, :] p3 = nodes[n3, :] p4 = p3 xi = [[p1[i], p2[i], p3[i], p4[i]]] savetxt(p3d, xi, fmt='%f') else: for iblock in range(nblocks): for i in [0, 1, 2]: (n1, n2, n3) = elements[iblock] p1 = nodes[n1, :] p2 = nodes[n2, :] p3 = nodes[n3, :] p4 = p3 xi = [[p1[i], p2[i], p3[i], p4[i]]] savetxt(p3d, xi, fmt='%f') #p3d.write("----\n") #x = [[p1[0], p2[0], p3[0], p4[0]]] #y = [[p1[1], p2[1], p3[1], p4[1]]] #z = [[p1[2], p2[2], p3[2], p4[2]]] #savetxt(p3d, x, fmt='%f') #savetxt(p3d, y, fmt='%f') #savetxt(p3d, z, fmt='%f') #p3d.write('\n') #p3d.write(' '.join(x) + '\n') #p3d.write(' '.join(y) + '\n') #p3d.write(' '.join(z) + '\n') #xxxz #yyyz #zzzz p3d.close()
def stl_to_nastran_filename(stl_filename, bdf_filename, nnodes_offset=0, nelements_offset=0, log=None): model = STLReader(log=log) model.read_stl(stl_filename) nid = nnodes_offset + 1 cid = None pid = 100 mid = 200 load_id = 10 nodal_normals = model.get_normals_at_nodes(model.elements) bdf = open(bdf_filename, 'wb') bdf.write('CEND\n') bdf.write('LOAD = %s\n' % load_id) bdf.write('BEGIN BULK\n') nid2 = 1 magnitude = 100. for (x, y, z) in model.nodes: card = ['GRID', nid, cid, x, y, z] bdf.write(print_card(card)) nx, ny, nz = nodal_normals[nid2 - 1] card = ['FORCE', load_id, nid, cid, magnitude, nx, ny, nz] bdf.write(print_card(card)) nid += 1 nid2 += 1 eid = nelements_offset + 1 for (n1, n2, n3) in (model.elements + (nnodes_offset + 1)): card = ['CTRIA3', eid, pid, n1, n2, n3] bdf.write(print_card(card)) eid += 1 t = 0.1 card = ['PSHELL', pid, mid, t] bdf.write(print_card(card)) E = 1e7 G = None nu = 0.3 card = ['MAT1', mid, E, G, nu] bdf.write(print_card(card)) bdf.write('ENDDATA\n') bdf.close()
def cart3d_to_stl_filename(cart3d_filename, stl_filename, log=None, debug=False): """ Converts a Cart3D file to STL format. :param cart3d_filename: path to the input Cart3D file :param stl_filename: path to the output STL file :param log: a logger object (or None) :param debug: True/False (used if log is not defined) """ cart3d = Cart3DReader(log=log, debug=debug) (nodes, elements, regions, loads) = cart3d.read_cart3d(cart3d_filename) stl = STLReader() stl.nodes = nodes - 1 stl.elements = elements - 1 stl.write_stl(stl_filename)
def cart3d_to_stl(cart3d, log=None, debug=False): """ Converts a Cart3DReader object to STL format. :param cart3d: a Cart3DReader object :param log: a logger object (or None) :param debug: True/False (used if log is not defined) :returns stl: an STLReader object """ normals = cart3d.normals() stl = STLReader(log=log, debug=debug) stl.nodes = cart3d.nodes stl.elements = cart3d.elements stl.write_stl(stl_filename) return stl
def stl_to_nastran_filename(stl_filename, bdf_filename, nnodes_offset=0, nelements_offset=0, log=None): model = STLReader(log=log) model.read_stl(stl_filename) nid = nnodes_offset + 1 cid = None pid = 100 mid = 200 load_id = 10 nodal_normals = model.get_normals_at_nodes(model.elements) bdf = open(bdf_filename, 'wb') bdf.write('CEND\n') #bdf.write('LOAD = %s\n' % load_id) bdf.write('BEGIN BULK\n') for (x, y, z) in model.nodes: card = ['GRID', nid, cid, x, y, z] bdf.write(print_card(card)) #nx, ny, nz = nodal_normals[nid - 1] #card = ['FORCE', load_id, nid, cid, 100, nx, ny, nz] #bdf.write(print_card(card)) nid += 1 eid = nelements_offset + 1 for (n1, n2, n3) in (model.elements + (nnodes_offset + 1)): card = ['CTRIA3', eid, pid, n1, n2, n3] bdf.write(print_card(card)) eid += 1 t = 0.1 card = ['PSHELL', pid, mid, t] bdf.write(print_card(card)) E = 1e7 G = None nu = 0.3 card = ['MAT1', mid, E, G, nu] bdf.write(print_card(card)) bdf.write('ENDDATA\n') bdf.close()
def nastran_to_stl_filename(bdf_filename, stl_filename, log=None): model = BDF(log=log) model.read_bdf(bdf_filename) #log.info('card_count = %s' % model.card_count) nnodes = len(model.nodes) nodes = zeros((nnodes, 3), dtype='float64') elements = [] i = 0 nodeid_to_i_map = {} for node_id, node in sorted(iteritems(model.nodes)): xyz = node.Position() if xyz[0] < 0.001: print(xyz, node_id) nodes[i, :] = xyz nodeid_to_i_map[node_id] = i i += 1 for eid, element in sorted(iteritems(model.elements)): if element.type in ['CQUADR']: continue elif element.type in ['CBAR', 'CBEAM', 'CONM2', 'RBE2', 'RBE3', 'CBUSH', 'CBUSH1D', 'CBUSH2D', 'CONROD', 'CROD', 'CELAS1', 'CELAS2', 'CELAS3', 'CELAS4', 'CDAMP1', 'CDAMP2', 'CDAMP3', 'CDAMP4',]: continue elif element.type in ['CQUAD4']: n1, n2, n3, n4 = element.nodeIDs() i1, i2, i3, i4 = nodeid_to_i_map[n1], nodeid_to_i_map[n2], nodeid_to_i_map[n3], nodeid_to_i_map[n3] elements.append([i1, i2, i3]) elements.append([i3, i4, i1]) elif element.type in ['CTRIA3', 'CTRIAR']: n1, n2, n3 = element.nodeIDs() i1, i2, i3 = nodeid_to_i_map[n1], nodeid_to_i_map[n2], nodeid_to_i_map[n3] elements.append([i1, i2, i3]) else: print(element.type) elements = array(elements, dtype='int32') stl = STLReader() stl.nodes = nodes stl.elements = elements stl.write_stl(stl_filename)
def main(): bdf_filename = 'bay.bdf' bc_file = 'bc' bdf_filename2 = 'bay2.bdf' cart3d_filename = 'bay.i.tri' stl_filename = 'bay.stl' #in_format = 'nastran' flip_normals = True volume_bdfname = 'bay.vol.bdf' module = __import__(bc_file) pid_map = module.__dict__['pid_map'] boundary_conditions = module.__dict__['boundary_conditions'] #print dir(module) #e = execfile(bc_file, globals(), locals()) #print globals() #print dir(e) pid_map boundary_conditions assert isinstance(pid_map, dict) assert isinstance(boundary_conditions, dict) get_bcs(bdf_filename, pid_map, boundary_conditions, log=None) #if flip_normals: # bdf = BDF() # bdf.read_bdf(bdf_filename, xref=False) # bdf.flip_normals() # bdf.write_bdf(bdf_filename2) # del bdf #else: #bdf_filename2 = bdf_filename if 0: nastran_to_cart3d_filename(bdf_filename, cart3d_filename, log=log) cart3d_to_stl_filename(cart3d_filename, stl_filename, log=log) else: nastran_to_stl_filename(bdf_filename, stl_filename, log=log) stl_to_nastran_filename(stl_filename, bdf_filename2, log=log) print "----------" #nastran_to_cart3d(bdf_filename2, cart3d_filename) #cart3d = Cart3dReader() #cart3d.read_cart3d(cart3d_filename) stl = STLReader() stl.read_stl(stl_filename) if flip_normals: stl.flip_normals() stl.project_boundary_layer(stl.nodes, stl.elements, volume_bdfname)
def main(): bdf_filename = 'bay.bdf' bc_file = 'bc' bdf_filename2 = 'bay2.bdf' cart3d_filename = 'bay.i.tri' stl_filename = 'bay.stl' #in_format = 'nastran' flip_normals = True volume_bdfname = 'bay.vol.bdf' module = __import__(bc_file) pid_map = module.__dict__['pid_map'] boundary_conditions = module.__dict__['boundary_conditions'] #print(dir(module)) #e = execfile(bc_file, globals(), locals()) #print(globals()) #print(dir(e)) pid_map boundary_conditions assert isinstance(pid_map, dict) assert isinstance(boundary_conditions, dict) get_bcs(bdf_filename, pid_map, boundary_conditions, log=None) #if flip_normals: #bdf = BDF() #bdf.read_bdf(bdf_filename, xref=False) #bdf.flip_normals() #bdf.write_bdf(bdf_filename2) #del bdf #else: #bdf_filename2 = bdf_filename if 0: nastran_to_cart3d_filename(bdf_filename, cart3d_filename, log=log) cart3d_to_stl_filename(cart3d_filename, stl_filename, log=log) else: nastran_to_stl_filename(bdf_filename, stl_filename, log=log) stl_to_nastran_filename(stl_filename, bdf_filename2, log=log) print("----------") #nastran_to_cart3d(bdf_filename2, cart3d_filename) #cart3d = Cart3dReader() #cart3d.read_cart3d(cart3d_filename) stl = STLReader() stl.read_stl(stl_filename) if flip_normals: stl.flip_normals() stl.project_boundary_layer(stl.nodes, stl.elements, volume_bdfname)
def main(): import os from pyNastran.converters.stl.stl_reader import STLReader m1 = STLReader() m1.read_stl('tetgen_test.stl') m1.flip_normals() m1.write_stl('tetgen_test_flipped.stl') del m1 os.system('tetgen.exe -pqcvVqY tetgen_test_flipped.stl') m = TetgenReader() base = 'tetgen_test_flipped.1' m.read_tetgen(base + '.node', base + '.smesh', base + '.ele', dimension_flag=3) m.write_nastran(base + '.bdf')
def cart3d_to_stl_filename(cart3d_filename, stl_filename, log=None, debug=False): """ Converts a Cart3D file to STL format. :param cart3d_filename: path to the input Cart3D file :param stl_filename: path to the output STL file :param log: a logger object (or None) :param debug: True/False (used if log is not defined) """ cart3d = Cart3DReader(log=log, debug=debug) cart3d.read_cart3d(cart3d_filename) stl = STLReader() stl.nodes = cart3d.nodes stl.elements = cart3d.elements stl.write_stl(stl_filename) return if 0: normals = model.normals() f = open(stl_filename, 'wb') f.write('solid cart3d_model\n') i = 0 for (n1, n2, n3) in model.elements: #f.write('solid cart3d_model\n') n = normals[i] f.write('loop\n') f.write(' facet normal %f %f %f\n' % (n[0], n[1], n[2])) f.write(' vertex %f %f %f\n' % (n1[0], n1[1], n1[2])) f.write(' vertex %f %f %f\n' % (n1[0], n1[1], n1[2])) f.write(' vertex %f %f %f\n' % (n1[0], n1[1], n1[2])) f.write(' endfacet') f.write('endloop\n') i += 1 f.close()
def cart3d_to_stl(cart3d, log=None, debug=False): """ Converts a Cart3DReader object to STL format. :param cart3d: a Cart3DReader object :param log: a logger object (or None) :param debug: True/False (used if log is not defined) :returns stl: an STLReader object """ normals = cart3d.normals() stl = STLReader(log=log, debug=debug) stl.nodes = nodes stl.elements = elements stl.write_stl(stl_filename) return stl
def load_stl_geometry(self, stl_filename, dirname): print "load_stl_geometry..." skipReading = self.removeOldGeometry(stl_filename) if skipReading: return model = STLReader(log=self.log, debug=False) #self.modelType = model.modelType model.read_stl(stl_filename) nodes = model.nodes elements = model.elements self.nNodes, three = nodes.shape self.nElements, three = elements.shape print("nNodes = ",self.nNodes) print("nElements = ", self.nElements) self.grid.Allocate(self.nElements, 1000) #self.gridResult.SetNumberOfComponents(self.nElements) self.grid2.Allocate(1, 1000) points = vtk.vtkPoints() points.SetNumberOfPoints(self.nNodes) #self.gridResult.Allocate(self.nNodes, 1000) #vectorReselt.SetNumberOfComponents(3) self.nidMap = {} #elem.SetNumberOfPoints(nNodes) if 0: fraction = 1. / self.nNodes # so you can color the nodes by ID for nid, node in sorted(nodes.iteritems()): points.InsertPoint(nid - 1, *node) self.gridResult.InsertNextValue(nid * fraction) #print str(element) #elem = vtk.vtkVertex() #elem.GetPointIds().SetId(0, i) #self.aQuadGrid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) #vectorResult.InsertTuple3(0, 0.0, 0.0, 1.0) assert nodes is not None nnodes, three = nodes.shape nid = 0 print "nnodes=%s" % nnodes for i in xrange(nnodes): points.InsertPoint(nid, nodes[i, :]) nid += 1 nelements, three = elements.shape #elements -= 1 for eid in xrange(nelements): elem = vtkTriangle() node_ids = elements[eid, :] elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) self.grid.InsertNextCell(5, elem.GetPointIds()) #elem.GetCellType() = 5 # vtkTriangle self.grid.SetPoints(points) self.grid.Modified() self.grid.Update() print("updated grid") # loadSTLResults - regions/loads self.TurnTextOn() self.scalarBar.VisibilityOff() self.scalarBar.Modified() cases = {} ID = 1 cases = self._fill_stl_case(cases, ID, elements) #self.finish_io() self.resultCases = cases self.caseKeys = sorted(cases.keys()) #print "caseKeys = ",self.caseKeys #print "type(caseKeys) = ",type(self.caseKeys) self.nCases = min(0, len(self.resultCases) - 1) # number of keys in dictionary self.iCase = 0 if self.nCases == 0 else -1 self.cycleResults() # start at nCase=0
def stl_reshape(data): if '--xy' not in data: data['--xy'] = False if '--yz' not in data: data['--yz'] = False if '--xz' not in data: data['--xz'] = False if '--scale' not in data: data['--scale'] = None if '--xscale' not in data: data['--xscale'] = None if '--yscale' not in data: data['--yscale'] = None if '--zscale' not in data: data['--zscale'] = None if '<xshift>' not in data: data['<xshift>'] = None if '<yshift>' not in data: data['<yshift>'] = None if '<zshift>' not in data: data['<zshift>'] = None if '--stats' not in data: data['--stats'] = None if '--mirror' not in data: data['--mirror'] = None if '--flip_normals' not in data: data['--flip_normals'] = None in_stl_filename = data['<in_stl_filename>'] out_stl_filename = data['<out_stl_filename>'] assert in_stl_filename != out_stl_filename stl = STLReader() stl.read_stl(in_stl_filename) if data['<fmt>'] in ['False', False]: is_binary = True fmt = None else: fmt = data['<fmt>'] is_binary = False print('is_binary=%s' % is_binary) if data['--xy'] or data['--yz'] or data['--xz']: scale = 1. if data['--scale'] is not None: scale = float(data['--scale']) if data['--xy']: assert data['--yz'] is False assert data['--xz'] is False axes = 'xy' elif data['--yz']: assert data['--xy'] is False assert data['--xz'] is False axes = 'yz' elif data['--xz']: assert data['--xy'] is False assert data['--yz'] is False axes = 'xz' #print('flip_axes = %r' % axes) #print(data) stl.flip_axes(axes, scale) elif data['--xscale'] or data['--yscale'] or data['--zscale']: xscale = 1. yscale = 1. zscale = 1. if data['--xscale'] is not None: xscale = float(data['--xscale'].strip("'")) if data['--yscale'] is not None: yscale = float(data['--yscale'].strip("'")) if data['--zscale'] is not None: zscale = float(data['--zscale'].strip("'")) x = deepcopy(stl.nodes[:, 0]) y = deepcopy(stl.nodes[:, 1]) z = deepcopy(stl.nodes[:, 2]) stl.nodes[:, 0] = x * xscale stl.nodes[:, 1] = y * yscale stl.nodes[:, 2] = z * zscale elif data['<xshift>'] or data['<yshift>'] or data['<zshift>']: xshift = 1. yshift = 1. zshift = 1. if data['<xshift>'] is not None: if isinstance(xshift, basestring): xshift = float(data['<xshift>'].strip("'")) else: xshift = float(data['<xshift>']) if data['<yshift>'] is not None: if isinstance(xshift, basestring): yshift = float(data['<yshift>'].strip("'")) else: yshift = float(data['<yshift>']) if data['<zshift>'] is not None: if isinstance(xshift, basestring): zshift = float(data['<zshift>'].strip("'")) else: zshift = float(data['<zshift>']) print('delta = (%s, %s, %s)' % (xshift, yshift, zshift)) stl.nodes[:, 0] += xshift stl.nodes[:, 1] += yshift stl.nodes[:, 2] += zshift elif data['--scale']: scale = float(data['--scale']) stl.nodes *= scale elif data['--stats']: xmax, ymax, zmax = stl.nodes.max(axis=0) xmin, ymin, zmin = stl.nodes.min(axis=0) print('xyz_max = (%g, %g, %g)' % (xmax, ymax, zmax)) print('xyz_min = (%g, %g, %g)' % (xmin, ymin, zmin)) return elif data['--mirror']: #plane = data['plane'] #assert plane in ['xy', 'yz', 'xz'], 'plane=%r' % plane xyz = data['<xyz>'] tol = float(data['<tol>']) stl.create_mirror_model(xyz, tol) elif data['--flip_normals']: stl.flip_normals() else: raise RuntimeError('unsupported reshape...data=%s' % data) stl.write_stl(out_stl_filename, is_binary=is_binary, float_fmt=fmt)
def load_stl_geometry(self, stl_filename, dirname): print "load_stl_geometry..." skipReading = self.removeOldGeometry(stl_filename) if skipReading: return model = STLReader(log=self.log, debug=False) #self.modelType = model.modelType model.read_stl(stl_filename) nodes = model.nodes elements = model.elements self.nNodes, three = nodes.shape self.nElements, three = elements.shape print("nNodes = ", self.nNodes) print("nElements = ", self.nElements) self.grid.Allocate(self.nElements, 1000) #self.gridResult.SetNumberOfComponents(self.nElements) self.grid2.Allocate(1, 1000) points = vtk.vtkPoints() points.SetNumberOfPoints(self.nNodes) #self.gridResult.Allocate(self.nNodes, 1000) #vectorReselt.SetNumberOfComponents(3) self.nidMap = {} #elem.SetNumberOfPoints(nNodes) if 0: fraction = 1. / self.nNodes # so you can color the nodes by ID for nid, node in sorted(nodes.iteritems()): points.InsertPoint(nid - 1, *node) self.gridResult.InsertNextValue(nid * fraction) #print str(element) #elem = vtk.vtkVertex() #elem.GetPointIds().SetId(0, i) #self.aQuadGrid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) #vectorResult.InsertTuple3(0, 0.0, 0.0, 1.0) assert nodes is not None nnodes, three = nodes.shape nid = 0 print "nnodes=%s" % nnodes for i in xrange(nnodes): points.InsertPoint(nid, nodes[i, :]) nid += 1 nelements, three = elements.shape #elements -= 1 for eid in xrange(nelements): elem = vtkTriangle() node_ids = elements[eid, :] elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) self.grid.InsertNextCell( 5, elem.GetPointIds()) #elem.GetCellType() = 5 # vtkTriangle self.grid.SetPoints(points) self.grid.Modified() self.grid.Update() print("updated grid") # loadSTLResults - regions/loads self.TurnTextOn() self.scalarBar.VisibilityOff() self.scalarBar.Modified() cases = {} ID = 1 cases = self._fill_stl_case(cases, ID, elements) #self.finish_io() self.resultCases = cases self.caseKeys = sorted(cases.keys()) #print "caseKeys = ",self.caseKeys #print "type(caseKeys) = ",type(self.caseKeys) self.nCases = min(0, len(self.resultCases) - 1) # number of keys in dictionary self.iCase = 0 if self.nCases == 0 else -1 self.cycleResults() # start at nCase=0
def load_stl_geometry(self, stl_filename, dirname, plot=True): print("load_stl_geometry...") skipReading = self.removeOldGeometry(stl_filename) if skipReading: return model = STLReader(log=self.log, debug=False) #self.modelType = model.modelType model.read_stl(stl_filename) nodes = model.nodes elements = model.elements if self.is_centroidal: normals = model.get_normals(elements) areas = model.get_area(elements) elif self.is_nodal: normals = None areas = None #normals = model.get_normals_at_nodes(elements) self.nNodes, three = nodes.shape self.nElements, three = elements.shape print("nNodes = %s" % self.nNodes) print("nElements = %s" % self.nElements) self.grid.Allocate(self.nElements, 1000) #self.gridResult.SetNumberOfComponents(self.nElements) self.grid2.Allocate(1, 1000) points = vtk.vtkPoints() points.SetNumberOfPoints(self.nNodes) #self.gridResult.Allocate(self.nNodes, 1000) #vectorReselt.SetNumberOfComponents(3) self.nidMap = {} #elem.SetNumberOfPoints(nNodes) if 0: fraction = 1. / self.nNodes # so you can color the nodes by ID for nid, node in sorted(iteritems(nodes)): points.InsertPoint(nid - 1, *node) self.gridResult.InsertNextValue(nid * fraction) #print str(element) #elem = vtk.vtkVertex() #elem.GetPointIds().SetId(0, i) #self.aQuadGrid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) #vectorResult.InsertTuple3(0, 0.0, 0.0, 1.0) assert nodes is not None nnodes, three = nodes.shape xmax, ymax, zmax = nodes.max(axis=0) xmin, ymin, zmin = nodes.min(axis=0) self.log.info('xmax=%s xmin=%s' % (xmax, xmin)) self.log.info('ymax=%s ymin=%s' % (ymax, ymin)) self.log.info('zmax=%s zmin=%s' % (zmax, zmin)) dim_max = max(xmax - xmin, ymax - ymin, zmax - zmin) self.update_axes_length(dim_max) nid = 0 print("nnodes=%s" % nnodes) for i in range(nnodes): points.InsertPoint(nid, nodes[i, :]) nid += 1 nelements, three = elements.shape #elements -= 1 for eid in range(nelements): elem = vtkTriangle() node_ids = elements[eid, :] elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) self.grid.InsertNextCell( 5, elem.GetPointIds()) #elem.GetCellType() = 5 # vtkTriangle self.grid.SetPoints(points) self.grid.Modified() self.grid.Update() print("updated grid") # loadSTLResults - regions/loads self.TurnTextOn() self.scalarBar.VisibilityOff() self.scalarBar.Modified() cases = {} self.iSubcaseNameMap = {} ID = 1 cases = self._fill_stl_case(cases, ID, elements, nodes, normals, areas) self._finish_results_io(cases)
def load_stl_geometry(self, stl_filename, dirname, plot=True): print("load_stl_geometry...") skipReading = self.removeOldGeometry(stl_filename) if skipReading: return model = STLReader(log=self.log, debug=False) #self.modelType = model.modelType model.read_stl(stl_filename) nodes = model.nodes elements = model.elements if self.is_centroidal: normals = model.get_normals(elements) areas = model.get_area(elements) elif self.is_nodal: normals = None areas = None #normals = model.get_normals_at_nodes(elements) self.nNodes, three = nodes.shape self.nElements, three = elements.shape print("nNodes = %s" % self.nNodes) print("nElements = %s" % self.nElements) self.grid.Allocate(self.nElements, 1000) #self.gridResult.SetNumberOfComponents(self.nElements) self.grid2.Allocate(1, 1000) points = vtk.vtkPoints() points.SetNumberOfPoints(self.nNodes) #self.gridResult.Allocate(self.nNodes, 1000) #vectorReselt.SetNumberOfComponents(3) self.nidMap = {} #elem.SetNumberOfPoints(nNodes) if 0: fraction = 1. / self.nNodes # so you can color the nodes by ID for nid, node in sorted(iteritems(nodes)): points.InsertPoint(nid - 1, *node) self.gridResult.InsertNextValue(nid * fraction) #print str(element) #elem = vtk.vtkVertex() #elem.GetPointIds().SetId(0, i) #self.aQuadGrid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) #vectorResult.InsertTuple3(0, 0.0, 0.0, 1.0) assert nodes is not None nnodes = nodes.shape[0] xmax, ymax, zmax = nodes.max(axis=0) xmin, ymin, zmin = nodes.min(axis=0) self.log.info('xmax=%s xmin=%s' % (xmax, xmin)) self.log.info('ymax=%s ymin=%s' % (ymax, ymin)) self.log.info('zmax=%s zmin=%s' % (zmax, zmin)) dim_max = max(xmax-xmin, ymax-ymin, zmax-zmin) self.update_axes_length(dim_max) nid = 0 print("nnodes=%s" % nnodes) for i in range(nnodes): points.InsertPoint(nid, nodes[i, :]) nid += 1 nelements = elements.shape[0] #elements -= 1 for eid in range(nelements): elem = vtkTriangle() node_ids = elements[eid, :] elem.GetPointIds().SetId(0, node_ids[0]) elem.GetPointIds().SetId(1, node_ids[1]) elem.GetPointIds().SetId(2, node_ids[2]) self.grid.InsertNextCell(5, elem.GetPointIds()) #elem.GetCellType() = 5 # vtkTriangle self.grid.SetPoints(points) self.grid.Modified() if hasattr(self.grid, 'Update'): self.grid.Update() print("updated grid") # loadSTLResults - regions/loads self.TurnTextOn() self.scalarBar.VisibilityOff() self.scalarBar.Modified() cases = {} self.iSubcaseNameMap = {} ID = 1 cases = self._fill_stl_case(cases, ID, elements, nodes, normals, areas) self._finish_results_io(cases)
def nastran_to_stl_filename(bdf_filename, stl_filename, log=None): model = BDF(log=log) model.read_bdf(bdf_filename) #log.info('card_count = %s' % model.card_count) nnodes = len(model.nodes) nodes = zeros((nnodes, 3), dtype='float64') elements = [] i = 0 nodeid_to_i_map = {} for node_id, node in sorted(iteritems(model.nodes)): xyz = node.Position() if xyz[0] < 0.001: print(xyz, node_id) nodes[i, :] = xyz nodeid_to_i_map[node_id] = i i += 1 for eid, element in sorted(iteritems(model.elements)): if element.type in ['CQUADR']: continue elif element.type in [ 'CBAR', 'CBEAM', 'CONM2', 'RBE2', 'RBE3', 'CBUSH', 'CBUSH1D', 'CBUSH2D', 'CONROD', 'CROD', 'CELAS1', 'CELAS2', 'CELAS3', 'CELAS4', 'CDAMP1', 'CDAMP2', 'CDAMP3', 'CDAMP4', ]: continue elif element.type in ['CQUAD4']: n1, n2, n3, n4 = element.nodeIDs() i1, i2, i3, i4 = nodeid_to_i_map[n1], nodeid_to_i_map[ n2], nodeid_to_i_map[n3], nodeid_to_i_map[n3] elements.append([i1, i2, i3]) elements.append([i3, i4, i1]) elif element.type in ['CTRIA3', 'CTRIAR']: n1, n2, n3 = element.nodeIDs() i1, i2, i3 = nodeid_to_i_map[n1], nodeid_to_i_map[ n2], nodeid_to_i_map[n3] elements.append([i1, i2, i3]) else: print(element.type) elements = array(elements, dtype='int32') stl = STLReader() stl.nodes = nodes stl.elements = elements stl.write_stl(stl_filename)
def stl_reshape(data): if '--xy' not in data: data['--xy'] = False if '--yz' not in data: data['--yz'] = False if '--xz' not in data: data['--xz'] = False if '--scale' not in data: data['--scale'] = None if '--xscale' not in data: data['--xscale'] = None if '--yscale' not in data: data['--yscale'] = None if '--zscale' not in data: data['--zscale'] = None if '<xshift>' not in data: data['<xshift>'] = None if '<yshift>' not in data: data['<yshift>'] = None if '<zshift>' not in data: data['<zshift>'] = None if '--stats' not in data: data['--stats'] = None if '--mirror' not in data: data['--mirror'] = None if '--flip_normals' not in data: data['--flip_normals'] = None in_stl_filename = data['<in_stl_filename>'] out_stl_filename = data['<out_stl_filename>'] assert in_stl_filename != out_stl_filename stl = STLReader() stl.read_stl(in_stl_filename) if data['<fmt>'] in ['False', False]: is_binary = True fmt = None else: fmt = data['<fmt>'] is_binary = False print('is_binary=%s' % is_binary) if data['--xy'] or data['--yz'] or data['--xz']: scale = 1. if data['--scale'] is not None: scale = float(data['--scale']) if data['--xy']: assert data['--yz'] is False assert data['--xz'] is False axes = 'xy' elif data['--yz']: assert data['--xy'] is False assert data['--xz'] is False axes = 'yz' elif data['--xz']: assert data['--xy'] is False assert data['--yz'] is False axes = 'xz' #print('flip_axes = %r' % axes) #print(data) stl.flip_axes(axes, scale) elif data['--xscale'] or data['--yscale'] or data['--zscale']: xscale = 1. yscale = 1. zscale = 1. if data['--xscale'] is not None: xscale = float(data['--xscale'].strip("'")) if data['--yscale'] is not None: yscale = float(data['--yscale'].strip("'")) if data['--zscale'] is not None: zscale = float(data['--zscale'].strip("'")) x = deepcopy(stl.nodes[:, 0]) y = deepcopy(stl.nodes[:, 1]) z = deepcopy(stl.nodes[:, 2]) stl.nodes[:, 0] = x * xscale stl.nodes[:, 1] = y * yscale stl.nodes[:, 2] = z * zscale elif data['<xshift>'] or data['<yshift>'] or data['<zshift>']: xshift = 1. yshift = 1. zshift = 1. if data['<xshift>'] is not None: if isinstance(xshift, basestring): xshift = float(data['<xshift>'].strip("'")) else: xshift = float(data['<xshift>']) if data['<yshift>'] is not None: if isinstance(xshift, basestring): yshift = float(data['<yshift>'].strip("'")) else: yshift = float(data['<yshift>']) if data['<zshift>'] is not None: if isinstance(xshift, basestring): zshift = float(data['<zshift>'].strip("'")) else: zshift = float(data['<zshift>']) print('delta = (%s, %s, %s)' % (xshift, yshift, zshift)) stl.nodes[:, 0] += xshift stl.nodes[:, 1] += yshift stl.nodes[:, 2] += zshift elif data['--scale'] : scale = float(data['--scale']) stl.nodes *= scale elif data['--stats'] : xmax, ymax, zmax = stl.nodes.max(axis=0) xmin, ymin, zmin = stl.nodes.min(axis=0) print('xyz_max = (%g, %g, %g)' % (xmax, ymax, zmax)) print('xyz_min = (%g, %g, %g)' % (xmin, ymin, zmin)) return elif data['--mirror']: #plane = data['plane'] #assert plane in ['xy', 'yz', 'xz'], 'plane=%r' % plane xyz = data['<xyz>'] tol = float(data['<tol>']) stl.create_mirror_model(xyz, tol) elif data['--flip_normals']: stl.flip_normals() else: raise RuntimeError('unsupported reshape...data=%s' % data) stl.write_stl(out_stl_filename, is_binary=is_binary, float_fmt=fmt)