[Drawkit] Programmatically adding shapes
Brad Larson
larson at sonoplot.com
Fri May 16 16:19:16 PDT 2008
I've dug down into the framework and I've gotten lost in trying to
figure out how to programmatically add a shape. Currently, all
addition of new objects is done via a DKDrawingTool, where you set up
a prototype object for that tool. Mouse events are passed to the
tool, which interprets them to draw the shapes, and attach them to the
current layer. For loading a file from disk in a non-native format,
you'll need to parse the file and add objects programmatically with
the properties specified in the file. I tried following the
initWithCoder methods all the way down, which might make this clear
due to their internal functioning, but got lost.
My crude start is this:
// Initialize the drawing object
DKDrawing *dwg = [[DKDrawing alloc] initWithSize:NSMakeSize(100, 100)];
// The following is based off of the DKDrawing makeDefaultDrawing
// attach a grid layer
[DKGridLayer setGridThemeColour:[[NSColor blackColor]
colorWithAlphaComponent:0.5]];
DKGridLayer* grid = [[DKGridLayer alloc] init];
[dr addLayer:grid];
[grid tweakDrawingMargins];
[grid release];
// attach a drawing layer and make it the active layer
DKObjectDrawingLayer* layer = [[DKObjectDrawingLayer alloc] init];
[dr addLayer:layer];
[dr setActiveLayer:layer];
[layer release];
// info layer
#ifdef ADD_INFO_LAYER
DKDrawingInfoLayer* infoLayer = [[DKDrawingInfoLayer alloc] init];
[dr addLayer:infoLayer];
[infoLayer setVisible:NO];
[infoLayer release];
#endif
// attach a guide layer
DKGuideLayer* guides = [[DKGuideLayer alloc] init];
[dr addLayer:guides];
[guides release];
// Create a shape to be inserted into the drawing
DKDrawableShape *currentShape = [[DKDrawableShape alloc] init];
// Make this shape a rectangle starting at (0,0) and ending at (50,50)
[shape setPath:[DKShapeFactory rect]];
[shape moveToPoint:NSMakePoint(0.0, 0.0)];
[shape setSize:NSMakeSize(50.0, 50.0)];
I don't know how to proceed. Do I add the shape to the active layer?
If so, how? I might also need to set a style in there, somewhere, for
the shape. Should I be doing this through a DKDrawingTool instead?
I can probably figure this out if I spend some more time reading
through the code, but I wanted to see if there might be an easier
explanation for the process.
______________________
Brad Larson
SonoPlot, Inc.
3030 Laura Lane, Suite 120
Middleton, WI 53562
More information about the Drawkit
mailing list