Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of Tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

This file is part of tendril 

See the COPYING, README, and INSTALL files for more information 

""" 

 

import csv 

from entitybase import EntityBase 

 

from tendril.conventions.electronics import fpiswire 

from tendril.conventions.electronics import parse_ident 

from tendril.utils.types.lengths import Length 

 

from tendril.utils import log 

logger = log.get_logger(__name__, log.DEFAULT) 

 

 

class OutputElnBomDescriptor(object): 

    def __init__(self, pcbname, cardfolder, configname, 

                 configurations, multiplier=1): 

        self.pcbname = pcbname 

        self.cardfolder = cardfolder 

        self.configname = configname 

        self.multiplier = multiplier 

        self.configurations = configurations 

 

 

class OutputBomLine(object): 

 

    def __init__(self, comp, parent): 

        assert isinstance(comp, EntityBase) 

        self.ident = comp.ident 

        self.refdeslist = [] 

        self.parent = parent 

 

    def add(self, comp): 

        assert isinstance(comp, EntityBase) 

        if comp.fillstatus == "DNP": 

            return 

        if comp.fillstatus == "CONF": 

            logger.warning("Configurable Component " 

                           "not Configured by Conf File : " + comp.refdes) 

        if comp.ident == self.ident: 

            self.refdeslist.append(comp.refdes) 

        else: 

            logger.error("Ident Mismatch") 

 

    @property 

    def quantity(self): 

        device, value, footprint = parse_ident(self.ident) 

        if device is None: 

            logger.warning("Device not identified : " + self.ident) 

        elif fpiswire(device): 

            try: 

                elen = Length(footprint) * 10 / 100 

                if elen < Length('5mm'): 

                    elen = Length('5mm') 

                elif elen > Length('1inch'): 

                    elen = Length('1inch') 

                return len(self.refdeslist) * (Length(footprint) + elen) * \ 

                    self.parent.descriptor.multiplier 

            except ValueError: 

                logger.error( 

                    "Problem parsing length for ident : " + self.ident 

                ) 

                raise 

        return len(self.refdeslist) * self.parent.descriptor.multiplier 

 

    def __repr__(self): 

        return "{0:<50} {1:<4} {2}".format( 

            self.ident, self.quantity, str(self.refdeslist) 

        ) 

 

 

class OutputBom(object): 

 

    def __init__(self, descriptor): 

        """ 

 

        :type descriptor: outputbase.OutputElnBomDescriptor 

        """ 

        self.lines = [] 

        self.descriptor = descriptor 

 

    def sort_by_ident(self): 

        self.lines.sort(key=lambda x: x.ident, reverse=False) 

        for line in self.lines: 

            line.refdeslist.sort() 

 

    def find_by_ident(self, ident): 

        for line in self.lines: 

            assert isinstance(line, OutputBomLine) 

            if line.ident == ident: 

                return line 

        return None 

 

    def insert_component(self, item): 

        assert isinstance(item, EntityBase) 

        line = self.find_by_ident(item.ident) 

        if line is None: 

            line = OutputBomLine(item, self) 

            self.lines.append(line) 

        line.add(item) 

 

    def multiply(self, factor, composite=False): 

        if composite is True: 

            self.descriptor.multiplier = self.descriptor.multiplier * factor 

        else: 

            self.descriptor.multiplier = factor 

 

 

class CompositeOutputBomLine(object): 

 

    def __init__(self, line, colcount): 

        self.ident = line.ident 

        self.columns = [0] * colcount 

 

    def add(self, line, column): 

        """ 

 

        :type line: outputbase.OutputBomLine 

        """ 

        if line.ident == self.ident: 

            self.columns[column] = line.quantity 

        else: 

            logger.error("Ident Mismatch") 

 

    @property 

    def quantity(self): 

        try: 

            return sum(self.columns) 

        except TypeError: 

            print self.ident 

            raise TypeError(self.columns) 

 

    def subset_qty(self, idxs): 

        try: 

            rval = 0 

            for idx in idxs: 

                rval += self.columns[idx] 

            return rval 

        except TypeError: 

            print self.ident 

            raise TypeError(self.columns) 

 

    def merge_line(self, cline): 

        for idx, column in enumerate(self.columns): 

            self.columns[idx] += cline.columns[idx] 

 

 

class CompositeOutputBom(object): 

    def __init__(self, bom_list, name=None): 

        self.descriptors = [] 

        self.lines = [] 

        self.colcount = len(bom_list) 

        self.descriptor = OutputElnBomDescriptor(None, None, name, None, 1) 

        i = 0 

        for bom in bom_list: 

            self.insert_bom(bom, i) 

            i += 1 

        self.sort_by_ident() 

 

    def get_subset_idxs(self, confignames): 

        rval = [] 

        for configname in confignames: 

            for idx, descriptor in enumerate(self.descriptors): 

                if descriptor.configname == configname: 

                    rval.append(idx) 

        return rval 

 

    def insert_bom(self, bom, i): 

        """ 

 

        :type bom: outputbase.OutputBom 

        """ 

        self.descriptors.append(bom.descriptor) 

        for line in bom.lines: 

            self.insert_line(line, i) 

 

    def insert_line(self, line, i): 

        """ 

 

        :type line: outputbase.OutputBomLine 

        """ 

        cline = self.find_by_ident(line.ident) 

        if cline is None: 

            cline = CompositeOutputBomLine(line, self.colcount) 

            self.lines.append(cline) 

        cline.add(line, i) 

 

    def find_by_ident(self, ident): 

        """ 

 

        :type line: outputbase.OutputBomLine 

        """ 

        for cline in self.lines: 

            assert isinstance(cline, CompositeOutputBomLine) 

            if cline.ident == ident: 

                return cline 

        return None 

 

    def sort_by_ident(self): 

        self.lines.sort(key=lambda x: x.ident, reverse=False) 

 

    def dump(self, stream): 

        writer = csv.writer(stream) 

        writer.writerow( 

            ['device'] + 

            [x.configname + ' x' + str(x.multiplier) 

             for x in self.descriptors] + 

            ['Total']) 

        for line in self.lines: 

            columns = [None if x == 0 else x for x in line.columns] 

            writer.writerow([line.ident] + columns + [line.quantity]) 

 

    def collapse_wires(self): 

        for line in self.lines: 

            device, value, footprint = parse_ident(line.ident) 

            if device is None: 

                continue 

            if fpiswire(device): 

                newident = device + ' ' + value 

                newline = self.find_by_ident(newident) 

                if newline is None: 

                    line.ident = newident 

                else: 

                    newline.merge_line(line) 

                    self.lines.remove(line) 

 

 

def load_cobom_from_file(f, name, tf=None): 

    bomlist = [] 

    header = [] 

    reader = csv.reader(f) 

    for line in reader: 

        line = [elem.strip() for elem in line] 

        if line[0] == 'device': 

            header = line 

            break 

 

    logger.info('Inserting External Boms') 

    oboms = [] 

    for head in header[1:-1]: 

        logger.info('Creating Bom : ' + head) 

        obom_descriptor = OutputElnBomDescriptor(head, 

                                                 None, 

                                                 head, None) 

        obom = OutputBom(obom_descriptor) 

        oboms.append(obom) 

 

    for line in reader: 

        line = [elem.strip() for elem in line] 

        if line[0] == '': 

            continue 

        if line[0] == 'END': 

            break 

        if tf and not tf.has_contextual_repr(line[0]): 

            print line[0] + ' Possibly not recognized' 

        if tf: 

            device, value, footprint = parse_ident( 

                tf.get_canonical_repr(line[0]) 

            ) 

        else: 

            device, value, footprint = parse_ident(line[0]) 

        logger.debug("Trying to insert line : " + line[0]) 

        # print base_tf.get_canonical_repr(line[0]) 

        from tendril.boms.electronics import EntityElnComp 

        item = EntityElnComp() 

        item.define('Undef', device, value, footprint) 

        for idx, col in enumerate(line[1:-1]): 

            if col != '': 

                if device and fpiswire(device): 

                    length = Length(col) 

                    if length > 0: 

                        wireitem = EntityElnComp() 

                        wireitem.define( 

                            'Undef', device, value, str(length) 

                        ) 

                        oboms[idx].insert_component(wireitem) 

                else: 

                    num = int(col) 

                    if num > 0: 

                        for i in range(num): 

                            oboms[idx].insert_component(item) 

 

    for obom in oboms: 

        logger.info('Inserting External Bom : ' + 

                    obom.descriptor.configname) 

        bomlist.append(obom) 

    cobom = CompositeOutputBom( 

        bomlist, 

        name=name 

    ) 

    return cobom