ResourceAssignment: Setting Start, Finish and Duration for task (C# .NET)

Hi,

Any news?

@FabioMartins

Thank you for writing back to us.

This issue has been resolved and will be a part of our next API release i.e. Aspose.Tasks for .NET 19.1 and it shall probably be available at the end of next week.

Hello @MuzammilKhan,

I tested on 19.1 and the same problem occurs.

Please, could you help me?

How do I implement my code to generate the scenario described?

  • Task with two resource assignment, when finish first assignment, should start the next one.
  • Task 1 - Start - 2018/01/01 - Finish - 2018/03/23 - Duration - 60 days
  • Resource assignment 1 - Start - 2018/01/01 - Finish 2018/01/31
  • Resource assignment 2 - Start - 2018/02/01 - Finish 2018/03/23

@FabioMartins

We are sorry for inconvenience.

Please give us some time to investigate this issue and we will update you soon.

@FabioMartins

Please follow the code sample given below to get the desired result:

private void Test()
{
    Project project = new Project("template.mpp") { CalculationMode = CalculationMode.None };
    project.Set(Prj.StartDate, new DateTime(2018, 1, 1));

    if (project.Calendars.GetByName("Padrão") == null)
    {
            this.CreateCalendar(project);
    }

    Resource rsc1 = project.Resources.Add("Resource 1 (Work)");
    rsc1.Set(Rsc.Type, ResourceType.Work);
    rsc1.Set(Rsc.StandardRate, 1m);
    rsc1.Set(Rsc.Code, "2158");

    Resource rsc2 = project.Resources.Add("Resource 2 (Work)");
    rsc2.Set(Rsc.Type, ResourceType.Work);
    rsc2.Set(Rsc.StandardRate, 1m);
    rsc2.Set(Rsc.Code, "2159");

    Task tsk1 = project.RootTask.Children.Add("Task - 01");
    Task tsk2 = tsk1.Children.Add("Task - 01.01");
    Task tsk3 = tsk2.Children.Add("Task - 01.01.001");
    tsk3.Set(Tsk.IsManual, false);
    tsk3.Set(Tsk.Start, new DateTime(2018, 1, 1, 9, 0, 0));
    tsk3.Set(Tsk.Duration, project.GetDuration(60, TimeUnitType.Day));
    tsk3.Set(Tsk.Type, TaskType.FixedDuration);
    tsk3.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan);
    tsk3.Set(Tsk.ConstraintDate, new DateTime(2018, 1, 1, 9, 0, 0));

    ResourceAssignment assignment1 = project.ResourceAssignments.Add(tsk3, rsc1);
    assignment1.Set(Asn.Start, new DateTime(2018, 1, 1, 9, 0, 0));
    assignment1.Set(Asn.Finish, new DateTime(2018, 1, 31, 18, 0, 0));
    assignment1.Set(Asn.Work, project.GetDuration(23, TimeUnitType.Day));

    ResourceAssignment assignment2 = project.ResourceAssignments.Add(tsk3, rsc2);
    assignment2.Set(Asn.Start, new DateTime(2018, 2, 1, 9, 0, 0));
    assignment2.Set(Asn.Finish, new DateTime(2018, 3, 23, 18, 0, 0));
    assignment2.Set(Asn.Work, project.GetDuration(37, TimeUnitType.Day));
    assignment2.Set(Asn.Delay, project.GetDuration(23, TimeUnitType.Day));

    project.Recalculate();
    project.RecalculateResourceFields();
    project.RecalculateResourceStartFinish();
}

private void CreateCalendar(Project project)
{
    var calendar = project.Calendars.Add("Padrão", project.Calendars.GetByName("Standard"));
    var workingTimes = new System.Collections.Generic.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);
}

Moreover, please share your feedback and feel free to write back to us if you need additional information.

Hi @alexanderefremov1, thanks for the feedback!

The code worked as intended !!!

But I had a new question, what should be done for material resources?

rsc2.Set(Rsc.Type, ResourceType.Material);

I got this result:result.png (13.7 KB)

