Suppose I want to write the following data to an mpp file:
dayyyy.png (12,4 КБ)
As you can see, Units = 1.0, Work = 1 day (selected via cursor), timephased data can be ignored.
I used the following code:
var project = new Project();
var resource = project.Resources.Add("Resource 1");
resource.Type = ResourceType.Material;
var task = project.RootTask.Children.Add("Task 1");
task.Start = new DateTime(2024, 3, 21, 8, 0, 0);
task.Duration = project.GetDuration(2, TimeUnitType.Day);
task.Finish = new DateTime(2024, 3, 22, 17, 0, 0);
var assignment = project.ResourceAssignments.Add(task, resource, 1.0d);
assignment.Start = new DateTime(2024, 3, 21, 8, 0, 0);
assignment.Work = project.GetDuration(1, TimeUnitType.Day);
assignment.Finish = new DateTime(2024, 3, 22, 17, 0, 0);
At first glance, the output file is correct, but work is measured in hours, not days.
dayhhhh.png (13,5 КБ)
So we add a line:
var project = new Project();
project.WorkFormat = TimeUnitType.Day;
The result is differet now… It’s 8 days and 8 units.
day8888.png (12,5 КБ)
Is this behavior intended? How can I get around it (besides just hardcoding division by 8)? In this particular example it’s not a big deal, and material resources have custom material labels anyway, but I work with projects that have day time unit type, and I can’t add accurate timephased data to resource assignments, because work is calculated incorrectly.
EDIT: apparently, I have found a workaround for now:
project.WorkFormat = TimeUnitType.Hour;
var assignment = project.ResourceAssignments.Add(task, resource, 1.0d);
project.WorkFormat = TimeUnitType.Day;
assignment.Start = new DateTime(2024, 3, 21, 8, 0, 0);
assignment.Work = project.GetDuration(1, TimeUnitType.Hour);
assignment.Finish = new DateTime(2024, 3, 22, 17, 0, 0);
It produces the correct result (1d, 1 unit).
EDIT2: It didn’t solve my problem, because adding timephased data to assignment and calling project.Recalculate() before saving still multiplies it by 8.
EDIT3: Here’s another solution which seems to make it calculate correctly:
project.WorkFormat = TimeUnitType.Hour;
project.Recalculate();
project.WorkFormat = TimeUnitType.Day;
Although, I’m not sure it won’t cause more problems.