File: GerberImportSub.m

package info (click to toggle)
cenon.app 4.0.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 35,392 kB
  • sloc: objc: 74,441; ansic: 1,270; sh: 124; makefile: 35
file content (538 lines) | stat: -rw-r--r-- 15,512 bytes parent folder | download
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* GerberImportSub.m
 * Subclass of Gerber-import managing the creation of graphic objects
 *
 * Copyright (C) 1996-2015 by Cenon GmbH
 * Author:   Georg Fleischmann
 *
 * created:  1996-05-03
 * modified: 2015-09-02 (-setBounds: Prefs_ImportMoveToOrigin)
 *
 * This file is part of the Cenon Import Library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Cenon Public License as
 * published by the Cenon GmbH. Among other things,
 * the License requires that the copyright notices and this notice
 * be preserved on all copies.
 *
 * This library 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 Cenon Public License for more details.
 *
 * You should have received a copy of the Cenon Public License along
 * with this library; see the file LICENSE. If not, write to Cenon.
 *
 * If you want to link this library to your proprietary software,
 * or for other uses which are not covered by the definitions
 * laid down in the Cenon Public License, vhf also offers a proprietary
 * license scheme. See the vhf internet pages or ask for details.
 *
 * Cenon GmbH, Schwabstr. 45, 72108 Rottenburg a.N., Germany
 * eMail: info@Cenon.com
 * http://www.Cenon.de
 */

#include <ctype.h>
#include <VHFShared/vhfCommonFunctions.h>
#include <VHFShared/types.h>
#include "PreferencesMacros.h"  // Prefs_ImportMoveToOrigin
#include "GerberImportSub.h"
#include "Graphics.h"
#include "GraphicObjects.subproj/HiddenArea.h"

@implementation GerberImportSub

/* origin graphics will be moved to in -setBounds
 * created: 2015-09-02
 */
- (void)setOrigin:(NSPoint)point
{
    boundsOrigin = point;
}


- init
{
    self = [super init];
    boundsOrigin = NSMakePoint(MMToInternal(10.0), MMToInternal(10.0));
    return self;
}

/* allocate a list
 */
- allocateList
{
    return [[NSMutableArray allocWithZone:[self zone]] init];
}

- (void)removeClearLayers:bList
{   int		i;
    NSColor	*whiteColor = [NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0];

    [[[HiddenArea new] autorelease] removeHiddenAreas:bList];

    for (i=(int)[(NSArray*)bList count]-1; i>=0; i--)
    {	VGraphic	*g=[bList objectAtIndex:i];

        /* remove the white graphics */
        if ( [[g color] isEqual:whiteColor] )
            [bList removeObjectAtIndex:i];
    }
}
/* laying a rectangle (4 lines) around hole list
 * AND build one polygon of the hole list -> so we change black to white and vice versa
 */
- (void)changeListPolarity:bList bounds:(NSRect)bounds
{   VPath       *g = [VPath path];
    VLine       *l = [VLine line];
    NSPoint     beg, end;
    int         i, cnt=[(NSArray*)bList count];

    [g setFilled:YES];
    [g setColor:state.color];
    [g setWidth:state.width];

    beg = end = bounds.origin;
    beg.x -= 10.0; beg.y -= 10.0; end.y -= 10.0;
    end.x += bounds.size.width + 10.0;
    [l setVertices:beg :end];
    [l setWidth:state.width];
    [l setColor:state.color];
    [[g list] addObject:l];
    beg = end;
    end.y += bounds.size.height + 20.0;
    l = [VLine line];
    [l setVertices:beg :end];
    [l setWidth:state.width];
    [l setColor:state.color];
    [[g list] addObject:l];
    beg = end;
    end.x -= bounds.size.width + 20.0;
    l = [VLine line];
    [l setVertices:beg :end];
    [l setWidth:state.width];
    [l setColor:state.color];
    [[g list] addObject:l];
    beg = end;
    end.y -= bounds.size.height + 20.0;
    l = [VLine line];
    [l setVertices:beg :end];
    [l setWidth:state.width];
    [l setColor:state.color];
    [[g list] addObject:l];

    for (i=0; i<cnt; i++)
    {	VGraphic	*gr = [bList objectAtIndex:i];

        if ( [gr isKindOfClass:[VPath class]] )
            [g addList:[(VPath*)gr list] at:[[g list] count]];
        else
            [[g list] addObject:l];
    }

    [bList removeAllObjects];
    [bList addObject:g];
}

