summaryrefslogtreecommitdiff
path: root/hyp2mat/lib/csxcad.cc
blob: a1ec2045a5d642434b67ed5b2fad156165135c8f (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * hyp2mat - convert hyperlynx files to matlab scripts
 * Copyright 2012 Koen De Vleeschauwer.
 *
 * This file is part of hyp2mat.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>

#include "csxcad.h"

using namespace Hyp2Mat;

CSXCAD::CSXCAD()
{
  /* CSXcad priorities */

  prio_dielectric = 100;  // FR4 dielectric
  prio_material   = 200;  // copper
  prio_via        = 300;  // via metal
  prio_drill      = 400;  // hole

  return;
}

void CSXCAD::export_edge(FloatPolygon& edge)
{
  std::cout << "pgon = [];" << std::endl;
  for (FloatPolygon::iterator i = edge.begin(); i != edge.end(); ++i) {
    std::cout << "pgon(:, end+1) = [" << i->x << ";" << i->y << "];" << std::endl;
    }
  return;
}

/*
 * quote a string using matlab conventions.
 */

std::string CSXCAD::string2matlab(std::string str)
{
   std::ostringstream ostring;

  // escape non-alpha characters, or WriteOpenEMS (in matlab) may crash on characters such as '%' in strings

   ostring << "'";
   for (unsigned int i = 0; i < str.size(); i++) {
     if (str[i] == '\'') ostring << '\'';
     if (str[i] == '%') ostring << '%';
     ostring << str[i];
     }
   ostring << "'";

   return ostring.str();
}

/* true if polygon list contains at least one (positive) polygon */

bool CSXCAD::contains_polygon(Hyp2Mat::FloatPolygons& polygons)
{
  bool result = false;

  for (FloatPolygons::iterator i = polygons.begin(); i != polygons.end(); ++i) {
    result = !i->is_hole;
    if (result) break;
    };

  return result;
}

/* true if polygon list contains at least one hole */

bool CSXCAD::contains_hole(Hyp2Mat::FloatPolygons& polygons)
{
  bool result = false;

  for (FloatPolygons::iterator i = polygons.begin(); i != polygons.end(); ++i) {
    result = i->is_hole;
    if (result) break;
    };

  return result;
}

  /*
   * Export dielectric
   * If pcb_outline is true, export exact board shape, including holes. 
   * If pcb_outline is false, export bounding box.
   */
 
void CSXCAD::export_board(Hyp2Mat::PCB& pcb, bool pcb_outline)
{
  /* CSXCAD coordinate grid definition */
  Bounds bounds = pcb.GetBounds();

  std::cout << "function CSX = pcb(CSX)" << std::endl;
  std::cout << "% matlab script created by hyp2mat" << std::endl;
  std::cout << "% create minimal mesh" << std::endl;
  std::cout << "mesh = {};" << std::endl;
  std::cout << "mesh.x = [" << bounds.x_min << " " << bounds.x_max << "];" << std::endl;
  std::cout << "mesh.y = [" << bounds.y_min << " " << bounds.y_max << "];" << std::endl;
  std::cout << "mesh.z = [" << bounds.z_min << " " << bounds.z_max << "];" << std::endl;
  std::cout << "% add mesh" << std::endl;
  std::cout << "CSX = DefineRectGrid(CSX, 1, mesh);" << std::endl;

  /*
   * Export the board. The board outline is positive; 
   */

  /*
   * create board material if at least one positive polygon present
   * We output one polygon for each dielectric layer, as each layer may have 
   * a different epsilon_r.
   */

  if (contains_polygon(pcb.board)) {

    for (FloatPolygons::iterator i = pcb.board.begin(); i != pcb.board.end(); ++i) {
      /* only output board material, not holes */
      if (i->is_hole) continue; 
      for (LayerList::reverse_iterator l = pcb.stackup.rbegin(); l != pcb.stackup.rend(); ++l) {
        /* only output dielectrics */
        if (l->layer_type != LAYER_DIELECTRIC) continue;

        /* Output command to create material */
        std::cout << "CSX = AddHyperLynxDielectric(CSX, 'Dielectric_" << l->layer_name << "', " << l->epsilon_r <<  ", " << l->loss_tangent << ");" << std::endl;

        /* output CSXCAD polygon */
        std::cout << "% board outline, layer " << l->layer_name << std::endl;
        int priority = prio_dielectric + i->nesting_level;
        if (pcb_outline) {
           /* Use AddLinPoly */
           export_edge(i->poly);
           std::cout << "CSX = AddLinPoly(CSX, 'Dielectric_" << l->layer_name << "', " << priority << ", 2, " << l->z0 << ", pgon, " << l->z1 - l->z0 << ");" << std::endl;
          }
        else {
          /* Use AddBox */
          std::cout << "CSX = AddBox(CSX, 'Dielectric_" << l->layer_name << "', " << priority << ", [ ";
          std::cout << bounds.x_min << ", " << bounds.y_min << ", " << l->z0 << "], [ ";
          std::cout << bounds.x_max << ", " << bounds.y_max << ", " << l->z1 << "]  );" << std::endl;
          }
        }
      }
    };


  /*
   * Export board cutouts
   */

  /* 
   * create board cutout material if at least one negative polygon present
   * For each cutout we create a single polygon which goes through all layers.
   */

  if (pcb_outline && contains_hole(pcb.board)) {
    std::cout << "% create board cutout material" << std::endl;
    std::cout << "CSX = AddMaterial( CSX, 'Drill');" << std::endl;
    std::cout << "CSX = SetMaterialProperty( CSX, 'Drill', 'Epsilon', 1, 'Mue', 1);" << std::endl;

    for (FloatPolygons::iterator i = pcb.board.begin(); i != pcb.board.end(); ++i) {
      /* output CSXCAD polygon */
      if (!i->is_hole) continue;
      std::cout << "% board cutout" << std::endl;
      export_edge(i->poly);
      int priority = prio_dielectric + i->nesting_level;
      std::cout << "CSX = AddLinPoly(CSX, 'Drill', " << priority << ", 2, " << bounds.z_min << ", pgon, " << bounds.z_max - bounds.z_min << ");" << std::endl;
      }
    };

  return;
}

  /*
   * Export copper.
   * If lossy_copper is true, take copper conductivity into account. 
   * If lossy_copper is false, assume copper is perfect conductor.
   */
 
void CSXCAD::export_layer(Hyp2Mat::PCB& pcb, Hyp2Mat::Layer& layer, bool lossy_copper, bool metal_3d, bool odd)
{
  std::string layer_material = layer.layer_name + "_copper";
  std::string layer_cutout = layer.layer_name + "_cutout";
  
  // create layer material if at least one positive polygon present
  if (contains_polygon(layer.metal)) {
      std::cout << "% create layer " << layer.layer_name << " material" << std::endl;

      double copper_resistivity = 0.0;
      if (lossy_copper && (layer.bulk_resistivity > 0)) copper_resistivity = layer.bulk_resistivity;
      
      if (metal_3d) 
        std::cout << "CSX = AddHyperLynxMetal3D(CSX, '" << layer_material << "', " << copper_resistivity <<  ", " <<  layer.thickness << ");" << std::endl;
      else
        std::cout << "CSX = AddHyperLynxMetal2D(CSX, '" << layer_material << "', " << copper_resistivity <<  ", " <<  layer.thickness << ");" << std::endl;
      };

  // create layer cutout material if at least one hole present
  if (contains_hole(layer.metal)) {
      std::cout << "% create layer " << layer.layer_name << " cutout" << std::endl;
      std::cout << "CSX = AddMaterial( CSX, '" << layer_cutout << "');" << std::endl;
      // outer and inner copper layers have different epsilonr
      std::cout << "CSX = SetMaterialProperty( CSX, '" << layer_cutout << "', 'Epsilon', " << layer.epsilon_r << ", 'Mue', 1);" << std::endl;
      };

  /*
   * Export the layer. 
   */

  std::string material;
 
  for (FloatPolygons::iterator i = layer.metal.begin(); i != layer.metal.end(); ++i) {
    /* output CSXCAD polygon */
    if (!i->is_hole) {
      std::cout << "% copper" << std::endl;
      material = layer_material;
      }
    else {
      std::cout << "% cutout" << std::endl;
      material = layer_cutout;
      };

    export_edge(i->poly);
    int priority = prio_material + i->nesting_level;
    /* Default is exporting copper as a 2D conducting sheet */
    if (metal_3d) {
      // We assume alternating layers of substrate and prepreg,and that metal layers are pushed into the prepreg.
      // This means that the top metal layer is pointing up, the second metal layer is pointing down, and so on.
      double thickness = layer.thickness;
      if (odd) thickness = -thickness;
      std::cout << "CSX = AddLinPoly(CSX, '" << material << "', " << priority << ", 2, " << layer.z0 << ", pgon, " << thickness << ");" << std::endl;
      }
    else
      std::cout << "CSX = AddPolygon(CSX, '" << material << "', " << priority << ", 2, " << layer.z0 << ", pgon);" << std::endl;
    }

  return;
}

/*
 * Export vias
 */

void CSXCAD::export_vias(Hyp2Mat::PCB& pcb)
{
  if (!pcb.via.empty()) {
    /* create via material */
    std::cout << "% via copper" << std::endl;
    std::cout << "CSX = AddMetal( CSX, 'via' );" << std::endl;
    for (ViaList::iterator i = pcb.via.begin(); i != pcb.via.end(); ++i) {
      double z0 = i->z0;
      double z1 = i->z1;
      std::cout << "CSX = AddCylinder(CSX, 'via', " << prio_via ;
      std::cout << ", [ " << i->x << " , " << i->y << " , " << z0;
      std::cout << " ], [ " << i->x << " , " << i->y << " , " << z1;
      std::cout << " ], " <<  i->radius << ");" << std::endl;
      }
    }
  return;
}
    
/*
 * Export devices
 */

void CSXCAD::export_devices(Hyp2Mat::PCB& pcb)
{
  std::cout << "% devices" << std::endl;
  std::cout << "CSX.HyperLynxDevice = {};" << std::endl;
  for (DeviceList::iterator i = pcb.device.begin(); i != pcb.device.end(); ++i) {
    std::cout << "CSX.HyperLynxDevice{end+1} = struct('name', " << string2matlab(i->name) << ", 'ref', " << string2matlab(i->ref);

    /* output device value if available */
    if (i->value_type == DEVICE_VALUE_FLOAT) 
      std::cout << ", 'value', " << i->value_float;
    else if (i->value_type == DEVICE_VALUE_STRING) 
       std::cout << ", 'value', " << string2matlab(i->value_string);

    std::cout << ", 'layer_name', " << string2matlab(i->layer_name);
    std::cout << ");" << std::endl; 
    }
}

/*
 * Export port
 */

void CSXCAD::export_ports(Hyp2Mat::PCB& pcb)
{
  std::cout << "% ports" << std::endl;
  std::cout << "CSX.HyperLynxPort = {};" << std::endl;
  for (PinList::iterator i = pcb.pin.begin(); i != pcb.pin.end(); ++i) {
    /* csxcad requires rectangular ports, axis aligned. Use the bounding box. XXX fixme ? Use largest inscribed rectangle instead */
    double x_max = 0, y_max = 0, x_min = 0, y_min = 0;
    bool first = true;
    for (FloatPolygon::iterator j = i->metal.begin(); j != i->metal.end(); ++j) {
      if ((j->x > x_max) || first) x_max = j->x;
      if ((j->y > y_max) || first) y_max = j->y;
      if ((j->x < x_min) || first) x_min = j->x;
      if ((j->y < y_min) || first) y_min = j->y;
      first = false;
      }

    /* determine whether port is on top or bottom layer of pcb */
    double dbottom = std::abs(i->z0 - pcb.stackup.back().z0);
    double dtop = std::abs(i->z1 - pcb.stackup.front().z1);
    bool on_top = dtop <= dbottom;

    std::cout << "CSX.HyperLynxPort{end+1} = struct('ref', " << string2matlab(i->ref);
    std::cout << ", 'xc', " << i->x  << ", 'yc', " << i->y << ", 'z', " << i->z0; 
    std::cout << ", 'x1', " << x_min  << ", 'y1', " << y_min ; 
    std::cout << ", 'x2', " << x_max  << ", 'y2', " << y_max ; 
    std::cout << ", 'position', " << (on_top ? "'top'" : "'bottom'"); 
    std::cout << ", 'layer_name', " <<  string2matlab(i->layer_name) << ");" << std::endl;
    }
  return;
}

/*
 * Write pcb to file in CSXCAD format 
 */

void CSXCAD::Write(const std::string& filename, Hyp2Mat::PCB pcb, bool pcb_outline, bool lossy_copper, bool metal_3d)
{

  /* open file for output */

  if ((filename != "-") && (freopen(filename.c_str(), "w", stdout) == NULL)) {
    std::cerr << "could not open '" << filename << "' for writing";
    return;
    }

  /* Export dielectric. If pcb_outline is true, export exact board shape, including holes. 
     If pcb_outline is false, export bounding box. */
  export_board(pcb, pcb_outline);

  /* Export copper */
  std::cout << "% copper" << std::endl;
  bool odd = false;
  for (LayerList::iterator l = pcb.stackup.begin(); l != pcb.stackup.end(); ++l) {
    if (l->layer_type == LAYER_DIELECTRIC) odd = !odd;
    export_layer(pcb, *l, lossy_copper, metal_3d, odd);
    }

  /* Export vias */
  export_vias(pcb);

  export_devices(pcb);

  export_ports(pcb);

  std::cout << "%not truncated" << std::endl;

  fclose(stdout);

  return;
}

/* not truncated */