private string StringForFirstPoint(ValidatedObject vObj, ref int index) { if (vObj.Vertexes.Count == 2) { return(Templates.Printing.Line(ref index, vObj.StartPoint.X, vObj.StartPoint.Y, false)); } return(""); }
private string StringForLine(ValidatedObject vObj, ref int index) { if (vObj.Type == AvailableTypes.LINE) { if (vObj.Vertexes.Count == 2) { return(Templates.Printing.Line(ref index, vObj.EndPoint.X, vObj.EndPoint.Y, vObj.Properties.Pumping)); } } return(""); }
private string StringForArc(ValidatedObject vObj, ref int index) { if (vObj.Type == AvailableTypes.ARC) { if (vObj.Vertexes.Count == 3) { return(Templates.Printing.Arc(ref index, vObj.EndPoint.X, vObj.EndPoint.Y, vObj.CenterPoint.X, vObj.CenterPoint.Y, vObj.IsClockWise, vObj.Properties.Pumping)); } } return(""); }
private string StringForCommand(ValidatedObject vObj, ref int index) { if (vObj.IsCommand) { switch (vObj.Properties.CommandType) { case CommandType.None: return(""); case CommandType.StopAndPump: return(Templates.Commands.StopAndPump(ref index, vObj.Properties.StopAndPump)); case CommandType.DisablePumping: return(Templates.Commands.DisablePumping(ref index, vObj.Properties.DisablePumping)); } } return(""); }
public bool GenerateGcode(List <ValidatedObject> vList, ValidatedObject firstLine) { if (vList.Count < 1) { throw new Exception("List of Valid objects can`t be empty"); } GcodeLines.Clear(); Dictionary <CommandType, ValidProperties> continiouslyCommands = new Dictionary <CommandType, ValidProperties>() { { CommandType.DisablePumping, new ValidProperties(CommandType.DisablePumping) { DisablePumping = false } } }; int lineNumber = 0; //set first segment string line = StringForFirstPoint(vList.First(), ref lineNumber); if (line == "") { throw new Exception("Null object in Valid objecs list"); } GcodeLines.Add(line); foreach (ValidatedObject vObj in vList) { if (vObj == null) { throw new Exception("Null object in Valid objecs list"); } if (vObj.IsCommand) { line = StringForCommand(vObj, ref lineNumber); if (line == "") { throw new Exception("Returned empty line"); } foreach (ValidProperties cmd in continiouslyCommands.Values) { if (cmd.CommandType == vObj.Properties.CommandType) { if (cmd.CommandType == CommandType.DisablePumping) { if (vObj.Properties.DisablePumping != cmd.DisablePumping) { cmd.DisablePumping = !cmd.DisablePumping; } } } } GcodeLines.Add(line); } else if (vObj.Type == AvailableTypes.LINE) { line = StringForLine(vObj, ref lineNumber); if (line == "") { throw new Exception("Returned empty line"); } GcodeLines.Add(line); } else if (vObj.Type == AvailableTypes.ARC) { if (!vObj.Properties.Pumping && !continiouslyCommands[CommandType.DisablePumping].DisablePumping) { line = StringForCommand(new ValidatedObject(new ValidProperties( CommandType.DisablePumping) { DisablePumping = true }), ref lineNumber); if (line == "") { throw new Exception("Returned empty line"); } GcodeLines.Add(line); line = StringForArc(vObj, ref lineNumber); if (line == "") { throw new Exception("Returned empty line"); } GcodeLines.Add(line); line = StringForCommand(new ValidatedObject(new ValidProperties( CommandType.DisablePumping) { DisablePumping = false }), ref lineNumber); if (line == "") { throw new Exception("Returned empty line"); } GcodeLines.Add(line); } else { line = StringForArc(vObj, ref lineNumber); if (line == "") { throw new Exception("Returned empty line"); } GcodeLines.Add(line); } } } return(true); }
public void StartValidation(List <UserEntity> uEntitys) { isValidated = false; if (uEntitys == null || uEntitys.Count == 0) { throw new Exception("User Entities collection is empty\n"); } //Clear all supported lists validList.Clear(); List <UserEntity> forPrint = new List <UserEntity>(); List <UserEntity> commands = new List <UserEntity>(); FirstLine = null; //Set temporary objects UserEntity firstEntity = null; UserEntity lastEntity = null; List <IntermediateObject> tempInterList = new List <IntermediateObject>(); int orderMax, orderMin, currOrder, index; orderMax = orderMin = uEntitys[0].Properties.Order; currOrder = -1; //Prevent validation iteration foreach (UserEntity uEntity in uEntitys) { /** * Checking available types for entities */ if (UserEntity.CheckType(uEntity.Entity) == false) { throw new Exception("For generating Gcode must using only Line, Polyline and Arc entities\n"); } /** * Finding Firt and Last lines. It`s need for orderinig inside entities */ if (uEntity.Properties.First == true) { if (firstEntity != null) { throw new Exception("First Line have to define only one time\n"); } firstEntity = uEntity; } else if (uEntity.Properties.Last == true) { if (lastEntity != null) { throw new Exception("Last Line have to define only one time\n"); } lastEntity = uEntity; } //Actions for separate printable and command entities if (uEntity.Properties.Printable == true) { /** * Convert all printable entities to polylines */ if (uEntity.ConvertToPolyline() == null) { throw new Exception("Problem with converting entities to polyline\n"); } /** * Filling lists */ if (currOrder == uEntity.Properties.Order) { throw new Exception("The order of entities cannot have multiple equal values\n"); } currOrder = uEntity.Properties.Order; orderMax = Math.Max(orderMax, currOrder); orderMin = Math.Min(orderMin, currOrder); forPrint.Add(uEntity); } else { if (uEntity.Type != AvailableTypes.LINE) { throw new Exception("For command entities available use only simple Lines\n"); } if (uEntity.Properties.Command) { commands.Add(uEntity); } } } /** * Checking values after prevent validation */ if (firstEntity == null || lastEntity == null) { throw new Exception("You have to define First line and last line"); } if (forPrint.Count == 0) { throw new Exception("There are no printable entities\n"); } else if (forPrint.Count == 1) { if (orderMax != orderMin || orderMin != currOrder) { throw new Exception("This is devil tricks. Ordering problem\n"); } } else if (forPrint.Count > 1) { if (orderMin == -1 || (orderMax <= orderMin)) { throw new Exception("Problem in entities ordering\n"); } } /** * Sorting printable entities by order */ forPrint = TreeNode.TreeFromList(forPrint).ToSortList(); /** * Mark userEntities according with continiously path; * Mark "isNeedReverse" if entity have to reverse. */ // Marking first line index = forPrint.First().IndexOfTouching(firstEntity.StartPoint); if (index == -1) { index = forPrint.First().IndexOfTouching(firstEntity.EndPoint); } if (index == -1) { throw new Exception("First line not connected to first entity\n"); } else if (index > 0) { forPrint.First().MarkToReverse(); } //Marking remaining for (int i = 0; i < forPrint.Count - 1; i++) { index = forPrint[i + 1].IndexOfTouching(forPrint[i].StartPoint); if (index == -1) { index = forPrint[i + 1].IndexOfTouching(forPrint[i].EndPoint); } if (index == -1) { throw new Exception("Entity with Order [" + forPrint[i].Properties.Order + "] has not connected to next segment\n"); } else if (index > 0) { forPrint[i + 1].MarkToReverse(); } } //Check last line index = forPrint.Last().IndexOfTouching(lastEntity.StartPoint); if (index == -1) { index = forPrint.Last().IndexOfTouching(lastEntity.EndPoint); } if (index == -1) { throw new Exception("Last line not connected to last entity\n"); } /** * Step that makes Validated objects list (function ToIntermediateList makes Reverse by marks) */ foreach (UserEntity uEntity in forPrint) { tempInterList = uEntity.ToIntermediateList(); if (tempInterList.Count > 0) { foreach (IntermediateObject iObj in tempInterList) { validList.Add(new ValidatedObject(iObj, new ValidProperties(uEntity.Properties))); } } else { throw new Exception("UserEntity with order [" + uEntity.Properties.Order + "] returned empty inermediate state"); } } /** * Final continiously path check */ // Check first segment if (validList.First().IndexOfTouching(firstEntity.StartPoint) == -1 && validList.First().IndexOfTouching(firstEntity.EndPoint) == -1) { throw new Exception("First line not connected to first segment\n"); } //Checking remaining for (int i = 0; i < forPrint.Count - 1; i++) { if (validList[i + 1].IndexOfTouching(validList[i].EndPoint) > 0) { throw new Exception("Segment [" + i + "] with Order [" + forPrint[i].Properties.Order + "] has not connected to next segment\n"); } } //Check last segment if (validList.Last().IndexOfTouching(lastEntity.StartPoint) == -1 && validList.Last().IndexOfTouching(lastEntity.EndPoint) == -1) { throw new Exception("Last line not connected to last segment\n"); } /** * Checking command lines */ foreach (UserEntity command in commands) { /** * Checking notPrintable lines touching to printable entities */ //Use for check that only one point of line touched to printable segments //TODO: Apply command to last point of segment index = -1; for (int i = 0; i < validList.Count; i++) { if (validList[i].IsEmpty == false) { if (validList[i].IndexOfTouching(command.StartPoint) == 0) { index = i; } if (validList[i].IndexOfTouching(command.EndPoint) == 0) { if (index != -1) { throw new Exception("Command line has to touch printing segment only once\n"); } index = i; } } } if (index != -1) { validList.Insert(index, new ValidatedObject(new ValidProperties(command.Properties))); } } isValidated = true; }