using System;
using Aspose.Tasks;
using Aspose.Tasks.Saving;

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

        Project project = new Project("template.mpp") { CalculationMode = CalculationMode.None };
        project.Set(Prj.StartDate, new DateTime(2018, 1, 1));

        if (project.Calendars.GetByName("Padrão") == null)
        {
            CreateCalendar(project);
        }

        Resource rsc1 = project.Resources.Add("Resource 1 (Work)");
        rsc1.Set(Rsc.Type, ResourceType.Work);
        rsc1.Set(Rsc.StandardRate, 1m);
        rsc1.Set(Rsc.Code, "2158");

        Resource rsc2 = project.Resources.Add("Resource 2 (Material)");
        rsc2.Set(Rsc.Type, ResourceType.Material);
        rsc2.Set(Rsc.StandardRate, 1m);
        rsc2.Set(Rsc.Code, "2159");

        Task tsk1 = project.RootTask.Children.Add("Task - 01");
        Task tsk2 = tsk1.Children.Add("Task - 01.01");
        Task tsk3 = tsk2.Children.Add("Task - 01.01.001");
        tsk3.Set(Tsk.IsManual, false);
        tsk3.Set(Tsk.Start, new DateTime(2018, 1, 1, 9, 0, 0));
        tsk3.Set(Tsk.Duration, project.GetDuration(60, TimeUnitType.Day));
        tsk3.Set(Tsk.Type, TaskType.FixedDuration);
        tsk3.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan);
        tsk3.Set(Tsk.ConstraintDate, new DateTime(2018, 1, 1, 9, 0, 0));

        ResourceAssignment assignment1 = project.ResourceAssignments.Add(tsk3, rsc1);
        assignment1.Set(Asn.Start, new DateTime(2018, 1, 1, 9, 0, 0));
        assignment1.Set(Asn.Finish, new DateTime(2018, 1, 31, 18, 0, 0));
        assignment1.Set(Asn.Work, project.GetDuration(23, TimeUnitType.Day));

        ResourceAssignment assignment2 = project.ResourceAssignments.Add(tsk3, rsc2);
        assignment2.Set(Asn.Start, new DateTime(2018, 2, 1, 9, 0, 0));
        assignment2.Set(Asn.Finish, new DateTime(2018, 3, 23, 18, 0, 0));
        assignment2.Set(Asn.Work, project.GetDuration(37, TimeUnitType.Day));
        assignment2.Set(Asn.Delay, project.GetDuration(23, TimeUnitType.Day));

        project.Recalculate();
        project.RecalculateResourceFields();
        project.RecalculateResourceStartFinish();

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

    static void CreateCalendar(Project project)
    {
        var calendar = project.Calendars.Add("Padrão", project.Calendars.GetByName("Standard"));
        var workingTimes = new System.Collections.Generic.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);
    }
}

}

The above code is the same, however with the definition of the second resource as Material (rsc2.Set (Rsc.Type, ResourceType.Material)).

@FabioMartins

We have logged this issue with ID “TASKSNET-2969” for further investigation. However, it is requested to kindly share desired output as well to get better idea.

Moreover, we will update you here once we have more information to share.

It’s the same situation:
Task with two resource assignments, when you finish the first assignment, you must start the next assignment.

  • Task 1 - Start - 2018/01/01 - Conclusion - 2018/03/23 - Duration - 60 days
  • Allocation of resources 1 - Beginning - 2018/01/01 - Completion 2018/01/31
  • Allocation of resources 2 - Beginning - 2018/02/01 - Completion 2018/03/23

But the second resource is material.

@FabioMartins

Thank you for your feedback. We have recorded the details and shall be considered for further investigation.

Any news?

Tks,

@FabioMartins

We are working on this issue and it is not resolved yet. We will update you here as soon as we have more information to share.

Any news?

Tks,

@FabioMartins

The issue under ID “TASKSNET-2969” is not resolved yet and we are working on this issue. We will update you as soon as it is resolved. We are hopeful that it will be resolved with upcoming release Aspose.Tasks for .NET 19.3.

@FabioMartins Hello!

Please, try the next code to get the desired result (it works in Aspose.Tasks for .NET 19.2)