/* allocate a group object
 * copy the objects in aList to the group, add the group to bList
 */
- (void)addStrokeList:(NSArray*)aList toList:(NSMutableArray*)bList
{   VGraphic    *g;

    if ([aList count] == 1)
        g = [aList objectAtIndex:0];
    else
    {   g = [VPath path];
        [(VPath*)g addList:aList at:[[(VPath*)g list] count]];
    }
    [g setFilled:NO];
    [g setWidth:state.width];
    [g setColor:state.color];
    [bList addObject:g];
}

/* allocate a filled path object
 * copy the objects in aList to this object, add the group to bList
 */
- (void)addFillList:(NSArray*)aList toList:(NSMutableArray*)bList
{   VGraphic    *g;

    if ([aList count] == 1)
        g = [aList objectAtIndex:0];
    else
    {   g = [VPath path];
        [(VPath*)g addList:aList at:[[(VPath*)g list] count]];
    }
    [g setFilled:YES];
    [g setWidth:0.0]; // instead state.width - only used in setPath and setPad -> need both width = 0 !
    [g setColor:state.color];
    if ([g respondsToSelector:@selector(fillColor)])
        [(VPath*)g setFillColor:[state.color copy]];
    [bList addObject:g];
}

- (void)addFillPath:(NSArray*)aList toList:(NSMutableArray*)bList
{   VGraphic    *g;

    if ([aList count] == 1)
        g = [aList objectAtIndex:0];
    else
    {   g = [VPath path];
        [(VPath*)g setList:aList];
    }
    [g setFilled:YES];
    [g setWidth:0.0];
    [g setColor:state.color];
    if ([g respondsToSelector:@selector(fillColor)])
        [(VPath*)g setFillColor:[state.color copy]];
    [bList addObject:g];
}

/* allocate a line object and add it to aList
 */
- (void)addLine:(NSPoint)beg :(NSPoint)end toList:aList
{   VLine   *g; // = [VLine line];

    if ( ![(NSArray*)aList count] )
    {   g = [VLine line];
        [g setVertices:beg :end];
        [g setWidth:state.width];
        [g setColor:state.color];
        [aList addObject:g];
    }
    else // check if last object is a line or polyline and has same end point like beg
    {   NSPoint e = {-1.0, -1.0};
        int     cnt = [(NSArray*)aList count];

        g = [aList objectAtIndex:cnt-1];
        if ( [g isKindOfClass:[VLine class]] )
            e = [g pointWithNum:1];
        else if ( [g isKindOfClass:[VPolyLine class]] )
            e = [(VPolyLine*)g pointWithNum:[(VPolyLine*)g ptsCount]-1];

        if ( e.x != -1.0 && e.y != -1.0 && SqrDistPoints(e, beg) < TOLERANCE
            && Diff(state.width, [g width]) < TOLERANCE && [state.color isEqual:[g color]] )
        {
            if ( [g isKindOfClass:[VLine class]] )
            {   VPolyLine	*pl = [VPolyLine polyLine];

                [pl setWidth:state.width];
                [pl setColor:state.color];
                [pl addPoint:[g pointWithNum:0]];
                [pl addPoint:e];
                [pl addPoint:end];
                [aList removeObjectAtIndex:cnt-1];
                [aList addObject:pl];
            }
            else
                [(VPolyLine*)g addPoint:end]; // we must add only the end point to last polyline in list
        }
        else
        {   g = [VLine line];
            [g setVertices:beg :end];
            [g setWidth:state.width];
            [g setColor:state.color];
            [aList addObject:g];
        }
    }
}

/* allocate a line object and add it to aList
 */
