1/* ICUTImportSub.m
2 * Subclass of icut-import managing the creation of graphic objects
3 *
4 * Copyright (C) 1996-2005 by vhf interservice GmbH
5 * Author:   Georg Fleischmann
6 *
7 * created:  2011-09-16
8 * modified: 2012-06-22 (shape added, any layer with names possible)
9 *
10 * This file is part of the vhf Import Library.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the vhf Public License as
14 * published by the vhf interservice GmbH. Among other things,
15 * the License requires that the copyright notices and this notice
16 * be preserved on all copies.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the vhf Public License for more details.
22 *
23 * You should have received a copy of the vhf Public License along
24 * with this library; see the file LICENSE. If not, write to vhf.
25 *
26 * If you want to link this library to your proprietary software,
27 * or for other uses which are not covered by the definitions
28 * laid down in the vhf Public License, vhf also offers a proprietary
29 * license scheme. See the vhf internet pages or ask for details.
30 *
31 * vhf interservice GmbH, Im Marxle 3, 72119 Altingen, Germany
32 * eMail: info@vhf.de
33 * http://www.vhf.de
34 */
35
36#include <VHFShared/vhfCommonFunctions.h>
37#include <VHFShared/types.h>
38#include "ICUTImportSub.h"
39#include "Graphics.h"
40#include "DocView.h"
41
42@implementation ICUTImportSub
43
44/* allocate a list
45 */
46- allocateList
47{
48    layerList = [[NSMutableArray allocWithZone:[self zone]] init];
49
50    {   LayerObject	*layerObject = [LayerObject layerObject];
51
52        [layerObject setString:@"Kamera Marker"];
53        [layerObject setType:LAYER_CAMERA];
54        [layerList addObject:layerObject];
55    }
56    return layerList;
57}
58
59- (NSMutableArray*)layerArrayWithName:(NSString*)name
60{   int	l;
61
62    if (!layerList)
63        layerList = [[NSMutableArray allocWithZone:[self zone]] init];
64    if (!name)
65        name = ops.cutcontour;
66
67    if (![layerList count])
68    {   LayerObject	*layerObject = [LayerObject layerObject];
69
70        [layerObject setString:@"Kamera Marker"];
71        [layerObject setType:LAYER_CAMERA];
72        [layerList addObject:layerObject];
73    }
74
75    for (l=0; l<(int)[layerList count]; l++)
76        if ([[[layerList objectAtIndex:l] string] isEqual:name])
77            return [[layerList objectAtIndex:l] list];
78
79    /* not yet in layerList */
80    {   LayerObject	*layerObject = [LayerObject layerObject];
81
82        [layerObject setString:name];
83        [layerList insertObject:layerObject atIndex:0];
84        return [layerObject list];
85    }
86    return nil;
87}
88
89
90/* add list as filled path to layer
91 * we simply add everything in a single list ignoring all layers
92 */
93- (void)addFillList:aList toLayer:(NSString*)layerName
94{   NSMutableArray  *array = [self layerArrayWithName:layerName];
95    VPath           *g = [VPath path];
96
97    if (!array)
98        array = [[layerList objectAtIndex:0] list];
99    [g addList:aList at:[[g list] count]];
100    [g setFilled:YES];
101    [array addObject:g];
102}
103/* allocate a filled path object
104 * copy the objects in aList to this object, add the group to bList
105 */
106- (void)addFillList:aList toList:(NSMutableArray*)bList
107{   VPath   *g = [VPath path];
108
109    [g addList:aList at:[(NSArray*)[g list] count]];
110    [g setFilled:YES];
111    [bList addObject:g];
112}
113
114/* add list as filled path to layer
115 * we simply add everything in a single list ignoring all layers
116 */
117- (void)addStrokeList:(NSArray*)aList toLayer:(NSString*)layerName
118{   NSMutableArray  *array = [self layerArrayWithName:layerName];
119    VPath           *g = [VPath path];
120
121    if (!array)
122        array = [[layerList objectAtIndex:0] list];
123    if ( [aList count] == 1 )
124        g = [aList objectAtIndex:0];
125    else
126    {   [g addList:aList at:[[g list] count]];
127        [g setFilled:NO];
128    }
129    [array addObject:g];
130}
131/* allocate a group object
132 * copy the objects in aList to the group, add the group to bList
133 */
134- (void)addStrokeList:(NSArray*)aList toList:(NSMutableArray*)bList
135{   VPath   *g = [VPath path];
136
137    if ( [aList count] == 1 )
138        g = [aList objectAtIndex:0];
139    else
140    {   [g addList:aList at:[[g list] count]];
141        [g setFilled:NO];
142    }
143    [bList addObject:g];
144}
145
146- (void)addLine:(NSPoint)beg :(NSPoint)end toLayer:(NSString*)layerName
147{   NSMutableArray	*array = [self layerArrayWithName:layerName];
148    VLine		*g;
149
150    if (!array)
151        array = [[layerList objectAtIndex:0] list];
152    g = [VLine line];
153    [g setVertices:beg :end];
154    [array addObject:g];
155}
156/* allocate a line object and add it to aList
157 */
158- (void)addLine:(NSPoint)beg :(NSPoint)end toList:(NSMutableArray*)aList
159{   VLine	*g = [VLine line];
160
161    [g setVertices:beg :end];
162    [aList addObject:g];
163}
164
165/* allocate an mark object and add it to aList
166 */
167 - (void)addMark:(NSPoint)origin toLayer:(NSString*)layerName
168{   NSMutableArray	*array = [self layerArrayWithName:@"Kamera Marker"]; // allways to Kamera Marker
169    VArc	*g = [VMark markWithOrigin:origin diameter:MMToInternal(5.0)];
170    int     i, cnt = 0;
171    BOOL    DoNotAdd = NO;
172
173    if (!array)
174        array = [[layerList objectAtIndex:0] list];
175    cnt = [array count];
176    /* check if allready in list */
177    for (i=0; i < cnt ; i++)
178    {   NSPoint originGr = [[array objectAtIndex:i] pointWithNum:0];
179
180        if ( SqrDistPoints(origin, originGr) < TOLERANCE )
181        {   DoNotAdd = YES;
182            break;
183        }
184    }
185    if ( !DoNotAdd )
186        [array addObject:g];
187}
188- (void)addMark:(NSPoint)origin toList:(NSMutableArray*)aList
189{   VArc	*g = [VMark markWithOrigin:origin diameter:MMToInternal(5.0)];
190    int     i, cnt = [(NSArray*)aList count];
191    BOOL    DoNotAdd = NO;
192
193    /* check if allready in list */
194    for (i=0; i < cnt ; i++)
195    {   NSPoint originGr = [[aList objectAtIndex:i] pointWithNum:0];
196
197        if ( SqrDistPoints(origin, originGr) < TOLERANCE )
198        {   DoNotAdd = YES;
199            break;
200        }
201    }
202    if ( !DoNotAdd )
203        [aList addObject:g];
204}
205
206- (void)addRect:(NSPoint)origin :(NSPoint)rsize toLayer:(NSString*)layerName
207{   NSMutableArray	*array = [self layerArrayWithName:layerName];
208    VRectangle	*g = [VRectangle rectangle];
209
210    if (!array)
211        array = [[layerList objectAtIndex:0] list];
212    [g setVertices:origin :rsize];
213    [array addObject:g];
214}
215- (void)addRect:(NSPoint)origin :(NSPoint)rsize toList:(NSMutableArray*)aList
216{   VRectangle	*g = [VRectangle rectangle];
217
218    [g setVertices:origin :rsize];
219    [aList addObject:g];
220}
221
222/* allocate a curve object and add it to aList
223 */
224- (void)addCurve:(NSPoint)p0 :(NSPoint)p1 :(NSPoint)p2 :(NSPoint)p3 toLayer:(NSString*)layerName
225{   NSMutableArray  *array = [self layerArrayWithName:layerName];
226    VCurve          *g = [VCurve curve];
227
228    if (!array)
229        array = [[layerList objectAtIndex:0] list];
230    [g setVertices:p0 :p1 :p2 :p3];
231    [array addObject:g];
232}
233- (void)addCurve:(NSPoint)p0 :(NSPoint)p1 :(NSPoint)p2 :(NSPoint)p3 toList:(NSMutableArray*)aList
234{   VCurve	*g = [VCurve curve];
235
236    [g setVertices:p0 :p1 :p2 :p3];
237    [aList addObject:g];
238}
239
240/* set the bounds
241 * we move the graphic to 0/0
242 */
243- (void)setBounds:(NSRect)bounds
244{   int			i, j;
245    NSPoint		p;
246    NSMutableArray	*array = [self list]; // LayerObjects !
247
248    p.x = - bounds.origin.x + MMToInternal(10.0);
249    p.y = - bounds.origin.y + MMToInternal(10.0);
250    for (i=[array count]-1; i>=0; i--)
251    {   NSMutableArray *llist = [[array objectAtIndex:i] list];
252
253        for (j=[llist count]-1; j>=0; j--)
254            [[llist objectAtIndex:j] moveBy:p];
255    }
256
257    if ( originUL )
258    {
259        bounds.origin.x += p.x;
260        bounds.origin.y += p.y;
261        p.x = bounds.origin.x + bounds.size.width / 2.0;
262        p.y = bounds.origin.y + bounds.size.height / 2.0;
263
264        for (i=[array count]-1; i>=0; i--)
265        {   NSMutableArray *llist = [[array objectAtIndex:i] list];
266
267            for (j=[llist count]-1; j>=0; j--)
268                [[llist objectAtIndex:j] mirrorAround:p];
269        }
270    }
271}
272
273
274@end
275