public void TASKSNET_2969()
{
    var lic = new License();
    lic.SetLicense("path_to_lic");
    Project project = new Project("template2016.mpp") { CalculationMode = CalculationMode.None };
    project.Set(Prj.StartDate, new DateTime(2018, 1, 1));

    if (project.Calendars.GetByName("Padrão") == null)
    {
        CreateCalendar(project);
    }

    Resource rsc1 = project.Resources.Add("Resource 1 (Work)");
    rsc1.Set(Rsc.Type, ResourceType.Work);
    rsc1.Set(Rsc.StandardRate, 1m);
    rsc1.Set(Rsc.Code, "2158");

    Resource rsc2 = project.Resources.Add("Resource 2 (Material)");
    rsc2.Set(Rsc.Type, ResourceType.Material);
    rsc2.Set(Rsc.StandardRate, 1m);
    rsc2.Set(Rsc.Code, "2159");

    Task tsk1 = project.RootTask.Children.Add("Task - 01");
    Task tsk2 = tsk1.Children.Add("Task - 01.01");
    Task tsk3 = tsk2.Children.Add("Task - 01.01.001");
    tsk3.Set(Tsk.IsManual, false);
    tsk3.Set(Tsk.Start, new DateTime(2018, 1, 1, 9, 0, 0));
    tsk3.Set(Tsk.Duration, project.GetDuration(60, TimeUnitType.Day));
    tsk3.Set(Tsk.Type, TaskType.FixedDuration);
    tsk3.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan);
    tsk3.Set(Tsk.ConstraintDate, new DateTime(2018, 1, 1, 9, 0, 0));

    ResourceAssignment assignment1 = project.ResourceAssignments.Add(tsk3, rsc1);
    assignment1.Set(Asn.Start, new DateTime(2018, 1, 1, 9, 0, 0));
    assignment1.Set(Asn.Finish, new DateTime(2018, 1, 31, 18, 0, 0));
    assignment1.Set(Asn.Work, project.GetDuration(23, TimeUnitType.Day));

    var assignment2 = project.ResourceAssignments.Add(tsk3, rsc2);
    assignment2.Set(Asn.Start, new DateTime(2018, 2, 1, 9, 0, 0));
    assignment2.Set(Asn.Finish, new DateTime(2018, 3, 23, 18, 0, 0));
    assignment2.Set(Asn.Work, project.GetDuration(296, TimeUnitType.Hour));
    assignment2.Set(Asn.Units, 296); 
    assignment2.Set(Asn.Delay, project.GetDuration(23, TimeUnitType.Day));

    project.Recalculate();
    project.RecalculateResourceFields();
    project.RecalculateResourceStartFinish();

    project.Save("TASKSNET_2969_out.mpp", SaveFileFormat.MPP);
}

The “template2016.mpp” is here template2016.zip (24.7 KB). It is the “template.mpp” but resaved in MSP 2016. The output generated by Aspose.Tasks is the same as generated by MS Project.

The issues you have found earlier (filed as TASKSNET-2969) have been fixed in this update.

Hi @alexanderefremov1, how are you?

I tested the same code (with your template), but the result was not as expected.

I’m using 19.3

Attached: Test.png (114.3 KB)

Would you help me?

@FabioMartins,

I have observed the image shared by you. An issue with ID TASKSNET-3126 has been created in our issue tracking system to further investigate the issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

Hello!

If you try to repeat the steps in the code in MS Project with template2016, you get exactly the same result as generated by AT. Please take a look on the screenshot. I opened template2016.mpp and repeat all steps in the code.

As far as the task has 60 days of fixed duration and you set the start of the second (material) assignment to 02/01/2018 then you’re getting 23 days of delay (from 01/01/2018 till 02/01/2018) plus 60 days of task’s duration = 83 days.

2019-04-04_13-58-24.png (73.1 KB)

If we remove the line:

assignment2.Set(Asn.Delay, project.GetDuration(23, TimeUnitType.Day));

What should be the final date of the resource (Material)?
25-04-2018 or 23-03-2018?

@FabioMartins,

We are verifying the details on our end will share the feedback with you in this regard as soon as possible.