- (void)addRect:(NSPoint)origin :(NSSize)size filled:(BOOL)fill toList:aList
{   VLine           *line;
    VPath           *g = [VPath path];
    NSMutableArray  *pList = [NSMutableArray array];
    NSPoint         p0, p1;

    line = [VLine line];
    [line setColor:state.color];
    p0 = origin;
    p1.x = origin.x + size.width;
    p1.y = origin.y;
    [line setVertices:p0 :p1];
    [pList addObject:line];

    line = [VLine line];
    [line setColor:state.color];
    p0 = p1;
    p1.x = origin.x + size.width;
    p1.y = origin.y + size.height;
    [line setVertices:p0 :p1];
    [pList addObject:line];

    line = [VLine line];
    [line setColor:state.color];
    p0 = p1;
    p1.x = origin.x;
    p1.y = origin.y + size.height;
    [line setVertices:p0 :p1];
    [pList addObject:line];

    line = [VLine line];
    [line setColor:state.color];
    p0 = p1;
    p1.x = origin.x;
    p1.y = origin.y;
    [line setVertices:p0 :p1];
    [pList addObject:line];

    [g setList:pList];
    [g setFilled:YES];
    [g setWidth:(fill) ? 0.0 : state.width];
    [g setColor:state.color];
    [g setFillColor:[state.color copy]];
    [aList addObject:g];
}

- (void)addCircle:(NSPoint)center :(float)radius filled:(BOOL)fill toList:aList
{   VArc		*g;
    NSPoint	start;

    start.x = center.x + radius;
    start.y = center.y;

    g = [VArc arc];
    [g setCenter:center start:start angle:360.0];
    [g setFilled:fill];
    [g setWidth:(fill) ? 0.0 :state.width];
    [g setColor:state.color];
    [g setFillColor:[state.color copy]];
    [aList addObject:g];
}

- (void)addOctagon:(NSPoint)center :(float)radius filled:(BOOL)fill toList:aList
{   NSPoint         p[8];
    VLine           *line;
    VPath           *g = [VPath path];
    NSMutableArray  *pList = [NSMutableArray array];
    int             i;

    for (i=0; i<8; i++)
    {	double	angle = DegToRad(22.5 + i * 45.0);

        p[i].x = center.x + radius*cos(angle);
        p[i].y = center.y + radius*sin(angle);
    }

    for (i=0; i<8; i++)
    {	line = [VLine line];
        [line setColor:state.color];
        [line setVertices:p[i] :(i>=7) ? p[0] : p[i+1]];
        [pList addObject:line];
    }

    [g setList:pList];
    [g setFilled:YES];
    [g setWidth:(fill) ? 0.0 :state.width];
    [g setColor:state.color];
    [g setFillColor:[state.color copy]];
    [aList addObject:g];
}

- (void)addObround:(NSPoint)center :(float)width :(float)height filled:(BOOL)fill toList:aList
{
    if ( width == height ) // circle
    {   VArc	*g;
        NSPoint	start;

        start.x = center.x + width/2.0;
        start.y = center.y;

        g = [VArc arc];
        [g setCenter:center start:start angle:360.0];
        [g setWidth:(fill) ? 0.0 :state.width];
        [g setFilled:fill];
        [g setColor:state.color];
        [g setFillColor:[state.color copy]];
        [aList addObject:g];
        return;
    }

    if ( width < height ) // vertical aranged
    {   VArc	*arc;
        VLine	*line;
        VPath	*path;
        NSPoint	s, e, c;
        float	radius = width/2.0, lineHalf = (height - width)/2.0; // w, h is the hole width, height !

        path = [VPath path];

        [path setFilled:YES];
        [path setWidth:0.0];
        [path setColor:state.color];
        [path setFillColor:[state.color copy]];

        s.x = e.x = center.x + radius;
        s.y = center.y - lineHalf;
        e.y = center.y + lineHalf;
  	line = [VLine line];
        [line setVertices:s :e];
        [line setWidth:0.0];
        [line setColor:state.color];
        [[path list] addObject:line];

        s = e;
        c.x = center.x;
        c.y = center.y + lineHalf;
        arc = [VArc arc];
        [arc setCenter:c start:s angle:180.0];
        [arc setWidth:0.0];
        [arc setColor:state.color];
        [arc setFilled:NO];
        [[path list] addObject:arc];

        s.x = e.x = center.x - radius;
        s.y = center.y + lineHalf;
        e.y = center.y - lineHalf;
  	line = [VLine line];
        [line setVertices:s :e];
        [line setWidth:0.0];
        [line setColor:state.color];
        [[path list] addObject:line];

        s = e;
        c.x = center.x;
        c.y = center.y - lineHalf;
        arc = [VArc arc];
        [arc setCenter:c start:s angle:180.0];
        [arc setWidth:0.0];
        [arc setColor:state.color];
        [arc setFilled:NO];
        [[path list] addObject:arc];

        [aList addObject:path];
    }
    else // if ( width > height ) // horicontal aranged
    {   VArc	*arc;
        VLine	*line;
        VPath	*path;
        NSPoint	s, e, c;
        float	radius = height/2.0, lineHalf = (width - height)/2.0; // w, h is the hole width, height !

        path = [VPath path];

        [path setFilled:YES];
        [path setWidth:0.0];
        [path setColor:state.color];
        [path setFillColor:[state.color copy]];

        s.y = e.y = center.y - radius;
        s.x = center.x - lineHalf;
        e.x = center.x + lineHalf;
  	line = [VLine line];
        [line setVertices:s :e];
        [line setWidth:0.0];
        [line setColor:state.color];
        [[path list] addObject:line];

        s = e;
        c.x = center.x + lineHalf;
        c.y = center.y;
        arc = [VArc arc];
        [arc setCenter:c start:s angle:180.0];
        [arc setWidth:0.0];
        [arc setColor:state.color];
        [arc setFilled:NO];
        [[path list] addObject:arc];

        s.y = e.y = center.y + radius;
        s.x = center.x + lineHalf;
        e.x = center.x - lineHalf;
  	line = [VLine line];
        [line setVertices:s :e];
        [line setWidth:0.0];
        [line setColor:state.color];
        [[path list] addObject:line];

        s = e;
        c.x = center.x - lineHalf;
        c.y = center.y;
        arc = [VArc arc];
        [arc setCenter:c start:s angle:180.0];
        [arc setWidth:0.0];
        [arc setColor:state.color];
        [arc setFilled:NO];
        [[path list] addObject:arc];

        [aList addObject:path];
    }
}

