Hi Sankar,
I have observed following issues in your code:
- Child count is manually set which is not recommended.
- BaseLineStart and BaseLine Finish dates are wrong.
- PercentageComeplete is set manually whereas setting ActualStart and ActualFinish set this valaue automatically.
- Task Start, Finish and Duration are set manually however it is recommended that only start and duration be set, while Finish date be calculated by the library itself.
- Summary task start, finish duration etc. is set manually however it should also be calculated by the library.
- While setting the Predecessor of a task, name of the predecessor task is set instead of ID.
Here is a sample code which can be used as guide line to accomplish the task.
static public void CreateProject()
{
Project prj = new Project(@“D:\Aspose\Blank2010.mpp”);
//add working days Saturday and Sunday
WorkingTime wt1 = new WorkingTime();
wt1.FromTime = new DateTime(1, 1, 1, 8, 0, 0, 0);
wt1.ToTime = new DateTime(1, 1, 1, 12, 0, 0, 0);
WorkingTime wt2 = new WorkingTime();
wt2.FromTime = new DateTime(1, 1, 1, 13, 0, 0, 0);
wt2.ToTime = new DateTime(1, 1, 1, 17, 0, 0, 0);
prj.Calendars[0].Days[6].DayWorking = true;
//Saturday
prj.Calendars[0].Days[6].WorkingTimes.Add(wt1);
prj.Calendars[0].Days[6].WorkingTimes.Add(wt2);
prj.Calendars[0].Days[6].DayWorking = true;
//Sunday
prj.Calendars[0].Days[0].WorkingTimes.Add(wt1);
prj.Calendars[0].Days[0].WorkingTimes.Add(wt2);
prj.Calendars[0].Days[0].DayWorking = true;
List<string> ResourceNamesList = new List<string>(new string[] { “Mark”, “bob”, “Jcob” });
List ResourcesList = new List();
Aspose.Tasks.Task rootTask = prj.RootTask;
for (int i = 0; i <= 2; i++)
{
Resource res = GetTestResource(ResourceNamesList[i]);
prj.Resources.Add(res);
}
//Define Tasks
List<string> SummaryNamesList = new List<string>(new string[] { “Activity 1”, “Activity 2”, “Activity 3” });
List<string> TaskNamesList = new List<string>(new string[] { “Task 1”, “Task 2”, “Task 3” });
for (int iSummary = 0; iSummary <= 2; iSummary++)
{
Aspose.Tasks.Task Summary = new Aspose.Tasks.Task(SummaryNamesList[iSummary]);
for (int iTask = 0; iTask <= 2; iTask++)
{
Aspose.Tasks.Task task = new Aspose.Tasks.Task(TaskNamesList[iTask]);
if (iTask == 0)
{
task.Start = (new DateTime(2014, 12, 1, 8, 0, 0)).AddDays(6 * iSummary + iTask);
task.ConstraintType = ConstraintType.StartNoEarlierThan;
task.ConstraintDate = task.Start;
}
else
{
//Other tasks start date will be set by adding predecessors
}
task.Duration = new TimeSpan(16 * (1 + iTask), 0, 0);
//Assign resources
ResourceAssignment assgn = new ResourceAssignment(task, prj.Resources[iTask + 1]);
assgn.Start = task.Start;
assgn.Finish = task.Finish;
assgn.Task.Work = assgn.Task.Duration;
assgn.RemainingWork = assgn.Task.Work;
assgn.RegularWork = assgn.RemainingWork;
assgn.Work = assgn.RegularWork;
prj.ResourceAssignments.Add(assgn);
//CurrentTasks.Add(task);
Summary.Children.Add(task);
}
rootTask.Children.Add(Summary);
}
//perform recalculations
Recalculate(prj);
//Add predecessors
prj.AddTaskLink(new TaskLink(prj.GetTaskById(2), prj.GetTaskById(3), TaskLinkType.FinishToStart));
//Add Percentage complete
prj.GetTaskById(2).ActualStart = prj.GetTaskById(2).Start;
prj.GetTaskById(2).ActualFinish = prj.GetTaskById(2).Start.AddDays(2);
prj.AddTaskLink(new TaskLink(prj.GetTaskById(3), prj.GetTaskById(4), TaskLinkType.FinishToStart));
prj.GetTaskById(3).ActualStart = prj.GetTaskById(3).Start;
prj.GetTaskById(3).ActualFinish = prj.GetTaskById(3).Start.AddDays(4);
prj.AddTaskLink(new TaskLink(prj.GetTaskById(6), prj.GetTaskById(7), TaskLinkType.FinishToStart));
prj.AddTaskLink(new TaskLink(prj.GetTaskById(7), prj.GetTaskById(8), TaskLinkType.FinishToStart));
prj.AddTaskLink(new TaskLink(prj.GetTaskById(10), prj.GetTaskById(11), TaskLinkType.FinishToStart));
prj.AddTaskLink(new TaskLink(prj.GetTaskById(11), prj.GetTaskById(12), TaskLinkType.FinishToStart));
Aspose.Tasks.Task.Recalculate(prj.RootTask); //This is new statment
prj.Save(“ResultProject.MPP”, Aspose.Tasks.Saving.SaveFileFormat.MPP);
}
static public Resource GetTestResource(string name)
{
Resource rsc = new Resource(name);
rsc.MaxUnits = 1;
rsc.Type = ResourceType.Work;
return rsc;
}
static void Recalculate(Project project)
{
project.CalcTaskIds();
project.CalcTaskUids();
project.CalcResourceIds();
project.CalcResourceUids();
project.CalcResourceAssignmentIds();
project.CalcResourceAssignmentUids();
project.CalcCalendarUids();
project.UpdateReferences();
}
Could you please follow this sample to modify your code and let us know your feedback?