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?