- (void)addPolygon:(NSPoint)center :(float)width :(int)sides filled:(BOOL)fill toList:aList
{   int             i;
    VLine           *line;
    VPath           *g = [VPath path];
    NSMutableArray  *pList = [NSMutableArray array];
    NSPoint         p[sides];
    float           a = 0.0, radius, alpha = 360.0/sides;

    radius = (width/2.0) / cos(DegToRad(alpha/2.0));

    for (i=0; i<sides; i++)
    {	double	angle = DegToRad(a + i * alpha);

        p[i].x = center.x + radius*cos(angle);
        p[i].y = center.y + radius*sin(angle);
    }

    for (i=0; i<sides; i++)
    {	line = [VLine line];
        [line setColor:state.color];
        [line setVertices:p[i] :(i>=sides-1) ? p[0] : p[i+1]];
        [pList addObject:line];
    }

    [g setList:pList];
    [g setFilled:YES];
    [g setWidth:(fill) ? 0.0 :state.width];
    [g setColor:state.color];
    [g setFillColor:[state.color copy]];
    [aList addObject:g];
}

/* allocate an arc object and add it to aList
 * center is the center of the arc
 * start is the start point
 * angle is the angle (negative for clockwise direction and positive for ccw direction)
 */
- (void)addArc:(NSPoint)center :(NSPoint)start :(float)angle toList:aList
{   VArc	*g;

    g = [VArc arc];
    [g setCenter:center start:start angle:angle];
    [g setWidth:state.width];
    [g setColor:state.color];
    [g setFillColor:[state.color copy]];
    [aList addObject:g];
}

- (void)moveListBy:(NSPoint)pt :aList
{   int	i;

    for (i=[(NSArray*)aList count]-1; i>=0; i--)
        [[aList objectAtIndex:i] moveBy:pt];
}

/* set the bounds
 * we move the graphic to 0/0
 * modified: 2015-09-02 (Prefs_ImportMoveToOrigin, boundsOrigin)
 */
- (void)setBounds:(NSRect)bounds
{   //DocView *view = [[(App*)NSApp currentDocument] documentView];
    //NSPoint origin = ((view) ? [[view origin] pointWithNum:0] : NSMakePoint(10.0, 10.0));

    if ( Prefs_ImportMoveToOrigin )
    {   int		i;
        NSPoint p;

        p.x = - bounds.origin.x + boundsOrigin.x;   //MMToInternal(10.0);
        p.y = - bounds.origin.y + boundsOrigin.y;   //MMToInternal(10.0);
        for (i=[(NSArray*)list count]-1; i>=0; i--)
            [[list objectAtIndex:i] moveBy:p];
    }
}

@end