Source

Pleasant3D_OpenSource / ToolPlugins / GCodeGenerator / GcodeKernel.m

Full commit
//
//  GCodeKernel.m
//  Pleasant3D
//
//  Created by Eberhard Rensch on 12.08.09.
//  Copyright 2009 Pleasant Software. All rights reserved.
//

#import "GCodeKernel.h"
#import "GcodeGenerator.h"

@implementation GCodeKernel

- (GCode*)generateGCode:(P3DLoops*)loops owner:(P3DToolBase*)owner
{
	NSMutableString* gcode = [[NSMutableString alloc] init];
		
	float travelSpeed = ((GcodeGenerator*)owner).travelFeedRate * 53; // TODO: Get factor from a machine settings file
	float extrudeSpeed = [[((GcodeGenerator*)owner).layerSettings.lastObject objectForKey:@"feedRate"] floatValue]* 53; // TODO: !!!
	
	[loops.layers enumerateObjectsUsingBlock:^(id layer, NSUInteger layerIndex, BOOL *stopLayers) {
		float z = (float)layerIndex*loops.extrusionHeight;
		
		*stopLayers = owner.abortRequested;
		
		[layer enumerateObjectsUsingBlock:^(id loop, NSUInteger loopIndex, BOOL *stopLoops) {
			NSInteger count = ((PSMutableIntegerArray*)loop).count;
			if(count>0)
			{
				InsetLoopCorner* corner = &(loops.loopCorners[[((PSMutableIntegerArray*)loop) integerAtIndex:0]]);
				[gcode appendFormat:@"G1 X%1.3f Y%1.3f Z%1.3f F%1.3f\n",corner->point[0], corner->point[1], z, travelSpeed];
				[gcode appendString:@"M101\n"];
				for(NSUInteger pointIndex=1;pointIndex<count;pointIndex++)
				{
					corner = &(loops.loopCorners[[((PSMutableIntegerArray*)loop) integerAtIndex:pointIndex]]);
					[gcode appendFormat:@"G1 X%1.3f Y%1.3f Z%1.3f F%1.3f\n",corner->point[0], corner->point[1], z, extrudeSpeed];
				}
				[gcode appendString:@"M103\n"];
			}
		}];
	}];
			
	return [[GCode alloc] initWithGCodeString:gcode];
}

@end