Thanks for the document link.
I had gone through the documents but still my problem has not resolved.
About InputMPP File:
File contain task “Supply Lot Sale Agreement” and one resource named “Builder” has already assign to it.
Now the requirement is to assign two more resource “Client” and “Sales Dept.” to the above task and assign timephased data 20d for june’2008 and 20d for july’2008 month.
And at the same time edit the timephase data of already assigned Resource “Builder” to 15 days and 20 days in June and July month.
Now the problem is for already assign resource “Builder”. Timephased data is not reflecting in the outputMPP file.
i am sharing my code below and attaching the ImputMPP , OutputMPP and ExpectedMPP snap shot.
Awaiting your quick reply.
public void UpdateMPPFile1()
{
string dataDir = “C:\ABC\”;
try
{
Project project = new Project(dataDir + "MPPFileUpdate2.mpp") { CalculationMode = CalculationMode.None };
Task tsk1 = project.SelectAllChildTasks().FirstOrDefault(t => t.Get(Tsk.Name) == "Supply Lot Sale Agreement");
foreach (ResourceAssignment assignment in tsk1.Assignments)
{
assignment.Set(Asn.WorkContour, WorkContourType.Contoured);
assignment.Set(Asn.Units, 0);
assignment.TimephasedData.Clear();
CreateTimephasedDataAssignment(
project,
assignment,
new DateTime(2008, 6, 1, 8, 0, 0),
new DateTime(2008, 6, 30, 16, 0, 0),
120 * 60, false);
CreateTimephasedDataAssignment(
project,
assignment,
new DateTime(2008, 7, 1, 8, 0, 0),
new DateTime(2008, 7, 31, 16, 0, 0),
160 * 60, true);
}
Resource rsc1 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Name) == "Sales Dept.");
ResourceAssignment assignment1 = project.ResourceAssignments.Add(tsk1, rsc1);
assignment1.Set(Asn.WorkContour, WorkContourType.Contoured);
assignment1.Set(Asn.Units, 0);
ClearTimephasedData(assignment1);
CreateTimephasedDataAssignment(
project,
assignment1,
new DateTime(2008, 6, 1, 8, 0, 0),
new DateTime(2008, 6, 30, 16, 0, 0),
160 * 60, false);
CreateTimephasedDataAssignment(
project,
assignment1,
new DateTime(2008, 7, 1, 8, 0, 0),
new DateTime(2008, 7, 31, 16, 0, 0),
160 * 60, true);
Resource rsc2 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Name) == "Client");
ResourceAssignment assignment2 = project.ResourceAssignments.Add(tsk1, rsc2);
assignment2.Set(Asn.WorkContour, WorkContourType.Contoured);
assignment2.Set(Asn.Units, 0);
ClearTimephasedData(assignment2);
CreateTimephasedDataAssignment(
project,
assignment2,
new DateTime(2008, 6, 1, 8, 0, 0),
new DateTime(2008, 6, 30, 16, 0, 0),
120 * 60, false);
CreateTimephasedDataAssignment(
project,
assignment2,
new DateTime(2008, 7, 1, 8, 0, 0),
new DateTime(2008, 7, 31, 16, 0, 0),
160 * 60, true);
project.RecalculateResourceStartFinish();
project.RecalculateResourceFields();
project.Recalculate();
// Save the project as MPP project file
project.Save(dataDir + "AfterLinking_out.mpp");//,Aspose.Tasks.Saving.SaveFileFormat.MPP);
}
catch (Exception e)
{
}
}
private static IEnumerable<DateTime> EachDay(DateTime from, DateTime to)
{
for (var day = from.Date; day.Date <= to.Date; day = day.AddDays(1))
{
yield return day;
}
}
private static int DaysWorking(Aspose.Tasks.Calendar calendar, DateTime dateStart, DateTime dateFinish)
{
int nDaysWorking = 0;
if (calendar != null)
{
var nExceptions = calendar.Exceptions.Count(p => p.FromDate >= dateStart && p.FromDate <= dateFinish);
foreach (DateTime day in EachDay(dateStart, dateFinish))
{
if (calendar.IsDayWorking(day))
{
nDaysWorking++;
}
}
return nDaysWorking - nExceptions;
}
foreach (DateTime day in EachDay(dateStart, dateFinish))
{
nDaysWorking++;
}
return nDaysWorking;
}
private static void ClearTimephasedData(ResourceAssignment assignment)
{
assignment.TimephasedData.Clear();
}
private static void CreateTimephasedDataAssignment(Project project, ResourceAssignment assignment, DateTime dateStart,
DateTime dateFinish, double workInMinutes, bool isLastPortion)
{
var cal = assignment.Get(Asn.Task).Get(Tsk.Calendar) == null ? project.Get(Prj.Calendar) : assignment.Get(Asn.Task).Get(Tsk.Calendar);
var nDaysWorking = DaysWorking(cal, dateStart, dateFinish);
double workPerDay = nDaysWorking == 0 ? 0 : workInMinutes / nDaysWorking;
workPerDay = Math.Round(workPerDay, 1);
var days = EachDay(dateStart, dateFinish).ToList();
while (!cal.IsDayWorking(days[0]))
{
days.RemoveAt(0);
}
var tds = new List<TimephasedData>();
DateTime lastNonWorkingDay = DateTime.MinValue;
DateTime lastWorkingDay = DateTime.MinValue;
foreach (DateTime day in days)
{
if (cal.IsDayWorking(day))
{
if (lastNonWorkingDay != DateTime.MinValue)
{
var start = lastWorkingDay.AddDays(1);
var finish = lastNonWorkingDay;
finish = cal.GetNextWorkingDayStart(finish);
TimephasedData item = TimephasedData.CreateWorkTimephased(
assignment.Get(Asn.Uid),
start,
finish,
TimeSpan.Zero,
TimeUnitType.Hour,
TimephasedDataType.AssignmentRemainingWork);
tds.Add(item);
lastNonWorkingDay = DateTime.MinValue;
}
var workStart = cal.GetNextWorkingDayStart(day.AddDays(-1));
var fromHours = TimeSpan.FromMinutes(workPerDay);
TimephasedData td = TimephasedData.CreateWorkTimephased(
assignment.Get(Asn.Uid),
workStart,
workStart.AddDays(1),
fromHours,
TimeUnitType.Hour,
TimephasedDataType.AssignmentRemainingWork);
tds.Add(td);
lastWorkingDay = workStart;
}
else
{
lastNonWorkingDay = day;
}
}<a class="attachment" href="/uploads/default/50216">ExpectedMPP.png</a> (32.3 KB)
InputMPP.png (31.7 KB)
OutputMPP.png (27.3 KB)
ExpectedMPP.png (32.3 KB)
if (isLastPortion)
{
tds[tds.Count - 1].Finish = cal.GetPreviousWorkingDayEnd(tds[tds.Count - 1].Finish);
}
assignment.TimephasedData.AddRange(tds);
}