How use Assignment TimephasedDate?

Hi @kashif.iqbal, how are you?

Could you help me one more time?

I’m trying to implement the topic:

But I’m not getting the expected return.

My example:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
          License license = new License();
          license.SetLicense("Aspose.Tasks.lic");

        Project project = new Project("Template.mpp");

        project.CalculationMode = CalculationMode.None;

        CreateResources(project);
        CreateTasks(project);
        CreateAssignment(project);

        project.Save(@"output1.xml", Aspose.Tasks.Saving.SaveFileFormat.XML);

        project.CalcResourceFields();
        project.CalcResourceStartFinish();
        project.Recalculate();

        project.Save(@"output.xml", Aspose.Tasks.Saving.SaveFileFormat.XML);
        project.Save(@"output.mpp", Aspose.Tasks.Saving.SaveFileFormat.MPP);

        Console.ReadKey();
    }


    static private void CreateResources(Project project)
    {
        Resource resource1 = project.Resources.Add("Projecto de Arquitetura (Work)");
        resource1.Set(Rsc.Name, "Projecto de Arquitetura (Work)");
        resource1.Set(Rsc.Type, ResourceType.Work);
        resource1.Set(Rsc.StandardRate, Convert.ToDecimal(1));
        resource1.Set(Rsc.Code, "2158");

        Console.WriteLine("Insumo : " + resource1.ToString());

        Resource resource2 = project.Resources.Add("Ajudante (Work)");
        resource2.Set(Rsc.Name, "Ajudante (Work)");
        resource2.Set(Rsc.Type, ResourceType.Work);
        resource2.Set(Rsc.StandardRate, Convert.ToDecimal(1));
        resource2.Set(Rsc.Code, "2102");

        Console.WriteLine("Insumo : " + resource2.ToString());

        Resource resource3 = project.Resources.Add("Servente (Work)");
        resource3.Set(Rsc.Name, "Servente (Work)");
        resource3.Set(Rsc.Type, ResourceType.Work);
        resource3.Set(Rsc.StandardRate, Convert.ToDecimal(1.8));
        resource3.Set(Rsc.Code, "2105");

        Console.WriteLine("Insumo : " + resource3.ToString());

        Resource resource4 = project.Resources.Add("Encanador (Work)");
        resource4.Set(Rsc.Name, "Encanador (Work)");
        resource4.Set(Rsc.Type, ResourceType.Work);
        resource4.Set(Rsc.StandardRate, Convert.ToDecimal(2));
        resource4.Set(Rsc.Code, "2118");

        Console.WriteLine("Insumo : " + resource4.ToString());

        Resource resource5 = project.Resources.Add("Papel Higiênico (Material)");
        resource5.Set(Rsc.Name, "Papel Higiênico (Material)");
        resource5.Set(Rsc.Type, ResourceType.Material);
        resource5.Set(Rsc.StandardRate, Convert.ToDecimal(20));
        resource5.Set(Rsc.Code, "21341");

        Console.WriteLine("Insumo : " + resource5.ToString());
    }

    static private void CreateTasks(Project project)
    {

        Task tsk1 = project.RootTask.Children.Add("Fase - 01");
        tsk1.Set(Tsk.IsManual, false);
        tsk1.Set(Tsk.Type, TaskType.FixedUnits);
        tsk1.Set(Tsk.Start, DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
        tsk1.Set(Tsk.Duration, project.GetDuration(90, TimeUnitType.Day));
        tsk1.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan); //Não iniciar antes de
        tsk1.Set(Tsk.ConstraintDate, tsk1.Get(Tsk.Start));
        tsk1.Set(Tsk.Type, TaskType.FixedDuration);

        Console.WriteLine("Tarefa : " + tsk1.ToString());


        //tsk1.Set(Tsk.Work, tsk1.Get(Tsk.Duration));

        Task tsk2 = tsk1.Children.Add("Sub - Fase - 01.01");
        tsk2.Set(Tsk.IsManual, false);
        tsk2.Set(Tsk.Type, TaskType.FixedUnits);
        tsk2.Set(Tsk.Start, DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
        tsk2.Set(Tsk.Duration, project.GetDuration(90, TimeUnitType.Day));
        tsk2.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan); //Não iniciar antes de
        tsk2.Set(Tsk.ConstraintDate, tsk1.Get(Tsk.Start));
        tsk2.Set(Tsk.Type, TaskType.FixedDuration);

        Console.WriteLine("Tarefa : " + tsk2.ToString());

        Task tsk3 = tsk2.Children.Add("Serviço 1 - VB e HR(Teste) -(0101001)");
        tsk3.Set(Tsk.IsManual, false);
        tsk3.Set(Tsk.Type, TaskType.FixedUnits);
        tsk3.Set(Tsk.Start, DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
        tsk3.Set(Tsk.Duration, project.GetDuration(60, TimeUnitType.Day));
        tsk3.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan); //Não iniciar antes de
        tsk3.Set(Tsk.ConstraintDate, tsk1.Get(Tsk.Start));
        tsk3.Set(Tsk.Type, TaskType.FixedDuration);

        Console.WriteLine("Tarefa : " + tsk3.ToString());


        Task tsk4 = tsk2.Children.Add("Serviço 2 - HH(Mão de Obra - HH) - (0101002)");
        tsk4.Set(Tsk.IsManual, false);
        tsk4.Set(Tsk.Type, TaskType.FixedUnits);
        tsk4.Set(Tsk.Start, DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
        tsk4.Set(Tsk.Duration, project.GetDuration(90, TimeUnitType.Day));
        tsk4.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan); //Não iniciar antes de
        tsk4.Set(Tsk.ConstraintDate, tsk1.Get(Tsk.Start));
        tsk4.Set(Tsk.Type, TaskType.FixedDuration);

        Console.WriteLine("Tarefa : " + tsk4.ToString());

        Task tsk5 = tsk2.Children.Add("Serviço 3 - (Material - CX) - (0101003)");
        tsk5.Set(Tsk.IsManual, false);
        tsk5.Set(Tsk.Type, TaskType.FixedUnits);
        tsk5.Set(Tsk.Start, DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
        tsk5.Set(Tsk.Duration, project.GetDuration(90, TimeUnitType.Day));
        tsk5.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan); //Não iniciar antes de
        tsk5.Set(Tsk.ConstraintDate, tsk1.Get(Tsk.Start));
        tsk5.Set(Tsk.Type, TaskType.FixedDuration);

        Console.WriteLine("Tarefa : " + tsk5.ToString());
    }

    static private void CreateAssignment(Project project)
    {
        Task tsk1 = project.SelectAllChildTasks().Where(t => t.Get(Tsk.Name) == "Serviço 1 - VB e HR(Teste) -(0101001)").FirstOrDefault();
        Resource rsc1 = project.Resources.Where(r => r.Get(Rsc.Code) == "2158").FirstOrDefault();

        ResourceAssignment assignment1 = project.ResourceAssignments.Add(tsk1, rsc1);

        assignment1.Set(Asn.Units, 1000);
        //assignment1.Set(Asn.Work, 1000); //work or units?

        ClearTimephasedData(assignment1);

        CreateTimephasedDataAssignment(project,
                                       assignment1,
                                       DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-01-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       800);

        CreateTimephasedDataAssignment(project,
                                       assignment1,
                                       DateTime.ParseExact("2018-02-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-02-28", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       100);

        CreateTimephasedDataAssignment(project,
                                       assignment1,
                                       DateTime.ParseExact("2018-03-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-03-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       100);

        Console.WriteLine("Atribuição : " + assignment1.ToString());


        Resource rsc2 = project.Resources.Where(r => r.Get(Rsc.Code) == "2102").FirstOrDefault();
        ResourceAssignment assignment2 = project.ResourceAssignments.Add(tsk1, rsc2);

        assignment2.Set(Asn.Units, 5000);

        ClearTimephasedData(assignment2);

        CreateTimephasedDataAssignment(project,
                                       assignment2,
                                       DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-01-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       4000);

        CreateTimephasedDataAssignment(project,
                                       assignment2,
                                       DateTime.ParseExact("2018-02-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-02-28", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       500);

        CreateTimephasedDataAssignment(project,
                                       assignment2,
                                       DateTime.ParseExact("2018-03-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-03-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       500);

        Console.WriteLine("Atribuição : " + assignment2.ToString());


        Task tsk2 = project.SelectAllChildTasks().Where(t => t.Get(Tsk.Name) == "Serviço 2 - HH(Mão de Obra - HH) - (0101002)").FirstOrDefault();
        Resource rsc3 = project.Resources.Where(r => r.Get(Rsc.Code) == "2105").FirstOrDefault();
        ResourceAssignment assignment3 = project.ResourceAssignments.Add(tsk2, rsc3);

        assignment3.Set(Asn.Units, 300);

        ClearTimephasedData(assignment3);

        CreateTimephasedDataAssignment(project,
                                       assignment3,
                                       DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-01-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       100);

        CreateTimephasedDataAssignment(project,
                                       assignment3,
                                       DateTime.ParseExact("2018-02-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-02-28", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       100);

        CreateTimephasedDataAssignment(project,
                                       assignment3,
                                       DateTime.ParseExact("2018-03-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-03-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       100);


        Console.WriteLine("Atribuição : " + assignment3.ToString());

        Resource rsc4 = project.Resources.Where(r => r.Get(Rsc.Code) == "2118").FirstOrDefault();
        ResourceAssignment assignment4 = project.ResourceAssignments.Add(tsk2, rsc4);

        assignment4.Set(Asn.Units, 700);

        ClearTimephasedData(assignment4);

        CreateTimephasedDataAssignment(project,
                                       assignment4,
                                       DateTime.ParseExact("2018-01-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-01-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       700);

        Console.WriteLine("Atribuição : " + assignment4.ToString());


        Task tsk3 = project.SelectAllChildTasks().Where(t => t.Get(Tsk.Name) == "Serviço 3 - (Material - CX) - (0101003)").FirstOrDefault();
        Resource rsc5 = project.Resources.Where(r => r.Get(Rsc.Code) == "21341").FirstOrDefault();
        ResourceAssignment assignment5 = project.ResourceAssignments.Add(tsk3, rsc5);

        ClearTimephasedData(assignment5);

        assignment5.Set(Asn.Units, 700);

        CreateTimephasedDataAssignment(project,
                                       assignment5,
                                       DateTime.ParseExact("2018-03-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       DateTime.ParseExact("2018-03-31", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                                       1);

        Console.WriteLine("Atribuição : " + assignment5.ToString());
    }


    static private string TimeSpanToTDString(TimeSpan timeSpan)
    {
        return string.Format("PT{0}D{1}H{2}M{3}S", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
    }

    static private void ClearTimephasedData(ResourceAssignment assignment)
    {
        foreach (TimephasedData tph in assignment.TimephasedData.ToList())
            assignment.TimephasedData.Remove(tph);
    }

    static private void CreateTimephasedDataAssignment(Project project,
                                                       ResourceAssignment assignment,
                                                       DateTime dateStart,
                                                       DateTime dateFinish,
                                                       float work)
    {
        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);
        var workPerDay = (nDaysWorking == 0 ? 0 : work / nDaysWorking);

        assignment.Set(Asn.WorkContour, WorkContourType.Contoured);

        foreach (DateTime day in EachDay(dateStart, dateFinish))
        {
            if (cal.IsDayWorking(day))
            {
                var dataInicio = ReturnDateStartWithTimeCalendar(cal, day);
                TimephasedData td = TimephasedData.CreateWorkTimephased(assignment.Get(Asn.Uid),
                                                dateStart,
                                                cal.GetFinishDateByStartAndWork(dateStart, project.GetDuration(1, TimeUnitType.Day)),
                                                TimeSpan.FromHours(workPerDay),
                                                TimeUnitType.HourEstimated,
                                                TimephasedDataType.AssignmentWork);
                assignment.TimephasedData.Add(td);
            }
        }
    }


    public static IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
    {
        for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
            yield return day;
    }

    public static int DaysWorking(Calendar calendar, DateTime dateStart, DateTime dateFinish)
    {
        int nDaysWorking = 0;
        if (calendar != null)
        {
            var nExceptions = calendar.Exceptions.Where(p => p.FromDate >= dateStart && p.FromDate <= dateFinish).Count();

            foreach (DateTime day in EachDay(dateStart, dateFinish))
                if (calendar.IsDayWorking(day))
                    nDaysWorking++;

            return (nDaysWorking - nExceptions);
        }
        else
        {
            foreach (DateTime day in EachDay(dateStart, dateFinish))
                nDaysWorking++;

            return nDaysWorking;
        }
    }

    public static DateTime ReturnDateStartWithTimeCalendar(Calendar calendar, DateTime date)
    {
        DateTime dateStart = date.Date;
        if (calendar != null)
        {
            var wt = calendar.WeekDays.Where(p => p.DayWorking == true).FirstOrDefault().WorkingTimes.ToList()[0];
            TimeSpan timeStart = new TimeSpan(wt.FromTime.Hour, wt.FromTime.Minute, wt.FromTime.Second);
            dateStart = dateStart + timeStart;
        }
        return dateStart;
    }
}

}
output.zip (30.6 KB)

There are cases similar to the topics:

Basically I need to distribute non-linearly (timepheased data) the work of an Assignment / Task.
The generated mpp is always linear work.

How can I solve this?

@FabioMartins,

Thank you for contacting Aspose support team.

We are analyzing this issue but could not find the definition of linear and non-linear timephased data. Please provide more detail about the issue along with any snapshots etc. for our understanding. An expected output MPP/XML file is also required which is created using MS Project (i.e. without using Aspose.Tasks). If possible please reduce the code to minimum so that issue can be reproduced with shortest code for better understanding and resolution.

I need to distribute daily task / assignment work:

Expected2.png (122.3 KB)
Expected.png (175.5 KB)
expectedMPP.zip (40.7 KB)

Could you explain with a code?

@FabioMartins,

Thank you for providing more details. This issue is reproduced and logged under Id:TASKSNET-2622 for detailed analysis later. We will write back here as soon as some feedback is ready to share.

The issues you have found earlier (filed as TASKSNET-2622) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan

@FabioMartins Hello,

Presented below is the code sample which generates the file you want to get.

public class Sample2622
{
    public static Project BuildProject()
    {
        Project project = new Project("Blank2010.mpp") { CalculationMode = CalculationMode.None };
        project.Set(Prj.DateFormat, DateFormat.DateDddMmDdYy);
        project.Set(Prj.StartDate, new DateTime(2018, 1, 1));
        project.Set(Prj.NewTasksAreManual, true);
        project.Set(Prj.ActualsInSync, true);
        
        CreateWBS(project);
        CreateCalendars(project);
        CreateResources(project);
        CreateTasks(project);
        CreateAssignments(project);
        
        project.RecalculateResourceStartFinish();
        project.RecalculateResourceFields();
        project.Recalculate();

        return project;
    }

    private static void CreateWBS(Project project)
    {
        project.WBSCodeDefinition = new WBSCodeDefinition
        {
            GenerateWBSCode = true, VerifyUniqueness = true
        };
        var wbsCodeMask = new WBSCodeMask {Length = 1};
        project.WBSCodeDefinition.CodeMaskCollection.Add(wbsCodeMask);
    }

    internal static void CreateCalendars(Project project)
    {
        var calendar = project.Calendars.Add("Padrão", project.Calendars.GetByName("Standard"));
        var workingTimes = new List<WorkingTime>
                               {
                                   new WorkingTime { FromTime = new DateTime(1, 1, 1, 9, 0, 0), ToTime = new DateTime(1, 1, 1, 12, 0, 0) },
                                   new WorkingTime { FromTime = new DateTime(1, 1, 1, 13, 0, 0), ToTime = new DateTime(1, 1, 1, 18, 0, 0) }
                               };
        calendar.WeekDays.Add(new WeekDay(DayType.Monday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Tuesday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Wednesday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Thursday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Friday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Saturday));
        calendar.WeekDays.Add(new WeekDay(DayType.Sunday));

        project.Set(Prj.Calendar, calendar);

        calendar = project.Calendars.Add("Calendário Padrão", project.Calendars.GetByName("Standard"));
        workingTimes = new List<WorkingTime>
                               {
                                   new WorkingTime { FromTime = new DateTime(1, 1, 1, 8, 0, 0), ToTime = new DateTime(1, 1, 1, 16, 0, 0) }
                               };
        calendar.WeekDays.Add(new WeekDay(DayType.Monday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Tuesday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Wednesday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Thursday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Friday, workingTimes));
        calendar.WeekDays.Add(new WeekDay(DayType.Saturday));
        calendar.WeekDays.Add(new WeekDay(DayType.Sunday));

        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 1, 1), ToDate = new DateTime(2008, 1, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 1, 25), ToDate = new DateTime(2008, 1, 25), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 2, 4), ToDate = new DateTime(2008, 2, 5), Occurrences = 2, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 3, 21), ToDate = new DateTime(2008, 3, 21), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 4, 21), ToDate = new DateTime(2008, 4, 21), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 5, 1), ToDate = new DateTime(2008, 5, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 5, 22), ToDate = new DateTime(2008, 5, 22), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 7, 9), ToDate = new DateTime(2008, 7, 9), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 9, 7), ToDate = new DateTime(2008, 9, 7), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 10, 12), ToDate = new DateTime(2008, 10, 12), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 11, 2), ToDate = new DateTime(2008, 11, 2), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 11, 15), ToDate = new DateTime(2008, 11, 15), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 11, 20), ToDate = new DateTime(2008, 11, 20), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2008, 12, 25), ToDate = new DateTime(2008, 12, 25), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 1, 1), ToDate = new DateTime(2009, 1, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 1, 25), ToDate = new DateTime(2009, 1, 25), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 2, 23), ToDate = new DateTime(2009, 2, 24), Occurrences = 2, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 4, 10), ToDate = new DateTime(2009, 4, 10), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 4, 21), ToDate = new DateTime(2009, 4, 21), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 5, 1), ToDate = new DateTime(2009, 5, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 6, 11), ToDate = new DateTime(2009, 6, 11), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 7, 9), ToDate = new DateTime(2009, 7, 9), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 9, 7), ToDate = new DateTime(2009, 9, 7), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 10, 12), ToDate = new DateTime(2009, 10, 12), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 11, 2), ToDate = new DateTime(2009, 11, 2), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2009, 12, 25), ToDate = new DateTime(2009, 12, 25), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 1, 1), ToDate = new DateTime(2010, 1, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 2, 15), ToDate = new DateTime(2010, 2, 16), Occurrences = 2, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 4, 2), ToDate = new DateTime(2010, 4, 2), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 4, 21), ToDate = new DateTime(2010, 4, 21), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 6, 3), ToDate = new DateTime(2010, 6, 3), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 9, 7), ToDate = new DateTime(2010, 9, 7), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 10, 12), ToDate = new DateTime(2010, 10, 12), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 11, 2), ToDate = new DateTime(2010, 11, 2), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2010, 11, 15), ToDate = new DateTime(2010, 11, 15), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2011, 4, 21), ToDate = new DateTime(2011, 4, 22), Occurrences = 2, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2011, 6, 23), ToDate = new DateTime(2011, 6, 23), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2011, 9, 7), ToDate = new DateTime(2011, 9, 7), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2011, 10, 12), ToDate = new DateTime(2011, 10, 12), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2011, 11, 2), ToDate = new DateTime(2011, 11, 2), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2011, 11, 15), ToDate = new DateTime(2011, 11, 15), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2014, 1, 1), ToDate = new DateTime(2014, 1, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2014, 5, 1), ToDate = new DateTime(2014, 5, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2014, 6, 19), ToDate = new DateTime(2014, 6, 19), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 1, 1), ToDate = new DateTime(2015, 1, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 1, 25), ToDate = new DateTime(2015, 1, 25), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 2, 16), ToDate = new DateTime(2015, 2, 18), Occurrences = 3, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 4, 3), ToDate = new DateTime(2015, 4, 3), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 4, 21), ToDate = new DateTime(2015, 4, 21), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 5, 1), ToDate = new DateTime(2015, 5, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 6, 4), ToDate = new DateTime(2015, 6, 4), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 7, 9), ToDate = new DateTime(2015, 7, 9), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 9, 7), ToDate = new DateTime(2015, 9, 7), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 10, 12), ToDate = new DateTime(2015, 10, 12), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 11, 2), ToDate = new DateTime(2015, 11, 2), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 11, 15), ToDate = new DateTime(2015, 11, 15), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 11, 20), ToDate = new DateTime(2015, 11, 20), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 12, 25), ToDate = new DateTime(2015, 12, 25), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2015, 12, 31), ToDate = new DateTime(2015, 12, 31), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2016, 6, 7), ToDate = new DateTime(2016, 6, 8), Occurrences = 2, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2016, 9, 30), ToDate = new DateTime(2016, 9, 30), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2017, 5, 1), ToDate = new DateTime(2017, 5, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2018, 1, 1), ToDate = new DateTime(2018, 1, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
        calendar.Exceptions.Add(new CalendarException { FromDate = new DateTime(2018, 5, 1), ToDate = new DateTime(2018, 5, 1), Occurrences = 1, Type = CalendarExceptionType.Daily, Period = 1 });
    }

    internal static void CreateResources(Project project)
    {
        Resource resource1 = project.Resources.Add("Ajudante (Work)");
        resource1.Set(Rsc.Name, "Ajudante (Work)");
        resource1.Set(Rsc.Initials, "A");
        resource1.Set(Rsc.Type, ResourceType.Work);
        resource1.Set(Rsc.StandardRate, Convert.ToDecimal(1));
        resource1.Set(Rsc.Code, "2102");

        Console.WriteLine("Insumo : " + resource1);
        
        Resource resource2 = project.Resources.Add("Servente (Work)");
        resource2.Set(Rsc.Name, "Servente (Work)");
        resource2.Set(Rsc.Initials, "S");
        resource2.Set(Rsc.Type, ResourceType.Work);
        resource2.Set(Rsc.StandardRate, Convert.ToDecimal(1.8));
        resource2.Set(Rsc.Code, "2105");

        Console.WriteLine("Insumo : " + resource2);
        
        Resource resource3 = project.Resources.Add("Encanador (Work)");
        resource3.Set(Rsc.Name, "Encanador (Work)");
        resource3.Set(Rsc.Initials, "E");
        resource3.Set(Rsc.Type, ResourceType.Work);
        resource3.Set(Rsc.StandardRate, Convert.ToDecimal(2));
        resource3.Set(Rsc.Code, "2118");

        Console.WriteLine("Insumo : " + resource3);
        
        Resource resource4 = project.Resources.Add("Projeto de arquitetura (Insumo VB) (Work)");
        resource4.Set(Rsc.Name, "Projecto de Arquitetura (Work)");
        resource4.Set(Rsc.Initials, "P");
        resource4.Set(Rsc.Type, ResourceType.Work);
        resource4.Set(Rsc.StandardRate, Convert.ToDecimal(1));
        resource4.Set(Rsc.Code, "2158");

        Console.WriteLine("Insumo : " + resource4);

        Resource resource5 = project.Resources.Add("Papel Higiênico (Material)");
        resource5.Set(Rsc.Name, "Papel Higiênico (Material)");
        resource5.Set(Rsc.Initials, "P");
        resource5.Set(Rsc.Type, ResourceType.Material);
        resource5.Set(Rsc.StandardRate, Convert.ToDecimal(20));
        resource5.Set(Rsc.StandardRateFormat, RateFormatType.MaterialResourceRate);
        resource5.Set(Rsc.Code, "21341");

        Console.WriteLine("Insumo : " + resource5);
    }

    internal static void CreateTasks(Project project)
    {
        var cal = project.Calendars.GetByName("Calendário Padrão");
        Task tsk1 = project.RootTask.Children.Add("Fase - 01");
        tsk1.Set(Tsk.IsEstimated, false);
        tsk1.Set(Tsk.Calendar, cal);
        tsk1.Set(Tsk.IgnoreResourceCalendar, true);
        tsk1.Set(Tsk.IsRollup, true);
        tsk1.Set(Tsk.IsPublished, false);

        Console.WriteLine("Tarefa : " + tsk1);

        Task tsk2 = tsk1.Children.Add("Sub - Fase - 01.01");
        tsk2.Set(Tsk.IsEstimated, false);
        tsk2.Set(Tsk.Calendar, cal);
        tsk2.Set(Tsk.IgnoreResourceCalendar, true);
        tsk2.Set(Tsk.IsRollup, true);
        tsk2.Set(Tsk.IsPublished, false);

        Console.WriteLine("Tarefa : " + tsk2);

        Task tsk3 = tsk2.Children.Add("Serviço 1 - VB e HR(Teste) -(0101001)");
        tsk3.Set(Tsk.IsManual, false);
        tsk3.Set(Tsk.IsEstimated, false);
        tsk3.Set(Tsk.Type, TaskType.FixedDuration);
        tsk3.Set(Tsk.Start, new DateTime(2018, 1, 1, 8, 0, 0));
        tsk3.Set(Tsk.Duration, project.GetDuration(90, TimeUnitType.Day));
        tsk3.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan);
        tsk3.Set(Tsk.ConstraintDate, new DateTime(2018, 1, 1, 8, 0, 0));
        tsk3.Set(Tsk.Calendar, cal);
        tsk3.Set(Tsk.IgnoreResourceCalendar, true);
        tsk3.Set(Tsk.LevelAssignments, false);
        tsk3.Set(Tsk.IsPublished, false);

        Console.WriteLine("Tarefa : " + tsk3);

        Task tsk4 = tsk2.Children.Add("Serviço 2 - HH(Mão de Obra - HH) - (0101002)");
        tsk4.Set(Tsk.IsManual, false);
        tsk4.Set(Tsk.IsEstimated, false);
        tsk4.Set(Tsk.Type, TaskType.FixedDuration);
        tsk4.Set(Tsk.Start, new DateTime(2018, 1, 1, 8, 0, 0));
        tsk4.Set(Tsk.Duration, project.GetDuration(90, TimeUnitType.Day));
        tsk4.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan);
        tsk4.Set(Tsk.ConstraintDate, new DateTime(2018, 1, 1, 8, 0, 0));
        tsk4.Set(Tsk.Calendar, cal);
        tsk4.Set(Tsk.IgnoreResourceCalendar, true);
        tsk4.Set(Tsk.LevelAssignments, false);

        Console.WriteLine("Tarefa : " + tsk4);

        Task tsk5 = tsk2.Children.Add("Serviço 3 - (Material - CX) - (0101003)");
        tsk5.Set(Tsk.IsManual, false);
        tsk5.Set(Tsk.IsEstimated, false);
        tsk5.Set(Tsk.Type, TaskType.FixedDuration);
        tsk5.Set(Tsk.Start, new DateTime(2018, 1, 1, 8, 0, 0));
        tsk5.Set(Tsk.Duration, project.GetDuration(90, TimeUnitType.Day));
        tsk5.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan);
        tsk5.Set(Tsk.ConstraintDate, new DateTime(2018, 1, 1, 8, 0, 0));
        tsk5.Set(Tsk.Calendar, cal);
        tsk5.Set(Tsk.IgnoreResourceCalendar, true);
        tsk5.Set(Tsk.LevelAssignments, false);

        Console.WriteLine("Tarefa : " + tsk5);
    }

    internal static void CreateAssignments(Project project)
    {
        Task tsk1 = project.SelectAllChildTasks().FirstOrDefault(t => t.Get(Tsk.Name) == "Serviço 1 - VB e HR(Teste) -(0101001)");
        
        //"Ajudante (Work)" resource
        Resource rsc1 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Code) == "2102");
        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(2018, 1, 1, 8, 0, 0),
            new DateTime(2018, 1, 31, 16, 0, 0),
            800 * 60 + 8, false);

        var tmp = assignment1.TimephasedData.ToList()[assignment1.TimephasedData.Count - 1];
        assignment1.TimephasedData.Remove(tmp);
        var tmp2 = assignment1.TimephasedData.ToList()[assignment1.TimephasedData.Count - 1];
        assignment1.TimephasedData.Remove(tmp2);
        TimephasedData td = TimephasedData.CreateWorkTimephased(
            assignment1.Get(Asn.Uid),
            tmp2.Start,
            tmp2.Finish,
            tmp2.ValueToDuration - TimeSpan.FromSeconds(36),
            TimeUnitType.HourEstimated,
            TimephasedDataType.AssignmentRemainingWork);
        assignment1.TimephasedData.Add(td);
        td = TimephasedData.CreateWorkTimephased(
            assignment1.Get(Asn.Uid),
            tmp.Start,
            tmp.Finish,
            tmp.ValueToDuration - TimeSpan.FromSeconds(36),
            TimeUnitType.HourEstimated,
            TimephasedDataType.AssignmentRemainingWork);
        assignment1.TimephasedData.Add(td);

        CreateTimephasedDataAssignment(
            project,
            assignment1,
            new DateTime(2018, 2, 1, 8, 0, 0),
            new DateTime(2018, 2, 28, 16, 0, 0),
            100 * 60, false);

        CreateTimephasedDataAssignment(
            project,
            assignment1,
            new DateTime(2018, 3, 1, 8, 0, 0),
            new DateTime(2018, 3, 31, 16, 0, 0),
            100 * 60 + 6, false);
        // last 2 months
        td = TimephasedData.CreateWorkTimephased(
            assignment1.Get(Asn.Uid),
            new DateTime(2018, 3, 31, 8, 0, 0), 
            new DateTime(2018, 4, 2, 8, 0, 0),
            TimeSpan.Zero, 
            TimeUnitType.Hour,
            TimephasedDataType.AssignmentRemainingWork);
        assignment1.TimephasedData.Add(td);
        td = TimephasedData.CreateWorkTimephased(
            assignment1.Get(Asn.Uid),
            new DateTime(2018, 4, 2, 8, 0, 0), 
            new DateTime(2018, 5, 8, 16, 0, 0),
            TimeSpan.Zero, 
            TimeUnitType.Hour,
            TimephasedDataType.AssignmentRemainingWork);
        assignment1.TimephasedData.Add(td);
        Console.WriteLine("Atribuição : " + assignment1);

        //"Projecto de Arquitetura (Work)" resource
        Resource rsc2 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Code) == "2158");
        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(2018, 1, 1, 8, 0, 0),
            new DateTime(2018, 1, 31, 16, 0, 0),
            4000 * 60 + 1.8f, false);

        CreateTimephasedDataAssignment(
            project,
            assignment2,
            new DateTime(2018, 2, 1, 8, 0, 0),
            new DateTime(2018, 2, 28, 16, 0, 0),
            500 * 60, false);

        CreateTimephasedDataAssignment(
            project,
            assignment2,
            new DateTime(2018, 3, 1, 8, 0, 0),
            new DateTime(2018, 3, 31, 16, 0, 0),
            500 * 60 + 4.2f, false);
        
        // last 2 months
        td = TimephasedData.CreateWorkTimephased(
            assignment2.Get(Asn.Uid),
            new DateTime(2018, 3, 31, 8, 0, 0), 
            new DateTime(2018, 4, 2, 8, 0, 0),
            TimeSpan.Zero, 
            TimeUnitType.Hour,
            TimephasedDataType.AssignmentRemainingWork);
        assignment2.TimephasedData.Add(td);
        td = TimephasedData.CreateWorkTimephased(
            assignment2.Get(Asn.Uid),
            new DateTime(2018, 4, 2, 8, 0, 0), 
            new DateTime(2018, 5, 8, 16, 0, 0),
            TimeSpan.Zero, 
            TimeUnitType.Hour,
            TimephasedDataType.AssignmentRemainingWork);
        assignment2.TimephasedData.Add(td);
        
        Console.WriteLine("Atribuição : " + assignment2);
        Task tsk2 = project.SelectAllChildTasks().FirstOrDefault(t => t.Get(Tsk.Name) == "Serviço 2 - HH(Mão de Obra - HH) - (0101002)");
        
        //Servente (Work)
        Resource rsc3 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Code) == "2105");
        ResourceAssignment assignment3 = project.ResourceAssignments.Add(tsk2, rsc3);
        assignment3.Set(Asn.WorkContour, WorkContourType.Contoured);
        assignment3.Set(Asn.Units, 0);
        
        ClearTimephasedData(assignment3);
        
        CreateTimephasedDataAssignment(
            project,
            assignment3,
            new DateTime(2018, 1, 1, 8, 0, 0),
            new DateTime(2018, 1, 31, 16, 0, 0),
            100 * 60 + 6, false);

        CreateTimephasedDataAssignment(
            project,
            assignment3,
            new DateTime(2018, 2, 1, 8, 0, 0),
            new DateTime(2018, 2, 28, 16, 0, 0),
            100 * 60 , false);

        CreateTimephasedDataAssignment(
            project,
            assignment3,
            new DateTime(2018, 3, 1, 8, 0, 0),
            new DateTime(2018, 3, 31, 16, 0, 0),
            100 * 60 + 6, true);

        // last 2 months
        td = TimephasedData.CreateWorkTimephased(
            assignment3.Get(Asn.Uid),
            new DateTime(2018, 3, 31, 8, 0, 0), 
            new DateTime(2018, 4, 2, 8, 0, 0),
            TimeSpan.Zero, 
            TimeUnitType.Hour,
            TimephasedDataType.AssignmentRemainingWork);
        assignment3.TimephasedData.Add(td);
        td = TimephasedData.CreateWorkTimephased(
            assignment3.Get(Asn.Uid),
            new DateTime(2018, 4, 2, 8, 0, 0), 
            new DateTime(2018, 5, 8, 16, 0, 0),
            TimeSpan.Zero, 
            TimeUnitType.Hour,
            TimephasedDataType.AssignmentRemainingWork);
        assignment3.TimephasedData.Add(td);
        
        Console.WriteLine("Atribuição : " + assignment3);
        //Encanador (Work)
        Resource rsc4 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Code) == "2118");
        ResourceAssignment assignment4 = project.ResourceAssignments.Add(tsk2, rsc4);
        assignment4.Set(Asn.WorkContour, WorkContourType.Contoured);
        assignment4.Set(Asn.Units, 0);
        
        ClearTimephasedData(assignment4);

        CreateTimephasedDataAssignment(
            project,
            assignment4,
            new DateTime(2018, 1, 1, 8, 0, 0),
            new DateTime(2018, 1, 31, 16, 0, 0),
            700 * 60 + 2.4, true);
        
        Console.WriteLine("Atribuição : " + assignment4);

        Task tsk3 = project.SelectAllChildTasks().FirstOrDefault(t => t.Get(Tsk.Name) == "Serviço 3 - (Material - CX) - (0101003)");
        //Papel Higiênico (Material)
        Resource rsc5 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Code) == "21341");
        ResourceAssignment assignment5 = project.ResourceAssignments.Add(tsk3, rsc5);
        
        assignment5.Set(Asn.WorkContour, WorkContourType.Contoured);

        ClearTimephasedData(assignment5);

        CreateTimephasedDataAssignment(
            project,
            assignment5,
            new DateTime(2018, 3, 1, 8, 0, 0),
            new DateTime(2018, 3, 31, 16, 0, 0),
            700 * 60 + 2.4, true);

        Console.WriteLine("Atribuição : " + assignment5);
    }

    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(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;
            }
        }

        if (isLastPortion)
        {
            tds[tds.Count - 1].Finish = cal.GetPreviousWorkingDayEnd(tds[tds.Count - 1].Finish);
        }
        
        assignment.TimephasedData.AddRange(tds);
    }
}

Here you can download the Blank2010.mpp.

Thank you.

Hello, I could not generate with this code.
errorcode.png (129.4 KB)

@FabioMartins

Thank you for your feedback.
We are investigating this issue and will share our findings soon.

@FabioMartins Could you attach your C# project? The issue cannot be reproduced on our side.

I prepared the sample project, could you check it on your side?